forked from knative/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinner_default_resource.go
157 lines (121 loc) · 4.93 KB
/
inner_default_resource.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
/*
Copyright 2019 The Knative Authors
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 testing
import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// InnerDefaultResource is a simple resource that's compatible with our webhook. It differs from
// Resource by not omitting empty `spec`, so can change when it round trips
// JSON -> Golang type -> JSON.
type InnerDefaultResource struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
// Note that this does _not_ have omitempty. So when JSON is round tripped through the Golang
// type, `spec: {}` will automatically be injected.
Spec InnerDefaultSpec `json:"spec"`
// Status is a simple status.
Status InnerDefaultStatus `json:"status,omitempty"`
}
// InnerDefaultSpec is the spec for InnerDefaultResource.
type InnerDefaultSpec struct {
Generation int64 `json:"generation,omitempty"`
FieldWithDefault string `json:"fieldWithDefault,omitempty"`
// Deprecated: This field is deprecated.
DeprecatedField string `json:"field,omitempty"`
SubFields *InnerDefaultSubSpec `json:"subfields,omitempty"`
}
// InnerDefaultSubSpec is a helper to test strict deprecated validation.
type InnerDefaultSubSpec struct {
// Deprecated: This field is deprecated.
DeprecatedString string `json:"string,omitempty"`
// Deprecated: This field is deprecated.
DeprecatedStringPtr *string `json:"stringPtr,omitempty"`
// Deprecated: This field is deprecated.
DeprecatedInt int64 `json:"int,omitempty"`
// Deprecated: This field is deprecated.
DeprecatedIntPtr *int64 `json:"intPtr,omitempty"`
// Deprecated: This field is deprecated.
DeprecatedMap map[string]string `json:"map,omitempty"`
// Deprecated: This field is deprecated.
DeprecatedSlice []string `json:"slice,omitempty"`
// Deprecated: This field is deprecated.
DeprecatedStruct InnerDefaultStruct `json:"struct,omitempty"`
// Deprecated: This field is deprecated.
DeprecatedStructPtr *InnerDefaultStruct `json:"structPtr,omitempty"`
InlinedStruct `json:",inline"`
*InlinedPtrStruct `json:",inline"`
// Deprecated: This field is deprecated.
DeprecatedNotJson string
}
// Adding complication helper.
type InnerDefaultStruct struct {
FieldAsString string `json:"fieldAsString,omitempty"`
// Deprecated: This field is deprecated.
DeprecatedField string `json:"field,omitempty"`
}
type InlinedStruct struct {
// Deprecated: This field is deprecated.
DeprecatedField string `json:"fieldA,omitempty"`
*InlinedPtrStruct `json:",inline"`
}
type InlinedPtrStruct struct {
// Deprecated: This field is deprecated.
DeprecatedField string `json:"fieldB,omitempty"`
}
// InnerDefaultStatus is the status for InnerDefaultResource.
type InnerDefaultStatus struct {
FieldAsString string `json:"fieldAsString,omitempty"`
}
// Check that ImmutableDefaultResource may be validated and defaulted.
var _ apis.Validatable = (*InnerDefaultResource)(nil)
var _ apis.Defaultable = (*InnerDefaultResource)(nil)
// SetDefaults sets default values.
func (i *InnerDefaultResource) SetDefaults(ctx context.Context) {
i.Spec.SetDefaults(ctx)
}
// SetDefaults sets default values.
func (cs *InnerDefaultSpec) SetDefaults(ctx context.Context) {
if cs.FieldWithDefault == "" {
cs.FieldWithDefault = "I'm a default."
}
}
// Validate validates the resource.
func (i *InnerDefaultResource) Validate(ctx context.Context) *apis.FieldError {
var errs *apis.FieldError
if apis.IsInUpdate(ctx) {
org := apis.GetBaseline(ctx).(*InnerDefaultResource)
errs = apis.CheckDeprecatedUpdate(ctx, i.Spec, org.Spec).ViaField("spec")
if i.Spec.SubFields != nil {
var orgSubFields interface{}
if org != nil && org.Spec.SubFields != nil {
orgSubFields = org.Spec.SubFields
}
errs = errs.Also(apis.CheckDeprecatedUpdate(ctx, i.Spec.SubFields, orgSubFields).ViaField("spec", "subFields"))
var orgDepStruct interface{}
if orgSubFields != nil {
orgDepStruct = org.Spec.SubFields.DeprecatedStruct
}
errs = errs.Also(apis.CheckDeprecatedUpdate(ctx, i.Spec.SubFields.DeprecatedStruct, orgDepStruct).ViaField("spec", "subFields", "deprecatedStruct"))
}
} else {
errs = apis.CheckDeprecated(ctx, i.Spec).ViaField("spec")
if i.Spec.SubFields != nil {
errs = errs.Also(apis.CheckDeprecated(ctx, i.Spec.SubFields).ViaField("spec", "subFields").
Also(apis.CheckDeprecated(ctx, i.Spec.SubFields.DeprecatedStruct).ViaField("deprecatedStruct")))
}
}
return errs
}