Skip to content

Commit

Permalink
Add conversion interface
Browse files Browse the repository at this point in the history
Adjust types to implement it
  • Loading branch information
qu1queee committed Aug 2, 2023
1 parent 0039e25 commit eeaeb3d
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 89 deletions.
19 changes: 19 additions & 0 deletions pkg/apis/build/v1alpha1/build_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0

package v1alpha1

import (
"context"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func (src *Build) ConvertTo(ctx context.Context, obj *unstructured.Unstructured) error {
return nil
}

func (src *Build) ConvertFrom(ctx context.Context, obj *unstructured.Unstructured) error {
return nil
}
46 changes: 39 additions & 7 deletions pkg/apis/build/v1beta1/build_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,52 @@
package v1beta1

import (
"context"

"github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/shipwright-io/build/pkg/ctxlog"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
runtime "k8s.io/apimachinery/pkg/runtime"
)

const (
BETA_GROUP_VERSION = "shipwright.io/v1beta1"
ALPHA_GROUP_VERSION = "shipwright.io/v1alpha1"
)

// ConvertTo converts this Build to the receiver APIVersion
func (src *Build) ConvertTo(bs *v1alpha1.Build) error {
func (src *Build) ConvertTo(ctx context.Context, obj *unstructured.Unstructured) error {
var bs v1alpha1.Build

bs.TypeMeta = src.TypeMeta
bs.TypeMeta.APIVersion = ALPHA_GROUP_VERSION
bs.ObjectMeta = src.ObjectMeta
return src.Spec.ConvertTo(&bs.Spec)

src.Spec.ConvertTo(&bs.Spec)

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

return nil
}

// ConvertFrom converts this Build to the implementer object APIVersion
func (src *Build) ConvertFrom(bs *v1alpha1.Build) error {
src.ObjectMeta = bs.ObjectMeta
return src.Spec.ConvertFrom(&bs.Spec)
func (src *Build) ConvertFrom(ctx context.Context, obj *unstructured.Unstructured) error {
var bs v1alpha1.Build

unstructured := obj.UnstructuredContent()
err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, &bs)
if err != nil {
ctxlog.Error(ctx, err, "failed unstructuring the convertedObject")
}
src.TypeMeta = bs.TypeMeta
src.TypeMeta.APIVersion = BETA_GROUP_VERSION

src.Spec.ConvertFrom(&bs.Spec)

return nil
}

func (dest *BuildSpec) ConvertFrom(orig *v1alpha1.BuildSpec) error {
Expand Down
144 changes: 83 additions & 61 deletions pkg/apis/build/v1beta1/buildrun_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,97 @@
package v1beta1

import (
"context"

"github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/conversion"
"github.com/shipwright-io/build/pkg/ctxlog"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
runtime "k8s.io/apimachinery/pkg/runtime"
)

// ConvertTo converts this BuildRun to the Hub version (v1alpha1)
func (src *BuildRun) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*v1alpha1.BuildRun)
dst.ObjectMeta = src.ObjectMeta

// BuildRunSpec BuildSpec
newBuildSpec := v1alpha1.BuildSpec{}
if err := src.Spec.Build.Build.ConvertTo(&newBuildSpec); err != nil {
return err
}

dst.Spec.BuildSpec = &newBuildSpec

// BuildRunSpec BuildRef
dst.Spec.BuildRef = &v1alpha1.BuildRef{
Name: src.Spec.Build.Name,
}

// BuildRunSpec ServiceAccount
dst.Spec.ServiceAccount = &v1alpha1.ServiceAccount{
Name: src.Spec.ServiceAccount,
}
// To Alpha
func (src *BuildRun) ConvertTo(ctx context.Context, obj *unstructured.Unstructured) {

// BuildRunSpec Timeout
dst.Spec.Timeout = src.Spec.Timeout
}

// BuildRunSpec ParamValues
dst.Spec.ParamValues = nil
for _, p := range src.Spec.ParamValues {
new := v1alpha1.ParamValue{}
p.convertTo(&new)
dst.Spec.ParamValues = append(dst.Spec.ParamValues, new)
}
// From Alpha
func (src *BuildRun) ConvertFrom(ctx context.Context, obj *unstructured.Unstructured) {
unstructured := obj.UnstructuredContent()
var build v1alpha1.Build

// BuildRunSpec Image
dst.Spec.Output = &v1alpha1.Image{
Image: src.Spec.Output.Image,
Credentials: &corev1.LocalObjectReference{
Name: *src.Spec.Output.PushSecret,
},
Annotations: src.Spec.Output.Annotations,
Labels: src.Spec.Output.Labels,
err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, &build)
if err != nil {
ctxlog.Error(ctx, err, "failed unstructuring the convertedObject")
}

// BuildRunSpec State
dst.Spec.State = (*v1alpha1.BuildRunRequestedState)(src.Spec.State)

// BuildRunSpec Env
dst.Spec.Env = src.Spec.Env

// BuildRunSpec Retention
dst.Spec.Retention = (*v1alpha1.BuildRunRetention)(src.Spec.Retention)
// you have a alpha build, which you will set in beta

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

}
return nil
}

// ConvertFrom converts from the Hub version (v1alpha1) to this version.
// TODO: Not needed?
func (dst *BuildRun) ConvertFrom(srcRaw conversion.Hub) error {
return nil
}
// // ConvertTo converts this BuildRun to the Hub version (v1alpha1)
// func (src *BuildRun) ConvertTo(dstRaw conversion.Hub) error {
// dst := dstRaw.(*v1alpha1.BuildRun)
// dst.ObjectMeta = src.ObjectMeta

// // BuildRunSpec BuildSpec
// newBuildSpec := v1alpha1.BuildSpec{}
// if err := src.Spec.Build.Build.ConvertTo(&newBuildSpec); err != nil {
// return err
// }

// dst.Spec.BuildSpec = &newBuildSpec

// // BuildRunSpec BuildRef
// dst.Spec.BuildRef = &v1alpha1.BuildRef{
// Name: src.Spec.Build.Name,
// }

// // BuildRunSpec ServiceAccount
// dst.Spec.ServiceAccount = &v1alpha1.ServiceAccount{
// Name: src.Spec.ServiceAccount,
// }

// // BuildRunSpec Timeout
// dst.Spec.Timeout = src.Spec.Timeout

// // BuildRunSpec ParamValues
// dst.Spec.ParamValues = nil
// for _, p := range src.Spec.ParamValues {
// new := v1alpha1.ParamValue{}
// p.convertTo(&new)
// dst.Spec.ParamValues = append(dst.Spec.ParamValues, new)
// }

// // BuildRunSpec Image
// dst.Spec.Output = &v1alpha1.Image{
// Image: src.Spec.Output.Image,
// Credentials: &corev1.LocalObjectReference{
// Name: *src.Spec.Output.PushSecret,
// },
// Annotations: src.Spec.Output.Annotations,
// Labels: src.Spec.Output.Labels,
// }

// // BuildRunSpec State
// dst.Spec.State = (*v1alpha1.BuildRunRequestedState)(src.Spec.State)

// // BuildRunSpec Env
// dst.Spec.Env = src.Spec.Env

// // BuildRunSpec Retention
// dst.Spec.Retention = (*v1alpha1.BuildRunRetention)(src.Spec.Retention)

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

// }
// return nil
// }

// // ConvertFrom converts from the Hub version (v1alpha1) to this version.
// // TODO: Not needed?
// func (dst *BuildRun) ConvertFrom(srcRaw conversion.Hub) error {
// return nil
// }
23 changes: 2 additions & 21 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/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"github.com/shipwright-io/build/pkg/ctxlog"

Expand Down Expand Up @@ -49,17 +48,7 @@ func convertSHPCR(Object *unstructured.Unstructured, toVersion string, ctx conte
if err != nil {
ctxlog.Error(ctx, err, "failed unstructuring the convertedObject")
}
var buildAlpha v1alpha1.Build

buildAlpha.TypeMeta = build.TypeMeta
buildAlpha.TypeMeta.APIVersion = ALPHA_GROUP_VERSION
build.ConvertTo(&buildAlpha)

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

} else {
return nil, statusErrorWithMessage("unsupported Kind")
Expand All @@ -72,17 +61,9 @@ func convertSHPCR(Object *unstructured.Unstructured, toVersion string, ctx conte
case BETA_GROUP_VERSION:
if convertedObject.Object[KIND] == BUILD_KIND {

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 = BETA_GROUP_VERSION
buildBeta.ConvertFrom(&buildAlpha)
buildBeta.ConvertFrom(ctx, convertedObject)

mapito, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&buildBeta)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions pkg/webhook/conversion/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0
package conversion

import (
"context"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type Conversion interface {
ConvertFrom(context.Context, *unstructured.Unstructured) error
ConvertTo(context.Context, *unstructured.Unstructured) error
}

0 comments on commit eeaeb3d

Please sign in to comment.