Skip to content

Commit

Permalink
Merge pull request #1379 from SaschaSchwarze0/sascha-migrate-buildrun…
Browse files Browse the repository at this point in the history
…-deletion

Add conversion code for buildrun deletion capability
  • Loading branch information
openshift-merge-robot authored Sep 14, 2023
2 parents 1d11d56 + 84842fe commit f4f82e5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
26 changes: 25 additions & 1 deletion pkg/apis/build/v1beta1/build_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ package v1beta1

import (
"context"
"strconv"

"github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/shipwright-io/build/pkg/ctxlog"
"github.com/shipwright-io/build/pkg/webhook"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
)

const (
Expand All @@ -33,6 +35,15 @@ func (src *Build) ConvertTo(ctx context.Context, obj *unstructured.Unstructured)
alphaBuild.ObjectMeta = src.ObjectMeta

src.Spec.ConvertTo(&alphaBuild.Spec)

// convert annotation-controlled features
if src.Spec.Retention != nil && src.Spec.Retention.AtBuildDeletion != nil {
if alphaBuild.ObjectMeta.Annotations == nil {
alphaBuild.ObjectMeta.Annotations = make(map[string]string, 1)
}
alphaBuild.ObjectMeta.Annotations[v1alpha1.AnnotationBuildRunDeletion] = strconv.FormatBool(*src.Spec.Retention.AtBuildDeletion)
}

mapito, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&alphaBuild)
if err != nil {
ctxlog.Error(ctx, err, "failed structuring the newObject")
Expand All @@ -59,6 +70,15 @@ func (src *Build) ConvertFrom(ctx context.Context, obj *unstructured.Unstructure

src.Spec.ConvertFrom(&alphaBuild.Spec)

// convert annotation-controlled features
if value, set := alphaBuild.Annotations[v1alpha1.AnnotationBuildRunDeletion]; set {
if src.Spec.Retention == nil {
src.Spec.Retention = &BuildRetention{}
}
src.Spec.Retention.AtBuildDeletion = pointer.Bool(value == "true")
delete(src.ObjectMeta.Annotations, v1alpha1.AnnotationBuildRunDeletion)
}

src.Status = BuildStatus{
Registered: alphaBuild.Status.Registered,
Reason: (*BuildReason)(alphaBuild.Status.Reason),
Expand Down Expand Up @@ -222,7 +242,11 @@ func (dest *BuildSpec) ConvertTo(bs *v1alpha1.BuildSpec) error {
bs.Env = dest.Env

// Handle BuildSpec Retention
if dest.Retention != nil {
if dest.Retention != nil &&
(dest.Retention.FailedLimit != nil ||
dest.Retention.SucceededLimit != nil ||
dest.Retention.TTLAfterFailed != nil ||
dest.Retention.TTLAfterSucceeded != nil) {
bs.Retention = &v1alpha1.BuildRetention{
FailedLimit: dest.Retention.FailedLimit,
SucceededLimit: dest.Retention.SucceededLimit,
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/build/v1beta1/build_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ type BuildRetention struct {
// AtBuildDeletion defines if related BuildRuns should be deleted when deleting the Build.
//
// +optional
AtBuildDeletion bool `json:"atBuildDeletion,omitempty"`
AtBuildDeletion *bool `json:"atBuildDeletion,omitempty"`
}

func init() {
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/build/v1beta1/zz_generated.deepcopy.go

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

15 changes: 13 additions & 2 deletions pkg/webhook/conversion/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/utils/pointer"
)

func getConversionReview(o string) (apiextensionsv1.ConversionReview, error) {
Expand Down Expand Up @@ -204,6 +205,8 @@ request:
output:
image: %s
pushSecret: %s
retention:
atBuildDeletion: true
`
o := fmt.Sprintf(buildTemplate, apiVersion,
desiredAPIVersion, ctxDir,
Expand Down Expand Up @@ -234,6 +237,9 @@ request:
},
ObjectMeta: v1.ObjectMeta{
Name: "buildkit-build",
Annotations: map[string]string{
v1alpha1.AnnotationBuildRunDeletion: "true",
},
},
Spec: v1alpha1.BuildSpec{
Source: v1alpha1.Source{
Expand Down Expand Up @@ -270,7 +276,7 @@ request:
// todo: figure out why we need to set this one
SingleValue: &v1alpha1.SingleValue{},
Values: []v1alpha1.SingleValue{
v1alpha1.SingleValue{
{
SecretValue: &v1alpha1.ObjectKeyRef{
Name: "npm-registry-access",
Key: "npm-auth-token",
Expand Down Expand Up @@ -416,6 +422,8 @@ request:
kind: Build
metadata:
name: buildkit-build
annotations:
build.shipwright.io/build-run-deletion: "true"
spec:
source:
contextDir: %s
Expand Down Expand Up @@ -494,6 +502,9 @@ request:
},
},
},
Retention: &v1beta1.BuildRetention{
AtBuildDeletion: pointer.Bool(true),
},
Trigger: &v1beta1.Trigger{
When: []v1beta1.TriggerWhen{
{
Expand Down Expand Up @@ -612,7 +623,7 @@ request:
// todo: figure out why we need to set this one
SingleValue: &v1beta1.SingleValue{},
Values: []v1beta1.SingleValue{
v1beta1.SingleValue{
{
SecretValue: &v1beta1.ObjectKeyRef{
Name: "npm-registry-access",
Key: "npm-auth-token",
Expand Down

0 comments on commit f4f82e5

Please sign in to comment.