Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 1baec60

Browse files
committed
either one of facility or metro must be given in packetcluster
Signed-off-by: Chris Privitere <[email protected]>
1 parent 44ca7d9 commit 1baec60

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

api/v1alpha3/packetcluster_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type PacketClusterSpec struct {
3030
ProjectID string `json:"projectID"`
3131

3232
// Facility represents the Packet facility for this cluster
33+
// +optional
3334
Facility string `json:"facility,omitempty"`
3435

3536
// Metro represents the Packet metro for this cluster

api/v1beta1/packetcluster_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type PacketClusterSpec struct {
3535
ProjectID string `json:"projectID"`
3636

3737
// Facility represents the Packet facility for this cluster
38+
// +optional
3839
Facility string `json:"facility,omitempty"`
3940

4041
// Metro represents the Packet metro for this cluster

api/v1beta1/packetcluster_webhook.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ func (c *PacketCluster) ValidateUpdate(oldRaw runtime.Object) error {
8585
)
8686
}
8787

88+
// Must have either one of Metro or Facility
89+
if len(c.Spec.Facility) == 0 && len(c.Spec.Metro) == 0 {
90+
allErrs = append(allErrs,
91+
field.Invalid(field.NewPath("spec", "Metro"),
92+
c.Spec.Metro, "field is required when Facility is not set"),
93+
)
94+
}
95+
96+
// Must not have both Metro and Facility
97+
if len(c.Spec.Facility) > 0 && len(c.Spec.Metro) > 0 {
98+
allErrs = append(allErrs,
99+
field.Invalid(field.NewPath("spec", "Facility"),
100+
c.Spec.Facility, "field is mutually exclusive with Metro"),
101+
)
102+
}
103+
88104
if len(allErrs) == 0 {
89105
return nil
90106
}

api/v1beta1/packetmachine_webhook.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ func (m *PacketMachine) ValidateUpdate(old runtime.Object) error {
6161
})
6262
}
6363

64+
// Either Metro or Facility must be set, but not both
65+
if m.Spec.Metro != "" && m.Spec.Facility != "" {
66+
return apierrors.NewInvalid(GroupVersion.WithKind("PacketMachine").GroupKind(), m.Name, field.ErrorList{
67+
field.Forbidden(field.NewPath("spec", "metro"), "cannot be set when spec.facility is set"),
68+
})
69+
} else if m.Spec.Metro == "" && m.Spec.Facility == "" {
70+
return apierrors.NewInvalid(GroupVersion.WithKind("PacketMachine").GroupKind(), m.Name, field.ErrorList{
71+
field.Required(field.NewPath("spec", "metro"), "must be set when spec.facility is not set"),
72+
})
73+
}
74+
6475
newPacketMachineSpec, _ := newPacketMachine["spec"].(map[string]interface{})
6576
oldPacketMachineSpec, _ := oldPacketMachine["spec"].(map[string]interface{})
6677

0 commit comments

Comments
 (0)