Skip to content

Commit 7634ee3

Browse files
committed
Validate fields
1 parent fd531a2 commit 7634ee3

13 files changed

+160
-349
lines changed

api/v1alpha1/ionoscloudcluster_types.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,38 @@ const (
8080
LoadBalancerForwardingRuleCreationFailedReason = "LoadBalancerForwardingRuleCreationFailed"
8181
)
8282

83-
// +kubebuilder:validation:Enum=de/txl;de/fra
83+
// +kubebuilder:validation:Enum=es/vlt;fr/par;de/txl;de/fra;gb-lhr;us-ewr;us-las;
8484
type Location string
8585

86+
func (r Location) String() string {
87+
return string(r)
88+
}
89+
8690
// IONOSCloudClusterSpec defines the desired state of IONOSCloudCluster
91+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.dataCenterID) || has(self.dataCenterID)", message="DataCenterID is required once set"
92+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.loadBalancerID) || has(self.loadBalancerID)", message="LoadBalancerID is required once set"
93+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.publicLanID) || has(self.publicLanID)", message="PublicLanID is required once set"
94+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.internetLanID) || has(self.internetLanID)", message="InternetLanID is required once set"
95+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.privateLanID) || has(self.privateLanID)", message="PrivateLanID is required once set"
8796
type IONOSCloudClusterSpec struct {
88-
// +kubebuilder:validation:Enum=es/vlt;fr/par;de/txl;de/fra;gb-lhr;us-ewr;us-las;
89-
Location string `json:"location"`
97+
98+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Location is immutable"
99+
Location Location `json:"location"`
100+
90101
// +kubebuilder:validation:MinLength=1
91102
IdentityName string `json:"identityName"`
92103
ControlPlaneEndpoint clusterv1.APIEndpoint `json:"controlPlaneEndpoint"`
93104

94-
DataCenterID string `json:"dataCenterID,omitempty"`
105+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="DataCenterID is immutable"
106+
DataCenterID string `json:"dataCenterID,omitempty"`
107+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="LoadBalancerID is immutable"
95108
LoadBalancerID string `json:"loadBalancerID,omitempty"`
96-
PublicLanID *int32 `json:"publicLanID,omitempty"`
97-
InternetLanID *int32 `json:"internetLanID,omitempty"`
98-
PrivateLanID *int32 `json:"privateLanID,omitempty"`
109+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="PublicLanID is immutable"
110+
PublicLanID *int32 `json:"publicLanID,omitempty"`
111+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="InternetLanID is immutable"
112+
InternetLanID *int32 `json:"internetLanID,omitempty"`
113+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="PrivateLanID is immutable"
114+
PrivateLanID *int32 `json:"privateLanID,omitempty"`
99115
}
100116

101117
// IONOSCloudClusterStatus defines the observed state of IONOSCloudCluster

api/v1alpha1/ionoscloudcluster_webhook.go

Lines changed: 0 additions & 82 deletions
This file was deleted.

api/v1alpha1/ionoscloudmachine_types.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,47 @@ const (
3131
)
3232

3333
// IONOSCloudMachineSpec defines the desired state of IONOSCloudMachine
34+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.providerID) || has(self.providerID)", message="ProviderID is required once set"
35+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.ip) || has(self.ip)", message="IP is required once set"
3436
type IONOSCloudMachineSpec struct {
3537
// The name of the resource.
3638
Name *string `json:"name,omitempty"`
3739

3840
// The availability zone in which the server should be provisioned.
3941
// +kubebuilder:default=AUTO
42+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="AvailabilityZone is immutable"
4043
AvailabilityZone *string `json:"availabilityZone,omitempty"`
4144
// The total number of cores for the enterprise server.
4245
// +kubebuilder:validation:Minimum=1
46+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Cores is immutable"
4347
Cores *int32 `json:"cores"`
4448
// CPU architecture on which server gets provisioned; not all CPU architectures are available in all datacenter regions; available CPU architectures can be retrieved from the datacenter resource; must not be provided for CUBE servers.
4549
// +kubebuilder:validation:MinLength=1
50+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="CpuFamily is immutable"
4651
CpuFamily *string `json:"cpuFamily"`
4752
// The memory size for the enterprise server in MB, such as 2048.
4853
// +kubebuilder:validation:Minimum=256
4954
// +kubebuilder:validation:MultipleOf=256
55+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Ram is immutable"
5056
Ram *int32 `json:"ram"`
5157
BootVolume IONOSVolumeSpec `json:"bootVolume"`
5258

53-
// primary ip of the virtual machine.
54-
IP *string `json:"ip,omitempty"`
55-
ProviderID string `json:"providerID,omitempty"`
56-
NetworkInterfaceID string `json:"networkInterfaceID,omitempty"`
57-
VolumeID string `json:"volumeID,omitempty"`
59+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="IP is immutable"
60+
IP *string `json:"ip,omitempty"`
61+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="ProviderID is immutable"
62+
ProviderID string `json:"providerID,omitempty"`
5863
}
64+
65+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.sshKeys) || has(self.sshKeys)", message="SSHKeys is required once set"
5966
type IONOSVolumeSpec struct {
60-
Type string `json:"type"`
61-
Size string `json:"size"`
67+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Type is immutable"
68+
Type string `json:"type"`
69+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Size is immutable"
70+
Size string `json:"size"`
71+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Image is immutable"
6272
Image string `json:"image"`
6373
// Public SSH keys are set on the image as authorized keys for appropriate SSH login to the instance using the corresponding private key. This field may only be set in creation requests. When reading, it always returns null. SSH keys are only supported if a public Linux image is used for the volume creation.
74+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="SSHKeys is immutable"
6475
SSHKeys *[]string `json:"sshKeys,omitempty"`
6576
}
6677

