github.com/cilium/cilium@v1.16.2/operator/k8s/resource_ctors.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package k8s 5 6 import ( 7 "errors" 8 "fmt" 9 "strconv" 10 11 "github.com/cilium/hive/cell" 12 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 13 "k8s.io/client-go/tools/cache" 14 15 cilium_api_v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2" 16 cilium_api_v2alpha1 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2alpha1" 17 "github.com/cilium/cilium/pkg/k8s/client" 18 "github.com/cilium/cilium/pkg/k8s/resource" 19 slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/core/v1" 20 "github.com/cilium/cilium/pkg/k8s/utils" 21 ) 22 23 func CiliumEndpointResource(lc cell.Lifecycle, cs client.Clientset, opts ...func(*metav1.ListOptions)) (resource.Resource[*cilium_api_v2.CiliumEndpoint], error) { 24 if !cs.IsEnabled() { 25 return nil, nil 26 } 27 lw := utils.ListerWatcherWithModifiers( 28 utils.ListerWatcherFromTyped[*cilium_api_v2.CiliumEndpointList](cs.CiliumV2().CiliumEndpoints("")), 29 opts..., 30 ) 31 indexers := cache.Indexers{ 32 cache.NamespaceIndex: cache.MetaNamespaceIndexFunc, 33 CiliumEndpointIndexIdentity: identityIndexFunc, 34 } 35 return resource.New[*cilium_api_v2.CiliumEndpoint]( 36 lc, lw, resource.WithMetric("CiliumEndpoint"), resource.WithIndexers(indexers)), nil 37 } 38 39 func identityIndexFunc(obj interface{}) ([]string, error) { 40 switch t := obj.(type) { 41 case *cilium_api_v2.CiliumEndpoint: 42 if t.Status.Identity != nil { 43 id := strconv.FormatInt(t.Status.Identity.ID, 10) 44 return []string{id}, nil 45 } 46 return []string{"0"}, nil 47 } 48 return nil, fmt.Errorf("%w - found %T", errors.New("object is not a *cilium_api_v2.CiliumEndpoint"), obj) 49 } 50 51 func CiliumEndpointSliceResource(lc cell.Lifecycle, cs client.Clientset, opts ...func(*metav1.ListOptions)) (resource.Resource[*cilium_api_v2alpha1.CiliumEndpointSlice], error) { 52 if !cs.IsEnabled() { 53 return nil, nil 54 } 55 lw := utils.ListerWatcherWithModifiers( 56 utils.ListerWatcherFromTyped[*cilium_api_v2alpha1.CiliumEndpointSliceList](cs.CiliumV2alpha1().CiliumEndpointSlices()), 57 opts..., 58 ) 59 return resource.New[*cilium_api_v2alpha1.CiliumEndpointSlice](lc, lw, resource.WithMetric("CiliumEndpointSlice")), nil 60 } 61 62 func CiliumNodeResource(lc cell.Lifecycle, cs client.Clientset, opts ...func(*metav1.ListOptions)) (resource.Resource[*cilium_api_v2.CiliumNode], error) { 63 if !cs.IsEnabled() { 64 return nil, nil 65 } 66 lw := utils.ListerWatcherWithModifiers( 67 utils.ListerWatcherFromTyped[*cilium_api_v2.CiliumNodeList](cs.CiliumV2().CiliumNodes()), 68 opts..., 69 ) 70 return resource.New[*cilium_api_v2.CiliumNode](lc, lw, 71 resource.WithMetric("CiliumNode"), 72 ), nil 73 } 74 75 func CiliumBGPClusterConfigResource(lc cell.Lifecycle, cs client.Clientset, opts ...func(*metav1.ListOptions)) (resource.Resource[*cilium_api_v2alpha1.CiliumBGPClusterConfig], error) { 76 if !cs.IsEnabled() { 77 return nil, nil 78 } 79 80 lw := utils.ListerWatcherWithModifiers( 81 utils.ListerWatcherFromTyped[*cilium_api_v2alpha1.CiliumBGPClusterConfigList](cs.CiliumV2alpha1().CiliumBGPClusterConfigs()), 82 opts..., 83 ) 84 return resource.New[*cilium_api_v2alpha1.CiliumBGPClusterConfig](lc, lw, resource.WithMetric("CiliumBGPClusterConfig")), nil 85 } 86 87 func CiliumBGPNodeConfigOverrideResource(lc cell.Lifecycle, cs client.Clientset, opts ...func(*metav1.ListOptions)) (resource.Resource[*cilium_api_v2alpha1.CiliumBGPNodeConfigOverride], error) { 88 if !cs.IsEnabled() { 89 return nil, nil 90 } 91 92 lw := utils.ListerWatcherWithModifiers( 93 utils.ListerWatcherFromTyped[*cilium_api_v2alpha1.CiliumBGPNodeConfigOverrideList](cs.CiliumV2alpha1().CiliumBGPNodeConfigOverrides()), 94 opts..., 95 ) 96 return resource.New[*cilium_api_v2alpha1.CiliumBGPNodeConfigOverride](lc, lw, resource.WithMetric("CiliumBGPNodeConfigOverride")), nil 97 } 98 99 func PodResource(lc cell.Lifecycle, cs client.Clientset, opts ...func(*metav1.ListOptions)) (resource.Resource[*slim_corev1.Pod], error) { 100 if !cs.IsEnabled() { 101 return nil, nil 102 } 103 lw := utils.ListerWatcherWithModifiers( 104 utils.ListerWatcherFromTyped[*slim_corev1.PodList](cs.Slim().CoreV1().Pods("")), 105 opts..., 106 ) 107 108 // The index will be used only by Operator Managing CIDs to reconcile NS labels changes 109 indexers := cache.Indexers{ 110 cache.NamespaceIndex: cache.MetaNamespaceIndexFunc, 111 } 112 113 return resource.New[*slim_corev1.Pod](lc, lw, 114 resource.WithMetric("Pod"), 115 resource.WithIndexers(indexers), 116 ), 117 nil 118 }