Skip to content

Commit

Permalink
Introduce default delete options
Browse files Browse the repository at this point in the history
At different sections of the code, very similar delete options were used.

Add `defaultDeleteOptions` variable and reuse it.
  • Loading branch information
HeavyWombat committed Mar 5, 2021
1 parent 010e592 commit 33233a0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
5 changes: 1 addition & 4 deletions internal/load/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@ func registerSingleBuild(kubeAccess KubeAccess, namespace string, name string, b
return nil, err
}

var graceperiod = int64(0)
deleteOptions := metav1.DeleteOptions{GracePeriodSeconds: &graceperiod}

if !buildRunOptions.skipDelete {
defer func() {
if err := deleteBuild(kubeAccess, build.Namespace, build.Name, &deleteOptions); err != nil {
if err := deleteBuild(kubeAccess, build.Namespace, build.Name, defaultDeleteOptions); err != nil {
warn("failed to delete build %s, %v\n", name, err)
}
}()
Expand Down
14 changes: 5 additions & 9 deletions internal/load/buildrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ import (
"sync"
"time"

buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/gonvenience/bunt"
"github.com/gonvenience/text"
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type buildRunOptions struct {
Expand Down Expand Up @@ -178,12 +177,9 @@ func ExecuteSingleBuildRun(kubeAccess KubeAccess, namespace string, name string,
return nil, err
}

var graceperiod = int64(0)
deleteOptions := metav1.DeleteOptions{GracePeriodSeconds: &graceperiod}

if !buildRunOptions.skipDelete {
defer func() {
if err := deleteBuild(kubeAccess, build.Namespace, build.Name, &deleteOptions); err != nil {
if err := deleteBuild(kubeAccess, build.Namespace, build.Name, defaultDeleteOptions); err != nil {
warn("failed to delete build %s, %v\n", name, err)
}
}()
Expand All @@ -196,7 +192,7 @@ func ExecuteSingleBuildRun(kubeAccess KubeAccess, namespace string, name string,

if !buildRunOptions.skipDelete {
defer func() {
if err := deleteBuildRun(kubeAccess, buildRun.Namespace, buildRun.Name, &deleteOptions); err != nil {
if err := deleteBuildRun(kubeAccess, buildRun.Namespace, buildRun.Name, defaultDeleteOptions); err != nil {
warn("failed to delete buildrun %s, %v\n", name, err)
}
}()
Expand Down
7 changes: 5 additions & 2 deletions internal/load/kubeops.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ import (

var (
defaultBuildRunWaitTimeout = time.Duration(5 * time.Minute)
defaultDeleteOptions = &metav1.DeleteOptions{
GracePeriodSeconds: ptr.Int64(0),
}
)

func newBuild(namespace string, name string, buildSpec buildv1alpha1.BuildSpec, annotations map[string]string) buildv1alpha1.Build {
Expand Down Expand Up @@ -85,7 +88,7 @@ func newBuildRun(name string, build buildv1alpha1.Build, generateServiceAccount
}

func applyBuild(kubeAccess KubeAccess, build buildv1alpha1.Build) (*buildv1alpha1.Build, error) {
if err := deleteBuild(kubeAccess, build.Namespace, build.Name, &metav1.DeleteOptions{GracePeriodSeconds: ptr.Int64(0)}); err != nil {
if err := deleteBuild(kubeAccess, build.Namespace, build.Name, defaultDeleteOptions); err != nil {
return nil, err
}

Expand All @@ -97,7 +100,7 @@ func applyBuild(kubeAccess KubeAccess, build buildv1alpha1.Build) (*buildv1alpha
}

func applyBuildRun(kubeAccess KubeAccess, buildRun buildv1alpha1.BuildRun) (*buildv1alpha1.BuildRun, error) {
if err := deleteBuildRun(kubeAccess, buildRun.Namespace, buildRun.Name, &metav1.DeleteOptions{GracePeriodSeconds: ptr.Int64(0)}); err != nil {
if err := deleteBuildRun(kubeAccess, buildRun.Namespace, buildRun.Name, defaultDeleteOptions); err != nil {
return nil, err
}

Expand Down

0 comments on commit 33233a0

Please sign in to comment.