api/v1alpha1/ionoscloudmachine_webhook.go

Lines changed: 0 additions & 93 deletions
This file was deleted.

api/v1alpha1/webhook_suite_test.go

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@ package v1alpha1
1818

1919
import (
2020
"context"
21-
"crypto/tls"
22-
"fmt"
23-
"net"
24-
"path/filepath"
25-
"testing"
26-
"time"
27-
2821
. "github.com/onsi/ginkgo/v2"
2922
. "github.com/onsi/gomega"
23+
"path/filepath"
24+
"testing"
3025

3126
admissionv1 "k8s.io/api/admission/v1"
3227
//+kubebuilder:scaffold:imports
@@ -63,9 +58,6 @@ var _ = BeforeSuite(func() {
6358
testEnv = &envtest.Environment{
6459
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
6560
ErrorIfCRDPathMissing: false,
66-
WebhookInstallOptions: envtest.WebhookInstallOptions{
67-
Paths: []string{filepath.Join("..", "..", "config", "webhook")},
68-
},
6961
}
7062

7163
var err error
@@ -88,43 +80,18 @@ var _ = BeforeSuite(func() {
8880
Expect(k8sClient).NotTo(BeNil())
8981

9082
// start webhook server using Manager
91-
webhookInstallOptions := &testEnv.WebhookInstallOptions
9283
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
9384
Scheme: scheme,
94-
Host: webhookInstallOptions.LocalServingHost,
95-
Port: webhookInstallOptions.LocalServingPort,
96-
CertDir: webhookInstallOptions.LocalServingCertDir,
9785
LeaderElection: false,
9886
MetricsBindAddress: "0",
9987
})
10088
Expect(err).NotTo(HaveOccurred())
10189

102-
err = (&IONOSCloudCluster{}).SetupWebhookWithManager(mgr)
103-
Expect(err).NotTo(HaveOccurred())
104-
105-
err = (&IONOSCloudMachine{}).SetupWebhookWithManager(mgr)
106-
Expect(err).NotTo(HaveOccurred())
107-
108-
//+kubebuilder:scaffold:webhook
109-
11090
go func() {
11191
defer GinkgoRecover()
11292
err = mgr.Start(ctx)
11393
Expect(err).NotTo(HaveOccurred())
11494
}()
115-
116-
// wait for the webhook server to get ready
117-
dialer := &net.Dialer{Timeout: time.Second}
118-
addrPort := fmt.Sprintf("%s:%d", webhookInstallOptions.LocalServingHost, webhookInstallOptions.LocalServingPort)
119-
Eventually(func() error {
120-
conn, err := tls.DialWithDialer(dialer, "tcp", addrPort, &tls.Config{InsecureSkipVerify: true})
121-
if err != nil {
122-
return err
123-
}
124-
conn.Close()
125-
return nil
126-
}).Should(Succeed())
127-
12895
})
12996

13097
var _ = AfterSuite(func() {

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/main.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,6 @@ func main() {
119119
os.Exit(1)
120120
}
121121

122-
if err = (&infrastructurev1alpha1.IONOSCloudCluster{}).SetupWebhookWithManager(mgr); err != nil {
123-
setupLog.Error(err, "unable to create webhook", "webhook", "IONOSCloudCluster")
124-
os.Exit(1)
125-
}
126-
if err = (&infrastructurev1alpha1.IONOSCloudMachine{}).SetupWebhookWithManager(mgr); err != nil {
127-
setupLog.Error(err, "unable to create webhook", "webhook", "IONOSCloudMachine")
128-
os.Exit(1)
129-
}
130122
if err = (&controller.IONOSCloudClusterIdentityReconciler{
131123
ControllerContext: &context.ControllerContext{
132124
Context: goctx.Background(),

config/crd/kustomization.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ resources:
99
- bases/infrastructure.cluster.x-k8s.io_ionoscloudclusteridentities.yaml
1010
#+kubebuilder:scaffold:crdkustomizeresource
1111

12-
patches:
12+
#patches:
1313
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix.
1414
# patches here are for enabling the conversion webhook for each CRD
15-
- path: patches/webhook_in_ionoscloudclusters.yaml
16-
- path: patches/webhook_in_ionoscloudmachines.yaml
15+
#- path: patches/webhook_in_ionoscloudclusters.yaml
16+
#- path: patches/webhook_in_ionoscloudmachines.yaml
1717
#- path: patches/webhook_in_ionoscloudclustertemplates.yaml
1818
#- path: patches/webhook_in_ionoscloudmachinetemplates.yaml
1919
#- path: patches/webhook_in_ionoscloudidentities.yaml
2020
#+kubebuilder:scaffold:crdkustomizewebhookpatch
2121

2222
# [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix.
2323
# patches here are for enabling the CA injection for each CRD
24-
- path: patches/cainjection_in_ionoscloudclusters.yaml
25-
- path: patches/cainjection_in_ionoscloudmachines.yaml
24+
#- path: patches/cainjection_in_ionoscloudclusters.yaml
25+
#- path: patches/cainjection_in_ionoscloudmachines.yaml
2626
#- path: patches/cainjection_in_ionoscloudclustertemplates.yaml
2727
#- path: patches/cainjection_in_ionoscloudmachinetemplates.yaml
2828
#- path: patches/cainjection_in_ionoscloudidentities.yaml

0 commit comments

Comments
 (0)