-
Notifications
You must be signed in to change notification settings - Fork 551
/
awscluster_webhook.go
139 lines (112 loc) · 4.81 KB
/
awscluster_webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha3
import (
"fmt"
"reflect"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
// log is for logging in this package.
var _ = logf.Log.WithName("awscluster-resource")
func (r *AWSCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha3-awscluster,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclusters,versions=v1alpha3,name=validation.awscluster.infrastructure.cluster.x-k8s.io,sideEffects=None
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1alpha3-awscluster,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclusters,versions=v1alpha3,name=default.awscluster.infrastructure.cluster.x-k8s.io,sideEffects=None
var (
_ webhook.Validator = &AWSCluster{}
_ webhook.Defaulter = &AWSCluster{}
)
func (r *AWSCluster) ValidateCreate() error {
var allErrs field.ErrorList
allErrs = append(allErrs, r.Spec.Bastion.Validate()...)
allErrs = append(allErrs, r.validateSSHKeyName()...)
return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
}
func (r *AWSCluster) ValidateDelete() error {
return nil
}
func (r *AWSCluster) ValidateUpdate(old runtime.Object) error {
var allErrs field.ErrorList
oldC, ok := old.(*AWSCluster)
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected an AWSCluster but got a %T", old))
}
if r.Spec.Region != oldC.Spec.Region {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "region"), r.Spec.Region, "field is immutable"),
)
}
existingLoadBalancer := &AWSLoadBalancerSpec{}
newLoadBalancer := &AWSLoadBalancerSpec{}
if oldC.Spec.ControlPlaneLoadBalancer != nil {
existingLoadBalancer = oldC.Spec.ControlPlaneLoadBalancer.DeepCopy()
}
if r.Spec.ControlPlaneLoadBalancer != nil {
newLoadBalancer = r.Spec.ControlPlaneLoadBalancer.DeepCopy()
}
if !reflect.DeepEqual(existingLoadBalancer.Scheme, newLoadBalancer.Scheme) {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "controlPlaneLoadBalancer", "scheme"),
r.Spec.ControlPlaneLoadBalancer.Scheme, "field is immutable"),
)
}
if !reflect.DeepEqual(oldC.Spec.ControlPlaneEndpoint, clusterv1.APIEndpoint{}) &&
!reflect.DeepEqual(r.Spec.ControlPlaneEndpoint, oldC.Spec.ControlPlaneEndpoint) {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "controlPlaneEndpoint"), r.Spec.ControlPlaneEndpoint, "field is immutable"),
)
}
// Modifying VPC id is not allowed because it will cause a new VPC creation if set to nil.
if !reflect.DeepEqual(oldC.Spec.NetworkSpec, NetworkSpec{}) &&
!reflect.DeepEqual(oldC.Spec.NetworkSpec.VPC, VPCSpec{}) &&
oldC.Spec.NetworkSpec.VPC.ID != "" {
if reflect.DeepEqual(r.Spec.NetworkSpec, NetworkSpec{}) ||
reflect.DeepEqual(r.Spec.NetworkSpec.VPC, VPCSpec{}) ||
oldC.Spec.NetworkSpec.VPC.ID != r.Spec.NetworkSpec.VPC.ID {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "networkSpec", "vpc", "id"),
r.Spec.IdentityRef, "field cannot be modified once set"))
}
}
// If a identityRef is already set, do not allow removal of it.
if oldC.Spec.IdentityRef != nil && r.Spec.IdentityRef == nil {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "identityRef"),
r.Spec.IdentityRef, "field cannot be set to nil"),
)
}
allErrs = append(allErrs, r.Spec.Bastion.Validate()...)
return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
}
func (r *AWSCluster) Default() {
SetDefaults_Bastion(&r.Spec.Bastion)
SetDefaults_NetworkSpec(&r.Spec.NetworkSpec)
if r.Spec.IdentityRef == nil {
r.Spec.IdentityRef = &AWSIdentityReference{
Kind: ControllerIdentityKind,
Name: AWSClusterControllerIdentityName,
}
}
}
func (r *AWSCluster) validateSSHKeyName() field.ErrorList {
return validateSSHKeyName(r.Spec.SSHKeyName)
}