forked from kubernetes-sigs/cluster-api-provider-gcp
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Set GCPMachinepoolMachine.Status.LatestModelApplied to false if some labels applied to node are not same as expected #25
Closed
tasdikrahman
wants to merge
7
commits into
newrelic-forks:nr-main
from
tasdikrahman:fix-gcpmachinepoolmachine-rotation-if-additionalLabels-outdated-on-node
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4565fd7
fix: instancegroupinstances: set gcpmachinepoolmachine.status.LatestM…
tasdikrahman c12c567
refactor: use hasAdditionalLabelsDiff to be called via HasLatestModel…
tasdikrahman 761d5d7
refactor: hasAdditionalLabelsDiff -> doesNotHaveAdditionalLabelsDiff …
tasdikrahman 3d762a4
chore: lint error fixes
tasdikrahman 6dab195
chore: lint: gofumpt -l -w .
tasdikrahman 9cf08e6
chore: lint: fix further
tasdikrahman 663def9
chore: lint: fix further
tasdikrahman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -302,8 +302,10 @@ func (m *MachinePoolMachineScope) ProviderID() string { | |
} | ||
|
||
// HasLatestModelApplied checks if the latest model is applied to the GCPMachinePoolMachine. | ||
func (m *MachinePoolMachineScope) HasLatestModelApplied(_ context.Context, instance *compute.Disk) (bool, error) { | ||
image := "" | ||
func (m *MachinePoolMachineScope) HasLatestModelApplied(ctx context.Context, instance *compute.Disk, labels map[string]string) (bool, error) { | ||
log := log.FromContext(ctx) | ||
|
||
var image string | ||
|
||
if m.GCPMachinePool.Spec.Image == nil { | ||
version := "" | ||
|
@@ -318,18 +320,45 @@ func (m *MachinePoolMachineScope) HasLatestModelApplied(_ context.Context, insta | |
// Get the image from the disk URL path to compare with the latest image name | ||
diskImage, err := url.Parse(instance.SourceImage) | ||
if err != nil { | ||
log.Error(err, "Error Parsing the Instance.SourceImage from the disk attached.") | ||
return false, err | ||
} | ||
instanceImage := path.Base(diskImage.Path) | ||
|
||
// Check if the image is the latest | ||
if image == instanceImage { | ||
// Check if the image is the latest and additionalLabels | ||
hasAdditionalLabelsDiff := m.doesNotHaveAdditionalLabelsDiff(ctx, labels) | ||
if image == instanceImage && hasAdditionalLabelsDiff { | ||
log.Info("Instance Image and AdditionalLabels are same as the ones specified in GCPMachinePool Spec, setting LatestModelApplied to true", "GCPMachinePoolMachine", m.GCPMachinePoolMachine) | ||
return true, nil | ||
} | ||
|
||
log.Info("One of either Instance Image and AdditionalLabels are not the same as the ones specified in GCPMachinePool Spec, setting LatestModelApplied to false", "GCPMachinePoolMachine", m.GCPMachinePoolMachine) | ||
return false, nil | ||
} | ||
|
||
// doesNotHaveAdditionalLabelsDiff Checks if the Labels applied to the instance are the latest as in the Instance Template. | ||
// We would need to ignore the two labels | ||
// - `capg-role` | ||
// - `capg-cluster-<CLUSTER-NAME>` | ||
// as they are added by default by CAPG and when we compare it with AdditionalLabels in GCPMachinePool.Spec with the instance present. | ||
// ref: https://github.com/newrelic-forks/cluster-api-provider-gcp/blob/ef2e7f1e64ebeeb5389c446fe4cf89026fcb8a8a/cloud/services/compute/instances/reconcile_test.go#L244-L24 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add a one-liner to say we need to ignore the 2 labels? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just added a change here 761d5d7, please check |
||
func (m *MachinePoolMachineScope) doesNotHaveAdditionalLabelsDiff(ctx context.Context, labels map[string]string) bool { | ||
diff := make(map[string]bool) | ||
log := log.FromContext(ctx) | ||
for _, k := range labels { | ||
if _, ok := m.GCPMachinePool.Spec.AdditionalLabels[k]; !ok { | ||
diff[k] = true | ||
} | ||
} | ||
_, hasCapgRoleKey := diff["capg-role"] | ||
_, hasCapgClusterKey := diff["capg-cluster-"+m.GCPMachinePool.ObjectMeta.Labels["cluster.x-k8s.io/cluster-name"]] | ||
if hasCapgRoleKey && hasCapgClusterKey && len(diff) == 2 { | ||
log.Info("There's no diff in AdditionalLabels which are present.", "GCPMachinePoolMachine", m.GCPMachinePoolMachine) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// CordonAndDrainNode cordon and drain the node for the GCPMachinePoolMachine. | ||
func (m *MachinePoolMachineScope) CordonAndDrainNode(ctx context.Context) error { | ||
log := log.FromContext(ctx) | ||
|
@@ -423,7 +452,6 @@ func (m *MachinePoolMachineScope) drainNode(ctx context.Context, node *corev1.No | |
Name: m.ClusterGetter.Name(), | ||
Namespace: m.GCPMachinePoolMachine.Namespace, | ||
}) | ||
|
||
if err != nil { | ||
log.Error(err, "Error creating a remote client while deleting Machine, won't retry") | ||
return nil | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the name
hasAdditionalLabelsDiff
is a bit confusing here. Ideally it should meandoesNotHaveAdditionalLabelsDiff
ornoAdditionalLabelsDiff
in the above context, correct?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just added a change here 761d5d7, please check