-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtemporalworker_webhook.go
More file actions
182 lines (148 loc) · 6.1 KB
/
temporalworker_webhook.go
File metadata and controls
182 lines (148 loc) · 6.1 KB
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
179
180
181
182
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2024 Datadog, Inc.
package v1alpha1
import (
"context"
"fmt"
"time"
"github.com/temporalio/temporal-worker-controller/internal/defaults"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
const (
maxTemporalWorkerDeploymentNameLen = 63
)
func (r *TemporalWorkerDeployment) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
//+kubebuilder:webhook:path=/mutate-temporal-io-temporal-io-v1alpha1-temporalworkerdeployment,mutating=true,failurePolicy=fail,sideEffects=None,groups=temporal.io.temporal.io,resources=temporalworkers,verbs=create;update,versions=v1alpha1,name=mtemporalworker.kb.io,admissionReviewVersions=v1
var _ webhook.CustomDefaulter = &TemporalWorkerDeployment{}
var _ webhook.CustomValidator = &TemporalWorkerDeployment{}
// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
func (r *TemporalWorkerDeployment) Default(ctx context.Context, obj runtime.Object) error {
dep, ok := obj.(*TemporalWorkerDeployment)
if !ok {
return apierrors.NewBadRequest("expected a TemporalWorkerDeployment")
}
if err := dep.Spec.Default(ctx); err != nil {
return err
}
return nil
}
func (s *TemporalWorkerDeploymentSpec) Default(ctx context.Context) error {
if s.SunsetStrategy.ScaledownDelay == nil {
s.SunsetStrategy.ScaledownDelay = &v1.Duration{Duration: defaults.ScaledownDelay}
}
if s.SunsetStrategy.DeleteDelay == nil {
s.SunsetStrategy.DeleteDelay = &v1.Duration{Duration: defaults.DeleteDelay}
}
if s.RollbackStrategy == nil {
s.RollbackStrategy = &RollbackStrategy{Strategy: RollbackAllAtOnce}
} else if s.RollbackStrategy.Strategy == "" {
s.RollbackStrategy.Strategy = RollbackAllAtOnce
}
return nil
}
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
func (r *TemporalWorkerDeployment) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
return r.validateForUpdateOrCreate(ctx, obj)
}
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
func (r *TemporalWorkerDeployment) ValidateUpdate(ctx context.Context, oldObj runtime.Object, newObj runtime.Object) (admission.Warnings, error) {
return r.validateForUpdateOrCreate(ctx, newObj)
}
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
func (r *TemporalWorkerDeployment) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
return nil, nil
}
func (r *TemporalWorkerDeployment) validateForUpdateOrCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
dep, ok := obj.(*TemporalWorkerDeployment)
if !ok {
return nil, apierrors.NewBadRequest("expected a TemporalWorkerDeployment")
}
return validateForUpdateOrCreate(nil, dep)
}
func validateForUpdateOrCreate(old, new *TemporalWorkerDeployment) (admission.Warnings, error) {
var allErrs field.ErrorList
if len(new.GetName()) > maxTemporalWorkerDeploymentNameLen {
allErrs = append(allErrs,
field.Invalid(field.NewPath("metadata.name"), new.GetName(), fmt.Sprintf("cannot be more than %d characters", maxTemporalWorkerDeploymentNameLen)),
)
}
allErrs = append(allErrs, validateRolloutStrategy(new.Spec.RolloutStrategy)...)
allErrs = append(allErrs, validateRollbackStrategy(*new.Spec.RollbackStrategy)...)
if len(allErrs) > 0 {
return nil, newInvalidErr(new, allErrs)
}
return nil, nil
}
func validateRolloutStrategy(s RolloutStrategy) []*field.Error {
var allErrs []*field.Error
if s.Strategy == UpdateProgressive {
allErrs = append(allErrs, validateProgressiveStrategySteps("spec.rollout.steps", s.Steps)...)
}
// Validate gate input fields
if s.Gate != nil {
gate := s.Gate
if gate.Input != nil && gate.InputFrom != nil {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec.rollout.gate"), "input & inputFrom",
"only one of input or inputFrom may be set"),
)
}
if gate.InputFrom != nil {
cm := gate.InputFrom.ConfigMapKeyRef
sec := gate.InputFrom.SecretKeyRef
if (cm == nil && sec == nil) || (cm != nil && sec != nil) {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec.rollout.gate.inputFrom"), gate.InputFrom,
"exactly one of configMapKeyRef or secretKeyRef must be set"),
)
}
}
}
return allErrs
}
func validateRollbackStrategy(s RollbackStrategy) []*field.Error {
var allErrs []*field.Error
if s.Strategy == RollbackProgressive {
allErrs = append(allErrs, validateProgressiveStrategySteps("spec.rollback.steps", s.Steps)...)
}
return allErrs
}
func validateProgressiveStrategySteps(specName string, steps []RolloutStep) []*field.Error {
var allErrs []*field.Error
if len(steps) == 0 {
allErrs = append(allErrs,
field.Invalid(field.NewPath(specName), steps, "steps are required for Progressive strategy"),
)
}
var lastRamp int
for i, step := range steps {
// Check duration >= 30s
if step.PauseDuration.Duration < 30*time.Second {
allErrs = append(allErrs,
field.Invalid(field.NewPath(fmt.Sprintf("%s[%d].pauseDuration", specName, i)), step.PauseDuration.Duration.String(), "pause duration must be at least 30s"),
)
}
// Check ramp value greater than last
if step.RampPercentage <= lastRamp {
allErrs = append(allErrs,
field.Invalid(field.NewPath(fmt.Sprintf("%s[%d].rampPercentage", specName, i)), step.RampPercentage, "rampPercentage must increase between each step"),
)
}
lastRamp = step.RampPercentage
}
return allErrs
}
func newInvalidErr(dep *TemporalWorkerDeployment, errs field.ErrorList) *apierrors.StatusError {
return apierrors.NewInvalid(dep.GroupVersionKind().GroupKind(), dep.GetName(), errs)
}