k8s.io/kubernetes@v1.29.3/pkg/registry/core/secret/strategy.go (about) 1 /* 2 Copyright 2015 The Kubernetes 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 secret 18 19 import ( 20 "context" 21 "crypto/tls" 22 "fmt" 23 24 "k8s.io/apimachinery/pkg/fields" 25 "k8s.io/apimachinery/pkg/labels" 26 "k8s.io/apimachinery/pkg/runtime" 27 "k8s.io/apimachinery/pkg/util/validation/field" 28 "k8s.io/apiserver/pkg/registry/generic" 29 "k8s.io/apiserver/pkg/registry/rest" 30 pkgstorage "k8s.io/apiserver/pkg/storage" 31 "k8s.io/apiserver/pkg/storage/names" 32 "k8s.io/kubernetes/pkg/api/legacyscheme" 33 api "k8s.io/kubernetes/pkg/apis/core" 34 "k8s.io/kubernetes/pkg/apis/core/validation" 35 ) 36 37 // strategy implements behavior for Secret objects 38 type strategy struct { 39 runtime.ObjectTyper 40 names.NameGenerator 41 } 42 43 // Strategy is the default logic that applies when creating and updating Secret 44 // objects via the REST API. 45 var Strategy = strategy{legacyscheme.Scheme, names.SimpleNameGenerator} 46 47 var _ = rest.RESTCreateStrategy(Strategy) 48 49 var _ = rest.RESTUpdateStrategy(Strategy) 50 51 func (strategy) NamespaceScoped() bool { 52 return true 53 } 54 55 func (strategy) PrepareForCreate(ctx context.Context, obj runtime.Object) { 56 secret := obj.(*api.Secret) 57 dropDisabledFields(secret, nil) 58 } 59 60 func (strategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList { 61 return validation.ValidateSecret(obj.(*api.Secret)) 62 } 63 64 // WarningsOnCreate returns warnings for the creation of the given object. 65 func (strategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { 66 return warningsForSecret(obj.(*api.Secret)) 67 } 68 69 func (strategy) Canonicalize(obj runtime.Object) { 70 } 71 72 func (strategy) AllowCreateOnUpdate() bool { 73 return false 74 } 75 76 func (strategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { 77 newSecret := obj.(*api.Secret) 78 oldSecret := old.(*api.Secret) 79 80 // this is weird, but consistent with what the validatedUpdate function used to do. 81 if len(newSecret.Type) == 0 { 82 newSecret.Type = oldSecret.Type 83 } 84 85 dropDisabledFields(newSecret, oldSecret) 86 } 87 88 func (strategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { 89 return validation.ValidateSecretUpdate(obj.(*api.Secret), old.(*api.Secret)) 90 } 91 92 // WarningsOnUpdate returns warnings for the given update. 93 func (strategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { 94 return warningsForSecret(obj.(*api.Secret)) 95 } 96 97 func dropDisabledFields(secret *api.Secret, oldSecret *api.Secret) { 98 } 99 100 func (strategy) AllowUnconditionalUpdate() bool { 101 return true 102 } 103 104 // GetAttrs returns labels and fields of a given object for filtering purposes. 105 func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) { 106 secret, ok := obj.(*api.Secret) 107 if !ok { 108 return nil, nil, fmt.Errorf("not a secret") 109 } 110 return labels.Set(secret.Labels), SelectableFields(secret), nil 111 } 112 113 // Matcher returns a selection predicate for a given label and field selector. 114 func Matcher(label labels.Selector, field fields.Selector) pkgstorage.SelectionPredicate { 115 return pkgstorage.SelectionPredicate{ 116 Label: label, 117 Field: field, 118 GetAttrs: GetAttrs, 119 } 120 } 121 122 // SelectableFields returns a field set that can be used for filter selection 123 func SelectableFields(obj *api.Secret) fields.Set { 124 objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&obj.ObjectMeta, true) 125 secretSpecificFieldsSet := fields.Set{ 126 "type": string(obj.Type), 127 } 128 return generic.MergeFieldsSets(objectMetaFieldsSet, secretSpecificFieldsSet) 129 } 130 131 func warningsForSecret(secret *api.Secret) []string { 132 var warnings []string 133 if secret.Type == api.SecretTypeTLS { 134 // Verify that the key matches the cert. 135 _, err := tls.X509KeyPair(secret.Data[api.TLSCertKey], secret.Data[api.TLSPrivateKeyKey]) 136 if err != nil { 137 warnings = append(warnings, err.Error()) 138 } 139 } 140 return warnings 141 }