Skip to content

Commit 9a87fa5

Browse files
authored
Merge pull request #14 from weaveworks/ttl-seconds-after-finished
TLseconds after finished. This adds support for the configuring the Job TTL for bootstrap Jobs. This allows them to be removed after a user-configurable period.
2 parents f882c84 + 4ff46e3 commit 9a87fa5

File tree

7 files changed

+535
-202
lines changed

7 files changed

+535
-202
lines changed

Makefile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION)
3838
IMAGE_TAG := $(shell tools/image-tag)
3939
# Image URL to use all building/pushing image targets
4040
IMG ?= $(IMAGE_TAG_BASE):$(IMAGE_TAG)
41-
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
42-
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
41+
CRD_OPTIONS ?= "crd"
4342
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
4443
ENVTEST_K8S_VERSION = 1.21
4544

@@ -127,11 +126,11 @@ undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/confi
127126

128127
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
129128
controller-gen: ## Download controller-gen locally if necessary.
130-
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.6.1)
129+
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.9.2)
131130

132131
KUSTOMIZE = $(shell pwd)/bin/kustomize
133132
kustomize: ## Download kustomize locally if necessary.
134-
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v3@v3.8.7)
133+
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v3@v4.5.7)
135134

136135
ENVTEST = $(shell pwd)/bin/setup-envtest
137136
envtest: ## Download envtest-setup locally if necessary.
@@ -146,7 +145,7 @@ TMP_DIR=$$(mktemp -d) ;\
146145
cd $$TMP_DIR ;\
147146
go mod init tmp ;\
148147
echo "Downloading $(2)" ;\
149-
GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
148+
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
150149
rm -rf $$TMP_DIR ;\
151150
}
152151
endef

api/v1alpha1/clusterbootstrapconfig_types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ type JobTemplate struct {
3737
//+kubebuilder:validation:Optional
3838
//+kubebuilder:default:=6
3939
BackoffLimit *int32 `json:"backoffLimit,optional"`
40+
41+
// ttlSecondsAfterFinished limits the lifetime of a Job that has finished
42+
// execution (either Complete or Failed). If this field is set,
43+
// ttlSecondsAfterFinished after the Job finishes, it is eligible to be
44+
// automatically deleted. When the Job is being deleted, its lifecycle
45+
// guarantees (e.g. finalizers) will be honored. If this field is unset,
46+
// the Job won't be automatically deleted. If this field is set to zero,
47+
// the Job becomes eligible to be deleted immediately after it finishes.
48+
// +optional
49+
TTLSecondsAfterFinished *int32 `json:"ttlSecondsAfterFinished,omitempty"`
50+
4051
// A batch/v1 Job is created with the Spec as the PodSpec.
4152
Spec corev1.PodSpec `json:"spec"`
4253
}

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/capi.weave.works_clusterbootstrapconfigs.yaml

Lines changed: 492 additions & 195 deletions
Large diffs are not rendered by default.

config/rbac/role.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
---
32
apiVersion: rbac.authorization.k8s.io/v1
43
kind: ClusterRole

controllers/bootstrap.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ func jobFromTemplate(cl *gitopsv1alpha1.GitopsCluster, jt capiv1alpha1.JobTempla
4141
Template: corev1.PodTemplateSpec{
4242
Spec: jt.Spec,
4343
},
44-
BackoffLimit: jt.BackoffLimit,
44+
BackoffLimit: jt.BackoffLimit,
45+
TTLSecondsAfterFinished: jt.TTLSecondsAfterFinished,
4546
},
4647
}
4748
}

controllers/bootstrap_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,27 @@ func makeTestPodSpecWithVolumes(volumes ...corev1.Volume) corev1.PodSpec {
120120
}
121121
}
122122

123+
func Test_bootstrapClusterWithConfig_sets_job_ttl(t *testing.T) {
124+
bc := makeTestClusterBootstrapConfig(func(cfg *capiv1alpha1.ClusterBootstrapConfig) {
125+
cfg.Spec.Template.TTLSecondsAfterFinished = ptrutils.Int32Ptr(66)
126+
})
127+
cl := makeTestCluster()
128+
tc := makeTestClient(t)
129+
130+
if err := bootstrapClusterWithConfig(context.TODO(), logr.Discard(), tc, cl, bc); err != nil {
131+
t.Fatal(err)
132+
}
133+
134+
var jobList batchv1.JobList
135+
if err := tc.List(context.TODO(), &jobList, client.InNamespace(testNamespace)); err != nil {
136+
t.Fatal(err)
137+
}
138+
139+
if ttl := *jobList.Items[0].Spec.TTLSecondsAfterFinished; ttl != 66 {
140+
t.Fatalf("got TTLSecondsAfterFinished %v, want %v", ttl, 66)
141+
}
142+
}
143+
123144
func makeTestCluster(opts ...func(*gitopsv1alpha1.GitopsCluster)) *gitopsv1alpha1.GitopsCluster {
124145
c := &gitopsv1alpha1.GitopsCluster{
125146
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)