Skip to content

Commit 7d78ed5

Browse files
Add webhook for IBMPowerVS resources (#488)
Signed-off-by: Prajyot-Parab <[email protected]>
1 parent 39f98af commit 7d78ed5

33 files changed

+647
-133
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ deploy: manifests $(KUSTOMIZE)
9999

100100
# Generate manifests e.g. CRD, RBAC etc.
101101
manifests: controller-gen
102-
#$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
102+
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
103103
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role paths="./..." output:crd:artifacts:config=config/crd/bases
104104

105105
.PHONY: generate-go-conversions-core

PROJECT

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
domain: cluster.x-k8s.io
22
repo: sigs.k8s.io/cluster-api-provider-ibmcloud
33
resources:
4-
# v1alpha4 types
54
- group: infrastructure
65
kind: IBMVPCCluster
76
version: v1alpha4
@@ -20,7 +19,6 @@ resources:
2019
- group: infrastructure
2120
kind: IBMPowerVSMachineTemplate
2221
version: v1alpha4
23-
# v1beta1 types
2422
- group: infrastructure
2523
kind: IBMVPCCluster
2624
version: v1beta1

api/v1beta1/common.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
func defaultIBMPowerVSMachineSpec(spec *IBMPowerVSMachineSpec) {
20+
21+
if spec.Memory == "" {
22+
spec.Memory = "8"
23+
}
24+
if spec.Processors == "" {
25+
spec.Processors = "0.25"
26+
}
27+
if spec.SysType == "" {
28+
spec.SysType = "s922"
29+
}
30+
if spec.ProcType == "" {
31+
spec.ProcType = "shared"
32+
}
33+
}
34+
35+
func validateIBMPowerVSSysType(spec IBMPowerVSMachineSpec) (bool, IBMPowerVSMachineSpec) {
36+
sysTypes := [...]string{"s922", "e980"}
37+
for _, st := range sysTypes {
38+
if spec.SysType == st {
39+
return true, IBMPowerVSMachineSpec{}
40+
}
41+
}
42+
return false, spec
43+
}
44+
45+
func validateIBMPowerVSProcType(spec IBMPowerVSMachineSpec) (bool, IBMPowerVSMachineSpec) {
46+
procTypes := [...]string{"shared", "dedicated", "capped"}
47+
for _, pt := range procTypes {
48+
if spec.ProcType == pt {
49+
return true, IBMPowerVSMachineSpec{}
50+
}
51+
}
52+
return false, spec
53+
}
54+
55+
func validateIBMPowerVSNetwork(network IBMPowerVSResourceReference) (bool, IBMPowerVSResourceReference) {
56+
if network.ID != nil && network.Name != nil {
57+
return false, network
58+
}
59+
return true, IBMPowerVSResourceReference{}
60+
}

api/v1beta1/ibmpowervscluster_types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2222
)
2323

24-
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
2524
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
2625

