Skip to content

Commit 7d65478

Browse files
committed
Move PatternsOperatorConfig to separate file and add convenient methods to get values
1 parent f7655a7 commit 7d65478

7 files changed

Lines changed: 89 additions & 95 deletions

File tree

internal/controller/argo.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func newArgoCD(name, namespace string, patternsOperatorConfig PatternsOperatorCo
6262
"g, cluster-admins, role:admin",
6363
"g, admin, role:admin",
6464
}
65-
for argoAdmin := range strings.SplitSeq(patternsOperatorConfig.getValueWithDefault("gitops.additionalArgoAdmins"), ",") {
65+
for argoAdmin := range strings.SplitSeq(patternsOperatorConfig.getStringValue("gitops.additionalArgoAdmins"), ",") {
6666
argoAdmin = strings.TrimSpace(argoAdmin)
6767
if argoAdmin != "" {
6868
argoPolicies = append(argoPolicies, "g, "+argoAdmin+", role:admin")
@@ -160,7 +160,7 @@ health_status.message = "An install plan for a subscription is pending installat
160160
return health_status`,
161161
},
162162
}
163-
if strings.EqualFold(patternsOperatorConfig.getValueWithDefault("gitops.applicationHealthCheckEnabled"), "true") {
163+
if patternsOperatorConfig.getBoolValue("gitops.applicationHealthCheckEnabled") {
164164
// As of ArgoCD 1.8 the Application health check was dropped (see https://github.com/argoproj/argo-cd/issues/3781),
165165
// but in app-of-apps pattern this is needed in order to implement children apps dependencies via sync-waves
166166
resourceHealthChecks = append(resourceHealthChecks, argooperator.ResourceHealthCheck{
@@ -978,9 +978,9 @@ func newArgoGiteaApplication(p *api.Pattern, patternsOperatorConfig PatternsOper
978978
},
979979
Project: "default",
980980
Source: &argoapi.ApplicationSource{
981-
RepoURL: patternsOperatorConfig.getValueWithDefault("gitea.helmRepoUrl"),
982-
TargetRevision: patternsOperatorConfig.getValueWithDefault("gitea.chartVersion"),
983-
Chart: patternsOperatorConfig.getValueWithDefault("gitea.chartName"),
981+
RepoURL: patternsOperatorConfig.getStringValue("gitea.helmRepoUrl"),
982+
TargetRevision: patternsOperatorConfig.getStringValue("gitea.chartVersion"),
983+
Chart: patternsOperatorConfig.getStringValue("gitea.chartName"),
984984
Helm: &argoapi.ApplicationSourceHelm{
985985
Parameters: parameters,
986986
},

internal/controller/defaults.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,3 @@ const (
8585

8686
// Experimental Capabilities that can be enabled
8787
// Currently none
88-
89-
var DefaultPatternsOperatorConfig = map[string]string{
90-
"gitops.catalogSource": GitOpsDefaultCatalogSource,
91-
"gitops.channel": GitOpsDefaultChannel,
92-
"gitops.sourceNamespace": GitOpsDefaultCatalogSourceNamespace,
93-
"gitops.installApprovalPlan": GitOpsDefaultApprovalPlan,
94-
"gitops.csv": GitOpsDefaultCSV,
95-
"gitops.additionalArgoAdmins": "",
96-
"gitops.applicationHealthCheckEnabled": "false",
97-
"gitea.chartName": GiteaChartName,
98-
"gitea.helmRepoUrl": GiteaHelmRepoUrl,
99-
"gitea.chartVersion": GiteaDefaultChartVersion,
100-
"catalog.image": "",
101-
}
102-
103-
type PatternsOperatorConfig map[string]string
104-
105-
func (g PatternsOperatorConfig) getValueWithDefault(k string) string {
106-
if v, present := g[k]; present {
107-
return v
108-
}
109-
if defaultValue, present := DefaultPatternsOperatorConfig[k]; present {
110-
return defaultValue
111-
}
112-
return ""
113-
}

internal/controller/pattern_controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ func (r *PatternReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
150150
return reconcile.Result{}, err
151151
}
152152

153-
// Set Patterns Operator config to defaults
154-
patternsOperatorConfig := DefaultPatternsOperatorConfig
153+
var patternsOperatorConfig PatternsOperatorConfig
155154

156155
// We try to get the configuration ConfigMap. The method getPatternsOperatorConfigMap returns nil, nil if the ConfigMap doesn't exist
157156
configCM, err := r.getPatternsOperatorConfigMap()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package controllers
2+
3+
import (
4+
"strings"
5+
)
6+
7+
type PatternsOperatorConfig map[string]string
8+
9+
var DefaultPatternsOperatorConfig = PatternsOperatorConfig{
10+
"gitops.catalogSource": GitOpsDefaultCatalogSource,
11+
"gitops.channel": GitOpsDefaultChannel,
12+
"gitops.sourceNamespace": GitOpsDefaultCatalogSourceNamespace,
13+
"gitops.installApprovalPlan": GitOpsDefaultApprovalPlan,
14+
"gitops.csv": GitOpsDefaultCSV,
15+
"gitops.additionalArgoAdmins": "",
16+
"gitops.applicationHealthCheckEnabled": "false",
17+
"gitea.chartName": GiteaChartName,
18+
"gitea.helmRepoUrl": GiteaHelmRepoUrl,
19+
"gitea.chartVersion": GiteaDefaultChartVersion,
20+
"catalog.image": "",
21+
}
22+
23+
func (g PatternsOperatorConfig) getStringValue(k string) string {
24+
if v, present := g[k]; present {
25+
return v
26+
}
27+
if defaultValue, present := DefaultPatternsOperatorConfig[k]; present {
28+
return defaultValue
29+
}
30+
return ""
31+
}
32+
33+
func (g PatternsOperatorConfig) getBoolValue(k string) bool {
34+
if v, present := g[k]; present {
35+
return strings.EqualFold(v, "true")
36+
}
37+
if defaultValue, present := DefaultPatternsOperatorConfig[k]; present {
38+
return strings.EqualFold(defaultValue, "true")
39+
}
40+
return false
41+
}

internal/controller/defaults_test.go renamed to internal/controller/patterns_operator_config_test.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,81 @@ import (
55
. "github.com/onsi/gomega"
66
)
77

8-
var _ = Describe("PatternsOperatorConfig getValueWithDefault", func() {
8+
var _ = Describe("PatternsOperatorConfig get values", func() {
99
Context("when the key exists in the config", func() {
1010
It("should return the config value", func() {
1111
config := PatternsOperatorConfig{
1212
"gitops.channel": "custom-channel",
1313
}
14-
Expect(config.getValueWithDefault("gitops.channel")).To(Equal("custom-channel"))
14+
Expect(config.getStringValue("gitops.channel")).To(Equal("custom-channel"))
1515
})
1616
})
1717

1818
Context("when the key does not exist in config but exists in defaults", func() {
1919
It("should return the default value for gitops.channel", func() {
2020
config := PatternsOperatorConfig{}
21-
Expect(config.getValueWithDefault("gitops.channel")).To(Equal(GitOpsDefaultChannel))
21+
Expect(config.getStringValue("gitops.channel")).To(Equal(GitOpsDefaultChannel))
2222
})
2323

2424
It("should return the default value for gitops.catalogSource", func() {
2525
config := PatternsOperatorConfig{}
26-
Expect(config.getValueWithDefault("gitops.catalogSource")).To(Equal(GitOpsDefaultCatalogSource))
26+
Expect(config.getStringValue("gitops.catalogSource")).To(Equal(GitOpsDefaultCatalogSource))
2727
})
2828

2929
It("should return the default value for gitops.sourceNamespace", func() {
3030
config := PatternsOperatorConfig{}
31-
Expect(config.getValueWithDefault("gitops.sourceNamespace")).To(Equal(GitOpsDefaultCatalogSourceNamespace))
31+
Expect(config.getStringValue("gitops.sourceNamespace")).To(Equal(GitOpsDefaultCatalogSourceNamespace))
3232
})
3333

3434
It("should return the default value for gitops.installApprovalPlan", func() {
3535
config := PatternsOperatorConfig{}
36-
Expect(config.getValueWithDefault("gitops.installApprovalPlan")).To(Equal(GitOpsDefaultApprovalPlan))
36+
Expect(config.getStringValue("gitops.installApprovalPlan")).To(Equal(GitOpsDefaultApprovalPlan))
37+
})
38+
39+
It("should return the default value for gitops.csv", func() {
40+
config := PatternsOperatorConfig{}
41+
Expect(config.getStringValue("gitops.csv")).To(Equal(""))
3742
})
3843

3944
It("should return the default value for gitops.additionalArgoAdmins", func() {
4045
config := PatternsOperatorConfig{}
41-
Expect(config.getValueWithDefault("gitops.additionalArgoAdmins")).To(Equal(""))
46+
Expect(config.getStringValue("gitops.additionalArgoAdmins")).To(Equal(""))
47+
})
48+
49+
It("should return the default value for gitops.applicationHealthCheckEnabled", func() {
50+
config := PatternsOperatorConfig{}
51+
Expect(config.getBoolValue("gitops.applicationHealthCheckEnabled")).To(BeFalse())
4252
})
4353

4454
It("should return the default value for gitea.chartName", func() {
4555
config := PatternsOperatorConfig{}
46-
Expect(config.getValueWithDefault("gitea.chartName")).To(Equal(GiteaChartName))
56+
Expect(config.getStringValue("gitea.chartName")).To(Equal(GiteaChartName))
4757
})
4858

4959
It("should return the default value for gitea.helmRepoUrl", func() {
5060
config := PatternsOperatorConfig{}
51-
Expect(config.getValueWithDefault("gitea.helmRepoUrl")).To(Equal(GiteaHelmRepoUrl))
61+
Expect(config.getStringValue("gitea.helmRepoUrl")).To(Equal(GiteaHelmRepoUrl))
5262
})
5363

5464
It("should return the default value for gitea.chartVersion", func() {
5565
config := PatternsOperatorConfig{}
56-
Expect(config.getValueWithDefault("gitea.chartVersion")).To(Equal(GiteaDefaultChartVersion))
66+
Expect(config.getStringValue("gitea.chartVersion")).To(Equal(GiteaDefaultChartVersion))
67+
})
68+
69+
It("should return the default value for catalog.image", func() {
70+
config := PatternsOperatorConfig{}
71+
Expect(config.getStringValue("catalog.image")).To(Equal(""))
5772
})
5873
})
5974

6075
Context("when the key does not exist in config or defaults", func() {
61-
It("should return an empty string", func() {
76+
It("should return an empty string for string parameters", func() {
77+
config := PatternsOperatorConfig{}
78+
Expect(config.getStringValue("nonexistent.key")).To(Equal(""))
79+
})
80+
It("should return false for boolean parameters", func() {
6281
config := PatternsOperatorConfig{}
63-
Expect(config.getValueWithDefault("nonexistent.key")).To(Equal(""))
82+
Expect(config.getBoolValue("nonexistent.key")).To(BeFalse())
6483
})
6584
})
6685

@@ -69,14 +88,14 @@ var _ = Describe("PatternsOperatorConfig getValueWithDefault", func() {
6988
config := PatternsOperatorConfig{
7089
"gitops.channel": "gitops-1.99",
7190
}
72-
Expect(config.getValueWithDefault("gitops.channel")).To(Equal("gitops-1.99"))
91+
Expect(config.getStringValue("gitops.channel")).To(Equal("gitops-1.99"))
7392
})
7493
})
7594

7695
Context("when config is nil", func() {
7796
It("should return the default value", func() {
7897
var config PatternsOperatorConfig
79-
Expect(config.getValueWithDefault("gitops.channel")).To(Equal(GitOpsDefaultChannel))
98+
Expect(config.getStringValue("gitops.channel")).To(Equal(GitOpsDefaultChannel))
8099
})
81100
})
82101
})
@@ -88,18 +107,16 @@ var _ = Describe("DefaultPatternsOperatorConfig", func() {
88107
"gitops.channel",
89108
"gitops.sourceNamespace",
90109
"gitops.installApprovalPlan",
110+
"gitops.csv",
111+
"gitops.additionalArgoAdmins",
112+
"gitops.applicationHealthCheckEnabled",
91113
"gitea.chartName",
92114
"gitea.helmRepoUrl",
93115
"gitea.chartVersion",
116+
"catalog.image",
94117
}
95118
for _, key := range expectedKeys {
96119
Expect(DefaultPatternsOperatorConfig).To(HaveKey(key))
97120
}
98121
})
99-
100-
It("should have correct default values", func() {
101-
Expect(DefaultPatternsOperatorConfig["gitops.catalogSource"]).To(Equal("redhat-operators"))
102-
Expect(DefaultPatternsOperatorConfig["gitops.sourceNamespace"]).To(Equal("openshift-marketplace"))
103-
Expect(DefaultPatternsOperatorConfig["gitops.installApprovalPlan"]).To(Equal("Automatic"))
104-
})
105122
})

internal/controller/subscription.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ func newSubscription(patternsOperatorConfig PatternsOperatorConfig, disableDefau
3434

3535
var installPlanApproval operatorv1alpha1.Approval
3636

37-
if patternsOperatorConfig.getValueWithDefault("gitops.installApprovalPlan") == "Manual" {
37+
if patternsOperatorConfig.getStringValue("gitops.installApprovalPlan") == "Manual" {
3838
installPlanApproval = operatorv1alpha1.ApprovalManual
3939
} else {
4040
installPlanApproval = operatorv1alpha1.ApprovalAutomatic
4141
}
4242

4343
spec := &operatorv1alpha1.SubscriptionSpec{
44-
CatalogSource: patternsOperatorConfig.getValueWithDefault("gitops.catalogSource"),
45-
CatalogSourceNamespace: patternsOperatorConfig.getValueWithDefault("gitops.sourceNamespace"),
44+
CatalogSource: patternsOperatorConfig.getStringValue("gitops.catalogSource"),
45+
CatalogSourceNamespace: patternsOperatorConfig.getStringValue("gitops.sourceNamespace"),
4646
Package: GitOpsDefaultPackageName,
47-
Channel: patternsOperatorConfig.getValueWithDefault("gitops.channel"),
48-
StartingCSV: patternsOperatorConfig.getValueWithDefault("gitops.csv"),
47+
Channel: patternsOperatorConfig.getStringValue("gitops.channel"),
48+
StartingCSV: patternsOperatorConfig.getStringValue("gitops.csv"),
4949
InstallPlanApproval: installPlanApproval,
5050
Config: &operatorv1alpha1.SubscriptionConfig{
5151
Env: newSubscriptionEnvVars(disableDefaultInstance),

internal/controller/values_test.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -405,40 +405,3 @@ var _ = Describe("CountApplicationsAndSets", func() {
405405
})
406406
})
407407
})
408-
409-
var _ = Describe("PatternsOperatorConfig getValueWithDefault", func() {
410-
Context("when value exists in config", func() {
411-
It("should return the configured value", func() {
412-
config := PatternsOperatorConfig{"key1": "value1"}
413-
Expect(config.getValueWithDefault("key1")).To(Equal("value1"))
414-
})
415-
})
416-
417-
Context("when value does not exist in config but has a default", func() {
418-
It("should return the default value", func() {
419-
config := PatternsOperatorConfig{}
420-
Expect(config.getValueWithDefault("gitops.channel")).To(Equal(GitOpsDefaultChannel))
421-
})
422-
})
423-
424-
Context("when value does not exist anywhere", func() {
425-
It("should return empty string", func() {
426-
config := PatternsOperatorConfig{}
427-
Expect(config.getValueWithDefault("nonexistent.key")).To(Equal(""))
428-
})
429-
})
430-
431-
Context("when config overrides a default", func() {
432-
It("should return the config value not the default", func() {
433-
config := PatternsOperatorConfig{"gitops.channel": "custom-channel"}
434-
Expect(config.getValueWithDefault("gitops.channel")).To(Equal("custom-channel"))
435-
})
436-
})
437-
438-
Context("when config is nil", func() {
439-
It("should return the default value", func() {
440-
var config PatternsOperatorConfig
441-
Expect(config.getValueWithDefault("gitops.channel")).To(Equal(GitOpsDefaultChannel))
442-
})
443-
})
444-
})

0 commit comments

Comments
 (0)