From a4fe3fa7892c318f2ccfeff850ec5e066d3ad021 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 | 432 ++++++++++++++++++- pkg/apis/build/v1beta1/build_conversion.go | 439 +------------------- 2 files changed, 433 insertions(+), 438 deletions(-) diff --git a/pkg/apis/build/v1alpha1/build_conversion.go b/pkg/apis/build/v1alpha1/build_conversion.go index c6638dd704..d69bc7cdb3 100644 --- a/pkg/apis/build/v1alpha1/build_conversion.go +++ b/pkg/apis/build/v1alpha1/build_conversion.go @@ -6,19 +6,441 @@ package v1alpha1 import ( "context" - "fmt" + "strconv" + "github.com/shipwright-io/build/pkg/apis/build/v1beta1" + "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 ( + 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 (src *Build) ConvertFrom(_ context.Context, _ *unstructured.Unstructured) error { - return fmt.Errorf("v1alpha1 is the current storage version, nothing to convert from") +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 +} + +// ConvertFrom converts a provided v1beta1.Build object into this v1alpha1.Build object. +func (src *Build) ConvertFrom(ctx context.Context, obj *unstructured.Unstructured) error { + + var betaBuild v1beta1.Build + + unstructured := obj.UnstructuredContent() + err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, &betaBuild) + if err != nil { + ctxlog.Error(ctx, err, "failed unstructuring the convertedObject") + } + + ctxlog.Debug(ctx, "Converting Build from beta to alpha", "namespace", betaBuild.Namespace, "name", betaBuild.Name) + + src.ObjectMeta = betaBuild.ObjectMeta + src.TypeMeta = betaBuild.TypeMeta + src.TypeMeta.APIVersion = alphaGroupVersion + + src.Spec.ConvertFrom(&betaBuild.Spec) + + // convert annotation-controlled features + if betaBuild.Spec.Retention != nil && betaBuild.Spec.Retention.AtBuildDeletion != nil { + // We must create a new Map as otherwise the addition is not kept + src.ObjectMeta.Annotations = map[string]string{} + for k, v := range betaBuild.Annotations { + src.ObjectMeta.Annotations[k] = v + } + src.ObjectMeta.Annotations[AnnotationBuildRunDeletion] = strconv.FormatBool(*betaBuild.Spec.Retention.AtBuildDeletion) + } + + // convert OCIArtifact to Bundle + if betaBuild.Spec.Source.OCIArtifact != nil { + src.Spec.Source.BundleContainer = &BundleContainer{ + Image: betaBuild.Spec.Source.OCIArtifact.Image, + Prune: (*PruneOption)(betaBuild.Spec.Source.OCIArtifact.Prune), + } + } + + return nil +} + +func (dest *BuildSpec) ConvertFrom(orig *v1beta1.BuildSpec) error { + // Handle BuildSpec Sources or Source + if orig.Source.Type == v1beta1.LocalType && orig.Source.LocalSource != nil { + dest.Sources = append(dest.Sources, BuildSource{ + Name: orig.Source.LocalSource.Name, + Type: LocalCopy, + Timeout: orig.Source.LocalSource.Timeout, + }) + } else { + dest.Source = getAlphaBuildSource(*orig) + } + + // Handle BuildSpec Trigger + if orig.Trigger != nil { + dest.Trigger = &Trigger{} + for _, t := range orig.Trigger.When { + tw := TriggerWhen{} + tw.convertToAlpha(t) + dest.Trigger.When = append(dest.Trigger.When, tw) + } + if orig.Trigger.TriggerSecret != nil { + dest.Trigger.SecretRef = &corev1.LocalObjectReference{Name: *orig.Trigger.TriggerSecret} + } + } + + // Handle BuildSpec Strategy + dest.Strategy = Strategy{ + Name: orig.StrategyName(), + Kind: (*BuildStrategyKind)(orig.Strategy.Kind), + } + + // Handle BuildSpec Builder, TODO + dest.Builder = nil + + // Handle BuildSpec ParamValues + dest.ParamValues = nil + for _, p := range orig.ParamValues { + if p.Name == "dockerfile" && p.SingleValue != nil { + dest.Dockerfile = p.SingleValue.Value + continue + } + + if p.Name == "builder-image" && p.SingleValue != nil { + dest.Builder = &Image{ + Image: *p.SingleValue.Value, + } + continue + } + param := ParamValue{} + param.convertToAlpha(p) + dest.ParamValues = append(dest.ParamValues, param) + } + + // Handle BuildSpec Output + dest.Output.Image = orig.Output.Image + dest.Output.Insecure = orig.Output.Insecure + if orig.Output.PushSecret != nil { + dest.Output.Credentials = &corev1.LocalObjectReference{} + dest.Output.Credentials.Name = *orig.Output.PushSecret + } + dest.Output.Annotations = orig.Output.Annotations + dest.Output.Labels = orig.Output.Labels + + // Handle BuildSpec Timeout + dest.Timeout = orig.Timeout + + // Handle BuildSpec Env + dest.Env = orig.Env + + // Handle BuildSpec Retention + if orig.Retention != nil && + (orig.Retention.FailedLimit != nil || + orig.Retention.SucceededLimit != nil || + orig.Retention.TTLAfterFailed != nil || + orig.Retention.TTLAfterSucceeded != nil) { + dest.Retention = &BuildRetention{ + FailedLimit: orig.Retention.FailedLimit, + SucceededLimit: orig.Retention.SucceededLimit, + TTLAfterFailed: orig.Retention.TTLAfterFailed, + TTLAfterSucceeded: orig.Retention.TTLAfterSucceeded, + } + } + + // Handle BuildSpec Volumes + dest.Volumes = []BuildVolume{} + for _, vol := range orig.Volumes { + aux := BuildVolume{ + Name: vol.Name, + VolumeSource: vol.VolumeSource, + } + dest.Volumes = append(dest.Volumes, aux) + } + + return nil +} + +func getAlphaBuildSource(src v1beta1.BuildSpec) Source { + source := Source{} + var credentials corev1.LocalObjectReference + var revision *string + + switch src.Source.Type { + case v1beta1.OCIArtifactType: + if src.Source.OCIArtifact != nil && src.Source.OCIArtifact.PullSecret != nil { + credentials = corev1.LocalObjectReference{ + Name: *src.Source.OCIArtifact.PullSecret, + } + } + source.BundleContainer = &BundleContainer{ + Image: src.Source.OCIArtifact.Image, + Prune: (*PruneOption)(src.Source.OCIArtifact.Prune), + } + default: + if src.Source.GitSource != nil && src.Source.GitSource.CloneSecret != nil { + credentials = corev1.LocalObjectReference{ + Name: *src.Source.GitSource.CloneSecret, + } + } + if src.Source.GitSource != nil { + source.URL = &src.Source.GitSource.URL + revision = src.Source.GitSource.Revision + } + + } + + if credentials.Name != "" { + source.Credentials = &credentials + } + + source.Revision = revision + source.ContextDir = src.Source.ContextDir + + return source +} + +func (p TriggerWhen) convertToAlpha(dest v1beta1.TriggerWhen) { + p.Name = dest.Name + p.Type = TriggerType(dest.Type) + + p.GitHub = &WhenGitHub{} + for _, e := range dest.GitHub.Events { + p.GitHub.Events = append(p.GitHub.Events, GitHubEventName(e)) + } + p.GitHub.Branches = dest.GetBranches(v1beta1.GitHubWebHookTrigger) + + p.Image = (*WhenImage)(dest.Image) + p.ObjectRef = (*WhenObjectRef)(dest.ObjectRef) + +} + +func (p ParamValue) convertToAlpha(dest v1beta1.ParamValue) { + + if dest.SingleValue != nil && dest.SingleValue.Value != nil { + p.SingleValue = &SingleValue{} + p.Value = dest.Value + } + + if dest.ConfigMapValue != nil { + p.SingleValue = &SingleValue{ + ConfigMapValue: (*ObjectKeyRef)(dest.ConfigMapValue), + } + } + + if dest.SecretValue != nil { + p.SingleValue = &SingleValue{ + SecretValue: (*ObjectKeyRef)(dest.SecretValue), + } + } + + p.Name = dest.Name + + for _, singleValue := range dest.Values { + p.Values = append(p.Values, SingleValue{ + Value: singleValue.Value, + ConfigMapValue: (*ObjectKeyRef)(singleValue.ConfigMapValue), + SecretValue: (*ObjectKeyRef)(singleValue.SecretValue), + }) + } } diff --git a/pkg/apis/build/v1beta1/build_conversion.go b/pkg/apis/build/v1beta1/build_conversion.go index c0199aae74..d7c7809059 100644 --- a/pkg/apis/build/v1beta1/build_conversion.go +++ b/pkg/apis/build/v1beta1/build_conversion.go @@ -6,446 +6,19 @@ package v1beta1 import ( "context" - "strconv" + "fmt" - "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 ( - betaGroupVersion = "shipwright.io/v1beta1" - alphaGroupVersion = "shipwright.io/v1alpha1" -) - -// ensure v1beta1 implements the Conversion interface +// ensure v1alpha1 implements the Conversion interface var _ webhook.Conversion = (*Build)(nil) -// ConvertTo converts this Build object to v1alpha1 format. -func (src *Build) ConvertTo(ctx context.Context, obj *unstructured.Unstructured) error { - ctxlog.Debug(ctx, "Converting Build from beta to alpha", "namespace", src.Namespace, "name", src.Name) - - var alphaBuild v1alpha1.Build - - alphaBuild.TypeMeta = src.TypeMeta - alphaBuild.TypeMeta.APIVersion = alphaGroupVersion - - alphaBuild.ObjectMeta = src.ObjectMeta - - src.Spec.ConvertTo(&alphaBuild.Spec) - - // convert annotation-controlled features - if src.Spec.Retention != nil && src.Spec.Retention.AtBuildDeletion != nil { - // We must create a new Map as otherwise the addition is not kept - alphaBuild.ObjectMeta.Annotations = map[string]string{} - for k, v := range src.Annotations { - alphaBuild.ObjectMeta.Annotations[k] = v - } - alphaBuild.ObjectMeta.Annotations[v1alpha1.AnnotationBuildRunDeletion] = strconv.FormatBool(*src.Spec.Retention.AtBuildDeletion) - } - - // convert OCIArtifact to Bundle - if src.Spec.Source.OCIArtifact != nil { - alphaBuild.Spec.Source.BundleContainer = &v1alpha1.BundleContainer{ - Image: src.Spec.Source.OCIArtifact.Image, - Prune: (*v1alpha1.PruneOption)(src.Spec.Source.OCIArtifact.Prune), - } - } - - mapito, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&alphaBuild) - if err != nil { - ctxlog.Error(ctx, err, "failed structuring the newObject") - } - obj.Object = mapito - - return nil - -} - -// ConvertFrom converts a provided v1alpha1.Build object into this v1beta1.Build object. -func (src *Build) ConvertFrom(ctx context.Context, obj *unstructured.Unstructured) error { - - var alphaBuild v1alpha1.Build - - unstructured := obj.UnstructuredContent() - err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, &alphaBuild) - if err != nil { - ctxlog.Error(ctx, err, "failed unstructuring the convertedObject") - } - - ctxlog.Debug(ctx, "Converting Build from alpha to beta", "namespace", alphaBuild.Namespace, "name", alphaBuild.Name) - - src.ObjectMeta = alphaBuild.ObjectMeta - src.TypeMeta = alphaBuild.TypeMeta - src.TypeMeta.APIVersion = betaGroupVersion - - 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), - Message: alphaBuild.Status.Message, - } - - return nil -} - -func (dest *BuildSpec) ConvertFrom(orig *v1alpha1.BuildSpec) error { - // Handle BuildSpec Source - specSource := 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 := v1alpha1.IsLocalCopyType(orig.Sources) - if isLocal { - specSource.Type = LocalType - specSource.LocalSource = &Local{ - Name: orig.Sources[index].Name, - Timeout: orig.Sources[index].Timeout, - } - } else { - if orig.Source.BundleContainer != nil { - specSource.Type = OCIArtifactType - specSource.OCIArtifact = &OCIArtifact{ - Image: orig.Source.BundleContainer.Image, - Prune: (*PruneOption)(orig.Source.BundleContainer.Prune), - } - if orig.Source.Credentials != nil { - specSource.OCIArtifact.PullSecret = &orig.Source.Credentials.Name - } - } else if orig.Source.URL != nil { - specSource.Type = GitType - specSource.GitSource = &Git{ - URL: *orig.Source.URL, - Revision: orig.Source.Revision, - } - if orig.Source.Credentials != nil { - specSource.GitSource.CloneSecret = &orig.Source.Credentials.Name - } - } - } - specSource.ContextDir = orig.Source.ContextDir - dest.Source = specSource - - // Handle BuildSpec Triggers - if orig.Trigger != nil { - dest.Trigger = &Trigger{} - for i := range orig.Trigger.When { - dest.Trigger.When = append(dest.Trigger.When, convertToBetaTriggers(&orig.Trigger.When[i])) - } - if orig.Trigger.SecretRef != nil { - dest.Trigger.TriggerSecret = &orig.Trigger.SecretRef.Name - } - } - - // Handle BuildSpec Strategy - dest.Strategy = Strategy{ - Name: orig.StrategyName(), - Kind: (*BuildStrategyKind)(orig.Strategy.Kind), - } - - // Handle BuildSpec ParamValues - for _, p := range orig.ParamValues { - param := convertBetaParamValue(p) - dest.ParamValues = append(dest.ParamValues, param) - } - - //handle spec.Dockerfile migration - if orig.Dockerfile != nil { - dockerfileParam := ParamValue{ - Name: "dockerfile", - SingleValue: &SingleValue{ - Value: orig.Dockerfile, - }, - } - dest.ParamValues = append(dest.ParamValues, dockerfileParam) - } - - // handle spec.Builder migration - if orig.Builder != nil { - builderParam := ParamValue{ - Name: "builder-image", - SingleValue: &SingleValue{ - Value: &orig.Builder.Image, - }, - } - dest.ParamValues = append(dest.ParamValues, builderParam) - } - - // Handle BuildSpec Output - dest.Output.Image = orig.Output.Image - dest.Output.Insecure = orig.Output.Insecure - if orig.Output.Credentials != nil { - dest.Output.PushSecret = &orig.Output.Credentials.Name - } - - dest.Output.Annotations = orig.Output.Annotations - dest.Output.Labels = orig.Output.Labels - - // Handle BuildSpec Timeout - dest.Timeout = orig.Timeout - - // Handle BuildSpec Env - dest.Env = orig.Env - - // Handle BuildSpec Retention - if orig.Retention != nil { - dest.Retention = &BuildRetention{ - FailedLimit: orig.Retention.FailedLimit, - SucceededLimit: orig.Retention.SucceededLimit, - TTLAfterFailed: orig.Retention.TTLAfterFailed, - TTLAfterSucceeded: orig.Retention.TTLAfterSucceeded, - } - } - - // Handle BuildSpec Volumes - dest.Volumes = []BuildVolume{} - for _, vol := range orig.Volumes { - aux := BuildVolume{ - Name: vol.Name, - VolumeSource: vol.VolumeSource, - } - dest.Volumes = append(dest.Volumes, aux) - } - - return nil -} - -func (dest *BuildSpec) ConvertTo(bs *v1alpha1.BuildSpec) error { - // Handle BuildSpec Sources or Source - if dest.Source.Type == LocalType && dest.Source.LocalSource != nil { - bs.Sources = append(bs.Sources, v1alpha1.BuildSource{ - Name: dest.Source.LocalSource.Name, - Type: v1alpha1.LocalCopy, - Timeout: dest.Source.LocalSource.Timeout, - }) - } else { - bs.Source = getAlphaBuildSource(*dest) - } - - // Handle BuildSpec Trigger - if dest.Trigger != nil { - bs.Trigger = &v1alpha1.Trigger{} - for _, t := range dest.Trigger.When { - tw := v1alpha1.TriggerWhen{} - t.convertToAlpha(&tw) - bs.Trigger.When = append(bs.Trigger.When, tw) - } - if dest.Trigger.TriggerSecret != nil { - bs.Trigger.SecretRef = &corev1.LocalObjectReference{Name: *dest.Trigger.TriggerSecret} - } - } - - // Handle BuildSpec Strategy - bs.Strategy = v1alpha1.Strategy{ - Name: dest.StrategyName(), - Kind: (*v1alpha1.BuildStrategyKind)(dest.Strategy.Kind), - } - - // Handle BuildSpec Builder, TODO - bs.Builder = nil - - // Handle BuildSpec ParamValues - bs.ParamValues = nil - for _, p := range dest.ParamValues { - if p.Name == "dockerfile" && p.SingleValue != nil { - bs.Dockerfile = p.SingleValue.Value - continue - } - - if p.Name == "builder-image" && p.SingleValue != nil { - bs.Builder = &v1alpha1.Image{ - Image: *p.SingleValue.Value, - } - continue - } - param := v1alpha1.ParamValue{} - p.convertToAlpha(¶m) - bs.ParamValues = append(bs.ParamValues, param) - } - - // Handle BuildSpec Output - bs.Output.Image = dest.Output.Image - bs.Output.Insecure = dest.Output.Insecure - if dest.Output.PushSecret != nil { - bs.Output.Credentials = &corev1.LocalObjectReference{} - bs.Output.Credentials.Name = *dest.Output.PushSecret - } - bs.Output.Annotations = dest.Output.Annotations - bs.Output.Labels = dest.Output.Labels - - // Handle BuildSpec Timeout - bs.Timeout = dest.Timeout - - // Handle BuildSpec Env - bs.Env = dest.Env - - // Handle BuildSpec Retention - 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, - TTLAfterFailed: dest.Retention.TTLAfterFailed, - TTLAfterSucceeded: dest.Retention.TTLAfterSucceeded, - } - } - - // Handle BuildSpec Volumes - bs.Volumes = []v1alpha1.BuildVolume{} - for _, vol := range dest.Volumes { - aux := v1alpha1.BuildVolume{ - Name: vol.Name, - VolumeSource: vol.VolumeSource, - } - bs.Volumes = append(bs.Volumes, aux) - } - - return nil -} - -func (p ParamValue) convertToAlpha(dest *v1alpha1.ParamValue) { - - if p.SingleValue != nil && p.SingleValue.Value != nil { - dest.SingleValue = &v1alpha1.SingleValue{} - dest.Value = p.Value - } - - if p.ConfigMapValue != nil { - dest.SingleValue = &v1alpha1.SingleValue{ - ConfigMapValue: (*v1alpha1.ObjectKeyRef)(p.ConfigMapValue), - } - } - - if p.SecretValue != nil { - dest.SingleValue = &v1alpha1.SingleValue{ - SecretValue: (*v1alpha1.ObjectKeyRef)(p.SecretValue), - } - } - - dest.Name = p.Name - - for _, singleValue := range p.Values { - dest.Values = append(dest.Values, v1alpha1.SingleValue{ - Value: singleValue.Value, - ConfigMapValue: (*v1alpha1.ObjectKeyRef)(singleValue.ConfigMapValue), - SecretValue: (*v1alpha1.ObjectKeyRef)(singleValue.SecretValue), - }) - } -} - -func (p TriggerWhen) convertToAlpha(dest *v1alpha1.TriggerWhen) { - dest.Name = p.Name - dest.Type = v1alpha1.TriggerType(p.Type) - - dest.GitHub = &v1alpha1.WhenGitHub{} - for _, e := range p.GitHub.Events { - dest.GitHub.Events = append(dest.GitHub.Events, v1alpha1.GitHubEventName(e)) - } - dest.GitHub.Branches = p.GetBranches(GitHubWebHookTrigger) - - dest.Image = (*v1alpha1.WhenImage)(p.Image) - dest.ObjectRef = (*v1alpha1.WhenObjectRef)(p.ObjectRef) - -} - -func convertBetaParamValue(orig v1alpha1.ParamValue) ParamValue { - p := ParamValue{} - if orig.SingleValue != nil && orig.SingleValue.Value != nil { - p.SingleValue = &SingleValue{} - p.Value = orig.Value - } - - if orig.ConfigMapValue != nil { - p.SingleValue = &SingleValue{} - p.ConfigMapValue = (*ObjectKeyRef)(orig.ConfigMapValue) - } - if orig.SecretValue != nil { - p.SingleValue = &SingleValue{} - p.SecretValue = (*ObjectKeyRef)(orig.SecretValue) - } - - p.Name = orig.Name - - for _, singleValue := range orig.Values { - p.Values = append(p.Values, SingleValue{ - Value: singleValue.Value, - ConfigMapValue: (*ObjectKeyRef)(singleValue.ConfigMapValue), - SecretValue: (*ObjectKeyRef)(singleValue.SecretValue), - }) - } - return p +func (src *Build) ConvertTo(_ context.Context, _ *unstructured.Unstructured) error { + return fmt.Errorf("v1beta1 is the current storage version, nothing to convert to") } -func convertToBetaTriggers(orig *v1alpha1.TriggerWhen) TriggerWhen { - dest := TriggerWhen{ - Name: orig.Name, - Type: TriggerType(orig.Type), - } - - dest.GitHub = &WhenGitHub{} - for _, e := range orig.GitHub.Events { - dest.GitHub.Events = append(dest.GitHub.Events, GitHubEventName(e)) - } - - dest.GitHub.Branches = orig.GetBranches(v1alpha1.GitHubWebHookTrigger) - dest.Image = (*WhenImage)(orig.Image) - dest.ObjectRef = (*WhenObjectRef)(orig.ObjectRef) - - return dest -} - -func getAlphaBuildSource(src BuildSpec) v1alpha1.Source { - source := v1alpha1.Source{} - var credentials corev1.LocalObjectReference - var revision *string - - switch src.Source.Type { - case OCIArtifactType: - if src.Source.OCIArtifact != nil && src.Source.OCIArtifact.PullSecret != nil { - credentials = corev1.LocalObjectReference{ - Name: *src.Source.OCIArtifact.PullSecret, - } - } - source.BundleContainer = &v1alpha1.BundleContainer{ - Image: src.Source.OCIArtifact.Image, - Prune: (*v1alpha1.PruneOption)(src.Source.OCIArtifact.Prune), - } - default: - if src.Source.GitSource != nil && src.Source.GitSource.CloneSecret != nil { - credentials = corev1.LocalObjectReference{ - Name: *src.Source.GitSource.CloneSecret, - } - } - if src.Source.GitSource != nil { - source.URL = &src.Source.GitSource.URL - revision = src.Source.GitSource.Revision - } - - } - - if credentials.Name != "" { - source.Credentials = &credentials - } - - source.Revision = revision - source.ContextDir = src.Source.ContextDir - - return source +func (src *Build) ConvertFrom(_ context.Context, _ *unstructured.Unstructured) error { + return fmt.Errorf("v1beta1 is the current storage version, nothing to convert from") }