Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cherry-pick] delete all the cronjobs if default config removed #473

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions pkg/reconciler/common/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,20 @@ func Prune(ctx context.Context, k kubernetes.Interface, tC *v1alpha1.TektonConfi
}

//may be reconciled by removing pruning spec from tektonConfig
//in this case delete all the cronjobs with the label pruneCronLabel
if pruner.removedFromTektonConfig(tC.Spec.Pruner) {
return pruner.checkAndDeleteCron(ctx, CronName, tC.Spec.TargetNamespace)
cronJobs, err := pruner.listCronJobs(ctx)
if err != nil {
return err
}
if len(cronJobs.Items) > 0 {
for _, cronJob := range cronJobs.Items {
if err := pruner.deleteCronJob(ctx, cronJob.Name, cronJob.Namespace); err != nil {
return err
}
}
}
return nil
}

//may be reconciled by removing/adding annotation on a Namespace
Expand Down Expand Up @@ -299,6 +311,24 @@ func pruneCommand(pru *pruneConfigPerNS, ns string) string {
return cmdArgs
}

func (pruner *Pruner) listCronJobs(ctx context.Context) (*batchv1.CronJobList, error) {
var opts = v1.ListOptions{
LabelSelector: fmt.Sprint(pruneCronLabel),
}
cronJobs, err := pruner.kc.BatchV1().CronJobs("").List(ctx, opts)
if err != nil {
if errors.IsNotFound(err) {
return cronJobs, nil
}
return cronJobs, err
}
return cronJobs, nil
}

func (pruner *Pruner) deleteCronJob(ctx context.Context, cronName, ns string) error {
return pruner.kc.BatchV1().CronJobs(ns).Delete(ctx, cronName, v1.DeleteOptions{})
}

func (pruner *Pruner) checkAndDeleteCron(ctx context.Context, cronName, ns string) error {
if _, err := pruner.kc.BatchV1().CronJobs(ns).Get(ctx, cronName, v1.GetOptions{}); err != nil {
if err != nil && errors.IsNotFound(err) {
Expand All @@ -308,7 +338,7 @@ func (pruner *Pruner) checkAndDeleteCron(ctx context.Context, cronName, ns strin
}

//if there is no error it means cron is exists, but no prune in config it means delete it
return pruner.kc.BatchV1().CronJobs(ns).Delete(ctx, cronName, v1.DeleteOptions{})
return pruner.deleteCronJob(ctx, cronName, ns)
}

func (pruner *Pruner) removedFromTektonConfig(prune v1alpha1.Prune) bool {
Expand All @@ -320,16 +350,8 @@ func (pruner *Pruner) removedFromTektonConfig(prune v1alpha1.Prune) bool {

func (pruner *Pruner) checkAnnotationsRemovedNamespaces(ctx context.Context, targetNs string) ([]string, error) {
var namespaces []string
var opts = v1.ListOptions{
LabelSelector: fmt.Sprint(pruneCronLabel),
}
cronJobs, err := pruner.kc.BatchV1().CronJobs("").List(ctx, opts)
if err != nil {
if errors.IsNotFound(err) {
return namespaces, nil
}
return namespaces, err
}

cronJobs, err := pruner.listCronJobs(ctx)
for _, cronjob := range cronJobs.Items {
cronNameSpace := cronjob.ObjectMeta.Namespace
ns, err := pruner.kc.CoreV1().Namespaces().Get(ctx, cronNameSpace, v1.GetOptions{})
Expand Down