Skip to content

Commit

Permalink
[feat] add a v1alpha2 version of LinodeObjectStorageBucket (#427)
Browse files Browse the repository at this point in the history
* v1alpha2 for linodeObjectStorageBucket

* converting cluster ID into regionID

* fixing lint changes

* Using conversion-gen to convert Linode OBJ api conversions

* adding lint suggestions

* handling region conversion from v1alpha2 -> v1alpha1

* fixing lint issue

* changing hostname condition statement

---------

Co-authored-by: Unnati Aggarwal <[email protected]>
Co-authored-by: Unnati Aggarwal <[email protected]>
  • Loading branch information
3 people committed Jul 31, 2024
1 parent d0d96a7 commit bd77f69
Show file tree
Hide file tree
Showing 42 changed files with 1,271 additions and 147 deletions.
12 changes: 12 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,16 @@ resources:
webhooks:
conversion: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
domain: cluster.x-k8s.io
group: infrastructure
kind: LinodeObjectStorageBucket
path: github.com/linode/cluster-api-provider-linode/api/v1alpha2
version: v1alpha2
webhooks:
conversion: true
validation: true
webhookVersion: v1
version: "3"
29 changes: 29 additions & 0 deletions api/v1alpha1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1alpha1

import (
"strings"

"k8s.io/apimachinery/pkg/conversion"

infrastructurev1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
Expand All @@ -43,3 +45,30 @@ func Convert_v1alpha2_LinodeMachineSpec_To_v1alpha1_LinodeMachineSpec(in *infras
// Ok to use the auto-generated conversion function, it simply drops the PlacementGroupRef, and copies everything else
return autoConvert_v1alpha2_LinodeMachineSpec_To_v1alpha1_LinodeMachineSpec(in, out, s)
}

func Convert_v1alpha1_LinodeObjectStorageBucketSpec_To_v1alpha2_LinodeObjectStorageBucketSpec(in *LinodeObjectStorageBucketSpec, out *infrastructurev1alpha2.LinodeObjectStorageBucketSpec, s conversion.Scope) error {
// WARNING: in.Cluster requires manual conversion: does not exist in peer-type
out.Region = in.Cluster
out.CredentialsRef = in.CredentialsRef
out.KeyGeneration = in.KeyGeneration
out.SecretType = in.SecretType
return nil
}

func Convert_v1alpha2_LinodeObjectStorageBucketSpec_To_v1alpha1_LinodeObjectStorageBucketSpec(in *infrastructurev1alpha2.LinodeObjectStorageBucketSpec, out *LinodeObjectStorageBucketSpec, s conversion.Scope) error {
// WARNING: in.Region requires manual conversion: does not exist in peer-type
out.Cluster = in.Region
out.CredentialsRef = in.CredentialsRef
out.KeyGeneration = in.KeyGeneration
out.SecretType = in.SecretType
return nil
}

func Convert_v1alpha2_LinodeObjectStorageBucket_To_v1alpha1_LinodeObjectStorageBucket(in *infrastructurev1alpha2.LinodeObjectStorageBucket, out *LinodeObjectStorageBucket, scope conversion.Scope) error {
if in.Status.Hostname != nil && *in.Status.Hostname != "" {
in.Spec.Region = strings.Split(*in.Status.Hostname, ".")[1]
} else {
in.Spec.Region += "-1"
}
return autoConvert_v1alpha2_LinodeObjectStorageBucket_To_v1alpha1_LinodeObjectStorageBucket(in, out, scope)
}
65 changes: 65 additions & 0 deletions api/v1alpha1/linodeobjectstoragebucket_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2023 Akamai Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"errors"

utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"

infrastructurev1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
)

// ConvertTo converts this LinodeObjectStorageBucket to the Hub version (v1alpha2).
func (src *LinodeObjectStorageBucket) ConvertTo(dstRaw conversion.Hub) error {
dst, ok := dstRaw.(*infrastructurev1alpha2.LinodeObjectStorageBucket)
if !ok {
return errors.New("failed to convert LinodeObjectStorageBucket version from v1alpha1 to v1alpha2")
}

if err := Convert_v1alpha1_LinodeObjectStorageBucket_To_v1alpha2_LinodeObjectStorageBucket(src, dst, nil); err != nil {
return err
}

// Manually restore data from annotations
restored := &LinodeObjectStorageBucket{}
if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
return err
}

return nil
}

