Skip to content

Commit 99ad887

Browse files
authored
Merge pull request #777 from k8s-infra-cherrypick-robot/cherry-pick-764-to-release-0.18
[release-0.18] 🐛 Fix appliedSpecHashAnnotation set if PreflightCheckPassed is not True
2 parents 214e271 + 2d45307 commit 99ad887

File tree

4 files changed

+111
-44
lines changed

4 files changed

+111
-44
lines changed

internal/controller/coreprovider_to_providers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
// It lists all the providers and if its PreflightCheckCondition is not True, this object will be added to the resulting request.
3535
// This means that notifications will only be sent to those objects that have not pass PreflightCheck.
3636
func newCoreProviderToProviderFuncMapForProviderList(k8sClient client.Client, providerList genericprovider.GenericProviderList) handler.MapFunc {
37-
providerListType := fmt.Sprintf("%t", providerList)
37+
providerListType := fmt.Sprintf("%T", providerList)
3838

3939
return func(ctx context.Context, obj client.Object) []reconcile.Request {
4040
log := ctrl.LoggerFrom(ctx).WithValues("provider", map[string]string{"name": obj.GetName(), "namespace": obj.GetNamespace()}, "providerListType", providerListType)

internal/controller/genericprovider_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (r *GenericProviderReconciler) Reconcile(ctx context.Context, req reconcile
164164

165165
r.Provider.SetAnnotations(annotations)
166166

167-
return res, err
167+
return res, ignoreCoreProviderWaitError(err)
168168
}
169169

170170
func patchProvider(ctx context.Context, provider operatorv1.GenericProvider, patchHelper *patch.Helper, options ...patch.Option) error {
@@ -206,7 +206,7 @@ func (r *GenericProviderReconciler) reconcile(ctx context.Context, provider gene
206206

207207
if !res.IsZero() || err != nil {
208208
// the steps are sequential, so we must be complete before progressing.
209-
return res, ignoreCoreProviderWaitError(err)
209+
return res, err
210210
}
211211
}
212212

internal/controller/genericprovider_controller_test.go

Lines changed: 107 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3232
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
3333
"sigs.k8s.io/cluster-api/util/conditions"
34+
"sigs.k8s.io/cluster-api/util/patch"
3435
"sigs.k8s.io/controller-runtime/pkg/client"
3536

3637
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
@@ -200,7 +201,7 @@ func TestConfigSecretChangesAreAppliedToTheDeployment(t *testing.T) {
200201

201202
g.Eventually(
202203
testDeploymentLabelValueGetter(ns.Name, testDeploymentName),
203-
30*time.Second,
204+
timeout,
204205
).Should(BeEquivalentTo("initial-value"))
205206

206207
t.Log("Provider deployment deployed")
@@ -213,7 +214,7 @@ func TestConfigSecretChangesAreAppliedToTheDeployment(t *testing.T) {
213214

214215
g.Eventually(
215216
testDeploymentLabelValueGetter(ns.Name, testDeploymentName),
216-
30*time.Second,
217+
timeout,
217218
).Should(BeEquivalentTo("updated-value"))
218219
}
219220

@@ -293,6 +294,17 @@ func TestReconcilerPreflightConditions(t *testing.T) {
293294
g.Expect(env.CreateAndWait(ctx, p)).To(Succeed())
294295
}
295296

297+
defer func() {
298+
objs := []client.Object{
299+
dummyConfigMap(tc.namespace),
300+
}
301+
for _, p := range tc.providers {
302+
objs = append(objs, p)
303+
}
304+
305+
g.Expect(env.CleanupAndWait(ctx, objs...)).To(Succeed())
306+
}()
307+
296308
g.Eventually(func() bool {
297309
for _, p := range tc.providers {
298310
if err := env.Get(ctx, client.ObjectKeyFromObject(p), p); err != nil {
@@ -306,20 +318,6 @@ func TestReconcilerPreflightConditions(t *testing.T) {
306318

307319
return false
308320
}, timeout).Should(BeEquivalentTo(true))
309-
310-
objs := []client.Object{}
311-
for _, p := range tc.providers {
312-
objs = append(objs, p)
313-
}
314-
315-
objs = append(objs, &corev1.ConfigMap{
316-
ObjectMeta: metav1.ObjectMeta{
317-
Name: testCurrentVersion,
318-
Namespace: tc.namespace,
319-
},
320-
})
321-
322-
g.Expect(env.CleanupAndWait(ctx, objs...)).To(Succeed())
323321
})
324322
}
325323
}
@@ -395,6 +393,11 @@ releaseSeries:
395393
t.Log("creating test provider", provider.GetName())
396394
g.Expect(env.CreateAndWait(ctx, provider)).To(Succeed())
397395

396+
defer func() {
397+
// Clean up
398+
g.Expect(env.CleanupAndWait(ctx, []client.Object{provider, dummyFutureConfigMap(namespace, currentVersion), dummyFutureConfigMap(namespace, tc.newVersion)}...)).To(Succeed())
399+
}()
400+
398401
g.Eventually(func() bool {
399402
if err := env.Get(ctx, client.ObjectKeyFromObject(provider), provider); err != nil {
400403
return false
@@ -509,24 +512,6 @@ releaseSeries:
509512

510513
return allSet
511514
}, 2*time.Second).Should(BeTrue())
512-
513-
// Clean up
514-
objs := []client.Object{provider}
515-
objs = append(objs, &corev1.ConfigMap{
516-
ObjectMeta: metav1.ObjectMeta{
517-
Name: currentVersion,
518-
Namespace: namespace,
519-
},
520-
})
521-
522-
objs = append(objs, &corev1.ConfigMap{
523-
ObjectMeta: metav1.ObjectMeta{
524-
Name: tc.newVersion,
525-
Namespace: namespace,
526-
},
527-
})
528-
529-
g.Expect(env.CleanupAndWait(ctx, objs...)).To(Succeed())
530515
})
531516
}
532517
}
@@ -573,9 +558,7 @@ func TestProviderShouldNotBeInstalledWhenCoreProviderNotReady(t *testing.T) {
573558
return false
574559
}
575560

