-
Notifications
You must be signed in to change notification settings - Fork 26
/
openstackbaremetalset_webhook.go
178 lines (144 loc) · 5.65 KB
/
openstackbaremetalset_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Copyright 2023.
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.
*/
// Generated by:
//
// operator-sdk create webhook --group baremetal --version v1beta1 --kind OpenStackBaremetalSet --programmatic-validation
//
package v1beta1
import (
"context"
"fmt"
metal3v1alpha1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/openstack-k8s-operators/lib-common/modules/common/labels"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
goClient "sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
// Client needed for API calls (manager's client, set by first SetupWebhookWithManager() call
// to any particular webhook)
var webhookClient goClient.Client
// log is for logging in this package.
var openstackbaremetalsetlog = logf.Log.WithName("openstackbaremetalset-resource")
// SetupWebhookWithManager - register this webhook with the controller manager
func (r *OpenStackBaremetalSet) SetupWebhookWithManager(mgr ctrl.Manager) error {
if webhookClient == nil {
webhookClient = mgr.GetClient()
}
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
// +kubebuilder:webhook:verbs=create;update;delete,path=/validate-baremetal-openstack-org-v1beta1-openstackbaremetalset,mutating=false,failurePolicy=fail,sideEffects=None,groups=baremetal.openstack.org,resources=openstackbaremetalsets,versions=v1beta1,name=vopenstackbaremetalset.kb.io,admissionReviewVersions=v1
var _ webhook.Validator = &OpenStackBaremetalSet{}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *OpenStackBaremetalSet) ValidateCreate() error {
openstackbaremetalsetlog.Info("validate create", "name", r.Name)
//
// Validate that there are enough available BMHs for the initial requested count
//
baremetalHostsList, err := GetBaremetalHosts(
context.TODO(),
webhookClient,
r.Spec.BmhNamespace,
r.Spec.BmhLabelSelector,
)
if err != nil {
return err
}
if _, err := VerifyBaremetalSetScaleUp(openstackbaremetalsetlog, r, baremetalHostsList, &metal3v1alpha1.BareMetalHostList{}); err != nil {
return err
}
return r.validateCr()
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *OpenStackBaremetalSet) ValidateUpdate(old runtime.Object) error {
openstackbaremetalsetlog.Info("validate update", "name", r.Name)
var ok bool
var oldInstance *OpenStackBaremetalSet
if oldInstance, ok = old.(*OpenStackBaremetalSet); !ok {
return fmt.Errorf("runtime object is not an OpenStackBaremetalSet")
}
//
// Force BmhLabelSelector and HardwareReqs to remain the same unless the *old* count of spec.BaremetalHosts was 0.
// We do this to maintain consistency across the gathered list of BMHs during reconcile.
//
oldCount := len(oldInstance.Spec.BaremetalHosts)
newCount := len(r.Spec.BaremetalHosts)
if oldCount > 0 &&
(!equality.Semantic.DeepEqual(r.Spec.BmhLabelSelector, oldInstance.Spec.BmhLabelSelector) ||
!equality.Semantic.DeepEqual(r.Spec.HardwareReqs, oldInstance.Spec.HardwareReqs)) {
return fmt.Errorf("cannot change \"bmhLabelSelector\" nor \"hardwareReqs\" when previous count of \"baremetalHosts\" > 0")
}
if newCount != oldCount {
//
// Don't allow count changes if instance.Status.BaremetalHosts contains any
// bmhRefs that are missing from Metal3 BMHs. We need to force the user to
// restore the old BMHs before allowing the OSBMS controller to perform any
// operations for scaling up or down.
//
// TODO: Create a specific context here instead of passing TODO()?
if err := VerifyBaremetalStatusBmhRefs(context.TODO(), webhookClient, r); err != nil {
return err
}
//
// Validate that there are enough available BMHs for a potential scale-up
//
if newCount > oldCount {
// Every BMH available that matches our (optional) labels
baremetalHostsList, err := GetBaremetalHosts(
context.TODO(),
webhookClient,
r.Spec.BmhNamespace,
r.Spec.BmhLabelSelector,
)
if err != nil {
return err
}
// All BMHs were are *already* using
existingBaremetalHosts, err := GetBaremetalHosts(
context.TODO(),
webhookClient,
r.Spec.BmhNamespace,
labels.GetLabels(r, labels.GetGroupLabel(ServiceName), map[string]string{}),
)
if err != nil {
return err
}
if _, err := VerifyBaremetalSetScaleUp(openstackbaremetalsetlog, r, baremetalHostsList, existingBaremetalHosts); err != nil {
return err
}
}
}
return r.validateCr()
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *OpenStackBaremetalSet) ValidateDelete() error {
openstackbaremetalsetlog.Info("validate delete", "name", r.Name)
return nil
}
func (r *OpenStackBaremetalSet) validateCr() error {
if err := r.checkBaseImageReqs(); err != nil {
return err
}
return nil
}
func (r *OpenStackBaremetalSet) checkBaseImageReqs() error {
// TODO(rabi): Uncomment after dataplane-operator api bump
// if r.Spec.OSImage == "" && r.Spec.ProvisionServerName == "" {
// return fmt.Errorf("either \"osImage\" or \"provisionServerName\" must be provided")
// }
return nil
}