Skip to content

Commit

Permalink
Convert for BuildStrategy and ClusterBuildStrategy
Browse files Browse the repository at this point in the history
Add convertion functions
Ensure converter knows about Strategies
Minor fix for test cases
  • Loading branch information
qu1queee committed Aug 14, 2023
1 parent 7dd06dc commit c7322d6
Show file tree
Hide file tree
Showing 8 changed files with 469 additions and 23 deletions.
24 changes: 24 additions & 0 deletions pkg/apis/build/v1alpha1/buildrun_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0

package v1alpha1

import (
"context"
"fmt"

"github.com/shipwright-io/build/pkg/webhook"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

// ensure v1beta1 implements the Conversion interface
var _ webhook.Conversion = (*BuildRun)(nil)

func (src *BuildRun) ConvertTo(ctx context.Context, obj *unstructured.Unstructured) error {
return fmt.Errorf("v1alpha1 is the current storage version, nothing to convert to")
}

func (src *BuildRun) ConvertFrom(ctx context.Context, obj *unstructured.Unstructured) error {
return fmt.Errorf("v1alpha1 is the current storage version, nothing to convert from")
}
24 changes: 24 additions & 0 deletions pkg/apis/build/v1alpha1/buildstrategy_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0

package v1alpha1

import (
"context"
"fmt"

"github.com/shipwright-io/build/pkg/webhook"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

// ensure v1beta1 implements the Conversion interface
var _ webhook.Conversion = (*BuildStrategy)(nil)

func (src *BuildStrategy) ConvertTo(ctx context.Context, obj *unstructured.Unstructured) error {
return fmt.Errorf("v1alpha1 is the current storage version, nothing to convert to")
}

func (src *BuildStrategy) ConvertFrom(ctx context.Context, obj *unstructured.Unstructured) error {
return fmt.Errorf("v1alpha1 is the current storage version, nothing to convert from")
}
24 changes: 24 additions & 0 deletions pkg/apis/build/v1alpha1/clusterbuildstrategy_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0

package v1alpha1

import (
"context"
"fmt"

"github.com/shipwright-io/build/pkg/webhook"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

// ensure v1beta1 implements the Conversion interface
var _ webhook.Conversion = (*ClusterBuildStrategy)(nil)

func (src *ClusterBuildStrategy) ConvertTo(ctx context.Context, obj *unstructured.Unstructured) error {
return fmt.Errorf("v1alpha1 is the current storage version, nothing to convert to")
}

func (src *ClusterBuildStrategy) ConvertFrom(ctx context.Context, obj *unstructured.Unstructured) error {
return fmt.Errorf("v1alpha1 is the current storage version, nothing to convert from")
}
12 changes: 8 additions & 4 deletions pkg/apis/build/v1beta1/buildrun_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ func (dest *BuildRunSpec) ConvertFrom(orig *v1alpha1.BuildRunSpec) error {
dest.Output = &Image{}
if orig.Output != nil {
dest.Output.Image = orig.Output.Image
dest.Output.Annotations = orig.Output.Annotations
dest.Output.Labels = orig.Output.Labels
}

if orig.Output != nil && orig.Output.Credentials != nil {
Expand All @@ -207,10 +209,12 @@ func (dest *BuildRunSpec) ConvertFrom(orig *v1alpha1.BuildRunSpec) error {
dest.Retention = (*BuildRunRetention)(orig.Retention)

// BuildRunSpec Volumes
for i, vol := range orig.Volumes {
dest.Volumes[i].Name = vol.Name
dest.Volumes[i].VolumeSource = vol.VolumeSource

dest.Volumes = []BuildVolume{}
for _, vol := range orig.Volumes {
dest.Volumes = append(dest.Volumes, BuildVolume{
Name: vol.Name,
VolumeSource: vol.VolumeSource,
})
}
return nil
}
140 changes: 133 additions & 7 deletions pkg/apis/build/v1beta1/buildstrategy_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,144 @@
package v1beta1

import (
"sigs.k8s.io/controller-runtime/pkg/conversion"
"context"

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

// ConvertTo converts this BuildStrategy to the Hub version (v1alpha1)
func (src *BuildStrategy) ConvertTo(dstRaw conversion.Hub) error {
// dst := dstRaw.(*v1alpha1.BuildStrategy)
// ensure v1beta1 implements the Conversion interface
var _ webhook.Conversion = (*BuildStrategy)(nil)

// To Alpha
func (src *BuildStrategy) ConvertTo(ctx context.Context, obj *unstructured.Unstructured) error {
var bs v1alpha1.BuildStrategy
bs.TypeMeta = src.TypeMeta
bs.TypeMeta.APIVersion = ALPHA_GROUP_VERSION
bs.ObjectMeta = src.ObjectMeta

bs.Spec.BuildSteps = []v1alpha1.BuildStep{}
for _, step := range src.Spec.Steps {

buildStep := v1alpha1.BuildStep{
Container: corev1.Container{
Name: step.Name,
Image: step.Image,
Command: step.Command,
Args: step.Args,
WorkingDir: step.WorkingDir,
Env: step.Env,
Resources: step.Resources,
VolumeMounts: step.VolumeMounts,
ImagePullPolicy: step.ImagePullPolicy,
},
}

if step.SecurityContext != nil {
buildStep.SecurityContext = step.SecurityContext
}

bs.Spec.BuildSteps = append(bs.Spec.BuildSteps, buildStep)
}

bs.Spec.Parameters = []v1alpha1.Parameter{}
for _, param := range src.Spec.Parameters {
bs.Spec.Parameters = append(bs.Spec.Parameters, v1alpha1.Parameter{
Name: param.Name,
Description: param.Description,
Type: v1alpha1.ParameterType(param.Type),
Default: param.Default,
Defaults: param.Defaults,
})
}

if src.Spec.SecurityContext != nil {
bs.Spec.SecurityContext = (*v1alpha1.BuildStrategySecurityContext)(src.Spec.SecurityContext)
}

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

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

return nil
}

// ConvertFrom converts from the Hub version (v1alpha1) to this version.
// TODO: Not needed?
func (dst *BuildStrategy) ConvertFrom(srcRaw conversion.Hub) error {
// From Alpha
func (src *BuildStrategy) ConvertFrom(ctx context.Context, obj *unstructured.Unstructured) error {
var br v1alpha1.BuildStrategy

unstructured := obj.UnstructuredContent()
err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, &br)
if err != nil {
ctxlog.Error(ctx, err, "failed unstructuring the buildrun convertedObject")
}

src.ObjectMeta = br.ObjectMeta
src.TypeMeta = br.TypeMeta
src.TypeMeta.APIVersion = BETA_GROUP_VERSION

src.Spec.Steps = []Step{}
for _, brStep := range br.Spec.BuildSteps {

step := Step{
Name: brStep.Name,
Image: brStep.Image,
Command: brStep.Command,
Args: brStep.Args,
WorkingDir: brStep.WorkingDir,
Env: brStep.Env,
Resources: brStep.Resources,
VolumeMounts: brStep.VolumeMounts,
ImagePullPolicy: brStep.ImagePullPolicy,
}

if brStep.SecurityContext != nil {
step.SecurityContext = brStep.SecurityContext
}

src.Spec.Steps = append(src.Spec.Steps, step)
}

src.Spec.Parameters = []Parameter{}
for _, param := range br.Spec.Parameters {
src.Spec.Parameters = append(src.Spec.Parameters, Parameter{
Name: param.Name,
Description: param.Description,
Type: ParameterType(param.Type),
Default: param.Default,
Defaults: param.Defaults,
})
}

if br.Spec.SecurityContext != nil {
src.Spec.SecurityContext = (*BuildStrategySecurityContext)(br.Spec.SecurityContext)
}

src.Spec.Volumes = []BuildStrategyVolume{}
for _, vol := range br.Spec.Volumes {
src.Spec.Volumes = append(src.Spec.Volumes, BuildStrategyVolume{
Overridable: vol.Overridable,
Name: vol.Name,
Description: &vol.Name,
VolumeSource: vol.VolumeSource,
})
}

return nil
}

0 comments on commit c7322d6

Please sign in to comment.