Skip to content

Commit c5bae1f

Browse files
Merge pull request #658 from mbaldessari/custom-hc
Allow for a custom healthcheck field in the operator configmap
2 parents 5422ed6 + 0087deb commit c5bae1f

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

internal/controller/argo.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"k8s.io/client-go/dynamic"
3535
"k8s.io/client-go/kubernetes"
3636
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
37+
"sigs.k8s.io/yaml"
3738

3839
argooperator "github.com/argoproj-labs/argocd-operator/api/v1beta1"
3940
argoapi "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
@@ -192,6 +193,15 @@ return health_status`,
192193
})
193194
}
194195

196+
if customChecksYAML := patternsOperatorConfig.getStringValue("gitops.customHealthChecks"); customChecksYAML != "" {
197+
var customChecks []argooperator.ResourceHealthCheck
198+
if err := yaml.Unmarshal([]byte(customChecksYAML), &customChecks); err != nil {
199+
log.Printf("Failed to parse gitops.customHealthChecks: %v", err)
200+
} else {
201+
resourceHealthChecks = append(resourceHealthChecks, customChecks...)
202+
}
203+
}
204+
195205
trueBool := true
196206
initVolumes := []v1.Volume{
197207
{

internal/controller/argo_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,6 +2241,56 @@ var _ = Describe("newArgoCD", func() {
22412241
Expect(argo.Spec.ResourceHealthChecks[1].Kind).To(Equal("Application"))
22422242
})
22432243

2244+
It("should append custom health checks from gitops.customHealthChecks", func() {
2245+
customYAML := `- group: apps
2246+
kind: Deployment
2247+
check: |
2248+
hs = {}
2249+
hs.status = "Healthy"
2250+
return hs
2251+
- group: batch
2252+
kind: Job
2253+
check: |
2254+
hs = {}
2255+
hs.status = "Progressing"
2256+
return hs`
2257+
argo = newArgoCD("test-argo", "test-ns", PatternsOperatorConfig{"gitops.customHealthChecks": customYAML})
2258+
Expect(argo.Spec.ResourceHealthChecks).To(HaveLen(3))
2259+
Expect(argo.Spec.ResourceHealthChecks[0].Group).To(Equal("operators.coreos.com"))
2260+
Expect(argo.Spec.ResourceHealthChecks[1].Group).To(Equal("apps"))
2261+
Expect(argo.Spec.ResourceHealthChecks[1].Kind).To(Equal("Deployment"))
2262+
Expect(argo.Spec.ResourceHealthChecks[2].Group).To(Equal("batch"))
2263+
Expect(argo.Spec.ResourceHealthChecks[2].Kind).To(Equal("Job"))
2264+
})
2265+
2266+
It("should append custom health checks alongside Application health check when both are enabled", func() {
2267+
customYAML := `- group: apps
2268+
kind: Deployment
2269+
check: |
2270+
hs = {}
2271+
hs.status = "Healthy"
2272+
return hs`
2273+
argo = newArgoCD("test-argo", "test-ns", PatternsOperatorConfig{
2274+
"gitops.applicationHealthCheckEnabled": "true",
2275+
"gitops.customHealthChecks": customYAML,
2276+
})
2277+
Expect(argo.Spec.ResourceHealthChecks).To(HaveLen(3))
2278+
Expect(argo.Spec.ResourceHealthChecks[0].Group).To(Equal("operators.coreos.com"))
2279+
Expect(argo.Spec.ResourceHealthChecks[1].Group).To(Equal("argoproj.io"))
2280+
Expect(argo.Spec.ResourceHealthChecks[2].Group).To(Equal("apps"))
2281+
})
2282+
2283+
It("should handle invalid YAML in gitops.customHealthChecks gracefully", func() {
2284+
argo = newArgoCD("test-argo", "test-ns", PatternsOperatorConfig{"gitops.customHealthChecks": "not: valid: yaml: list"})
2285+
Expect(argo.Spec.ResourceHealthChecks).To(HaveLen(1))
2286+
Expect(argo.Spec.ResourceHealthChecks[0].Group).To(Equal("operators.coreos.com"))
2287+
})
2288+
2289+
It("should not add custom health checks when gitops.customHealthChecks is empty", func() {
2290+
argo = newArgoCD("test-argo", "test-ns", PatternsOperatorConfig{"gitops.customHealthChecks": ""})
2291+
Expect(argo.Spec.ResourceHealthChecks).To(HaveLen(1))
2292+
})
2293+
22442294
})
22452295

22462296
var _ = Describe("commonSyncPolicy", func() {

internal/controller/patterns_operator_config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var DefaultPatternsOperatorConfig = PatternsOperatorConfig{
2020
"gitops.csv": GitOpsDefaultCSV,
2121
"gitops.additionalArgoAdmins": "",
2222
"gitops.applicationHealthCheckEnabled": "false",
23+
"gitops.customHealthChecks": "",
2324
"gitea.chartName": GiteaChartName,
2425
"gitea.helmRepoUrl": GiteaHelmRepoUrl,
2526
"gitea.chartVersion": GiteaDefaultChartVersion,

0 commit comments

Comments
 (0)