diff --git a/pkg/api/v1alpha1/cluster_webhook.go b/pkg/api/v1alpha1/cluster_webhook.go index 703fbcd0ef44..41e2ca5bad69 100644 --- a/pkg/api/v1alpha1/cluster_webhook.go +++ b/pkg/api/v1alpha1/cluster_webhook.go @@ -334,7 +334,7 @@ func validateImmutableFieldsCluster(new, old *Cluster) field.ErrorList { } } - if !new.Spec.GitOpsRef.Equal(old.Spec.GitOpsRef) { + if !new.Spec.GitOpsRef.Equal(old.Spec.GitOpsRef) && !old.IsSelfManaged() { allErrs = append( allErrs, field.Forbidden(specPath.Child("GitOpsRef"), fmt.Sprintf("field is immutable %v", new.Spec.GitOpsRef))) diff --git a/pkg/api/v1alpha1/cluster_webhook_test.go b/pkg/api/v1alpha1/cluster_webhook_test.go index feefe38afae1..1d3416bdf2ec 100644 --- a/pkg/api/v1alpha1/cluster_webhook_test.go +++ b/pkg/api/v1alpha1/cluster_webhook_test.go @@ -692,8 +692,19 @@ func TestClusterValidateUpdateGitOpsRefImmutableNilEqual(t *testing.T) { g.Expect(c.ValidateUpdate(cOld)).To(Succeed()) } +func TestClusterValidateUpdateGitOpsRefMutableManagementCluster(t *testing.T) { + cOld := baseCluster() + cOld.Spec.GitOpsRef = nil + c := cOld.DeepCopy() + c.Spec.GitOpsRef = &v1alpha1.Ref{Name: "test2", Kind: "GitOpsConfig"} + + g := NewWithT(t) + g.Expect(c.ValidateUpdate(cOld)).To(Succeed()) +} + func TestClusterValidateUpdateGitOpsRefImmutable(t *testing.T) { cOld := baseCluster() + cOld.SetManagedBy("management-cluster") cOld.Spec.GitOpsRef = &v1alpha1.Ref{} c := cOld.DeepCopy() c.Spec.GitOpsRef = &v1alpha1.Ref{Name: "test2", Kind: "GitOpsConfig2"} @@ -704,6 +715,7 @@ func TestClusterValidateUpdateGitOpsRefImmutable(t *testing.T) { func TestClusterValidateUpdateGitOpsRefImmutableName(t *testing.T) { cOld := baseCluster() + cOld.SetManagedBy("management-cluster") cOld.Spec.GitOpsRef = &v1alpha1.Ref{ Name: "test1", Kind: "GitOpsConfig", } @@ -716,6 +728,7 @@ func TestClusterValidateUpdateGitOpsRefImmutableName(t *testing.T) { func TestClusterValidateUpdateGitOpsRefImmutableKind(t *testing.T) { cOld := baseCluster() + cOld.SetManagedBy("management-cluster") cOld.Spec.GitOpsRef = &v1alpha1.Ref{ Name: "test", Kind: "GitOpsConfig1", } @@ -728,6 +741,7 @@ func TestClusterValidateUpdateGitOpsRefImmutableKind(t *testing.T) { func TestClusterValidateUpdateGitOpsRefOldNilImmutable(t *testing.T) { cOld := baseCluster() + cOld.SetManagedBy("management-cluster") cOld.Spec.GitOpsRef = nil c := cOld.DeepCopy() @@ -739,6 +753,7 @@ func TestClusterValidateUpdateGitOpsRefOldNilImmutable(t *testing.T) { func TestClusterValidateUpdateGitOpsRefNewNilImmutable(t *testing.T) { cOld := baseCluster() + cOld.SetManagedBy("management-cluster") cOld.Spec.GitOpsRef = &v1alpha1.Ref{ Name: "test", Kind: "GitOpsConfig", }