Skip to content

Commit 6c91f90

Browse files
authored
Merge pull request #835 from Danil-Grigorev/secret-hash-by-default
🌱 Replace CM hash with secret hash
2 parents ad2db37 + f27c1ed commit 6c91f90

File tree

2 files changed

+35
-92
lines changed

2 files changed

+35
-92
lines changed

internal/controller/genericprovider_controller.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -174,40 +174,8 @@ func (r *GenericProviderReconciler) Reconcile(ctx context.Context, req reconcile
174174
}, nil
175175
}
176176

177-
// Check if spec hash stays the same and don't go further in this case.
178-
specHash, err := calculateHash(ctx, r.Client, r.Provider)
179-
if err != nil {
180-
return ctrl.Result{}, err
181-
}
182-
183-
if r.Provider.GetAnnotations()[appliedSpecHashAnnotation] == specHash {
184-
log.Info("No changes detected, skipping further steps")
185-
186-
return ctrl.Result{}, nil
187-
}
188-
189177
res, err := r.reconcile(ctx)
190178

191-
annotations := r.Provider.GetAnnotations()
192-
if annotations == nil {
193-
annotations = map[string]string{}
194-
}
195-
196-
// Set the spec hash annotation if reconciliation was successful or reset it otherwise.
197-
if res.IsZero() && err == nil {
198-
// Recalculate spec hash in case it was changed during reconciliation process.
199-
specHash, err := calculateHash(ctx, r.Client, r.Provider)
200-
if err != nil {
201-
return ctrl.Result{}, err
202-
}
203-
204-
annotations[appliedSpecHashAnnotation] = specHash
205-
} else {
206-
annotations[appliedSpecHashAnnotation] = ""
207-
}
208-
209-
r.Provider.SetAnnotations(annotations)
210-
211179
return ctrl.Result{
212180
Requeue: res.Requeue,
213181
RequeueAfter: res.RequeueAfter,
@@ -344,14 +312,6 @@ func providerHash(ctx context.Context, client client.Client, hash hash.Hash, pro
344312
return nil
345313
}
346314

347-
func calculateHash(ctx context.Context, k8sClient client.Client, provider genericprovider.GenericProvider) (string, error) {
348-
hash := sha256.New()
349-
350-
err := providerHash(ctx, k8sClient, hash, provider)
351-
352-
return fmt.Sprintf("%x", hash.Sum(nil)), err
353-
}
354-
355315
// ApplyFromCache applies provider configuration from cache and returns true if the cache did not change.
356316
func (p *PhaseReconciler) ApplyFromCache(ctx context.Context) (*Result, error) {
357317
log := log.FromContext(ctx)

internal/controller/genericprovider_controller_test.go

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package controller
1818

1919
import (
20-
"context"
2120
"testing"
2221
"time"
2322

@@ -745,14 +744,13 @@ func TestProviderConfigSecretChanges(t *testing.T) {
745744
g.Expect(env.CreateAndWait(ctx, secret.DeepCopy())).To(Succeed())
746745
objs = append(objs, secret)
747746

748-
initialHash, err := calculateHash(ctx, env.Client, provider)
749-
g.Expect(err).ToNot(HaveOccurred())
750-
751747
t.Log("creating test provider", provider.GetName())
752748
g.Expect(env.CreateAndWait(ctx, provider.DeepCopy())).To(Succeed())
753749
objs = append(objs, provider)
754750

755-
g.Eventually(generateExpectedResultChecker(provider, initialHash, corev1.ConditionTrue), timeout).Should(BeEquivalentTo(true))
751+
g.Eventually(generateExpectedResultChecker(provider, corev1.ConditionTrue, func(s string) bool { return s != "" }), timeout).Should(BeEquivalentTo(true))
752+
753+
initialHash := provider.GetAnnotations()[appliedSpecHashAnnotation]
756754

757755
g.Eventually(func() error {
758756
if err := env.Client.Get(ctx, client.ObjectKeyFromObject(secret), secret); err != nil {
@@ -765,52 +763,32 @@ func TestProviderConfigSecretChanges(t *testing.T) {
765763
return env.Client.Update(ctx, secret)
766764
}).Should(Succeed())
767765

768-
var updatedDataHash string
769-
770766
if tc.expectSameHash {
771-
g.Eventually(func() string {
772-
updatedDataHash, err = calculateHash(ctx, env.Client, provider)
773-
g.Expect(err).ToNot(HaveOccurred())
767+
g.Consistently(func(g Gomega) {
768+
err := env.Client.Get(ctx, client.ObjectKeyFromObject(provider), provider)
769+
g.Expect(client.IgnoreNotFound(err)).ToNot(HaveOccurred())
774770

775-
return updatedDataHash
776-
}, 15*time.Second).Should(Equal(initialHash))
771+
g.Expect(provider.GetAnnotations()[appliedSpecHashAnnotation]).To(Equal(initialHash))
772+
}, 15*time.Second).Should(Succeed())
777773
} else {
778-
g.Eventually(func() string {
779-
updatedDataHash, err = calculateHash(ctx, env.Client, provider)
780-
g.Expect(err).ToNot(HaveOccurred())
774+
g.Eventually(func(g Gomega) {
775+
err := env.Client.Get(ctx, client.ObjectKeyFromObject(provider), provider)
776+
g.Expect(client.IgnoreNotFound(err)).ToNot(HaveOccurred())
781777

782-
return updatedDataHash
783-
}, 15*time.Second).ShouldNot(Equal(initialHash))
778+
g.Expect(provider.GetAnnotations()[appliedSpecHashAnnotation]).ToNot(Equal(initialHash))
779+
}, 15*time.Second).ShouldNot(Succeed())
784780
}
785-
786-
g.Eventually(func() error {
787-
if err := env.Client.Get(ctx, client.ObjectKeyFromObject(provider), provider); err != nil {
788-
return err
789-
}
790-
791-
// Set a label to ensure that provider was changed
792-
labels := provider.GetLabels()
793-
if labels == nil {
794-
labels = map[string]string{}
795-
}
796-
labels["my-label"] = "some-value"
797-
provider.SetLabels(labels)
798-
provider.SetManagedFields(nil)
799-
800-
return env.Client.Update(ctx, provider)
801-
}).Should(Succeed())
802-
803-
g.Eventually(generateExpectedResultChecker(provider, updatedDataHash, corev1.ConditionTrue), timeout).Should(BeEquivalentTo(true))
804781
})
805782
}
806783
}
807784

808785
func TestProviderSpecChanges(t *testing.T) {
809786
testCases := []struct {
810-
name string
811-
spec operatorv1.ProviderSpec
812-
updatedSpec operatorv1.ProviderSpec
813-
expectError bool
787+
name string
788+
spec operatorv1.ProviderSpec
789+
updatedSpec operatorv1.ProviderSpec
790+
expectHashChange bool
791+
expectError bool
814792
}{
815793
{
816794
name: "same spec, hash annotation doesn't change",
@@ -860,9 +838,10 @@ func TestProviderSpecChanges(t *testing.T) {
860838
},
861839
},
862840
},
841+
expectHashChange: true,
863842
},
864843
{
865-
name: "upgrade to a non-existent version, hash annotation is empty",
844+
name: "upgrade to a non-existent version, hash annotation unchanged",
866845
expectError: true,
867846
spec: operatorv1.ProviderSpec{
868847
Version: testCurrentVersion,
@@ -906,12 +885,6 @@ func TestProviderSpecChanges(t *testing.T) {
906885
updatedProvider := provider.DeepCopy()
907886
updatedProvider.SetSpec(tc.updatedSpec)
908887

909-
specHash, err := calculateHash(context.Background(), env.Client, provider)
910-
g.Expect(err).ToNot(HaveOccurred())
911-
912-
updatedSpecHash, err := calculateHash(context.Background(), env.Client, updatedProvider)
913-
g.Expect(err).ToNot(HaveOccurred())
914-
915888
namespace := "test-provider-spec-changes"
916889

917890
ns, err := env.CreateNamespace(ctx, namespace)
@@ -929,7 +902,11 @@ func TestProviderSpecChanges(t *testing.T) {
929902
g.Expect(env.Cleanup(ctx, provider, dummyConfigMap(namespace))).To(Succeed())
930903
}()
931904

932-
g.Eventually(generateExpectedResultChecker(provider, specHash, corev1.ConditionTrue), timeout).Should(BeEquivalentTo(true))
905+
g.Eventually(generateExpectedResultChecker(provider, corev1.ConditionTrue, func(s string) bool {
906+
return s != ""
907+
}), timeout).Should(BeEquivalentTo(true))
908+
909+
currentHash := provider.GetAnnotations()[appliedSpecHashAnnotation]
933910

934911
g.Eventually(func() error {
935912
if err := env.Client.Get(ctx, client.ObjectKeyFromObject(provider), provider); err != nil {
@@ -952,22 +929,28 @@ func TestProviderSpecChanges(t *testing.T) {
952929
}).Should(Succeed())
953930

954931
if !tc.expectError {
955-
g.Eventually(generateExpectedResultChecker(provider, updatedSpecHash, corev1.ConditionTrue), timeout).Should(BeEquivalentTo(true))
932+
g.Eventually(generateExpectedResultChecker(provider, corev1.ConditionTrue, func(s string) bool {
933+
if tc.expectHashChange {
934+
return s != currentHash
935+
}
936+
937+
return s == currentHash
938+
}), timeout).Should(BeEquivalentTo(true))
956939
} else {
957-
g.Eventually(generateExpectedResultChecker(provider, "", corev1.ConditionFalse), timeout).Should(BeEquivalentTo(true))
940+
g.Eventually(generateExpectedResultChecker(provider, corev1.ConditionFalse, func(s string) bool { return s == currentHash }), timeout).Should(BeEquivalentTo(true))
958941
}
959942
})
960943
}
961944
}
962945

963-
func generateExpectedResultChecker(provider genericprovider.GenericProvider, specHash string, condStatus corev1.ConditionStatus) func() bool {
946+
func generateExpectedResultChecker(provider genericprovider.GenericProvider, condStatus corev1.ConditionStatus, hashCheck func(string) bool) func() bool {
964947
return func() bool {
965948
if err := env.Get(ctx, client.ObjectKeyFromObject(provider), provider); err != nil {
966949
return false
967950
}
968951

969952
// In case of error we don't want the spec annotation to be updated
970-
if provider.GetAnnotations()[appliedSpecHashAnnotation] != specHash {
953+
if !hashCheck(provider.GetAnnotations()[appliedSpecHashAnnotation]) {
971954
return false
972955
}
973956

0 commit comments

Comments
 (0)