2726
const (
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
import (
20+
apierrors "k8s.io/apimachinery/pkg/api/errors"
21+
"k8s.io/apimachinery/pkg/runtime"
22+
"k8s.io/apimachinery/pkg/runtime/schema"
23+
"k8s.io/apimachinery/pkg/util/validation/field"
24+
ctrl "sigs.k8s.io/controller-runtime"
25+
logf "sigs.k8s.io/controller-runtime/pkg/log"
26+
"sigs.k8s.io/controller-runtime/pkg/webhook"
27+
)
28+
29+
// log is for logging in this package.
30+
var ibmpowervsclusterlog = logf.Log.WithName("ibmpowervscluster-resource")
31+
32+
func (r *IBMPowerVSCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
33+
return ctrl.NewWebhookManagedBy(mgr).
34+
For(r).
35+
Complete()
36+
}
37+
38+
//+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-ibmpowervscluster,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclusters,verbs=create;update,versions=v1beta1,name=mibmpowervscluster.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
39+
40+
var _ webhook.Defaulter = &IBMPowerVSCluster{}
41+
42+
// Default implements webhook.Defaulter so a webhook will be registered for the type
43+
func (r *IBMPowerVSCluster) Default() {
44+
ibmpowervsclusterlog.Info("default", "name", r.Name)
45+
}
46+
47+
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
48+
//+kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-ibmpowervscluster,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclusters,versions=v1beta1,name=vibmpowervscluster.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
49+
50+
var _ webhook.Validator = &IBMPowerVSCluster{}
51+
52+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
53+
func (r *IBMPowerVSCluster) ValidateCreate() error {
54+
ibmpowervsclusterlog.Info("validate create", "name", r.Name)
55+
return r.validateIBMPowerVSCluster()
56+
}
57+
58+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
59+
func (r *IBMPowerVSCluster) ValidateUpdate(old runtime.Object) error {
60+
ibmpowervsclusterlog.Info("validate update", "name", r.Name)
61+
return r.validateIBMPowerVSCluster()
62+
}
63+
64+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
65+
func (r *IBMPowerVSCluster) ValidateDelete() error {
66+
ibmpowervsclusterlog.Info("validate delete", "name", r.Name)
67+
return nil
68+
}
69+
70+
func (r *IBMPowerVSCluster) validateIBMPowerVSCluster() error {
71+
var allErrs field.ErrorList
72+
if err := r.validateIBMPowerVSClusterNetwork(); err != nil {
73+
allErrs = append(allErrs, err)
74+
}
75+
if len(allErrs) == 0 {
76+
return nil
77+
}
78+
79+
return apierrors.NewInvalid(
80+
schema.GroupKind{Group: "infrastructure.cluster.x-k8s.io", Kind: "IBMPowerVSCluster"},
81+
r.Name, allErrs)
82+
}
83+
84+
func (r *IBMPowerVSCluster) validateIBMPowerVSClusterNetwork() *field.Error {
85+
if res, net := validateIBMPowerVSNetwork(r.Spec.Network); !res {
86+
return field.Invalid(field.NewPath("spec", "network"), net, "Only one of Network - ID or Name may be specified")
87+
}
88+
return nil
89+
}

api/v1beta1/ibmpowervsmachine_types.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2222
)
2323

24-
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
2524
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
2625

