-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathwebhook_common.go
181 lines (154 loc) · 5.52 KB
/
webhook_common.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
179
180
181
// Copyright 2020 Intel Corporation. All Rights Reserved.
//
// 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 v1
import (
"context"
"errors"
"fmt"
"path/filepath"
"regexp"
"strings"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/version"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
const sha256RE = "@sha256:[0-9a-f]{64}$"
var errObjType = errors.New("invalid object")
var errValidation = errors.New("invalid resource")
// common functions for webhooks
type commonDevicePluginDefaulter struct {
defaultImage string
}
var _ admission.CustomDefaulter = &commonDevicePluginDefaulter{}
type commonDevicePluginValidator struct {
expectedImage string
expectedInitImage string
expectedVersion version.Version
}
var _ admission.CustomValidator = &commonDevicePluginValidator{}
// Default implements admission.CustomDefaulter so a webhook will be registered for the type.
func (r *commonDevicePluginDefaulter) Default(ctx context.Context, obj runtime.Object) error {
logf.FromContext(ctx).Info("default")
// type switches can have only one type in a case so the same repeats for
// all xDevicePlugin types.
// TODO: implement receivers if more complex logic is needed.
switch v := obj.(type) {
case *DlbDevicePlugin:
if len(v.Spec.Image) == 0 {
v.Spec.Image = r.defaultImage
}
case *DsaDevicePlugin:
if len(v.Spec.Image) == 0 {
v.Spec.Image = r.defaultImage
}
case *FpgaDevicePlugin:
if len(v.Spec.Image) == 0 {
v.Spec.Image = r.defaultImage
}
case *GpuDevicePlugin:
if len(v.Spec.Image) == 0 {
v.Spec.Image = r.defaultImage
}
case *IaaDevicePlugin:
if len(v.Spec.Image) == 0 {
v.Spec.Image = r.defaultImage
}
case *QatDevicePlugin:
if len(v.Spec.Image) == 0 {
v.Spec.Image = r.defaultImage
}
case *SgxDevicePlugin:
if len(v.Spec.Image) == 0 {
v.Spec.Image = r.defaultImage
}
default:
return fmt.Errorf("%w: expected an xDevicePlugin object but got %T", errObjType, obj)
}
return nil
}
// ValidateCreate implements admission.CustomValidator so a webhook will be registered for the type.
func (r *commonDevicePluginValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
logf.FromContext(ctx).Info("validate create")
switch v := obj.(type) {
case *DlbDevicePlugin:
return nil, v.validatePlugin(r)
case *DsaDevicePlugin:
return nil, v.validatePlugin(r)
case *GpuDevicePlugin:
return nil, v.validatePlugin(ctx, r)
case *FpgaDevicePlugin:
return nil, v.validatePlugin(r)
case *IaaDevicePlugin:
return nil, v.validatePlugin(r)
case *QatDevicePlugin:
return nil, v.validatePlugin(r)
case *SgxDevicePlugin:
return nil, v.validatePlugin(r)
default:
return nil, fmt.Errorf("%w: expected an xDevicePlugin object but got %T", errObjType, obj)
}
}
// ValidateUpdate implements admission.CustomValidator so a webhook will be registered for the type.
func (r *commonDevicePluginValidator) ValidateUpdate(ctx context.Context, oldObj runtime.Object, newObj runtime.Object) (admission.Warnings, error) {
logf.FromContext(ctx).Info("validate update")
switch v := oldObj.(type) {
case *DlbDevicePlugin:
return nil, v.validatePlugin(r)
case *DsaDevicePlugin:
return nil, v.validatePlugin(r)
case *GpuDevicePlugin:
return nil, v.validatePlugin(ctx, r)
case *FpgaDevicePlugin:
return nil, v.validatePlugin(r)
case *IaaDevicePlugin:
return nil, v.validatePlugin(r)
case *QatDevicePlugin:
return nil, v.validatePlugin(r)
case *SgxDevicePlugin:
return nil, v.validatePlugin(r)
default:
return nil, fmt.Errorf("%w: expected an xDevicePlugin object but got %T", errObjType, oldObj)
}
}
// ValidateDelete implements admission.CustomValidator so a webhook will be registered for the type.
func (r *commonDevicePluginValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
logf.FromContext(ctx).Info("validate delete")
return nil, nil
}
func validatePluginImage(image, expectedImageName string, expectedMinVersion *version.Version) error {
imageRe := regexp.MustCompile(expectedImageName + sha256RE)
if imageRe.MatchString(image) {
return nil
}
// Ignore registry, vendor and extract the image name with the tag
parts := strings.SplitN(filepath.Base(image), ":", 2)
if len(parts) != 2 {
return fmt.Errorf("%w: incorrect image field %q", errValidation, image)
}
imageName := parts[0]
versionStr := parts[1]
// If user provided faulty SHA digest, the image name may include @sha256 suffix so strip it.
if strings.TrimSuffix(imageName, "@sha256") != expectedImageName {
return fmt.Errorf("%w: incorrect image name %q. Make sure you use '<vendor>/%s'", errValidation, imageName, expectedImageName)
}
ver, err := version.ParseSemantic(versionStr)
if err != nil {
return fmt.Errorf("%w: %w: Make sure it's either valid SHA digest or semver tag", errValidation, err)
}
if !ver.AtLeast(expectedMinVersion) {
return fmt.Errorf("%w: version %q is too low. Should be at least %q", errValidation, ver, expectedMinVersion)
}
return nil
}