Skip to content

Commit

Permalink
support startingDeadlineSeconds in pruner cron jobs
Browse files Browse the repository at this point in the history
Signed-off-by: Jeeva Kandasamy <jkandasa@redhat.com>
  • Loading branch information
jkandasa authored and tekton-robot committed Mar 21, 2024
1 parent 90d15f1 commit eb9e5f2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/TektonConfig.md
Expand Up @@ -282,6 +282,7 @@ Example:
pruner:
disabled: false
schedule: "0 8 * * *"
startingDeadlineSeconds: 100 # optional
resources:
- taskrun
- pipelinerun
Expand All @@ -292,6 +293,7 @@ pruner:
```
- `disabled` : if the value set as `true`, pruner feature will be disabled (default: `false`)
- `schedule`: how often to run the pruner job. User can understand the schedule syntax [here][schedule].
- `startingDeadlineSeconds`: Optional deadline in seconds for starting the job if it misses scheduled time for any reason. Missed jobs executions will be counted as failed ones
- `resources`: supported resources for auto prune are `taskrun` and `pipelinerun`
- `keep`: maximum number of resources to keep while deleting or removing resources
- `keep-since`: retain the resources younger than the specified value in minutes
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/operator/v1alpha1/tektonconfig_types.go
Expand Up @@ -66,6 +66,9 @@ type Prune struct {
KeepSince *uint `json:"keep-since,omitempty"`
// How frequent pruning should happen
Schedule string `json:"schedule,omitempty"`
// Optional deadline in seconds for starting the job if it misses scheduled time for any reason.
// Missed jobs executions will be counted as failed ones.
StartingDeadlineSeconds *int64 `json:"startingDeadlineSeconds,omitempty"`
}

func (p Prune) IsEmpty() bool {
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/operator/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 19 additions & 6 deletions pkg/reconciler/common/prune.go
Expand Up @@ -258,23 +258,28 @@ func (pr *Pruner) reconcileCronJobs() error {
return nil
}

// to compute hash include, pruneConfigs, nodeSelector, toleration, priorityClass
// to compute hash include, pruneConfigs, startingDeadlineSeconds, nodeSelector, toleration, priorityClass
func (pr *Pruner) computeHash(pruneConfigs []pruneConfig) (string, error) {
// to compute hash additionally include, nodeSelector, tolerations, priorityClassName
// to update cronjobs if there is a change on those fields
targetObject := struct {
PruneConfigs []pruneConfig
NodeSelector map[string]string
Tolerations []corev1.Toleration
PriorityClassName string
Script string
PruneConfigs []pruneConfig
StartingDeadlineSeconds *int64
NodeSelector map[string]string
Tolerations []corev1.Toleration
PriorityClassName string
Script string
}{
PruneConfigs: pruneConfigs,
NodeSelector: pr.tektonConfig.Spec.Config.NodeSelector,
Tolerations: pr.tektonConfig.Spec.Config.Tolerations,
PriorityClassName: pr.tektonConfig.Spec.Config.PriorityClassName,
Script: prunerCommand,
}
// update StartingDeadlineSeconds
if pr.tektonConfig.Spec.Pruner.StartingDeadlineSeconds != nil {
targetObject.StartingDeadlineSeconds = ptr.Int64(*pr.tektonConfig.Spec.Pruner.StartingDeadlineSeconds)
}
return hash.Compute(targetObject)
}

Expand Down Expand Up @@ -514,6 +519,13 @@ func (pr *Pruner) createCronJobs(cronJobsToBeCreated map[string]string, pruneCon
allowPrivilegedEscalation := false
runAsUser := ptr.Int64(65532)
fsGroup := ptr.Int64(65532)
var startingDeadlineSeconds *int64

// update startingDeadlineSeconds
if pr.tektonConfig.Spec.Pruner.StartingDeadlineSeconds != nil {
startingDeadlineSeconds = ptr.Int64(*pr.tektonConfig.Spec.Pruner.StartingDeadlineSeconds)
pr.logger.Infow("StartingDeadlineSeconds added", "value", startingDeadlineSeconds)
}

// if it is a openshift platform remove the user and fsGroup ids
// those ids will be allocated dynamically
Expand All @@ -535,6 +547,7 @@ func (pr *Pruner) createCronJobs(cronJobsToBeCreated map[string]string, pruneCon
ConcurrencyPolicy: batchv1.ForbidConcurrent,
FailedJobsHistoryLimit: &failedJobsHistoryLimit,
SuccessfulJobsHistoryLimit: &successfulJobsHistoryLimit,
StartingDeadlineSeconds: startingDeadlineSeconds,
JobTemplate: batchv1.JobTemplateSpec{
Spec: batchv1.JobSpec{
TTLSecondsAfterFinished: &ttlSecondsAfterFinished,
Expand Down
40 changes: 40 additions & 0 deletions pkg/reconciler/common/prune_test.go
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
k8sTesting "k8s.io/client-go/testing"
"knative.dev/pkg/ptr"
)

// reactor required for the GenerateName field to work when using the fake client
Expand Down Expand Up @@ -748,6 +749,42 @@ func TestPrunerReconcile(t *testing.T) {
"*/12 * * * *": "ns-seven;--keep=100,--keep-since=42;pipelinerun,taskrun;true",
},
},
{ // reconcile #13
// add startingDeadlineSeconds
name: "TestStartingDeadlineSecondsAdd",
applyChanges: func(tektonConfig *v1alpha1.TektonConfig, client *fake.Clientset, t *testing.T) func() {
tektonConfig.Spec.Pruner.StartingDeadlineSeconds = ptr.Int64(100)
return nil
},
scheduleAndArgs: map[string]string{
"*/5 * * * *": "ns-one;--keep=100;pipelinerun,taskrun;false ns-two;--keep=100;pipelinerun,taskrun;false",
"*/12 * * * *": "ns-seven;--keep=100,--keep-since=42;pipelinerun,taskrun;true",
},
},
{ // reconcile #14
// update startingDeadlineSeconds
name: "TestStartingDeadlineSecondsUpdate",
applyChanges: func(tektonConfig *v1alpha1.TektonConfig, client *fake.Clientset, t *testing.T) func() {
tektonConfig.Spec.Pruner.StartingDeadlineSeconds = ptr.Int64(201)
return nil
},
scheduleAndArgs: map[string]string{
"*/5 * * * *": "ns-one;--keep=100;pipelinerun,taskrun;false ns-two;--keep=100;pipelinerun,taskrun;false",
"*/12 * * * *": "ns-seven;--keep=100,--keep-since=42;pipelinerun,taskrun;true",
},
},
{ // reconcile #15
// remove startingDeadlineSeconds
name: "TestStartingDeadlineSecondsRemove",
applyChanges: func(tektonConfig *v1alpha1.TektonConfig, client *fake.Clientset, t *testing.T) func() {
tektonConfig.Spec.Pruner.StartingDeadlineSeconds = nil
return nil
},
scheduleAndArgs: map[string]string{
"*/5 * * * *": "ns-one;--keep=100;pipelinerun,taskrun;false ns-two;--keep=100;pipelinerun,taskrun;false",
"*/12 * * * *": "ns-seven;--keep=100,--keep-since=42;pipelinerun,taskrun;true",
},
},
},
},
}
Expand Down Expand Up @@ -815,6 +852,9 @@ func TestPrunerReconcile(t *testing.T) {
assert.Equal(t, test.tektonConfig.Spec.Config.Tolerations, podSpec.Tolerations)
assert.Equal(t, test.tektonConfig.Spec.Config.NodeSelector, podSpec.NodeSelector)
assert.Equal(t, test.tektonConfig.Spec.Config.PriorityClassName, podSpec.PriorityClassName)

// verify startingDeadlineSeconds
assert.Equal(t, test.tektonConfig.Spec.Pruner.StartingDeadlineSeconds, cronJob.Spec.StartingDeadlineSeconds)
}

// confirm all the schedules verified
Expand Down

0 comments on commit eb9e5f2

Please sign in to comment.