-
Notifications
You must be signed in to change notification settings - Fork 52
OCPBUGS-63411: machinesync: Create Cluster API InfraMachine if not exists while Cluster API Machine exists #402
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -989,20 +989,19 @@ var _ = Describe("With a running MachineSync Reconciler", func() { | |
| }) | ||
|
|
||
| Context("and the InfraMachine does not exist", func() { | ||
| It("should update the synchronized condition on the MAPI machine to False", func() { | ||
| // TODO: Revert this to a useful error message. | ||
| // We changed how fetchCAPIInfraResources behaves, so now we fail at a later point in the code. | ||
| // This still fails, the behaviour is the same - it's just a less useful error for an end user. | ||
| // We need to revisit fetchCAPIInfraResources. | ||
| It("should create the Cluster API InfraMachine", func() { | ||
| Eventually(k.Object(mapiMachine), timeout).Should( | ||
| HaveField("Status.Conditions", ContainElement( | ||
| SatisfyAll( | ||
| HaveField("Type", Equal(consts.SynchronizedCondition)), | ||
| HaveField("Status", Equal(corev1.ConditionFalse)), | ||
| HaveField("Reason", Equal("FailedToConvertCAPIMachineToMAPI")), | ||
| HaveField("Message", ContainSubstring("unexpected InfraMachine type, expected AWSMachine, got <nil>")), | ||
| HaveField("Status", Equal(corev1.ConditionTrue)), | ||
| HaveField("Reason", Equal("ResourceSynchronized")), | ||
| HaveField("Message", ContainSubstring("Successfully synchronized CAPI Machine to MAPI")), | ||
| ))), | ||
| ) | ||
|
|
||
| capiInfraMachine := awsv1resourcebuilder.AWSMachine().WithName(mapiMachine.Name).WithNamespace(capiNamespace.Name).Build() | ||
| Eventually(k.Get(capiInfraMachine), timeout).Should(Succeed()) | ||
| }) | ||
|
Comment on lines
+992
to
1005
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. Verify the synchronization message direction. The test expects the message to contain The test assertion should expect "MAPI Machine to CAPI" not "CAPI Machine to MAPI". 🔎 Proposed fix It("should create the Cluster API InfraMachine", func() {
Eventually(k.Object(mapiMachine), timeout).Should(
HaveField("Status.Conditions", ContainElement(
SatisfyAll(
HaveField("Type", Equal(consts.SynchronizedCondition)),
HaveField("Status", Equal(corev1.ConditionTrue)),
HaveField("Reason", Equal("ResourceSynchronized")),
- HaveField("Message", ContainSubstring("Successfully synchronized CAPI Machine to MAPI")),
+ HaveField("Message", Equal("Successfully synchronized MAPI Machine to CAPI")),
))),
)
capiInfraMachine := awsv1resourcebuilder.AWSMachine().WithName(mapiMachine.Name).WithNamespace(capiNamespace.Name).Build()
Eventually(k.Get(capiInfraMachine), timeout).Should(Succeed())
})🤖 Prompt for AI Agents |
||
| }) | ||
| }) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| Copyright 2024 Red Hat, Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package machinesync | ||
|
|
||
| import ( | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| configv1 "github.com/openshift/api/config/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| awsv1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" | ||
| clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
| ) | ||
|
|
||
| // Unit tests for fetchCAPIInfraResources because reconciling deletion depends on it. | ||
| var _ = Describe("Unit tests for fetchCAPIInfraResources", func() { | ||
| var reconciler *MachineSyncReconciler | ||
| var capiMachine *clusterv1.Machine | ||
|
|
||
| BeforeEach(func() { | ||
| capiMachine = &clusterv1.Machine{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-machine", | ||
| Namespace: "test-namespace", | ||
| }, | ||
| Spec: clusterv1.MachineSpec{ | ||
| ClusterName: "test-cluster", | ||
| InfrastructureRef: clusterv1.ContractVersionedObjectReference{ | ||
| APIGroup: awsv1.GroupVersion.Group, | ||
| Kind: "AWSMachine", | ||
| Name: "test-machine", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| reconciler = &MachineSyncReconciler{ | ||
| Scheme: testEnv.Scheme, | ||
| Platform: configv1.AWSPlatformType, | ||
| } | ||
| }) | ||
|
|
||
| Describe("when fetching Cluster API Infrastructure Resources", func() { | ||
| Context("when capiMachine is nil", func() { | ||
| It("should return nil and no error", func() { | ||
| reconciler.Client = fake.NewClientBuilder().WithScheme(testEnv.Scheme).Build() | ||
| infraCluster, infraMachine, err := reconciler.fetchCAPIInfraResources(ctx, nil) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(infraCluster).To(BeNil()) | ||
| Expect(infraMachine).To(BeNil()) | ||
| }) | ||
| }) | ||
|
|
||
| Context("and Infrastructure Cluster is not present", func() { | ||
| It("should return nil for both infra cluster and infra machine", func() { | ||
| reconciler.Client = fake.NewClientBuilder().WithScheme(testEnv.Scheme).Build() | ||
|
|
||
| infraCluster, infraMachine, err := reconciler.fetchCAPIInfraResources(ctx, capiMachine) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err).To(MatchError(ContainSubstring("failed to get Cluster API infrastructure cluster"))) | ||
| Expect(infraCluster).To(BeNil()) | ||
| Expect(infraMachine).To(BeNil()) | ||
| }) | ||
| }) | ||
|
|
||
| Context("and Infrastructure is present", func() { | ||
| var objs []client.Object | ||
| BeforeEach(func() { | ||
| objs = []client.Object{ | ||
| &awsv1.AWSCluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: capiMachine.Spec.ClusterName, | ||
| Namespace: capiMachine.Namespace, | ||
| }, | ||
| }, | ||
| } | ||
| }) | ||
|
|
||
| It("should return nil if infrastructure machine is not present", func() { | ||
| reconciler.Client = fake.NewClientBuilder().WithScheme(testEnv.Scheme).WithObjects(objs...).Build() | ||
| infraCluster, infraMachine, err := reconciler.fetchCAPIInfraResources(ctx, capiMachine) | ||
|
|
||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(infraCluster).ToNot(BeNil()) | ||
| Expect(infraMachine).To(BeNil()) | ||
| }) | ||
|
|
||
| It("should return infrastructure machine if it is present", func() { | ||
| objs = append(objs, &awsv1.AWSMachine{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: capiMachine.Name, | ||
| Namespace: capiMachine.Namespace, | ||
| }, | ||
| }) | ||
| reconciler.Client = fake.NewClientBuilder().WithScheme(testEnv.Scheme).WithObjects(objs...).Build() | ||
| infraCluster, infraMachine, err := reconciler.fetchCAPIInfraResources(ctx, capiMachine) | ||
|
|
||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(infraCluster).ToNot(BeNil()) | ||
| Expect(infraMachine).ToNot(BeNil()) | ||
| }) | ||
| }) | ||
| }) | ||
| }) |
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.
Consider validating
InfrastructureRef.NamewhencapiMachineis not nil.When
capiMachineis not nil butcapiMachine.Spec.InfrastructureRef.Nameis empty, the method will search for an infra machine with an empty name, which could return misleading results. Consider adding validation or falling back tomapiMachine.Name:Suggested fix
func (r *MachineSyncReconciler) doesCAPIInfraMachineExist(ctx context.Context, capiMachine *clusterv1.Machine, mapiMachine *mapiv1beta1.Machine) (bool, error) { namespace := r.CAPINamespace name := mapiMachine.Name - if capiMachine != nil { + if capiMachine != nil && capiMachine.Spec.InfrastructureRef.Name != "" { name = capiMachine.Spec.InfrastructureRef.Name namespace = capiMachine.Namespace } infraMachine, err := r.fetchCAPIInfraMachine(ctx, name, namespace) if err != nil { return false, fmt.Errorf("checking existence: %w", err) } return infraMachine != nil, nil }