Skip to content

Commit

Permalink
Support activeDeadlineSeconds for apprepository-controller (#6967)
Browse files Browse the repository at this point in the history
### Description of the change

Implements #6966 

### Benefits

AppRepository CronJobs supports
[activeDeadlineSeconds](https://kubernetes.io/docs/concepts/workloads/controllers/job/#job-termination-and-cleanup)

### Possible drawbacks

I'm not a golang developer, `go test` passed, not sure if there is
anything else I should be testing?

### Applicable issues

#6966

---------

Signed-off-by: Michael Nelson <[email protected]>
Co-authored-by: Michael Nelson <[email protected]>
  • Loading branch information
rophy and absoludity authored Oct 19, 2023
1 parent a6843f4 commit 181e686
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/apprepository-controller/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func setFlags(c *cobra.Command) {
c.Flags().StringVar(&serveOpts.UserAgentComment, "user-agent-comment", "", "UserAgent comment used during outbound requests")
c.Flags().StringVar(&serveOpts.Crontab, "crontab", "*/10 * * * *", "CronTab to specify schedule")
c.Flags().StringVar(&serveOpts.TTLSecondsAfterFinished, "ttl-lifetime-afterfinished-job", "3600", "Lifetime limit after which the resource Jobs are deleted expressed in seconds by default is 3600 (1h)")
c.Flags().StringVar(&serveOpts.ActiveDeadlineSeconds, "active-deadline-seconds", "", "Seconds after which running pods of the resource Jobs will be terminated.")
c.Flags().Int32Var(&serveOpts.SuccessfulJobsHistoryLimit, "successful-jobs-history-limit", 3, "Number of successful finished jobs to retain")
c.Flags().Int32Var(&serveOpts.FailedJobsHistoryLimit, "failed-jobs-history-limit", 1, "Number of failed finished jobs to retain")
c.Flags().StringVar(&serveOpts.ConcurrencyPolicy, "concurrency-policy", "Replace", "How to treat concurrent executions of a Job. Valid values are: 'Allow', 'Forbid' and 'Replace'")
Expand Down
4 changes: 4 additions & 0 deletions cmd/apprepository-controller/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestParseFlagsCorrect(t *testing.T) {
UserAgentComment: "",
Crontab: "*/10 * * * *",
TTLSecondsAfterFinished: "3600",
ActiveDeadlineSeconds: "",
CustomAnnotations: []string{""},
CustomLabels: []string{""},
ParsedCustomAnnotations: map[string]string{},
Expand Down Expand Up @@ -77,6 +78,7 @@ func TestParseFlagsCorrect(t *testing.T) {
UserAgentComment: "",
Crontab: "*/10 * * * *",
TTLSecondsAfterFinished: "3600",
ActiveDeadlineSeconds: "",
CustomAnnotations: []string{""},
CustomLabels: []string{""},
ParsedCustomAnnotations: map[string]string{},
Expand Down Expand Up @@ -111,6 +113,7 @@ func TestParseFlagsCorrect(t *testing.T) {
"--custom-annotations", "extra13=extra13",
"--custom-labels", "foo14=bar14,foo14x=bar14x",
"--ttl-lifetime-afterfinished-job", "1200",
"--active-deadline-seconds", "300",
"--v1-beta1-cron-jobs", "true",
"--successful-jobs-history-limit", "33",
"--failed-jobs-history-limit", "11",
Expand All @@ -134,6 +137,7 @@ func TestParseFlagsCorrect(t *testing.T) {
UserAgentComment: "foo11",
Crontab: "foo12",
TTLSecondsAfterFinished: "1200",
ActiveDeadlineSeconds: "300",
CustomAnnotations: []string{"foo13=bar13", "foo13x=bar13x", "extra13=extra13"},
CustomLabels: []string{"foo14=bar14", "foo14x=bar14x"},
ParsedCustomAnnotations: map[string]string{"foo13": "bar13", "foo13x": "bar13x", "extra13": "extra13"},
Expand Down
13 changes: 13 additions & 0 deletions cmd/apprepository-controller/server/job_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ func generateJobSpec(apprepo *apprepov1alpha1.AppRepository, config Config, rest

return batchv1.JobSpec{
TTLSecondsAfterFinished: ttlLifetimeJobs(config),
ActiveDeadlineSeconds: activeDeadlineSeconds(config),
Template: podTemplateSpec,
}
}
Expand Down Expand Up @@ -331,6 +332,18 @@ func ttlLifetimeJobs(config Config) *int32 {
return nil
}

// activeDeadlineSeconds return active deadline seconds set by user otherwise return nil
func activeDeadlineSeconds(config Config) *int64 {
if config.ActiveDeadlineSeconds != "" {
configDeadline, err := strconv.ParseInt(config.ActiveDeadlineSeconds, 10, 64)
if err == nil {
result := int64(configDeadline)
return &result
}
}
return nil
}

// cronJobName returns a unique name for the CronJob managed by an AppRepository
func cronJobName(namespace, name string, addDash bool) string {
// the "apprepo--sync-" string has 14 chars, which leaves us 52-14=38 chars for the final name
Expand Down
22 changes: 22 additions & 0 deletions cmd/apprepository-controller/server/job_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
const repoSyncImage = "bitnami/kubeapps-asset-syncer:2.0.0-scratch-r2"

var defaultTTL = int32(3600)
var defaultActiveDeadline = int64(300)

func Test_newCronJob(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -74,6 +75,7 @@ func Test_newCronJob(t *testing.T) {
JobTemplate: batchv1.JobTemplateSpec{
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -158,6 +160,7 @@ func Test_newCronJob(t *testing.T) {
JobTemplate: batchv1.JobTemplateSpec{
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -255,6 +258,7 @@ func Test_newCronJob(t *testing.T) {
JobTemplate: batchv1.JobTemplateSpec{
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -349,6 +353,7 @@ func Test_newCronJob(t *testing.T) {
JobTemplate: batchv1.JobTemplateSpec{
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -449,6 +454,7 @@ func Test_newCronJob(t *testing.T) {
JobTemplate: batchv1.JobTemplateSpec{
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -543,6 +549,7 @@ func Test_newCronJob(t *testing.T) {
JobTemplate: batchv1.JobTemplateSpec{
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -666,6 +673,7 @@ func Test_newSyncJob(t *testing.T) {
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -738,6 +746,7 @@ func Test_newSyncJob(t *testing.T) {
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -810,6 +819,7 @@ func Test_newSyncJob(t *testing.T) {
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -896,6 +906,7 @@ func Test_newSyncJob(t *testing.T) {
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -989,6 +1000,7 @@ func Test_newSyncJob(t *testing.T) {
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1093,6 +1105,7 @@ func Test_newSyncJob(t *testing.T) {
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1199,6 +1212,7 @@ func Test_newSyncJob(t *testing.T) {
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1304,6 +1318,7 @@ func Test_newSyncJob(t *testing.T) {
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1390,6 +1405,7 @@ func Test_newSyncJob(t *testing.T) {
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1476,6 +1492,7 @@ func Test_newSyncJob(t *testing.T) {
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1563,6 +1580,7 @@ func Test_newSyncJob(t *testing.T) {
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1651,6 +1669,7 @@ func Test_newSyncJob(t *testing.T) {
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1733,6 +1752,7 @@ func Test_newCleanupJob(t *testing.T) {
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1785,6 +1805,7 @@ func Test_newCleanupJob(t *testing.T) {
},
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &defaultTTL,
ActiveDeadlineSeconds: &defaultActiveDeadline,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -2002,6 +2023,7 @@ func makeDefaultConfig() Config {
DBSecretKey: "postgresql-root-password",
UserAgentComment: "",
TTLSecondsAfterFinished: "3600",
ActiveDeadlineSeconds: "300",
Crontab: "*/10 * * * *",
CustomAnnotations: []string{},
CustomLabels: []string{},
Expand Down
1 change: 1 addition & 0 deletions cmd/apprepository-controller/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Config struct {
UserAgentComment string
Crontab string
TTLSecondsAfterFinished string
ActiveDeadlineSeconds string
SuccessfulJobsHistoryLimit int32
FailedJobsHistoryLimit int32
ConcurrencyPolicy string
Expand Down
1 change: 1 addition & 0 deletions cmd/kubeapps-apis/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ RUN /tmp/buf lint ./cmd/kubeapps-apis
# Build the main grpc server
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
GOPROXY="https://proxy.golang.org,direct" \
go build \
-ldflags "-X github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/cmd.version=$VERSION" \
./cmd/kubeapps-apis
Expand Down

0 comments on commit 181e686

Please sign in to comment.