Skip to content

Commit

Permalink
Delete associated jobs when deleting a service
Browse files Browse the repository at this point in the history
  • Loading branch information
srisco committed Sep 11, 2020
1 parent a389aaf commit 0eedd22
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pkg/backends/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ func (k *KubeBackend) DeleteService(name string) error {
log.Println(delErr.Error())
}

// Delete all the service's jobs
if err := deleteServiceJobs(name, k.namespace, k.kubeClientset); err != nil {
log.Printf("Error deleting associated jobs for service \"%s\": %v\n", name, err)
}

return nil
}

Expand Down Expand Up @@ -291,3 +296,24 @@ func deleteServiceConfigMap(name string, namespace string, kubeClientset *kubern

return nil
}

func deleteServiceJobs(name string, namespace string, kubeClientset *kubernetes.Clientset) error {
// ListOptions to select all the associated jobs with the specified service
listOpts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", types.ServiceLabel, name),
}

// Create DeleteOptions and configure PropagationPolicy for deleting associated pods in background
background := metav1.DeletePropagationBackground
delOpts := metav1.DeleteOptions{
PropagationPolicy: &background,
}

// Delete jobs
err := kubeClientset.BatchV1().Jobs(namespace).DeleteCollection(context.TODO(), delOpts, listOpts)
if err != nil {
return err
}

return nil
}
5 changes: 5 additions & 0 deletions pkg/backends/openfaas.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ func (of *OpenfaasBackend) DeleteService(name string) error {
log.Println(delErr.Error())
}

// Delete all the service's jobs
if err := deleteServiceJobs(name, of.namespace, of.kubeClientset); err != nil {
log.Printf("Error deleting associated jobs for service \"%s\": %v\n", name, err)
}

return nil
}

Expand Down

0 comments on commit 0eedd22

Please sign in to comment.