Skip to content

Commit d5bcc3b

Browse files
authored
Merge pull request #33 from fluxcd/health-checks
Improve health checking
2 parents 6cfe2ff + 7e584dc commit d5bcc3b

File tree

4 files changed

+52
-39
lines changed

4 files changed

+52
-39
lines changed

README.md

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,15 @@ Specifications:
3131
## Usage
3232

3333
The kustomize-controller is part of a composable GitOps toolkit and depends on
34-
[source-controller](https://github.com/fluxcd/source-controller) to provide the raw Kubernetes
35-
manifests and `kustomization.yaml` file.
34+
[source-controller](https://github.com/fluxcd/source-controller) to acquire the Kubernetes
35+
manifests from Git repositories.
3636

3737
### Install the controllers
3838

39-
Install source-controller with:
39+
Install the source and kustomize controllers in the `kustomize-system` namespace:
4040

4141
```bash
42-
kustomize build https://github.com/fluxcd/source-controller//config/default?ref=v0.0.1-alpha.4 \
43-
kubectl apply -f-
44-
```
45-
46-
Install kustomize-controller with:
47-
48-
```bash
49-
kustomize build https://github.com/fluxcd/kustomize-controller//config/default?ref=v0.0.1-alpha.7 \
42+
kustomize build https://github.com/fluxcd/kustomize-controller//config/default?ref=master \
5043
kubectl apply -f-
5144
```
5245

@@ -62,7 +55,7 @@ metadata:
6255
namespace: kustomize-system
6356
spec:
6457
interval: 1m
65-
url: https://github.com/stefanprodan/podinfo-deploy
58+
url: https://github.com/stefanprodan/podinfo
6659
ref:
6760
branch: master
6861
```
@@ -95,29 +88,31 @@ metadata:
9588
namespace: kustomize-system
9689
spec:
9790
interval: 5m
98-
path: "./overlays/dev/"
91+
path: "./deploy/overlays/dev/"
9992
prune: true
10093
sourceRef:
10194
kind: GitRepository
10295
name: podinfo
10396
validation: client
10497
healthChecks:
10598
- kind: Deployment
106-
name: podinfo
99+
name: frontend
100+
namespace: dev
101+
- kind: Deployment
102+
name: backend
107103
namespace: dev
108104
timeout: 80s
109105
```
110106
111-
> **Note** that if your repository contains only plain Kubernetes manifests,
112-
> the controller will
107+
> **Note** that if your repository contains only plain Kubernetes manifests, the controller will
113108
> [automatically generate](docs/spec/v1alpha1/kustomization.md#generate-kustomizationyaml)
114109
> a kustomization.yaml file inside the specified path.
115110
116111
A detailed explanation of the Kustomization object and its fields
117112
can be found in the [specification doc](docs/spec/v1alpha1/README.md).
118113
119114
Based on the above definition, the kustomize-controller fetches the Git repository content from source-controller,
120-
generates Kubernetes manifests by running kustomize build inside `./overlays/dev/`,
115+
generates Kubernetes manifests by running kustomize build inside `./deploy/overlays/dev/`,
121116
and validates them with a dry-run apply. If the manifests pass validation, the controller will apply them
122117
on the cluster and starts the health assessment of the deployed workload. If the health checks are passing, the
123118
Kustomization object status transitions to a ready state.
@@ -127,7 +122,7 @@ Kustomization object status transitions to a ready state.
127122
You can wait for the kustomize controller to complete the deployment with:
128123

129124
```bash
130-
kubectl wait kustomization/podinfo-dev --for=condition=ready
125+
kubectl -n kustomize-system wait kustomization/podinfo-dev --for=condition=ready
131126
```
132127

133128
When the controller finishes the reconciliation, it will log the applied objects:
@@ -142,20 +137,24 @@ kubectl -n kustomize-system logs deploy/kustomize-controller | jq .
142137
"ts": 1587195448.071468,
143138
"logger": "controllers.Kustomization",
144139
"msg": "Kustomization applied in 1.436096591s",
145-
"kustomization": "default/podinfo-dev",
140+
"kustomization": "kustomize-system/podinfo-dev",
146141
"output": {
147142
"namespace/dev": "created",
148-
"service/podinfo": "created",
149-
"deployment.apps/podinfo": "created",
150-
"horizontalpodautoscaler.autoscaling/podinfo": "created"
143+
"service/frontend": "created",
144+
"deployment.apps/frontend": "created",
145+
"horizontalpodautoscaler.autoscaling/frontend": "created",
146+
"service/backend": "created",
147+
"deployment.apps/backend": "created",
148+
"horizontalpodautoscaler.autoscaling/backend": "created"
151149
}
152150
}
153151
```
154152

155153
You can trigger a kustomize build and apply any time with:
156154

157155
```bash
158-
kubectl annotate --overwrite kustomization/podinfo-dev kustomize.fluxcd.io/syncAt="$(date +%s)"
156+
kubectl -n kustomize-system annotate --overwrite kustomization/podinfo-dev \
157+
kustomize.fluxcd.io/syncAt="$(date +%s)"
159158
```
160159

161160
When the source controller pulls a new Git revision, the kustomize controller will detect that the
@@ -175,8 +174,8 @@ status:
175174

176175
```json
177176
{
178-
"kustomization": "default/podinfo-dev",
179-
"error": "Error from server (NotFound): error when creating \"podinfo-dev.yaml\": namespaces \"dev\" not found\n"
177+
"kustomization": "kustomize-system/podinfo-dev",
178+
"error": "Error from server (NotFound): error when creating podinfo-dev.yaml: namespaces dev not found"
180179
}
181180
```
182181

@@ -196,7 +195,7 @@ metadata:
196195
namespace: kustomize-system
197196
spec:
198197
interval: 10m
199-
path: "./profiles/default/"
198+
path: "./istio/system/"
200199
sourceRef:
201200
kind: GitRepository
202201
name: istio
@@ -234,9 +233,9 @@ metadata:
234233
namespace: kustomize-system
235234
spec:
236235
interval: 5m
237-
url: https://github.com/stefanprodan/podinfo-deploy
236+
url: https://github.com/stefanprodan/podinfo
238237
ref:
239-
semver: ">=0.0.1-rc.1 <1.0.0"
238+
semver: ">=3.2.3 <4.0.0"
240239
```
241240

242241
With `ref.semver` we configure source controller to pull the Git tags and create an artifact from the most recent tag
@@ -252,14 +251,14 @@ metadata:
252251
namespace: kustomize-system
253252
spec:
254253
interval: 10m
255-
path: "./overlays/production/"
254+
path: "./deploy/overlays/production/"
256255
sourceRef:
257256
kind: GitRepository
258257
name: podinfo-releases
259258
```
260259

261-
Based on the above definition, the kustomize controller will build and apply a kustomization that matches the semver range
262-
set in the Git repository manifest.
260+
Based on the above definition, the kustomize controller will apply the kustomization that matches the semver range
261+
set in the Git repository.
263262

264263
### Configure alerting
265264

controllers/gitrepository_controller.go renamed to controllers/gitrepository_watcher.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ import (
3434
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1alpha1"
3535
)
3636

37-
// KustomizationReconciler watches a GitRepository object
37+
// GitRepositoryWatcher watches GitRepository objects for revision changes
38+
// and triggers a sync for all the Kustomizations that reference a changed source
3839
type GitRepositoryWatcher struct {
3940
client.Client
4041
Log logr.Logger
@@ -54,7 +55,7 @@ func (r *GitRepositoryWatcher) Reconcile(req ctrl.Request) (ctrl.Result, error)
5455
}
5556

5657
log := r.Log.WithValues(strings.ToLower(repo.Kind), req.NamespacedName)
57-
log.Info("New artifact detected")
58+
log.Info("new artifact detected")
5859

5960
// get the list of kustomizations that are using this Git repository
6061
var list kustomizev1.KustomizationList

controllers/kustomization_controller.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,17 @@ func (r *KustomizationReconciler) checkHealth(kustomization kustomizev1.Kustomiz
513513
var alerts string
514514

515515
for _, check := range kustomization.Spec.HealthChecks {
516-
cmd := fmt.Sprintf("kubectl -n %s rollout status %s %s --timeout=%s",
517-
check.Namespace, check.Kind, check.Name, kustomization.GetTimeout())
516+
cmd := fmt.Sprintf("until kubectl -n %s get %s %s ; do sleep 2; done",
517+
check.Namespace, check.Kind, check.Name)
518518
command := exec.CommandContext(ctx, "/bin/sh", "-c", cmd)
519+
if _, err := command.CombinedOutput(); err != nil {
520+
return fmt.Errorf("health check timeout for %s '%s/%s': %w",
521+
check.Kind, check.Namespace, check.Name, err)
522+
}
523+
524+
cmd = fmt.Sprintf("kubectl -n %s rollout status %s %s --timeout=%s",
525+
check.Namespace, check.Kind, check.Name, kustomization.GetTimeout())
526+
command = exec.CommandContext(ctx, "/bin/sh", "-c", cmd)
519527
output, err := command.CombinedOutput()
520528
if err != nil {
521529
if errors.Is(err, context.DeadlineExceeded) {

docs/spec/README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@ for example a validation controller such as OPA Gatekeeper should be up and runn
1515
applying other manifests on the cluster. Another example is a service mesh admission controller,
1616
the proxy injector must be functional before deploying applications into the mesh.
1717

18-
When operating a cluster, different teams may wish to receive notification about the status
19-
of their CD pipelines. For example, the on-call team would receive alerts about all
20-
failures in the prod namespace, while the frontend team may wish to be alerted when a new version
21-
of the frontend app was deployed and if the deployment is healthy, no matter the namespace.
18+
When a cluster is shared with multiple teams, a cluster admin may wish to assign roles and service
19+
accounts to each team. The manifests owned by a team will be applied on the cluster using
20+
the team's account thus ensuring isolation between teams. For example, an admin can
21+
restrict the operations performed on the cluster by a team to a single namespace.
2222

2323
When dealing with an incident, one may wish to suspend the reconciliation of some workloads and
2424
pin the reconciliation of others to a specific Git revision, without having to stop the reconciler
2525
and affect the whole cluster.
2626

27+
When operating a cluster, different teams may wish to receive notification about the status
28+
of their CD pipelines. For example, the on-call team would receive alerts about all
29+
failures in the prod namespace, while the frontend team may wish to be alerted when a new version
30+
of the frontend app was deployed and if the deployment is healthy, no matter the namespace.
31+
2732
## Design
2833

2934
The reconciliation process can be defined with a Kubernetes custom resource

0 commit comments

Comments
 (0)