Skip to content

Commit

Permalink
feat: allow changing strategy from ApplyAlways to Reconcile (#20)
Browse files Browse the repository at this point in the history
Allows us to migrate ClusterResourceSets to the upstream Reconcile strategy and use CAPI directly with the next release
  • Loading branch information
dkoshkin authored Oct 9, 2023
1 parent 72247b1 commit 84190e3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
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

0 comments on commit 84190e3

Please sign in to comment.