knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1/podspec_defaults.go (about) 1 /* 2 Copyright 2021 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 v1 18 19 import ( 20 "context" 21 ) 22 23 // PodSpecDefaulter is a callback to validate a PodSpecable. 24 type PodSpecDefaulter func(context.Context, *WithPod) 25 26 // SetDefaults implements apis.Defaultable 27 func (wp *WithPod) SetDefaults(ctx context.Context) { 28 if psd := GetPodSpecDefaulter(ctx); psd != nil { 29 psd(ctx, wp) 30 } 31 } 32 33 // psdKey is used for associating a PodSpecDefaulter with a context.Context 34 type psdKey struct{} 35 36 func WithPodSpecDefaulter(ctx context.Context, psd PodSpecDefaulter) context.Context { 37 return context.WithValue(ctx, psdKey{}, psd) 38 } 39 40 // GetPodSpecDefaulter extracts the PodSpecDefaulter from the context. 41 func GetPodSpecDefaulter(ctx context.Context) PodSpecDefaulter { 42 untyped := ctx.Value(psdKey{}) 43 if untyped == nil { 44 return nil 45 } 46 return untyped.(PodSpecDefaulter) 47 } 48 49 // PodDefaulter is a callback to validate a Pod. 50 type PodDefaulter func(context.Context, *Pod) 51 52 // SetDefaults implements apis.Defaultable 53 func (p *Pod) SetDefaults(ctx context.Context) { 54 if pd := GetPodDefaulter(ctx); pd != nil { 55 pd(ctx, p) 56 } 57 } 58 59 // pdKey is used for associating a PodDefaulter with a context.Context 60 type pdKey struct{} 61 62 func WithPodDefaulter(ctx context.Context, pd PodDefaulter) context.Context { 63 return context.WithValue(ctx, pdKey{}, pd) 64 } 65 66 // GetPodDefaulter extracts the PodDefaulter from the context. 67 func GetPodDefaulter(ctx context.Context) PodDefaulter { 68 untyped := ctx.Value(pdKey{}) 69 if untyped == nil { 70 return nil 71 } 72 return untyped.(PodDefaulter) 73 }