-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] add a v1alpha2 version of LinodeObjectStorageBucket (#427)
* 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
1 parent
d0d96a7
commit bd77f69
Showing
42 changed files
with
1,271 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
141
api/v1alpha1/linodeobjectstoragebucket_conversion_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}), | ||
), | ||
), | ||
) | ||
} |
Oops, something went wrong.