Skip to content

Commit de723c7

Browse files
kstrenkovalpiwowar
authored andcommitted
Add tempest webhook to test-operator
This patch adds tempest webhook to check workflow option is not set when debug mode is set to true. It is to avoid unwanted behavior when in debug mode. Webhook can be later applied to check different configurations validity.
1 parent 79edf42 commit de723c7

23 files changed

+656
-40
lines changed

PROJECT

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ resources:
2020
kind: Tempest
2121
path: github.com/openstack-k8s-operators/test-operator/api/v1beta1
2222
version: v1beta1
23+
webhooks:
24+
defaulting: true
25+
validation: true
26+
webhookVersion: v1
2327
- api:
2428
crdVersion: v1
2529
namespaced: true

api/v1beta1/tempest_types.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ type TempestRunSpec struct {
172172
// +kubebuilder:validation:Optional
173173
// Extra images that should be downloaded inside the test pod and uploaded to
174174
// openstack.
175-
ExtraImages []ExtraImagesType `json:"extraImages"`
175+
ExtraImages []ExtraImagesType `json:"extraImages,omitempty"`
176176
}
177177

178178
// TempestconfRunSpec - is used to configure execution of discover-tempest-config
@@ -346,7 +346,6 @@ type TempestSpec struct {
346346
// spawned by the test operator.
347347
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
348348

349-
350349
// +kubebuilder:validation:Optional
351350
// +kubebuilder:default=false
352351
// Activate debug mode. When debug mode is activated any error encountered

api/v1beta1/tempest_types_workflow.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type WorkflowTempestRunSpec struct {
6161
// +kubebuilder:validation:Optional
6262
// Extra images that should be downloaded inside the test pod and uploaded to
6363
// openstack.
64-
ExtraImages *[]ExtraImagesType `json:"extraImagesType"`
64+
ExtraImages *[]ExtraImagesType `json:"extraImagesType,omitempty"`
6565
}
6666

6767
// TempestconfRunSpec - is used to configure execution of discover-tempest-config
@@ -70,114 +70,114 @@ type WorkflowTempestRunSpec struct {
7070
type WorkflowTempestconfRunSpec struct {
7171
// +kubebuilder:validation:Optional
7272
// Indicate whether discover-tempest-config should be executed with --create
73-
Create *bool `json:"create"`
73+
Create *bool `json:"create,omitempty"`
7474

7575
// +kubebuilder:validation:Optional
7676
// Indicate whether discover-tempest-config should be executed with
7777
// --collect-timing
78-
CollectTiming *bool `json:"collectTiming"`
78+
CollectTiming *bool `json:"collectTiming,omitempty"`
7979

8080
// +kubebuilder:validation:Optional
8181
// Indicate whether discover-tempest-config should be executed with --insecure
82-
Insecure *bool `json:"insecure"`
82+
Insecure *bool `json:"insecure,omitempty"`
8383

8484
// +kubebuilder:validation:Optional
8585
// Indicate whether discover-tempest-config should be executed with
8686
// --no-default-deployer
87-
NoDefaultDeployer *bool `json:"noDefaultDeployer"`
87+
NoDefaultDeployer *bool `json:"noDefaultDeployer,omitempty"`
8888

8989
// +kubebuilder:validation:Optional
9090
// Indicate whether discover-tempest-config should be executed with --debug
91-
Debug *bool `json:"debug"`
91+
Debug *bool `json:"debug,omitempty"`
9292

9393
// +kubebuilder:validation:Optional
9494
// Indicate whether discover-tempest-config should be executed with --verbose
95-
Verbose *bool `json:"verbose"`
95+
Verbose *bool `json:"verbose,omitempty"`
9696

9797
// +kubebuilder:validation:Optional
9898
// Indicate whether discover-tempest-config should be executed with --non-admin
99-
NonAdmin *bool `json:"nonAdmin"`
99+
NonAdmin *bool `json:"nonAdmin,omitempty"`
100100

101101
// +kubebuilder:validation:Optional
102102
// Indicate whether discover-tempest-config should be executed with --retry-image
103-
RetryImage *bool `json:"retryImage"`
103+
RetryImage *bool `json:"retryImage,omitempty"`
104104

105105
// +kubebuilder:validation:Optional
106106
// Indicate whether discover-tempest-config should be executed with
107107
// --convert-to-raw
108-
ConvertToRaw *bool `json:"convertToRaw"`
108+
ConvertToRaw *bool `json:"convertToRaw,omitempty"`
109109

110110
// +kubebuilder:validation:Optional
111111
// The content of this variable will be passed to discover-tempest-config via
112112
// the --out parameter
113-
Out *string `json:"out"`
113+
Out *string `json:"out,omitempty"`
114114

115115
// +kubebuilder:validation:Optional
116116
// A content of deployer_input.ini that is passed to tempest via --deployer-input
117-
DeployerInput *string `json:"deployerInput"`
117+
DeployerInput *string `json:"deployerInput,omitempty"`
118118

119119
// +kubebuilder:validation:Optional
120120
// A content of accounts.yaml that is passed to tempest via --test-acounts
121-
TestAccounts *string `json:"testAccounts"`
121+
TestAccounts *string `json:"testAccounts,omitempty"`
122122

123123
// +kubebuilder:validation:Optional
124124
// The content of this variable will be passed to discover-tempest-config via
125125
// the --create-accounts-file
126-
CreateAccountsFile *string `json:"createAccountsFile"`
126+
CreateAccountsFile *string `json:"createAccountsFile,omitempty"`
127127

128128
// +kubebuilder:validation:Optional
129129
// A content of profile.yaml that is passed to tempest via --profile
130-
Profile *string `json:"profile"`
130+
Profile *string `json:"profile,omitempty"`
131131

132132
// +kubebuilder:validation:Optional
133133
// The content of this variable will be passed to discover-tempest-config via
134134
// --generate-profile
135-
GenerateProfile *string `json:"generateProfile"`
135+
GenerateProfile *string `json:"generateProfile,omitempty"`
136136

137137
// +kubebuilder:validation:Optional
138138
// The content of this variable will be passed to discover-tempest-config via
139139
// --image-disk-format
140-
ImageDiskFormat *string `json:"imageDiskFormat"`
140+
ImageDiskFormat *string `json:"imageDiskFormat,omitempty"`
141141

142142
// +kubebuilder:validation:Optional
143143
// The content of this variable will be passed to discover-tempest-config via
144144
// --image
145-
Image *string `json:"image"`
145+
Image *string `json:"image,omitempty"`
146146

147147
// +kubebuilder:validation:Optional
148148
// The content of this variable will be passed to discover-tempest-config via
149149
// --flavor-min-mem
150-
FlavorMinMem *int64 `json:"flavorMinMem"`
150+
FlavorMinMem *int64 `json:"flavorMinMem,omitempty"`
151151

152152
// +kubebuilder:validation:Optional
153153
// The content of this variable will be passed to discover-tempest-config via
154154
// --flavor-min-disk
155-
FlavorMinDisk *int64 `json:"flavorMinDisk"`
155+
FlavorMinDisk *int64 `json:"flavorMinDisk,omitempty"`
156156

157157
// +kubebuilder:validation:Optional
158158
// The content of this variable will be passed to discover-tempest-config via
159159
// --network-id
160-
NetworkID *string `json:"networkID"`
160+
NetworkID *string `json:"networkID,omitempty"`
161161

162162
// +kubebuilder:validation:Optional
163163
// The content of this variable will be passed to discover-tempest-config via
164164
// --append
165-
Append *string `json:"append"`
165+
Append *string `json:"append,omitempty"`
166166

167167
// +kubebuilder:validation:Optional
168168
// The content of this variable will be passed to discover-tempest-config via
169169
// --remove
170-
Remove *string `json:"remove"`
170+
Remove *string `json:"remove,omitempty"`
171171

172172
// +kubebuilder:validation:Optional
173173
// The content of this variable will be appended at the end of the command
174174
// that executes discover-tempest-config (override values).
175-
Overrides *string `json:"overrides"`
175+
Overrides *string `json:"overrides,omitempty"`
176176

177177
// +kubebuilder:validation:Optional
178178
// The content of this variable will be passed to discover-tempest-config via
179179
// --timeout
180-
Timeout *int64 `json:"timeout"`
180+
Timeout *int64 `json:"timeout,omitempty"`
181181
}
182182

183183
// TempestSpec - configuration of execution of tempest. For specific configuration
@@ -191,12 +191,12 @@ type WorkflowTempestSpec struct {
191191
// +kubebuilder:validation:Optional
192192
// Name of a storage class that is used to create PVCs for logs storage. Required
193193
// if default storage class does not exist.
194-
StorageClass *string `json:"storageClass"`
194+
StorageClass *string `json:"storageClass,omitempty"`
195195

196196
// +kubebuilder:validation:Optional
197197
// An URL of a tempest container image that should be used for the execution
198198
// of tempest tests.
199-
ContainerImage *string `json:"containerImage"`
199+
ContainerImage *string `json:"containerImage,omitempty"`
200200

201201
// +kubebuilder:validation:Optional
202202
// By default test-operator executes the test-pods sequentially if multiple
@@ -217,11 +217,11 @@ type WorkflowTempestSpec struct {
217217

218218
// +kubebuilder:validation:Optional
219219
// OpenStackConfigMap is the name of the ConfigMap containing the clouds.yaml
220-
OpenStackConfigMap *string `json:"openStackConfigMap"`
220+
OpenStackConfigMap *string `json:"openStackConfigMap,omitempty"`
221221

222222
// +kubebuilder:validation:Optional
223223
// OpenStackConfigSecret is the name of the Secret containing the secure.yaml
224-
OpenStackConfigSecret *string `json:"openStackConfigSecret"`
224+
OpenStackConfigSecret *string `json:"openStackConfigSecret,omitempty"`
225225

226226
// +kubebuilder:validation:Optional
227227
// NetworkAttachments is a list of NetworkAttachment resource names to expose

api/v1beta1/tempest_webhook.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Copyright 2023.
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+
//
18+
// Generated by:
19+
//
20+
// operator-sdk create webhook --group test --version v1beta1 --kind Tempest --programmatic-validation --defaulting
21+
//
22+
23+
package v1beta1
24+
25+
import (
26+
"errors"
27+
"k8s.io/apimachinery/pkg/runtime"
28+
ctrl "sigs.k8s.io/controller-runtime"
29+
logf "sigs.k8s.io/controller-runtime/pkg/log"
30+
"sigs.k8s.io/controller-runtime/pkg/webhook"
31+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
32+
)
33+
34+
// TempestDefaults -
35+
type TempestDefaults struct {
36+
ContainerImageURL string
37+
}
38+
39+
var tempestDefaults TempestDefaults
40+
41+
// log is for logging in this package.
42+
var tempestlog = logf.Log.WithName("tempest-resource")
43+
44+
func (r *Tempest) SetupWebhookWithManager(mgr ctrl.Manager) error {
45+
return ctrl.NewWebhookManagedBy(mgr).
46+
For(r).
47+
Complete()
48+
}
49+
50+
//+kubebuilder:webhook:path=/mutate-test-openstack-org-v1beta1-tempest,mutating=true,failurePolicy=fail,sideEffects=None,groups=test.openstack.org,resources=tempests,verbs=create;update,versions=v1beta1,name=mtempest.kb.io,admissionReviewVersions=v1
51+
52+
var _ webhook.Defaulter = &Tempest{}
53+
54+
// Default implements webhook.Defaulter so a webhook will be registered for the type
55+
func (r *Tempest) Default() {
56+
tempestlog.Info("default", "name", r.Name)
57+
58+
r.Spec.Default()
59+
}
60+
61+
// Default - set defaults for this Tempest spec.
62+
func (spec *TempestSpec) Default() {
63+
if spec.ContainerImage == "" {
64+
spec.ContainerImage = tempestDefaults.ContainerImageURL
65+
}
66+
}
67+
68+
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
69+
//+kubebuilder:webhook:path=/validate-test-openstack-org-v1beta1-tempest,mutating=false,failurePolicy=fail,sideEffects=None,groups=test.openstack.org,resources=tempests,verbs=create;update,versions=v1beta1,name=vtempest.kb.io,admissionReviewVersions=v1
70+
71+
var _ webhook.Validator = &Tempest{}
72+
73+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
74+
func (r *Tempest) ValidateCreate() (admission.Warnings, error) {
75+
tempestlog.Info("validate create", "name", r.Name)
76+
77+
if len(r.Spec.Workflow) > 0 && r.Spec.Debug {
78+
return nil, errors.New("Workflow variable must be empty to run debug mode!")
79+
}
80+
return nil, nil
81+
}
82+
83+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
84+
func (r *Tempest) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
85+
tempestlog.Info("validate update", "name", r.Name)
86+
87+
// TODO(user): fill in your validation logic upon object update.
88+
return nil, nil
89+
}
90+
91+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
92+
func (r *Tempest) ValidateDelete() (admission.Warnings, error) {
93+
tempestlog.Info("validate delete", "name", r.Name)
94+
95+
// TODO(user): fill in your validation logic upon object deletion.
96+
return nil, nil
97+
}

0 commit comments

Comments
 (0)