knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/testing/inner_default_resource.go (about) 1 /* 2 Copyright 2019 The Knative Authors 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 package testing 18 19 import ( 20 "context" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "knative.dev/pkg/apis" 24 ) 25 26 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 27 28 // InnerDefaultResource is a simple resource that's compatible with our webhook. It differs from 29 // Resource by not omitting empty `spec`, so can change when it round trips 30 // JSON -> Golang type -> JSON. 31 type InnerDefaultResource struct { 32 metav1.TypeMeta `json:",inline"` 33 metav1.ObjectMeta `json:"metadata,omitempty"` 34 35 // Note that this does _not_ have omitempty. So when JSON is round tripped through the Golang 36 // type, `spec: {}` will automatically be injected. 37 Spec InnerDefaultSpec `json:"spec"` 38 39 // Status is a simple status. 40 Status InnerDefaultStatus `json:"status,omitempty"` 41 } 42 43 // InnerDefaultSpec is the spec for InnerDefaultResource. 44 type InnerDefaultSpec struct { 45 Generation int64 `json:"generation,omitempty"` 46 47 FieldWithDefault string `json:"fieldWithDefault,omitempty"` 48 49 // Deprecated: This field is deprecated, and will emit an error if set. 50 DeprecatedField string `json:"field,omitempty"` 51 52 // ToBeDeprecatedField: This field is deprecated, and will emit a warning 53 // if set. 54 ToBeDeprecatedField string `json:"fieldWillWarn,omitempty"` 55 56 SubFields *InnerDefaultSubSpec `json:"subfields,omitempty"` 57 } 58 59 // InnerDefaultSubSpec is a helper to test strict deprecated validation. 60 type InnerDefaultSubSpec struct { 61 // Deprecated: This field is deprecated. 62 DeprecatedString string `json:"string,omitempty"` 63 64 // Deprecated: This field is deprecated. 65 DeprecatedStringPtr *string `json:"stringPtr,omitempty"` 66 67 // Deprecated: This field is deprecated. 68 DeprecatedInt int64 `json:"int,omitempty"` 69 70 // Deprecated: This field is deprecated. 71 DeprecatedIntPtr *int64 `json:"intPtr,omitempty"` 72 73 // Deprecated: This field is deprecated. 74 DeprecatedMap map[string]string `json:"map,omitempty"` 75 76 // Deprecated: This field is deprecated. 77 DeprecatedSlice []string `json:"slice,omitempty"` 78 79 // Deprecated: This field is deprecated. 80 DeprecatedStruct InnerDefaultStruct `json:"struct,omitempty"` 81 82 // Deprecated: This field is deprecated. 83 DeprecatedStructPtr *InnerDefaultStruct `json:"structPtr,omitempty"` 84 85 InlinedStruct `json:",inline"` 86 *InlinedPtrStruct `json:",inline"` 87 88 // Deprecated: This field is deprecated. 89 DeprecatedNotJSON string 90 } 91 92 // InnerDefaultStruct is a helper to test defaulting of an inner struct. 93 type InnerDefaultStruct struct { 94 FieldAsString string `json:"fieldAsString,omitempty"` 95 96 // Deprecated: This field is deprecated. 97 DeprecatedField string `json:"field,omitempty"` 98 } 99 100 type InlinedStruct struct { 101 // Deprecated: This field is deprecated. 102 DeprecatedField string `json:"fieldA,omitempty"` 103 *InlinedPtrStruct `json:",inline"` 104 } 105 106 type InlinedPtrStruct struct { 107 // Deprecated: This field is deprecated. 108 DeprecatedField string `json:"fieldB,omitempty"` 109 } 110 111 // InnerDefaultStatus is the status for InnerDefaultResource. 112 type InnerDefaultStatus struct { 113 FieldAsString string `json:"fieldAsString,omitempty"` 114 } 115 116 // Check that ImmutableDefaultResource may be validated and defaulted. 117 var ( 118 _ apis.Validatable = (*InnerDefaultResource)(nil) 119 _ apis.Defaultable = (*InnerDefaultResource)(nil) 120 ) 121 122 // SetDefaults sets default values. 123 func (i *InnerDefaultResource) SetDefaults(ctx context.Context) { 124 i.Spec.SetDefaults(ctx) 125 } 126 127 // SetDefaults sets default values. 128 func (cs *InnerDefaultSpec) SetDefaults(ctx context.Context) { 129 if cs.FieldWithDefault == "" { 130 cs.FieldWithDefault = "I'm a default." 131 } 132 } 133 134 // Validate validates the resource. 135 func (i *InnerDefaultResource) Validate(ctx context.Context) *apis.FieldError { 136 var errs *apis.FieldError 137 if apis.IsInUpdate(ctx) { 138 org := apis.GetBaseline(ctx).(*InnerDefaultResource) 139 errs = apis.CheckDeprecatedUpdate(ctx, i.Spec, org.Spec).ViaField("spec") 140 if i.Spec.SubFields != nil { 141 var orgSubFields interface{} 142 if org != nil && org.Spec.SubFields != nil { 143 orgSubFields = org.Spec.SubFields 144 } 145 146 errs = errs.Also(apis.CheckDeprecatedUpdate(ctx, i.Spec.SubFields, orgSubFields).ViaField("spec", "subFields")) 147 148 var orgDepStruct interface{} 149 if orgSubFields != nil { 150 orgDepStruct = org.Spec.SubFields.DeprecatedStruct 151 } 152 153 errs = errs.Also(apis.CheckDeprecatedUpdate(ctx, i.Spec.SubFields.DeprecatedStruct, orgDepStruct).ViaField("spec", "subFields", "deprecatedStruct")) 154 } 155 } else { 156 errs = apis.CheckDeprecated(ctx, i.Spec).ViaField("spec") 157 if i.Spec.SubFields != nil { 158 errs = errs.Also(apis.CheckDeprecated(ctx, i.Spec.SubFields).ViaField("spec", "subFields"). 159 Also(apis.CheckDeprecated(ctx, i.Spec.SubFields.DeprecatedStruct).ViaField("deprecatedStruct"))) 160 } 161 } 162 163 if i.Spec.ToBeDeprecatedField != "" { 164 errs = errs.Also(apis.ErrDisallowedFields("fieldWillWarn").At(apis.WarningLevel).ViaField("spec")) 165 } 166 return errs 167 }