github.com/cilium/cilium@v1.16.2/pkg/k8s/client/getters.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package client 5 6 import ( 7 "context" 8 "fmt" 9 10 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 11 12 cilium_v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2" 13 slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/core/v1" 14 ) 15 16 // Getters is a set of methods for retrieving common objects. 17 type Getters interface { 18 GetSecrets(ctx context.Context, namespace, name string) (map[string][]byte, error) 19 GetK8sNode(ctx context.Context, nodeName string) (*slim_corev1.Node, error) 20 GetCiliumNode(ctx context.Context, nodeName string) (*cilium_v2.CiliumNode, error) 21 } 22 23 // clientsetGetters implements the Getters interface in terms of the clientset. 24 type clientsetGetters struct { 25 Clientset 26 } 27 28 // GetSecrets returns the secrets found in the given namespace and name. 29 func (cs *clientsetGetters) GetSecrets(ctx context.Context, ns, name string) (map[string][]byte, error) { 30 if !cs.IsEnabled() { 31 return nil, fmt.Errorf("GetSecrets: No k8s, cannot access k8s secrets") 32 } 33 34 result, err := cs.CoreV1().Secrets(ns).Get(ctx, name, metav1.GetOptions{}) 35 if err != nil { 36 return nil, err 37 } 38 return result.Data, nil 39 } 40 41 // GetK8sNode returns the node with the given nodeName. 42 func (cs *clientsetGetters) GetK8sNode(ctx context.Context, nodeName string) (*slim_corev1.Node, error) { 43 if !cs.IsEnabled() { 44 return nil, fmt.Errorf("GetK8sNode: No k8s, cannot access k8s nodes") 45 } 46 47 return cs.Slim().CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{}) 48 } 49 50 // GetCiliumNode returns the CiliumNode with the given nodeName. 51 func (cs *clientsetGetters) GetCiliumNode(ctx context.Context, nodeName string) (*cilium_v2.CiliumNode, error) { 52 if !cs.IsEnabled() { 53 return nil, fmt.Errorf("GetK8sNode: No k8s, cannot access k8s nodes") 54 } 55 56 return cs.CiliumV2().CiliumNodes().Get(ctx, nodeName, metav1.GetOptions{}) 57 }