-
Notifications
You must be signed in to change notification settings - Fork 38
/
kthreescontrolplane_webhook.go
88 lines (69 loc) · 3.47 KB
/
kthreescontrolplane_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
/*
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 v1beta2
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
// SetupWebhookWithManager will setup the webhooks for the KThreesControlPlane.
func (in *KThreesControlPlane) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(in).
WithDefaulter(&KThreesControlPlane{}).
WithValidator(&KThreesControlPlane{}).
Complete()
}
// +kubebuilder:webhook:verbs=create;update,path=/validate-controlplane-cluster-x-k8s-io-v1beta2-kthreescontrolplane,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=controlplane.cluster.x-k8s.io,resources=kthreescontrolplanes,versions=v1beta2,name=validation.kthreescontrolplane.controlplane.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// +kubebuilder:webhook:verbs=create;update,path=/mutate-controlplane-cluster-x-k8s-io-v1beta2-kthreescontrolplane,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=controlplane.cluster.x-k8s.io,resources=kthreescontrolplanes,versions=v1beta2,name=default.kthreescontrolplane.controlplane.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
var _ admission.CustomDefaulter = &KThreesControlPlane{}
var _ admission.CustomValidator = &KThreesControlPlane{}
// ValidateCreate will do any extra validation when creating a KThreesControlPlane.
func (in *KThreesControlPlane) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
return []string{}, nil
}
// ValidateUpdate will do any extra validation when updating a KThreesControlPlane.
func (in *KThreesControlPlane) ValidateUpdate(_ context.Context, _, _ runtime.Object) (admission.Warnings, error) {
return []string{}, nil
}
// ValidateDelete allows you to add any extra validation when deleting.
func (in *KThreesControlPlane) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
return []string{}, nil
}
// Default will set default values for the KThreesControlPlane.
func (in *KThreesControlPlane) Default(_ context.Context, obj runtime.Object) error {
c, ok := obj.(*KThreesControlPlane)
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected a KThreesControlPlane but got a %T", obj))
}
defaultKThreesControlPlaneSpec(&c.Spec, c.Namespace)
return nil
}
func defaultKThreesControlPlaneSpec(s *KThreesControlPlaneSpec, namespace string) {
if s.Replicas == nil {
replicas := int32(1)
s.Replicas = &replicas
}
if s.MachineTemplate.InfrastructureRef.Namespace == "" {
s.MachineTemplate.InfrastructureRef.Namespace = namespace
}
if s.KThreesConfigSpec.ServerConfig.DisableCloudController == nil {
s.KThreesConfigSpec.ServerConfig.DisableCloudController = ptr.To(true)
}
if s.KThreesConfigSpec.ServerConfig.CloudProviderName == nil {
s.KThreesConfigSpec.ServerConfig.CloudProviderName = ptr.To("external")
}
}