Skip to content

Commit 9cefd10

Browse files
committed
customize v1beta1 affinity group conversion to v1beta2
1 parent 18bc4f5 commit 9cefd10

File tree

5 files changed

+59
-112
lines changed

5 files changed

+59
-112
lines changed

api/v1beta1/cloudstackaffinitygroup_conversion.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
"context"
21+
conv "k8s.io/apimachinery/pkg/conversion"
2022
"sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta2"
23+
"sigs.k8s.io/controller-runtime/pkg/client"
2124
"sigs.k8s.io/controller-runtime/pkg/conversion"
2225
)
2326

@@ -30,3 +33,44 @@ func (dst *CloudStackAffinityGroup) ConvertFrom(srcRaw conversion.Hub) error { /
3033
src := srcRaw.(*v1beta2.CloudStackAffinityGroup)
3134
return Convert_v1beta2_CloudStackAffinityGroup_To_v1beta1_CloudStackAffinityGroup(src, dst, nil)
3235
}
36+
37+
//nolint:golint,revive,stylecheck
38+
func Convert_v1beta1_CloudStackAffinityGroup_To_v1beta2_CloudStackAffinityGroup(in *CloudStackAffinityGroup, out *v1beta2.CloudStackAffinityGroup, s conv.Scope) error {
39+
out.ObjectMeta = in.ObjectMeta
40+
41+
csCluster := &v1beta2.CloudStackCluster{}
42+
clusterName := in.ObjectMeta.Labels["cluster.x-k8s.io/cluster-name"]
43+
namespace := in.ObjectMeta.Namespace
44+
key := client.ObjectKey{Name: clusterName, Namespace: namespace}
45+
if err := v1beta2.K8sClient.Get(context.TODO(), key, csCluster); err != nil {
46+
return err
47+
}
48+
49+
out.Spec = v1beta2.CloudStackAffinityGroupSpec{
50+
Type: in.Spec.Type,
51+
Name: in.Spec.Name,
52+
ID: in.Spec.ID,
53+
FailureDomainName: csCluster.Spec.FailureDomains[0].Name,
54+
}
55+
56+
out.Status = v1beta2.CloudStackAffinityGroupStatus{
57+
Ready: in.Status.Ready,
58+
}
59+
return nil
60+
}
61+
62+
//nolint:golint,revive,stylecheck
63+
func Convert_v1beta2_CloudStackAffinityGroup_To_v1beta1_CloudStackAffinityGroup(in *v1beta2.CloudStackAffinityGroup, out *CloudStackAffinityGroup, s conv.Scope) error {
64+
out.ObjectMeta = in.ObjectMeta
65+
66+
out.Spec = CloudStackAffinityGroupSpec{
67+
Type: in.Spec.Type,
68+
Name: in.Spec.Name,
69+
ID: in.Spec.ID,
70+
}
71+
72+
out.Status = CloudStackAffinityGroupStatus{
73+
Ready: in.Status.Ready,
74+
}
75+
return nil
76+
}

api/v1beta1/cloudstackaffinitygroup_types.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
AffinityGroupFinalizer = "affinitygroup.infrastructure.cluster.x-k8s.io"
2525
)
2626

27+
//+k8s:conversion-gen=false
2728
// CloudStackAffinityGroupSpec defines the desired state of CloudStackAffinityGroup
2829
type CloudStackAffinityGroupSpec struct {
2930
// Mutually exclusive parameter with AffinityGroupIDs.
@@ -38,6 +39,7 @@ type CloudStackAffinityGroupSpec struct {
3839
ID string `json:"id,omitempty"`
3940
}
4041

42+
//+k8s:conversion-gen=false
4143
// CloudStackAffinityGroupStatus defines the observed state of CloudStackAffinityGroup
4244
type CloudStackAffinityGroupStatus struct {
4345
// Reflects the readiness of the CS Affinity Group.
@@ -46,7 +48,7 @@ type CloudStackAffinityGroupStatus struct {
4648

4749
//+kubebuilder:object:root=true
4850
//+kubebuilder:subresource:status
49-
51+
//+k8s:conversion-gen=false
5052
// CloudStackAffinityGroup is the Schema for the cloudstackaffinitygroups API
5153
type CloudStackAffinityGroup struct {
5254
metav1.TypeMeta `json:",inline"`

api/v1beta1/conversion_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import (
2626
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2727
)
2828

29-
30-
3129
var _ = Describe("Conversion", func() {
3230
BeforeEach(func() { // Reset test vars to initial state.
3331
})

api/v1beta1/zz_generated.conversion.go

Lines changed: 10 additions & 107 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/kustomization.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ patchesStrategicMerge:
2121
- patches/webhook_in_cloudstackmachines.yaml
2222
- patches/webhook_in_cloudstackmachinetemplates.yaml
2323
- patches/webhook_in_cloudstackisolatednetworks.yaml
24-
- patches/webhook_in_cloudstackzones.yaml
24+
# - patches/webhook_in_cloudstackzones.yaml
2525
- patches/webhook_in_cloudstackaffinitygroups.yaml
2626
- patches/webhook_in_cloudstackmachinestatecheckers.yaml
27-
- patches/webhook_in_cloudstackfailuredomains.yaml
27+
# - patches/webhook_in_cloudstackfailuredomains.yaml
2828
#+kubebuilder:scaffold:crdkustomizewebhookpatch
2929

3030
# patches here are for enabling the CA injection for each CRD

0 commit comments

Comments
 (0)