knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/injection/clients/dynamicclient/fake/fake.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 fake 18 19 import ( 20 "context" 21 22 "k8s.io/apimachinery/pkg/runtime" 23 "k8s.io/client-go/dynamic/fake" 24 k8sscheme "k8s.io/client-go/kubernetes/scheme" 25 "k8s.io/client-go/rest" 26 27 "knative.dev/pkg/injection" 28 "knative.dev/pkg/injection/clients/dynamicclient" 29 "knative.dev/pkg/logging" 30 ) 31 32 func init() { 33 injection.Fake.RegisterClient(withClient) 34 } 35 36 func withClient(ctx context.Context, cfg *rest.Config) context.Context { 37 scheme := runtime.NewScheme() 38 k8sscheme.AddToScheme(scheme) 39 ctx, _ = With(ctx, scheme) 40 return ctx 41 } 42 43 func With(ctx context.Context, scheme *runtime.Scheme, objects ...runtime.Object) (context.Context, *fake.FakeDynamicClient) { 44 cs := fake.NewSimpleDynamicClient(scheme, objects...) 45 return context.WithValue(ctx, dynamicclient.Key{}, cs), cs 46 } 47 48 // Get extracts the Kubernetes client from the context. 49 func Get(ctx context.Context) *fake.FakeDynamicClient { 50 untyped := ctx.Value(dynamicclient.Key{}) 51 if untyped == nil { 52 logging.FromContext(ctx).Panicf( 53 "Unable to fetch %T from context.", (*fake.FakeDynamicClient)(nil)) 54 } 55 return untyped.(*fake.FakeDynamicClient) 56 }