Skip to content
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
3 changes: 0 additions & 3 deletions api/v1alpha1/pattern_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@ func convertToPattern(obj runtime.Object) (*Pattern, error) {
}

func validateVariantAlias(p *Pattern) error {
if p.Spec.ClusterGroupName != "" && p.Spec.Variant != "" {
return fmt.Errorf("spec.variant and spec.clusterGroupName are mutually exclusive, set only one")
}
if p.Spec.ClusterGroupName == "" && p.Spec.Variant == "" {
return fmt.Errorf("one of spec.variant or spec.clusterGroupName must be set")
}
Expand Down
22 changes: 14 additions & 8 deletions api/v1alpha1/pattern_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestValidateCreate_AllowsVariantOnly(t *testing.T) {
}
}

func TestValidateCreate_RejectsBothSet(t *testing.T) {
func TestValidateCreate_AllowsBothSet(t *testing.T) {
scheme := runtime.NewScheme()
if err := AddToScheme(scheme); err != nil {
t.Fatalf("failed to add scheme: %v", err)
Expand All @@ -156,9 +156,12 @@ func TestValidateCreate_RejectsBothSet(t *testing.T) {
},
}

_, err := validator.ValidateCreate(context.Background(), p)
if err == nil {
t.Error("expected error when both variant and clusterGroupName are set, got nil")
warnings, err := validator.ValidateCreate(context.Background(), p)
if err != nil {
t.Errorf("expected no error when both variant and clusterGroupName are set, got: %v", err)
}
if warnings != nil {
t.Errorf("expected no warnings, got: %v", warnings)
}
}

Expand Down Expand Up @@ -190,7 +193,7 @@ func TestValidateCreate_RejectsBothEmpty(t *testing.T) {
}
}

func TestValidateUpdate_RejectsBothSet(t *testing.T) {
func TestValidateUpdate_AllowsBothSet(t *testing.T) {
scheme := runtime.NewScheme()
if err := AddToScheme(scheme); err != nil {
t.Fatalf("failed to add scheme: %v", err)
Expand All @@ -210,9 +213,12 @@ func TestValidateUpdate_RejectsBothSet(t *testing.T) {
},
}

_, err := validator.ValidateUpdate(context.Background(), p, p)
if err == nil {
t.Error("expected error when both variant and clusterGroupName are set on update, got nil")
warnings, err := validator.ValidateUpdate(context.Background(), p, p)
if err != nil {
t.Errorf("expected no error when both variant and clusterGroupName are set on update, got: %v", err)
}
if warnings != nil {
t.Errorf("expected no warnings, got: %v", warnings)
}
}

Expand Down
7 changes: 7 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ rules:
- list
- patch
- update
- apiGroups:
- route.openshift.io
resources:
- routes
verbs:
- get
- list
- apiGroups:
- view.open-cluster-management.io
resources:
Expand Down
10 changes: 6 additions & 4 deletions internal/controller/pattern_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ type PatternReconciler struct {
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;create;update;watch
//+kubebuilder:rbac:groups="view.open-cluster-management.io",resources=managedclusterviews,verbs=create
//+kubebuilder:rbac:groups="cluster.open-cluster-management.io",resources=managedclusters,verbs=list;delete
//+kubebuilder:rbac:groups="route.openshift.io",resources=routes,verbs=list;get

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down Expand Up @@ -700,11 +701,12 @@ func (r *PatternReconciler) applyDefaults(input *api.Pattern) (*api.Pattern, err
multiSourceBool := true
output.Spec.MultiSourceConfig.Enabled = &multiSourceBool
}
switch {
case output.Spec.ClusterGroupName == "" && output.Spec.Variant != "":
if output.Spec.Variant != "" {
if output.Spec.ClusterGroupName != "" && output.Spec.ClusterGroupName != output.Spec.Variant {
logOnce(fmt.Sprintf("Both spec.variant (%q) and spec.clusterGroupName (%q) are set, spec.variant will take precedence",
output.Spec.Variant, output.Spec.ClusterGroupName))
}
output.Spec.ClusterGroupName = output.Spec.Variant
case output.Spec.ClusterGroupName != "" && output.Spec.Variant == "":
output.Spec.Variant = output.Spec.ClusterGroupName
}
if output.Spec.MultiSourceConfig.HelmRepoUrl == "" {
output.Spec.MultiSourceConfig.HelmRepoUrl = GiteaHelmRepoUrl
Expand Down
13 changes: 10 additions & 3 deletions internal/controller/pattern_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,13 @@ var _ = Describe("pattern controller - applyDefaults", func() {
Expect(*output.Spec.MultiSourceConfig.Enabled).To(BeTrue())
})

It("should sync Variant from ClusterGroupName when Variant is empty", func() {
It("should keep ClusterGroupName when Variant is empty", func() {
p := buildPatternManifest()
p.Spec.ClusterGroupName = "hub"
p.Spec.Variant = ""
output, err := reconciler.applyDefaults(p)
Expect(err).ToNot(HaveOccurred())
Expect(output.Spec.ClusterGroupName).To(Equal("hub"))
Expect(output.Spec.Variant).To(Equal("hub"))
})

It("should sync ClusterGroupName from Variant when ClusterGroupName is empty", func() {
Expand All @@ -530,7 +529,15 @@ var _ = Describe("pattern controller - applyDefaults", func() {
output, err := reconciler.applyDefaults(p)
Expect(err).ToNot(HaveOccurred())
Expect(output.Spec.ClusterGroupName).To(Equal("factory"))
Expect(output.Spec.Variant).To(Equal("factory"))
})

It("should let Variant win when both are set", func() {
p := buildPatternManifest()
p.Spec.ClusterGroupName = "hub"
p.Spec.Variant = "factory"
output, err := reconciler.applyDefaults(p)
Expect(err).ToNot(HaveOccurred())
Expect(output.Spec.ClusterGroupName).To(Equal("factory"))
})

It("should default HelmRepoUrl when empty", func() {
Expand Down