From 7988a82cde751bba9b9a0359c39d3aca92190cbc Mon Sep 17 00:00:00 2001 From: encalada Date: Mon, 8 Jan 2024 12:12:18 -0500 Subject: [PATCH] wip --- pkg/apis/build/v1alpha1/build_conversion.go | 209 +++++++++++++++++++- 1 file changed, 207 insertions(+), 2 deletions(-) diff --git a/pkg/apis/build/v1alpha1/build_conversion.go b/pkg/apis/build/v1alpha1/build_conversion.go index c6638dd704..347150eefe 100644 --- a/pkg/apis/build/v1alpha1/build_conversion.go +++ b/pkg/apis/build/v1alpha1/build_conversion.go @@ -8,15 +8,220 @@ import ( "context" "fmt" + "github.com/shipwright-io/build/pkg/apis/build/v1beta1" + "github.com/shipwright-io/build/pkg/ctxlog" "github.com/shipwright-io/build/pkg/webhook" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + runtime "k8s.io/apimachinery/pkg/runtime" + "k8s.io/utils/pointer" +) + +const ( + betaGroupVersion = "shipwright.io/v1beta1" + alphaGroupVersion = "shipwright.io/v1alpha1" ) // ensure v1alpha1 implements the Conversion interface var _ webhook.Conversion = (*Build)(nil) -func (src *Build) ConvertTo(_ context.Context, _ *unstructured.Unstructured) error { - return fmt.Errorf("v1alpha1 is the current storage version, nothing to convert to") +// ConvertTo converts this Build object to v1beta1 format. +func (src *Build) ConvertTo(ctx context.Context, obj *unstructured.Unstructured) error { + ctxlog.Debug(ctx, "Converting Build from alpha to beta", "namespace", src.Namespace, "name", src.Name) + + var betaBuild v1beta1.Build + + betaBuild.TypeMeta = src.TypeMeta + betaBuild.TypeMeta.APIVersion = betaGroupVersion + betaBuild.ObjectMeta = src.ObjectMeta + + src.Spec.ConvertTo(&betaBuild.Spec) + + // convert annotation-controlled features + if value, set := src.Annotations[AnnotationBuildRunDeletion]; set { + if betaBuild.Spec.Retention == nil { + betaBuild.Spec.Retention = &v1beta1.BuildRetention{} + } + betaBuild.Spec.Retention.AtBuildDeletion = pointer.Bool(value == "true") + delete(betaBuild.ObjectMeta.Annotations, AnnotationBuildRunDeletion) + } + + betaBuild.Status = v1beta1.BuildStatus{ + Registered: src.Status.Registered, + Reason: (*v1beta1.BuildReason)(src.Status.Reason), + Message: src.Status.Message, + } + + mapito, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&betaBuild) + if err != nil { + ctxlog.Error(ctx, err, "failed structuring the newObject") + } + obj.Object = mapito + + return nil +} + +func (orig *BuildSpec) ConvertTo(bs *v1beta1.BuildSpec) error { + // Handle BuildSpec Source + + // only interested on spec.sources as long as an item of the list + // is of the type LocalCopy. Otherwise, we move into bundle or git types. + index, isLocal := IsLocalCopyType(orig.Sources) + if isLocal { + bs.Source.Type = v1beta1.LocalType + bs.Source.LocalSource = &v1beta1.Local{ + Name: orig.Sources[index].Name, + Timeout: orig.Sources[index].Timeout, + } + } else { + if orig.Source.BundleContainer != nil { + bs.Source.Type = v1beta1.OCIArtifactType + bs.Source.OCIArtifact = &v1beta1.OCIArtifact{ + Image: orig.Source.BundleContainer.Image, + Prune: (*v1beta1.PruneOption)(orig.Source.BundleContainer.Prune), + } + if orig.Source.Credentials != nil { + bs.Source.OCIArtifact.PullSecret = &orig.Source.Credentials.Name + } + } else if orig.Source.URL != nil { + bs.Source.Type = v1beta1.GitType + bs.Source.GitSource = &v1beta1.Git{ + URL: *orig.Source.URL, + Revision: orig.Source.Revision, + } + if orig.Source.Credentials != nil { + bs.Source.GitSource.CloneSecret = &orig.Source.Credentials.Name + } + } + } + bs.Source.ContextDir = orig.Source.ContextDir + + // Handle BuildSpec Triggers + if orig.Trigger != nil { + bs.Trigger = &v1beta1.Trigger{} + for i := range orig.Trigger.When { + bs.Trigger.When = append(bs.Trigger.When, convertToBetaTriggers(&orig.Trigger.When[i])) + } + if orig.Trigger.SecretRef != nil { + bs.Trigger.TriggerSecret = &orig.Trigger.SecretRef.Name + } + } + + // Handle BuildSpec Strategy + bs.Strategy = v1beta1.Strategy{ + Name: orig.StrategyName(), + Kind: (*v1beta1.BuildStrategyKind)(orig.Strategy.Kind), + } + + // Handle BuildSpec ParamValues + for _, p := range orig.ParamValues { + param := convertBetaParamValue(p) + bs.ParamValues = append(bs.ParamValues, param) + } + + //handle spec.Dockerfile migration + if orig.Dockerfile != nil { + dockerfileParam := v1beta1.ParamValue{ + Name: "dockerfile", + SingleValue: &v1beta1.SingleValue{ + Value: orig.Dockerfile, + }, + } + bs.ParamValues = append(bs.ParamValues, dockerfileParam) + } + + // handle spec.Builder migration + if orig.Builder != nil { + builderParam := v1beta1.ParamValue{ + Name: "builder-image", + SingleValue: &v1beta1.SingleValue{ + Value: &orig.Builder.Image, + }, + } + bs.ParamValues = append(bs.ParamValues, builderParam) + } + + // Handle BuildSpec Output + bs.Output.Image = orig.Output.Image + bs.Output.Insecure = orig.Output.Insecure + if orig.Output.Credentials != nil { + bs.Output.PushSecret = &orig.Output.Credentials.Name + } + + bs.Output.Annotations = orig.Output.Annotations + bs.Output.Labels = orig.Output.Labels + + // Handle BuildSpec Timeout + bs.Timeout = orig.Timeout + + // Handle BuildSpec Env + bs.Env = orig.Env + + // Handle BuildSpec Retention + if orig.Retention != nil { + bs.Retention = &v1beta1.BuildRetention{ + FailedLimit: orig.Retention.FailedLimit, + SucceededLimit: orig.Retention.SucceededLimit, + TTLAfterFailed: orig.Retention.TTLAfterFailed, + TTLAfterSucceeded: orig.Retention.TTLAfterSucceeded, + } + } + + // Handle BuildSpec Volumes + bs.Volumes = []v1beta1.BuildVolume{} + for _, vol := range orig.Volumes { + aux := v1beta1.BuildVolume{ + Name: vol.Name, + VolumeSource: vol.VolumeSource, + } + bs.Volumes = append(bs.Volumes, aux) + } + return nil +} + +func convertToBetaTriggers(orig *TriggerWhen) v1beta1.TriggerWhen { + dest := v1beta1.TriggerWhen{ + Name: orig.Name, + Type: v1beta1.TriggerType(orig.Type), + } + + dest.GitHub = &v1beta1.WhenGitHub{} + for _, e := range orig.GitHub.Events { + dest.GitHub.Events = append(dest.GitHub.Events, v1beta1.GitHubEventName(e)) + } + + dest.GitHub.Branches = orig.GetBranches(GitHubWebHookTrigger) + dest.Image = (*v1beta1.WhenImage)(orig.Image) + dest.ObjectRef = (*v1beta1.WhenObjectRef)(orig.ObjectRef) + + return dest +} + +func convertBetaParamValue(orig ParamValue) v1beta1.ParamValue { + p := v1beta1.ParamValue{} + if orig.SingleValue != nil && orig.SingleValue.Value != nil { + p.SingleValue = &v1beta1.SingleValue{} + p.Value = orig.Value + } + + if orig.ConfigMapValue != nil { + p.SingleValue = &v1beta1.SingleValue{} + p.ConfigMapValue = (*v1beta1.ObjectKeyRef)(orig.ConfigMapValue) + } + if orig.SecretValue != nil { + p.SingleValue = &v1beta1.SingleValue{} + p.SecretValue = (*v1beta1.ObjectKeyRef)(orig.SecretValue) + } + + p.Name = orig.Name + + for _, singleValue := range orig.Values { + p.Values = append(p.Values, v1beta1.SingleValue{ + Value: singleValue.Value, + ConfigMapValue: (*v1beta1.ObjectKeyRef)(singleValue.ConfigMapValue), + SecretValue: (*v1beta1.ObjectKeyRef)(singleValue.SecretValue), + }) + } + return p } func (src *Build) ConvertFrom(_ context.Context, _ *unstructured.Unstructured) error {