Skip to content
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

✨ allow changing strategy from ApplyAlways to Reconcile #20

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions exp/addons/api/v1beta1/clusterresourceset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func (m *ClusterResourceSet) Default() {
// ClusterResourceSet Strategy defaults to ApplyOnce.
if m.Spec.Strategy == "" {
m.Spec.Strategy = string(ClusterResourceSetStrategyApplyOnce)
} else if m.Spec.Strategy == string(ClusterResourceSetStrategyApplyAlways) {
m.Spec.Strategy = string(ClusterResourceSetStrategyReconcile)
}
}

Expand Down Expand Up @@ -98,10 +100,15 @@ func (m *ClusterResourceSet) validate(old *ClusterResourceSet) error {
}

if old != nil && old.Spec.Strategy != "" && old.Spec.Strategy != m.Spec.Strategy {
allErrs = append(
allErrs,
field.Invalid(field.NewPath("spec", "strategy"), m.Spec.Strategy, "field is immutable"),
)
// Allow changing from ApplyAlways (a strategy that was added in this fork) to Reconcile.
// ApplyAlways is an "alias" for Reconcile and migrating to Reconcile will enable us to stop using a fork.
if !(old.Spec.Strategy == string(ClusterResourceSetStrategyApplyAlways) &&
m.Spec.Strategy == string(ClusterResourceSetStrategyReconcile)) {
allErrs = append(
allErrs,
field.Invalid(field.NewPath("spec", "strategy"), m.Spec.Strategy, "field is immutable"),
)
}
}

if old != nil && !reflect.DeepEqual(old.Spec.ClusterSelector, m.Spec.ClusterSelector) {
Expand Down
29 changes: 29 additions & 0 deletions exp/addons/api/v1beta1/clusterresourceset_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ func TestClusterResourcesetDefault(t *testing.T) {
g.Expect(clusterResourceSet.Spec.Strategy).To(Equal(string(ClusterResourceSetStrategyApplyOnce)))
}

func TestClusterResourcesetDefaultWithClusterResourceSetStrategyApplyAlways(t *testing.T) {
g := NewWithT(t)
clusterResourceSet := &ClusterResourceSet{
Spec: ClusterResourceSetSpec{
Strategy: string(ClusterResourceSetStrategyApplyAlways),
},
}
defaultingValidationCRS := clusterResourceSet.DeepCopy()
defaultingValidationCRS.Spec.ClusterSelector = metav1.LabelSelector{
MatchLabels: map[string]string{"foo": "bar"},
}
t.Run("for ClusterResourceSet", utildefaulting.DefaultValidateTest(defaultingValidationCRS))
clusterResourceSet.Default()

g.Expect(clusterResourceSet.Spec.Strategy).To(Equal(string(ClusterResourceSetStrategyReconcile)))
}

func TestClusterResourceSetLabelSelectorAsSelectorValidation(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -104,6 +121,18 @@ func TestClusterResourceSetStrategyImmutable(t *testing.T) {
newStrategy: "",
expectErr: true,
},
{
name: "when the Strategy has changed, but the old value was ApplyAlways",
oldStrategy: string(ClusterResourceSetStrategyApplyAlways),
newStrategy: string(ClusterResourceSetStrategyReconcile),
expectErr: false,
},
{
name: "when the Strategy has changed, but the old value was ApplyAlways and the new value is ApplyOnce",
oldStrategy: string(ClusterResourceSetStrategyApplyAlways),
newStrategy: string(ClusterResourceSetStrategyApplyOnce),
expectErr: true,
},
}

for _, tt := range tests {
Expand Down
Loading