Skip to content

Commit

Permalink
Add conversion for Build
Browse files Browse the repository at this point in the history
From v1alpha1 to v1beta1
  • Loading branch information
qu1queee committed Aug 2, 2023
1 parent 67f043b commit 88bd994
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 7 deletions.
6 changes: 4 additions & 2 deletions pkg/apis/build/v1alpha1/build_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package v1alpha1

func (*Build) Hub() {

// ConvertTo converts this Build to the Hub version (v1alpha1)
func (src *Build) ConvertFrom(bs *Build) error {
bs.ObjectMeta = src.ObjectMeta
return nil
}
149 changes: 147 additions & 2 deletions pkg/apis/build/v1beta1/build_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,109 @@ func (src *Build) ConvertTo(bs *v1alpha1.Build) error {
return src.Spec.ConvertTo(&bs.Spec)
}

func (src *Build) ConvertFrom(bs *v1alpha1.Build) error {
src.ObjectMeta = bs.ObjectMeta
return src.Spec.ConvertFrom(&bs.Spec)
}

func (dest *BuildSpec) ConvertFrom(orig *v1alpha1.BuildSpec) error {

specSource := Source{}

if orig.Source.BundleContainer != nil {
specSource.Type = OCIArtifactType
specSource.OCIArtifact = &OCIArtifact{
Image: orig.Source.BundleContainer.Image,
Prune: (*PruneOption)(orig.Source.BundleContainer.Prune),
PullSecret: &orig.Source.Credentials.Name,
}
} else {
specSource.Type = GitType
specSource.GitSource = &Git{
URL: orig.Source.URL,
Revision: orig.Source.Revision,
CloneSecret: &orig.Source.Credentials.Name,
}
}

specSource.ContextDir = orig.Source.ContextDir

dest.Source = specSource

// Triggers
if orig.Trigger != nil {
dest.Trigger = &Trigger{}
for _, t := range orig.Trigger.When {
tw := convertToBetaTriggers(&t)
dest.Trigger.When = append(dest.Trigger.When, tw)
}
if orig.Trigger.SecretRef != nil {
dest.Trigger.TriggerSecret = &orig.Trigger.SecretRef.Name
}
}

// Strategy
dest.Strategy = Strategy{
Name: orig.StrategyName(),
Kind: (*BuildStrategyKind)(orig.Strategy.Kind),
APIVersion: orig.Strategy.APIVersion,
}

// BuildSpec ParamValues
dest.ParamValues = []ParamValue{}
for _, p := range orig.ParamValues {
new := convertBetaParamValue(p)
dest.ParamValues = append(dest.ParamValues, new)
}

// BuildSpec Output
dest.Output.Image = orig.Output.Image
if orig.Output.Credentials != nil {
dest.Output.PushSecret = &orig.Output.Credentials.Name
}

dest.Output.Annotations = orig.Output.Annotations
dest.Output.Labels = orig.Output.Labels

// BuildSpec Timeout
dest.Timeout = orig.Timeout

// BuildSpec Env
dest.Env = orig.Env

// BuildSpec Retention
dest.Retention = &BuildRetention{}
if orig.Retention != nil {
if orig.Retention.FailedLimit != nil {
dest.Retention.FailedLimit = orig.Retention.FailedLimit
}
if orig.Retention.SucceededLimit != nil {
dest.Retention.SucceededLimit = orig.Retention.SucceededLimit
}
if orig.Retention.TTLAfterFailed != nil {
dest.Retention.TTLAfterFailed = orig.Retention.TTLAfterFailed
}
if orig.Retention.TTLAfterSucceeded != nil {
dest.Retention.TTLAfterSucceeded = orig.Retention.TTLAfterSucceeded
}
}

// 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 (srcSpec *BuildSpec) ConvertTo(bs *v1alpha1.BuildSpec) error {
// BuildSpec Source
bs.Source = getBuildSource(*srcSpec)
bs.Source = getAlphaBuildSource(*srcSpec)

// BuildSpec Trigger
if srcSpec.Trigger != nil {
Expand Down Expand Up @@ -140,7 +240,52 @@ func (p TriggerWhen) convertTo(dest *v1alpha1.TriggerWhen) {

}

func getBuildSource(src BuildSpec) v1alpha1.Source {
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.ConfigMapValue = &ObjectKeyRef{}
p.ConfigMapValue = (*ObjectKeyRef)(orig.ConfigMapValue)
}
if orig.SecretValue != nil {
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 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
Expand Down
32 changes: 31 additions & 1 deletion pkg/webhook/conversion/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func convertSHPCR(Object *unstructured.Unstructured, toVersion string, ctx conte
convertedObject := Object.DeepCopy()
fromVersion := Object.GetAPIVersion()

if fromVersion == "shipwright.io/v1alpha1" {
if fromVersion == toVersion {
ctxlog.Info(ctx, "nothing to convert")
return convertedObject, statusSucceed()
}
Expand Down Expand Up @@ -55,6 +55,36 @@ func convertSHPCR(Object *unstructured.Unstructured, toVersion string, ctx conte
}
convertedObject.Object = mapito

} else {
return nil, statusErrorWithMessage("unsupported Kind")
}
default:
return nil, statusErrorWithMessage("unexpected conversion version to %q", toVersion)
}
case "shipwright.io/v1alpha1":
switch toVersion {
case "shipwright.io/v1beta1":
if convertedObject.Object["kind"] == "Build" {

// Convert the unstructured object to cluster.
unstructured := convertedObject.UnstructuredContent()
var buildAlpha v1alpha1.Build
err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, &buildAlpha)
if err != nil {
ctxlog.Error(ctx, err, "failed unstructuring the convertedObject")
}
var buildBeta v1beta1.Build

buildBeta.TypeMeta = buildAlpha.TypeMeta
buildBeta.TypeMeta.APIVersion = "shipwright.io/v1alpha1"
buildBeta.ConvertFrom(&buildAlpha)

mapito, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&buildBeta)
if err != nil {
ctxlog.Error(ctx, err, "failed structuring the newObject")
}
convertedObject.Object = mapito

} else {
return nil, statusErrorWithMessage("unsupported Kind")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhook/conversion/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var _ = Describe("ConvertCRD", func() {
var apiVersion = "apiextensions.k8s.io/v1"
var desiredAPIVersion = "shipwright.io/v1alpha1"

It("converts for source OCIArtifacts type", func() {
It("converts for spec source OCIArtifacts type", func() {
ctxDir := "docker-build"
image := "dockerhub/foobar/hello"
pruneOption := "AfterPull"
Expand Down Expand Up @@ -98,7 +98,7 @@ request:
Expect(build.Spec.Source.ContextDir).To(Equal(&ctxDir))
Expect(build.Spec.Source.Revision).To(BeNil())
})
It("converts for source GitSource type", func() {
It("converts for spec source GitSource type", func() {
ctxDir := "docker-build"
url := "https://github.com/shipwright-io/sample-go"
revision := "main"
Expand Down

0 comments on commit 88bd994

Please sign in to comment.