Skip to content

Commit c6e5706

Browse files
schrejsbueringer
andauthored
🌱 ipam: add spec.clusterName to IPAddressClaim (#10182)
* ipam: add spec.clusterName to IPAddressClaim * fix fuzzy conversion tests and omit empty spec.ClusterName Co-authored-by: Stefan Bueringer <[email protected]> --------- Co-authored-by: Stefan Bueringer <[email protected]>
1 parent 51d38e0 commit c6e5706

File tree

4 files changed

+77
-14
lines changed

4 files changed

+77
-14
lines changed

config/crd/bases/ipam.cluster.x-k8s.io_ipaddressclaims.yaml

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

exp/ipam/api/v1alpha1/conversion.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
apiconversion "k8s.io/apimachinery/pkg/conversion"
2021
"sigs.k8s.io/controller-runtime/pkg/conversion"
2122

23+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2224
ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1beta1"
2325
)
2426

@@ -49,13 +51,46 @@ func (dst *IPAddressList) ConvertFrom(srcRaw conversion.Hub) error {
4951
func (src *IPAddressClaim) ConvertTo(dstRaw conversion.Hub) error {
5052
dst := dstRaw.(*ipamv1.IPAddressClaim)
5153

52-
return Convert_v1alpha1_IPAddressClaim_To_v1beta1_IPAddressClaim(src, dst, nil)
54+
if err := Convert_v1alpha1_IPAddressClaim_To_v1beta1_IPAddressClaim(src, dst, nil); err != nil {
55+
return err
56+
}
57+
58+
if src.ObjectMeta.Labels != nil {
59+
dst.Spec.ClusterName = src.ObjectMeta.Labels[clusterv1.ClusterNameLabel]
60+
if dst.ObjectMeta.Annotations != nil {
61+
if clusterNameLabelWasSet, ok := dst.ObjectMeta.Annotations["conversion.cluster.x-k8s.io/cluster-name-label-set"]; ok {
62+
if clusterNameLabelWasSet == "false" {
63+
delete(dst.ObjectMeta.Labels, clusterv1.ClusterNameLabel)
64+
}
65+
delete(dst.ObjectMeta.Annotations, "conversion.cluster.x-k8s.io/cluster-name-label-set")
66+
}
67+
}
68+
}
69+
70+
return nil
5371
}
5472

5573
func (dst *IPAddressClaim) ConvertFrom(srcRaw conversion.Hub) error {
5674
src := srcRaw.(*ipamv1.IPAddressClaim)
5775

58-
return Convert_v1beta1_IPAddressClaim_To_v1alpha1_IPAddressClaim(src, dst, nil)
76+
if err := Convert_v1beta1_IPAddressClaim_To_v1alpha1_IPAddressClaim(src, dst, nil); err != nil {
77+
return err
78+
}
79+
80+
if src.Spec.ClusterName != "" {
81+
if dst.ObjectMeta.Labels == nil {
82+
dst.ObjectMeta.Labels = map[string]string{}
83+
}
84+
if _, ok := dst.ObjectMeta.Labels[clusterv1.ClusterNameLabel]; !ok {
85+
if dst.ObjectMeta.Annotations == nil {
86+
dst.ObjectMeta.Annotations = map[string]string{}
87+
}
88+
dst.ObjectMeta.Annotations["conversion.cluster.x-k8s.io/cluster-name-label-set"] = "false"
89+
}
90+
dst.ObjectMeta.Labels[clusterv1.ClusterNameLabel] = src.Spec.ClusterName
91+
}
92+
93+
return nil
5994
}
6095

6196
func (src *IPAddressClaimList) ConvertTo(dstRaw conversion.Hub) error {
@@ -69,3 +104,7 @@ func (dst *IPAddressClaimList) ConvertFrom(srcRaw conversion.Hub) error {
69104

70105
return Convert_v1beta1_IPAddressClaimList_To_v1alpha1_IPAddressClaimList(src, dst, nil)
71106
}
107+
108+
func Convert_v1beta1_IPAddressClaimSpec_To_v1alpha1_IPAddressClaimSpec(from *ipamv1.IPAddressClaimSpec, to *IPAddressClaimSpec, scope apiconversion.Scope) error {
109+
return autoConvert_v1beta1_IPAddressClaimSpec_To_v1alpha1_IPAddressClaimSpec(from, to, scope)
110+
}

exp/ipam/api/v1alpha1/zz_generated.conversion.go

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

exp/ipam/api/v1beta1/ipaddressclaim_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import (
2525

2626
// IPAddressClaimSpec is the desired state of an IPAddressClaim.
2727
type IPAddressClaimSpec struct {
28+
// ClusterName is the name of the Cluster this object belongs to.
29+
// +optional
30+
ClusterName string `json:"clusterName,omitempty"`
31+
2832
// PoolRef is a reference to the pool from which an IP address should be created.
2933
PoolRef corev1.TypedLocalObjectReference `json:"poolRef"`
3034
}

0 commit comments

Comments
 (0)