576-
providerInstalled := conditions.Get(coreProvider, operatorv1.ProviderInstalledCondition)
577-
578-
return providerInstalled != nil && providerInstalled.Status == corev1.ConditionFalse
561+
return conditions.Has(coreProvider, operatorv1.ProviderInstalledCondition) && conditions.IsFalse(coreProvider, operatorv1.ProviderInstalledCondition)
579562
}, timeout).Should(BeEquivalentTo(true))
580563

581564
g.Consistently(func() bool {
@@ -589,6 +572,88 @@ func TestProviderShouldNotBeInstalledWhenCoreProviderNotReady(t *testing.T) {
589572
}, timeout/3).Should(BeEquivalentTo(true))
590573
}
591574

575+
func TestReconcilerPreflightConditionsFromCoreProviderEvents(t *testing.T) {
576+
namespace := "test-core-provider-events"
577+
coreProvider := &operatorv1.CoreProvider{
578+
ObjectMeta: metav1.ObjectMeta{
579+
Name: "cluster-api",
580+
},
581+
Spec: operatorv1.CoreProviderSpec{
582+
ProviderSpec: operatorv1.ProviderSpec{
583+
Version: testCurrentVersion,
584+
},
585+
},
586+
}
587+
infrastructureProvider := &operatorv1.InfrastructureProvider{
588+
ObjectMeta: metav1.ObjectMeta{
589+
Name: "vsphere",
590+
},
591+
Spec: operatorv1.InfrastructureProviderSpec{
592+
ProviderSpec: operatorv1.ProviderSpec{
593+
Version: testCurrentVersion,
594+
},
595+
},
596+
}
597+
598+
g := NewWithT(t)
599+
t.Log("Ensure namespace exists", namespace)
600+
g.Expect(env.EnsureNamespaceExists(ctx, namespace)).To(Succeed())
601+
602+
g.Expect(env.CreateAndWait(ctx, dummyConfigMap(namespace))).To(Succeed())
603+
604+
for _, p := range []genericprovider.GenericProvider{coreProvider, infrastructureProvider} {
605+
insertDummyConfig(p)
606+
p.SetNamespace(namespace)
607+
t.Log("creating test provider", p.GetName())
608+
g.Expect(env.CreateAndWait(ctx, p)).To(Succeed())
609+
}
610+
611+
defer func() {
612+
g.Expect(env.CleanupAndWait(ctx, []client.Object{coreProvider, infrastructureProvider, dummyConfigMap(namespace)}...)).To(Succeed())
613+
}()
614+
615+
g.Eventually(func() bool {
616+
if err := env.Get(ctx, client.ObjectKeyFromObject(infrastructureProvider), infrastructureProvider); err != nil {
617+
return false
618+
}
619+
620+
if conditions.Has(infrastructureProvider, operatorv1.PreflightCheckCondition) && conditions.IsFalse(infrastructureProvider, operatorv1.PreflightCheckCondition) {
621+
return true
622+
}
623+
624+
return false
625+
}, timeout).Should(BeEquivalentTo(true))
626+
627+
g.Eventually(func() bool {
628+
if err := env.Get(ctx, client.ObjectKeyFromObject(coreProvider), coreProvider); err != nil {
629+
return false
630+
}
631+
632+
if conditions.IsTrue(coreProvider, operatorv1.PreflightCheckCondition) && conditions.IsTrue(coreProvider, operatorv1.ProviderInstalledCondition) {
633+
return true
634+
}
635+
636+
return false
637+
}, timeout).Should(BeEquivalentTo(true))
638+
639+
patchHelper, err := patch.NewHelper(coreProvider, env)
640+
g.Expect(err).ToNot(HaveOccurred())
641+
conditions.MarkTrue(coreProvider, clusterv1.ReadyCondition)
642+
g.Expect(patchHelper.Patch(ctx, coreProvider)).To(Succeed())
643+
644+
g.Eventually(func() bool {
645+
if err := env.Get(ctx, client.ObjectKeyFromObject(infrastructureProvider), infrastructureProvider); err != nil {
646+
return false
647+
}
648+
649+
if conditions.IsTrue(infrastructureProvider, operatorv1.PreflightCheckCondition) {
650+
return true
651+
}
652+
653+
return false
654+
}, timeout).Should(BeEquivalentTo(true))
655+
}
656+
592657
func TestProviderConfigSecretChanges(t *testing.T) {
593658
testCases := []struct {
594659
name string
@@ -859,6 +924,11 @@ func TestProviderSpecChanges(t *testing.T) {
859924
t.Log("creating test provider", provider.GetName())
860925
g.Expect(env.CreateAndWait(ctx, provider.DeepCopy())).To(Succeed())
861926

927+
defer func() {
928+
// Clean up
929+
g.Expect(env.Cleanup(ctx, provider, dummyConfigMap(namespace))).To(Succeed())
930+
}()
931+
862932
g.Eventually(generateExpectedResultChecker(provider, specHash, corev1.ConditionTrue), timeout).Should(BeEquivalentTo(true))
863933

864934
g.Eventually(func() error {
@@ -886,9 +956,6 @@ func TestProviderSpecChanges(t *testing.T) {
886956
} else {
887957
g.Eventually(generateExpectedResultChecker(provider, "", corev1.ConditionFalse), timeout).Should(BeEquivalentTo(true))
888958
}
889-
890-
// Clean up
891-
g.Expect(env.Cleanup(ctx, provider, ns)).To(Succeed())
892959
})
893960
}
894961
}

internal/controller/secrets_to_providers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const (
3838
// It lists all the providers matching spec.configSecret.name values with the secret name querying by index.
3939
// If the provider references a secret without a namespace, it will assume the secret is in the same namespace as the provider.
4040
func newSecretToProviderFuncMapForProviderList(k8sClient client.Client, providerList genericprovider.GenericProviderList) handler.MapFunc {
41-
providerListType := fmt.Sprintf("%t", providerList)
41+
providerListType := fmt.Sprintf("%T", providerList)
4242

4343
return func(ctx context.Context, secret client.Object) []reconcile.Request {
4444
log := ctrl.LoggerFrom(ctx).WithValues("secret", map[string]string{"name": secret.GetName(), "namespace": secret.GetNamespace()}, "providerListType", providerListType)

0 commit comments

Comments
 (0)