Skip to content

Commit

Permalink
Merge pull request #934 from pivotal/service-binding-conversion
Browse files Browse the repository at this point in the history
Create kpack.io/services annotation when converting to v1alpha1 to prevent loss of data
  • Loading branch information
tomkennedy513 authored Mar 9, 2022
2 parents 1b6bab0 + d039493 commit 4c350cd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
27 changes: 25 additions & 2 deletions pkg/apis/build/v1alpha2/image_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1alpha2

import (
"context"
"encoding/json"
"fmt"

"knative.dev/pkg/apis"
Expand All @@ -12,9 +13,21 @@ import (
func (i *Image) ConvertTo(_ context.Context, to apis.Convertible) error {
switch toImage := to.(type) {
case *v1alpha1.Image:
toImage.ObjectMeta = i.ObjectMeta
i.ObjectMeta.DeepCopyInto(&toImage.ObjectMeta)
i.Spec.convertTo(&toImage.Spec)
i.Status.convertTo(&toImage.Status)
if build := i.Spec.Build; build != nil {
if len(build.Services) > 0 {
bytes, err := json.Marshal(build.Services)
if err != nil {
return err
}
if toImage.Annotations == nil {
toImage.Annotations = map[string]string{}
}
toImage.Annotations["kpack.io/services"] = string(bytes)
}
}
default:
return fmt.Errorf("unknown version, got: %T", toImage)
}
Expand All @@ -24,9 +37,19 @@ func (i *Image) ConvertTo(_ context.Context, to apis.Convertible) error {
func (i *Image) ConvertFrom(_ context.Context, from apis.Convertible) error {
switch fromImage := from.(type) {
case *v1alpha1.Image:
i.ObjectMeta = fromImage.ObjectMeta
fromImage.ObjectMeta.DeepCopyInto(&i.ObjectMeta)
i.Spec.convertFrom(&fromImage.Spec)
i.Status.convertFrom(&fromImage.Status)
if servicesJson, ok := i.Annotations["kpack.io/services"]; ok {
var services Services
err := json.Unmarshal([]byte(servicesJson), &services)
if err != nil {
return err
}

i.Spec.Build.Services = services
delete(i.Annotations, "kpack.io/services")
}
default:
return fmt.Errorf("unknown version, got: %T", fromImage)
}
Expand Down
16 changes: 13 additions & 3 deletions pkg/apis/build/v1alpha2/image_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func testImageConversion(t *testing.T, when spec.G, it spec.S) {

v1alpha2Image := &Image{
ObjectMeta: metav1.ObjectMeta{
Name: "my-super-convertable-image",
Annotations: map[string]string{},
Name: "my-super-convertable-image",
},
Spec: ImageSpec{
Tag: "my-tag",
Expand All @@ -47,7 +48,13 @@ func testImageConversion(t *testing.T, when spec.G, it spec.S) {
SuccessBuildHistoryLimit: &buildHistoryLimit,
ImageTaggingStrategy: corev1alpha1.BuildNumber,
Build: &ImageBuild{
Services: nil,
Services: Services{
{
Kind: "Secret",
Name: "some-secret",
APIVersion: "v1",
},
},
Env: []corev1.EnvVar{
{
Name: "blah",
Expand Down Expand Up @@ -92,6 +99,9 @@ func testImageConversion(t *testing.T, when spec.G, it spec.S) {
v1alpha1Image := &v1alpha1.Image{
ObjectMeta: metav1.ObjectMeta{
Name: "my-super-convertable-image",
Annotations: map[string]string{
"kpack.io/services": `[{"kind":"Secret","name":"some-secret","apiVersion":"v1"}]`,
},
},
Spec: v1alpha1.ImageSpec{
Tag: "my-tag",
Expand Down Expand Up @@ -242,7 +252,6 @@ func testImageConversion(t *testing.T, when spec.G, it spec.S) {
v1alpha2Image.Spec.Build = nil
v1alpha2Image.Spec.Notary = nil
v1alpha2Image.Spec.Cache = nil
v1alpha1Image.Annotations = nil

testV1alpha1Image := &v1alpha1.Image{}
err := v1alpha2Image.ConvertTo(context.TODO(), testV1alpha1Image)
Expand All @@ -251,6 +260,7 @@ func testImageConversion(t *testing.T, when spec.G, it spec.S) {
v1alpha1Image.Spec.Build = nil
v1alpha1Image.Spec.Notary = nil
v1alpha1Image.Spec.CacheSize = nil
v1alpha1Image.Annotations = map[string]string{}
require.Equal(t, testV1alpha1Image, v1alpha1Image)

testV1alpha2Image := &Image{}
Expand Down

0 comments on commit 4c350cd

Please sign in to comment.