Skip to content

Commit

Permalink
on br conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
qu1queee committed Aug 4, 2023
1 parent 8ce7677 commit 42c1b08
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 12 deletions.
5 changes: 4 additions & 1 deletion pkg/apis/build/v1beta1/build_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,10 @@ func getAlphaBuildSource(src BuildSpec) v1alpha1.Source {

}

source.Credentials = &credentials
if credentials.Name != "" {
source.Credentials = &credentials
}

source.Revision = revision
source.ContextDir = src.Source.ContextDir

Expand Down
50 changes: 45 additions & 5 deletions pkg/apis/build/v1beta1/buildrun_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ func (src *BuildRun) ConvertTo(ctx context.Context, obj *unstructured.Unstructur
bs.Spec.Retention = (*v1alpha1.BuildRunRetention)(src.Spec.Retention)

// BuildRunSpec Volumes
for i, vol := range src.Spec.Volumes {
bs.Spec.Volumes[i].Name = vol.Name
bs.Spec.Volumes[i].VolumeSource = vol.VolumeSource

bs.Spec.Volumes = []v1alpha1.BuildVolume{}
for _, vol := range src.Spec.Volumes {
bs.Spec.Volumes = append(bs.Spec.Volumes, v1alpha1.BuildVolume{
Name: vol.Name,
VolumeSource: vol.VolumeSource,
})
}

mapito, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&bs)
if err != nil {
ctxlog.Error(ctx, err, "failed structuring the newObject")
Expand Down Expand Up @@ -116,7 +119,44 @@ func (src *BuildRun) ConvertFrom(ctx context.Context, obj *unstructured.Unstruct

src.Spec.ConvertFrom(&br.Spec)

// src.Status = BuildRunStatus{}
sources := []SourceResult{}
for _, s := range br.Status.Sources {
sr := SourceResult{
Name: s.Name,
Git: (*GitSourceResult)(s.Git),
OciArtifact: (*OciArtifactSourceResult)(s.Bundle),
}
sources = append(sources, sr)
}

conditions := []Condition{}

for _, c := range br.Status.Conditions {
ct := Condition{
Type: Type(c.Type),
Status: c.Status,
LastTransitionTime: c.LastTransitionTime,
Reason: c.Reason,
Message: c.Message,
}
conditions = append(conditions, ct)
}

buildBeta := Build{}
if br.Status.BuildSpec != nil {
buildBeta.Spec.ConvertFrom(br.Status.BuildSpec)
}

src.Status = BuildRunStatus{
Sources: sources,
Output: (*Output)(br.Status.Output),
Conditions: conditions,
TaskRunName: br.Status.LatestTaskRunRef,
StartTime: br.Status.StartTime,
CompletionTime: br.Status.CompletionTime,
BuildSpec: &buildBeta.Spec,
FailureDetails: src.Status.FailureDetails,
}

return nil
}
Expand Down
6 changes: 0 additions & 6 deletions pkg/webhook/conversion/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"fmt"

"github.com/davecgh/go-spew/spew"
"github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"github.com/shipwright-io/build/pkg/ctxlog"

Expand Down Expand Up @@ -39,13 +38,9 @@ func convertSHPCR(Object *unstructured.Unstructured, toVersion string, ctx conte

switch Object.GetAPIVersion() {
case BETA_GROUP_VERSION:
spew.Dump("perrooooo")
spew.Dump(convertedObject)
switch toVersion {

case ALPHA_GROUP_VERSION:
spew.Dump("perroooo222o")
spew.Dump(convertedObject)
if convertedObject.Object[KIND] == BUILD_KIND {

unstructured := convertedObject.UnstructuredContent()
Expand All @@ -57,7 +52,6 @@ func convertSHPCR(Object *unstructured.Unstructured, toVersion string, ctx conte
build.ConvertTo(ctx, convertedObject)

} else if convertedObject.Object[KIND] == BUILDRUN_KIND {
spew.Dump("mucho viento")
unstructured := convertedObject.UnstructuredContent()
var buildRun v1beta1.BuildRun
err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, &buildRun)
Expand Down
127 changes: 127 additions & 0 deletions pkg/webhook/conversion/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,124 @@ request:
Expect(build.Spec.Output.PushSecret).To(Equal(&secretName))
})
})

Context("for a BuildRun CR from v1beta1 to v1alpha1", func() {
var desiredAPIVersion = "shipwright.io/v1alpha1"

It("converts for spec Build", func() {
pruneOption := "AfterPull"
buildTemplate := `kind: ConversionReview
apiVersion: %s
request:
uid: 0000-0000-0000-0000
desiredAPIVersion: %s
objects:
- apiVersion: shipwright.io/v1beta1
kind: BuildRun
metadata:
name: buildkit-run
spec:
build:
spec:
source:
type: OCI
contextDir: %s
ociArtifact:
image: %s
prune: %s
pullSecret: %s
`
o := fmt.Sprintf(buildTemplate, apiVersion,
desiredAPIVersion, ctxDir,
image, pruneOption,
secretName)

conversionReview, err := getConversionReview(o)
Expect(err).To(BeNil())
Expect(conversionReview.Response.Result.Status).To(Equal(v1.StatusSuccess))

convertedObj, err := ToUnstructured(conversionReview)
Expect(err).To(BeNil())

buildRun, err := toV1Alpha1BuildRunObject(convertedObj)
Expect(err).To(BeNil())

Expect(buildRun.Spec.BuildSpec.Source.Credentials.Name).To(Equal(secretName))
Expect(buildRun.Spec.BuildSpec.Source.BundleContainer.Image).To(Equal(image))
Expect(*buildRun.Spec.BuildSpec.Source.BundleContainer.Prune).To(Equal(v1alpha1.PruneAfterPull))
Expect(buildRun.Spec.BuildSpec.Source.ContextDir).To(Equal(&ctxDir))
})

It("converts for spec paramas, output, env, retention and volumes", func() {
refBuild := "buildkit-build"
sa := "foobar"
timeout := "10s"
ttl := "10m"
sizeLimit := "500Mi"
buildTemplate := `kind: ConversionReview
apiVersion: %s
request:
uid: 0000-0000-0000-0000
desiredAPIVersion: %s
objects:
- apiVersion: shipwright.io/v1beta1
kind: BuildRun
metadata:
name: buildkit-run
spec:
build:
name: %s
serviceAccount: %s
timeout: %s
paramValues:
- name: foobar
value: bar
output:
image: %s
pushSecret: %s
annotations:
foo: bar
labels:
foo2: bar2
env:
- name: one
value: two
retention:
ttlAfterFailed: %s
volumes:
- name: volume1
emptyDir:
sizeLimit: %s
`
o := fmt.Sprintf(buildTemplate, apiVersion,
desiredAPIVersion, refBuild, sa, timeout,
image, secretName, ttl, sizeLimit)

conversionReview, err := getConversionReview(o)
Expect(err).To(BeNil())
Expect(conversionReview.Response.Result.Status).To(Equal(v1.StatusSuccess))

convertedObj, err := ToUnstructured(conversionReview)
Expect(err).To(BeNil())

buildRun, err := toV1Alpha1BuildRunObject(convertedObj)
Expect(err).To(BeNil())

Expect(buildRun.Spec.BuildRef.Name).To(Equal(refBuild))
Expect(*buildRun.Spec.ServiceAccount.Name).To(Equal(sa))
Expect(buildRun.Spec.Timeout.Duration).To(Equal(time.Duration(10 * time.Second)))
Expect(len(buildRun.Spec.ParamValues)).To(Equal(1))
Expect(buildRun.Spec.Output.Image).To(Equal(image))
Expect(buildRun.Spec.Output.Credentials.Name).To(Equal(secretName))
Expect(len(buildRun.Spec.Output.Annotations)).To(Equal(1))
Expect(len(buildRun.Spec.Output.Labels)).To(Equal(1))
Expect(len(buildRun.Spec.Env)).To(Equal(1))
Expect(buildRun.Spec.Env[0].Name).To(Equal("one"))
Expect(buildRun.Spec.Env[0].Value).To(Equal("two"))
Expect(buildRun.Spec.Retention.TTLAfterFailed.Duration).To(Equal(time.Duration(10 * time.Minute)))
Expect(buildRun.Spec.Volumes[0].Name).To(Equal("volume1"))
})
})
})

func ToUnstructured(conversionReview apiextensionsv1.ConversionReview) (unstructured.Unstructured, error) {
Expand All @@ -603,6 +721,15 @@ func toV1Alpha1BuildObject(convertedObject unstructured.Unstructured) (v1alpha1.
return build, nil
}

func toV1Alpha1BuildRunObject(convertedObject unstructured.Unstructured) (v1alpha1.BuildRun, error) {
var build v1alpha1.BuildRun
u := convertedObject.UnstructuredContent()
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u, &build); err != nil {
return build, err
}
return build, nil
}

func toV1Beta1BuildObject(convertedObject unstructured.Unstructured) (v1beta1.Build, error) {
var build v1beta1.Build
u := convertedObject.UnstructuredContent()
Expand Down

0 comments on commit 42c1b08

Please sign in to comment.