Skip to content

Commit

Permalink
crd, kube-1.31: add validations to the model
Browse files Browse the repository at this point in the history
The provide cmd had to be updated to comply with the validations
introduced in this PR.

Signed-off-by: Miguel Duarte Barroso <[email protected]>
  • Loading branch information
maiqueb committed Sep 19, 2024
1 parent 4d83649 commit 0649404
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
25 changes: 24 additions & 1 deletion artifacts/k8s.cni.cncf.io_ipamclaims.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,33 @@ spec:
description: The pod interface name for which this allocation was
created
type: string
x-kubernetes-validations:
- message: Interface is immutable
rule: self == oldSelf
ipRequests:
description: The IPs requested by the user
description: The IPs (v4, v6) requested by the user for this particular
network attachment
items:
description: CIDR represents
type: string
x-kubernetes-validations:
- message: CIDR is invalid
rule: isCIDR(self)
maxItems: 2
type: array
x-kubernetes-validations:
- message: IPRequests are immutable
rule: self == oldSelf
- message: When 2 CIDRs are set, they must be from different IP families
rule: size(self) != 2 || isCIDR(self[0]) && isCIDR(self[1]) && cidr(self[0]).ip().family()
!= cidr(self[1]).ip().family()
network:
description: The network name for which this persistent allocation
was created
type: string
x-kubernetes-validations:
- message: Network is immutable
rule: self == oldSelf
required:
- interface
- network
Expand Down Expand Up @@ -120,7 +138,12 @@ spec:
description: The list of IP addresses (v4, v6) that were allocated
for the pod interface
items:
description: CIDR represents
type: string
x-kubernetes-validations:
- message: CIDR is invalid
rule: isCIDR(self)
maxItems: 2
type: array
ownerPod:
description: The name of the pod holding the IPAMClaim
Expand Down
7 changes: 4 additions & 3 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ func main() {
Name: "example",
},
Spec: v1alpha1.IPAMClaimSpec{
Network: "tenantblue",
Interface: "iface321",
Network: "tenantblue",
Interface: "iface321",
IPRequests: []v1alpha1.CIDR{"10.10.10.0/24", "fd10::0/64"},
},
}

Expand All @@ -61,7 +62,7 @@ func main() {
)
}()

ipamClaim.Status.IPs = []v1alpha1.CIDR{"winner", "winner", "chicken", "dinner"}
ipamClaim.Status.IPs = []v1alpha1.CIDR{"192.168.0.0/16", "fd00:abcd::0/64"}
_, err = exampleClient.K8sV1alpha1().IPAMClaims("default").UpdateStatus(
context.Background(),
ipamClaim,
Expand Down
11 changes: 10 additions & 1 deletion pkg/crd/ipamclaims/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,24 @@ type IPAMClaim struct {

type IPAMClaimSpec struct {
// The network name for which this persistent allocation was created
// +kubebuilder:validation:Required
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Network is immutable"
Network string `json:"network"`
// The pod interface name for which this allocation was created
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Interface is immutable"
Interface string `json:"interface"`
// The IPs requested by the user
// The IPs (v4, v6) requested by the user for this particular network attachment
// +optional
// +kubebuilder:validation:MaxItems=2
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="IPRequests are immutable"
// +kubebuilder:validation:XValidation:rule="size(self) != 2 || isCIDR(self[0]) && isCIDR(self[1]) && cidr(self[0]).ip().family() != cidr(self[1]).ip().family()", message="When 2 CIDRs are set, they must be from different IP families"
IPRequests []CIDR `json:"ipRequests,omitempty"`
}

// IPAMClaimStatus contains the observed status of the IPAMClaim.
type IPAMClaimStatus struct {
// The list of IP addresses (v4, v6) that were allocated for the pod interface
// +kubebuilder:validation:MaxItems=2
IPs []CIDR `json:"ips"`
// The name of the pod holding the IPAMClaim
OwnerPod string `json:"ownerPod"`
Expand All @@ -56,4 +63,6 @@ type IPAMClaimList struct {
Items []IPAMClaim `json:"items"`
}

// CIDR represents
// +kubebuilder:validation:XValidation:rule="isCIDR(self)", message="CIDR is invalid"
type CIDR string

0 comments on commit 0649404

Please sign in to comment.