// ConvertFrom converts from the Hub version (v1alpha2) to this version.
func (dst *LinodeObjectStorageBucket) ConvertFrom(srcRaw conversion.Hub) error {
src, ok := srcRaw.(*infrastructurev1alpha2.LinodeObjectStorageBucket)
if !ok {
return errors.New("failed to convert LinodeObjectStorageBucket version from v1alpha2 to v1alpha1")
}

if err := Convert_v1alpha2_LinodeObjectStorageBucket_To_v1alpha1_LinodeObjectStorageBucket(src, dst, nil); err != nil {
return err
}

// Preserve Hub data on down-conversion.
if err := utilconversion.MarshalData(src, dst); err != nil {
return err
}

return nil
}
141 changes: 141 additions & 0 deletions api/v1alpha1/linodeobjectstoragebucket_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
Copyright 2023 Akamai Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"

infrav1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
"github.com/linode/cluster-api-provider-linode/mock"

. "github.com/linode/cluster-api-provider-linode/mock/mocktest"
)

const (
ConversionDataAnnotation = "cluster.x-k8s.io/conversion-data"
)

func TestLinodeObjectStorageBucketConvertTo(t *testing.T) {
t.Parallel()

src := &LinodeObjectStorageBucket{
ObjectMeta: metav1.ObjectMeta{Name: "test-bucket"},
Spec: LinodeObjectStorageBucketSpec{
Cluster: "us-mia-1",
CredentialsRef: &corev1.SecretReference{
Namespace: "default",
Name: "cred-secret",
},
KeyGeneration: ptr.To(1),
SecretType: "Opaque",
},
Status: LinodeObjectStorageBucketStatus{},
}
expectedDst := &infrav1alpha2.LinodeObjectStorageBucket{
ObjectMeta: metav1.ObjectMeta{Name: "test-bucket"},
Spec: infrav1alpha2.LinodeObjectStorageBucketSpec{
Region: "us-mia-1",
CredentialsRef: &corev1.SecretReference{
Namespace: "default",
Name: "cred-secret",
},
KeyGeneration: ptr.To(1),
SecretType: "Opaque",
},
Status: infrav1alpha2.LinodeObjectStorageBucketStatus{},
}
dst := &infrav1alpha2.LinodeObjectStorageBucket{}

NewSuite(t, mock.MockLinodeClient{}).Run(
OneOf(
Path(
Call("convert v1alpha1 to v1alpha2", func(ctx context.Context, mck Mock) {
err := src.ConvertTo(dst)
if err != nil {
t.Fatalf("ConvertTo failed: %v", err)
}
}),
Result("conversion succeeded", func(ctx context.Context, mck Mock) {
if diff := cmp.Diff(expectedDst, dst); diff != "" {
t.Errorf("ConvertTo() mismatch (-expected +got):\n%s", diff)
}
}),
),
),
)
}

func TestLinodeObjectStorageBucketFrom(t *testing.T) {
t.Parallel()

src := &infrav1alpha2.LinodeObjectStorageBucket{
ObjectMeta: metav1.ObjectMeta{Name: "test-bucket"},
Spec: infrav1alpha2.LinodeObjectStorageBucketSpec{
Region: "us-mia",
CredentialsRef: &corev1.SecretReference{
Namespace: "default",
Name: "cred-secret",
},
KeyGeneration: ptr.To(1),
SecretType: "Opaque",
},
Status: infrav1alpha2.LinodeObjectStorageBucketStatus{},
}
expectedDst := &LinodeObjectStorageBucket{
ObjectMeta: metav1.ObjectMeta{
Name: "test-bucket",
Annotations: map[string]string{
ConversionDataAnnotation: `{"spec":{"credentialsRef":{"name":"cred-secret","namespace":"default"},"keyGeneration":1,"region":"us-mia-1","secretType":"Opaque"},"status":{"ready":false}}`,
},
},
Spec: LinodeObjectStorageBucketSpec{
Cluster: "us-mia-1",
CredentialsRef: &corev1.SecretReference{
Namespace: "default",
Name: "cred-secret",
},
KeyGeneration: ptr.To(1),
SecretType: "Opaque",
},
Status: LinodeObjectStorageBucketStatus{},
}
dst := &LinodeObjectStorageBucket{}

NewSuite(t, mock.MockLinodeClient{}).Run(
OneOf(
Path(
Call("convert v1alpha2 to v1alpha1", func(ctx context.Context, mck Mock) {
err := dst.ConvertFrom(src)
if err != nil {
t.Fatalf("ConvertFrom failed: %v", err)
}
}),
Result("conversion succeeded", func(ctx context.Context, mck Mock) {
if diff := cmp.Diff(expectedDst, dst); diff != "" {
t.Errorf("ConvertFrom() mismatch (-expected +got):\n%s", diff)
}
}),
),
),
)
}
Loading

0 comments on commit bd77f69

Please sign in to comment.