2726
const (
@@ -45,16 +44,20 @@ type IBMPowerVSMachineSpec struct {
4544
Image IBMPowerVSResourceReference `json:"image"`
4645

4746
// SysType is the System type used to host the vsi
48-
SysType string `json:"sysType"`
47+
// +optional
48+
SysType string `json:"sysType,omitempty"`
4949

5050
// ProcType is the processor type, e.g: dedicated, shared, capped
51-
ProcType string `json:"procType"`
51+
// +optional
52+
ProcType string `json:"procType,omitempty"`
5253

5354
// Processors is Number of processors allocated
54-
Processors string `json:"processors"`
55+
// +optional
56+
Processors string `json:"processors,omitempty"`
5557

5658
// Memory is Amount of memory allocated (in GB)
57-
Memory string `json:"memory"`
59+
// +optional
60+
Memory string `json:"memory,omitempty"`
5861

5962
// Network is the reference to the Network to use for this instance.
6063
Network IBMPowerVSResourceReference `json:"network"`
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
import (
20+
apierrors "k8s.io/apimachinery/pkg/api/errors"
21+
"k8s.io/apimachinery/pkg/runtime"
22+
"k8s.io/apimachinery/pkg/runtime/schema"
23+
"k8s.io/apimachinery/pkg/util/validation/field"
24+
ctrl "sigs.k8s.io/controller-runtime"
25+
logf "sigs.k8s.io/controller-runtime/pkg/log"
26+
"sigs.k8s.io/controller-runtime/pkg/webhook"
27+
)
28+
29+
// log is for logging in this package.
30+
var ibmpowervsmachinelog = logf.Log.WithName("ibmpowervsmachine-resource")
31+
32+
func (r *IBMPowerVSMachine) SetupWebhookWithManager(mgr ctrl.Manager) error {
33+
return ctrl.NewWebhookManagedBy(mgr).
34+
For(r).
35+
Complete()
36+
}
37+
38+
//+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-ibmpowervsmachine,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsmachines,verbs=create;update,versions=v1beta1,name=mibmpowervsmachine.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
39+
40+
var _ webhook.Defaulter = &IBMPowerVSMachine{}
41+
42+
// Default implements webhook.Defaulter so a webhook will be registered for the type
43+
func (r *IBMPowerVSMachine) Default() {
44+
ibmpowervsmachinelog.Info("default", "name", r.Name)
45+
defaultIBMPowerVSMachineSpec(&r.Spec)
46+
}
47+
48+
//+kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-ibmpowervsmachine,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsmachines,versions=v1beta1,name=vibmpowervsmachine.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
49+
50+
var _ webhook.Validator = &IBMPowerVSMachine{}
51+
52+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
53+
func (r *IBMPowerVSMachine) ValidateCreate() error {
54+
ibmpowervsmachinelog.Info("validate create", "name", r.Name)
55+
return r.validateIBMPowerVSMachine()
56+
}
57+
58+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
59+
func (r *IBMPowerVSMachine) ValidateUpdate(old runtime.Object) error {
60+
ibmpowervsmachinelog.Info("validate update", "name", r.Name)
61+
return r.validateIBMPowerVSMachine()
62+
}
63+
64+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
65+
func (r *IBMPowerVSMachine) ValidateDelete() error {
66+
ibmpowervsmachinelog.Info("validate delete", "name", r.Name)
67+
return nil
68+
}
69+
70+
func (r *IBMPowerVSMachine) validateIBMPowerVSMachine() error {
71+
var allErrs field.ErrorList
72+
if err := r.validateIBMPowerVSMachineSysType(); err != nil {
73+
allErrs = append(allErrs, err)
74+
}
75+
if err := r.validateIBMPowerVSMachineProcType(); err != nil {
76+
allErrs = append(allErrs, err)
77+
}
78+
if err := r.validateIBMPowerVSMachineNetwork(); err != nil {
79+
allErrs = append(allErrs, err)
80+
}
81+
if len(allErrs) == 0 {
82+
return nil
83+
}
84+
85+
return apierrors.NewInvalid(
86+
schema.GroupKind{Group: "infrastructure.cluster.x-k8s.io", Kind: "IBMPowerVSMachine"},
87+
r.Name, allErrs)
88+
}
89+
90+
func (r *IBMPowerVSMachine) validateIBMPowerVSMachineSysType() *field.Error {
91+
if res, spec := validateIBMPowerVSSysType(r.Spec); !res {
92+
return field.Invalid(field.NewPath("spec", "sysType"), spec.SysType, "Invalid System Type")
93+
}
94+
return nil
95+
}
96+
97+
func (r *IBMPowerVSMachine) validateIBMPowerVSMachineProcType() *field.Error {
98+
if res, spec := validateIBMPowerVSProcType(r.Spec); !res {
99+
return field.Invalid(field.NewPath("spec", "procType"), spec.ProcType, "Invalid Processor Type")
100+
}
101+
return nil
102+
}
103+
104+
func (r *IBMPowerVSMachine) validateIBMPowerVSMachineNetwork() *field.Error {
105+
if res, net := validateIBMPowerVSNetwork(r.Spec.Network); !res {
106+
return field.Invalid(field.NewPath("spec", "network"), net, "Only one of Network - ID or Name may be specified")
107+
}
108+
return nil
109+
}

api/v1beta1/ibmpowervsmachinetemplate_types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2121
)
2222

23-
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
2423
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
2524

2625
// IBMPowerVSMachineTemplateSpec defines the desired state of IBMPowerVSMachineTemplate

0 commit comments

Comments
 (0)