Skip to content

Commit

Permalink
Fix the BuildRun conversion from alpha to beta when .spec.serviceAcco…
Browse files Browse the repository at this point in the history
…unt.generate=true
  • Loading branch information
SaschaSchwarze0 committed Feb 6, 2024
1 parent ea6018e commit fec5af0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/apis/build/v1beta1/buildrun_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ func (dest *BuildRunSpec) ConvertFrom(orig *v1alpha1.BuildRunSpec) error {

if orig.ServiceAccount != nil {
dest.ServiceAccount = orig.ServiceAccount.Name
if orig.ServiceAccount.Generate != nil && *orig.ServiceAccount.Generate {
dest.ServiceAccount = pointer.String(".generate")
}
}

dest.Timeout = orig.Timeout
Expand Down
58 changes: 58 additions & 0 deletions pkg/webhook/conversion/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,63 @@ request:
Expect(buildRun).To(BeComparableTo(desiredBuildRun))
})

It("converts for spec a generated serviceAccount", func() {
// Create the yaml in v1alpha1
buildRunTemplate := `kind: ConversionReview
apiVersion: %s
request:
uid: 0000-0000-0000-0000
desiredAPIVersion: %s
objects:
- apiVersion: shipwright.io/v1alpha1
kind: BuildRun
metadata:
name: buildkit-run
spec:
buildRef:
name: a_build
serviceAccount:
generate: true
output:
image: foobar
`
o := fmt.Sprintf(buildRunTemplate, apiVersion, desiredAPIVersion)

// Invoke the /convert webhook endpoint
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 := toV1Beta1BuildRunObject(convertedObj)
Expect(err).To(BeNil())

// Prepare our desired v1beta1 BuildRun
desiredBuildRun := v1beta1.BuildRun{
ObjectMeta: v1.ObjectMeta{
Name: "buildkit-run",
},
TypeMeta: v1.TypeMeta{
APIVersion: "shipwright.io/v1beta1",
Kind: "BuildRun",
},
Spec: v1beta1.BuildRunSpec{
Build: v1beta1.ReferencedBuild{
Name: pointer.String("a_build"),
},
ServiceAccount: pointer.String(".generate"),
Output: &v1beta1.Image{
Image: "foobar",
},
},
}

// Use ComparableTo and assert the whole object
Expect(buildRun).To(BeComparableTo(desiredBuildRun))
})

It("converts for spec Build buildref", func() {
// Create the yaml in v1alpha1
buildTemplate := `kind: ConversionReview
Expand Down Expand Up @@ -1215,6 +1272,7 @@ request:
Expect(buildRun).To(BeComparableTo(desiredBuildRun))
})
})

Context("for a BuildStrategy spec from v1beta1 to v1alpha1", func() {
var desiredAPIVersion = "shipwright.io/v1alpha1"
It("converts the strategy", func() {
Expand Down

0 comments on commit fec5af0

Please sign in to comment.