/
configuration.go
171 lines (151 loc) · 5.81 KB
/
configuration.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
/*
Copyright 2017 Google Inc. 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 webhook
import (
"context"
"errors"
"fmt"
"path"
"reflect"
"strings"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
"github.com/knative/serving/pkg/logging"
"github.com/mattbaird/jsonpatch"
corev1 "k8s.io/api/core/v1"
)
func errMissingField(fieldPath string) error {
return fmt.Errorf("Configuration is missing %q", fieldPath)
}
func errDisallowedFields(fieldPaths string) error {
return fmt.Errorf("The configuration spec must not set the field(s): %s", fieldPaths)
}
var (
errEmptySpecInConfiguration = errMissingField("spec")
errEmptyRevisionTemplateInSpec = errMissingField("spec.revisionTemplate")
errEmptyContainerInRevisionTemplate = errMissingField("spec.revisionTemplate.spec.container")
errInvalidConfigurationInput = errors.New("failed to convert input into Configuration")
)
// ValidateConfiguration is Configuration resource specific validation and mutation handler
func ValidateConfiguration(ctx context.Context) ResourceCallback {
return func(patches *[]jsonpatch.JsonPatchOperation, old GenericCRD, new GenericCRD) error {
_, newConfiguration, err := unmarshalConfigurations(ctx, old, new, "ValidateConfiguration")
if err != nil {
return err
}
if err := validateConfiguration(newConfiguration); err != nil {
return err
}
return nil
}
}
func validateConfiguration(configuration *v1alpha1.Configuration) error {
if reflect.DeepEqual(configuration.Spec, v1alpha1.ConfigurationSpec{}) {
return errEmptySpecInConfiguration
}
if err := validateConfigurationSpec(&configuration.Spec); err != nil {
return err
}
return validateConcurrencyModel(configuration.Spec.RevisionTemplate.Spec.ConcurrencyModel)
}
func validateConfigurationSpec(configurationSpec *v1alpha1.ConfigurationSpec) error {
if err := validateTemplate(&configurationSpec.RevisionTemplate); err != nil {
return err
}
return nil
}
func validateTemplate(template *v1alpha1.RevisionTemplateSpec) error {
if reflect.DeepEqual(*template, v1alpha1.RevisionTemplateSpec{}) {
return errEmptyRevisionTemplateInSpec
}
if template.Spec.ServingState != "" {
return errDisallowedFields("revisionTemplate.spec.servingState")
}
if err := validateContainer(template.Spec.Container); err != nil {
return err
}
return nil
}
func validateConcurrencyModel(value v1alpha1.RevisionRequestConcurrencyModelType) error {
switch value {
case v1alpha1.RevisionRequestConcurrencyModelType(""), v1alpha1.RevisionRequestConcurrencyModelMulti, v1alpha1.RevisionRequestConcurrencyModelSingle:
return nil
default:
return fmt.Errorf("Unrecognized value for concurrencyModel: %q", value)
}
}
func validateContainer(container corev1.Container) error {
if reflect.DeepEqual(container, corev1.Container{}) {
return errEmptyContainerInRevisionTemplate
}
// Some corev1.Container fields are set by Knative Serving controller. We disallow them
// here to avoid silently overwriting these fields and causing confusions for
// the users. See pkg/controller/revision.MakeElaPodSpec for the list of fields
// overridden.
var ignoredFields []string
if container.Name != "" {
ignoredFields = append(ignoredFields, "revisionTemplate.spec.container.name")
}
if !reflect.DeepEqual(container.Resources, corev1.ResourceRequirements{}) {
ignoredFields = append(ignoredFields, "revisionTemplate.spec.container.resources")
}
if len(container.Ports) > 0 {
ignoredFields = append(ignoredFields, "revisionTemplate.spec.container.ports")
}
if len(container.VolumeMounts) > 0 {
ignoredFields = append(ignoredFields, "revisionTemplate.spec.container.volumeMounts")
}
if len(ignoredFields) > 0 {
// Complain about all ignored fields so that user can remove them all at once.
return errDisallowedFields(strings.Join(ignoredFields, ", "))
}
return nil
}
// SetConfigurationDefaults set defaults on an configurations.
func SetConfigurationDefaults(ctx context.Context) ResourceDefaulter {
return func(patches *[]jsonpatch.JsonPatchOperation, crd GenericCRD) error {
_, config, err := unmarshalConfigurations(ctx, nil, crd, "SetConfigurationDefaults")
if err != nil {
return err
}
return setConfigurationSpecDefaults(patches, "/spec", config.Spec)
}
}
func setConfigurationSpecDefaults(patches *[]jsonpatch.JsonPatchOperation, patchBase string, spec v1alpha1.ConfigurationSpec) error {
if spec.RevisionTemplate.Spec.ConcurrencyModel == "" {
*patches = append(*patches, jsonpatch.JsonPatchOperation{
Operation: "add",
Path: path.Join(patchBase, "revisionTemplate/spec/concurrencyModel"),
Value: v1alpha1.RevisionRequestConcurrencyModelMulti,
})
}
return nil
}
func unmarshalConfigurations(
ctx context.Context, old GenericCRD, new GenericCRD, fnName string) (*v1alpha1.Configuration, *v1alpha1.Configuration, error) {
logger := logging.FromContext(ctx)
var oldConfiguration *v1alpha1.Configuration
if old != nil {
var ok bool
oldConfiguration, ok = old.(*v1alpha1.Configuration)
if !ok {
return nil, nil, errInvalidConfigurationInput
}
}
logger.Infof("%s: OLD Configuration is\n%+v", fnName, oldConfiguration)
newConfiguration, ok := new.(*v1alpha1.Configuration)
if !ok {
return nil, nil, errInvalidConfigurationInput
}
logger.Infof("%s: NEW Configuration is\n%+v", fnName, newConfiguration)
return oldConfiguration, newConfiguration, nil
}