github.com/oam-dev/cluster-gateway@v1.9.0/pkg/util/singleton/loopback.go (about) 1 package singleton 2 3 import ( 4 "time" 5 6 "k8s.io/apimachinery/pkg/util/wait" 7 "k8s.io/apiserver/pkg/server" 8 utilfeature "k8s.io/apiserver/pkg/util/feature" 9 corev1informer "k8s.io/client-go/informers/core/v1" 10 "k8s.io/client-go/kubernetes" 11 corev1lister "k8s.io/client-go/listers/core/v1" 12 clientgorest "k8s.io/client-go/rest" 13 "k8s.io/client-go/tools/cache" 14 "k8s.io/klog/v2" 15 ocmclient "open-cluster-management.io/api/client/cluster/clientset/versioned" 16 clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" 17 clusterv1Lister "open-cluster-management.io/api/client/cluster/listers/cluster/v1" 18 "sigs.k8s.io/apiserver-runtime/pkg/util/loopback" 19 "sigs.k8s.io/controller-runtime/pkg/client" 20 controllerruntimeconfig "sigs.k8s.io/controller-runtime/pkg/client/config" 21 22 "github.com/oam-dev/cluster-gateway/pkg/config" 23 "github.com/oam-dev/cluster-gateway/pkg/featuregates" 24 "github.com/oam-dev/cluster-gateway/pkg/util/cert" 25 clusterutil "github.com/oam-dev/cluster-gateway/pkg/util/cluster" 26 "github.com/oam-dev/cluster-gateway/pkg/util/scheme" 27 ) 28 29 var kubeClient kubernetes.Interface 30 var ocmClient ocmclient.Interface 31 var ctrlClient client.Client 32 33 var secretInformer cache.SharedIndexInformer 34 var secretLister corev1lister.SecretLister 35 36 var secretControl cert.SecretControl 37 38 var clusterInformer cache.SharedIndexInformer 39 var clusterLister clusterv1Lister.ManagedClusterLister 40 var clusterControl clusterutil.OCMClusterControl 41 42 func GetSecretControl() cert.SecretControl { 43 return secretControl 44 } 45 46 func GetOCMClient() ocmclient.Interface { 47 return ocmClient 48 } 49 50 func GetKubeClient() kubernetes.Interface { 51 return kubeClient 52 } 53 54 func GetCtrlClient() client.Client { 55 return ctrlClient 56 } 57 58 func SetCtrlClient(cli client.Client) { 59 ctrlClient = cli 60 } 61 62 func InitLoopbackClient(ctx server.PostStartHookContext) error { 63 var err error 64 cfg := loopback.GetLoopbackMasterClientConfig() 65 if cfg == nil { 66 if cfg, err = controllerruntimeconfig.GetConfig(); err != nil { 67 return err 68 } 69 } 70 copiedCfg := clientgorest.CopyConfig(cfg) 71 copiedCfg.RateLimiter = nil 72 kubeClient, err = kubernetes.NewForConfig(copiedCfg) 73 if err != nil { 74 return err 75 } 76 ocmClient, err = ocmclient.NewForConfig(copiedCfg) 77 if err != nil { 78 return err 79 } 80 ctrlClient, err = client.New(copiedCfg, client.Options{Scheme: scheme.Scheme}) 81 if err != nil { 82 return err 83 } 84 if utilfeature.DefaultMutableFeatureGate.Enabled(featuregates.SecretCache) { 85 if err := setInformer(kubeClient, ctx.StopCh); err != nil { 86 return err 87 } 88 secretControl = cert.NewCachedSecretControl(config.SecretNamespace, secretLister) 89 } 90 if secretControl == nil { 91 secretControl = cert.NewDirectApiSecretControl(config.SecretNamespace, kubeClient) 92 } 93 94 if utilfeature.DefaultMutableFeatureGate.Enabled(featuregates.OCMClusterCache) { 95 installed, err := clusterutil.IsOCMManagedClusterInstalled(ocmClient) 96 if err != nil { 97 klog.Error(err) 98 } else if !installed { 99 klog.Infof("OCM ManagedCluster CRD not installed, skip bootstrapping informer for OCM ManagedCluster") 100 } else if err := setOCMClusterInformer(ocmClient, ctx.StopCh); err != nil { 101 return err 102 } 103 clusterControl = clusterutil.NewCacheOCMClusterControl(clusterLister) 104 } 105 if clusterControl == nil { 106 clusterControl = clusterutil.NewDirectOCMClusterControl(ocmClient) 107 } 108 109 return nil 110 } 111 112 func setInformer(k kubernetes.Interface, stopCh <-chan struct{}) error { 113 secretInformer = corev1informer.NewSecretInformer(k, config.SecretNamespace, 0, cache.Indexers{ 114 cache.NamespaceIndex: cache.MetaNamespaceIndexFunc, 115 }) 116 secretLister = corev1lister.NewSecretLister(secretInformer.GetIndexer()) 117 go secretInformer.Run(stopCh) 118 return wait.PollImmediateUntil(time.Second, func() (done bool, err error) { 119 return secretInformer.HasSynced(), nil 120 }, stopCh) 121 } 122 123 // SetSecretControl is for test only 124 func SetSecretControl(ctrl cert.SecretControl) { 125 secretControl = ctrl 126 } 127 128 // SetOCMClient is for test only 129 func SetOCMClient(c ocmclient.Interface) { 130 ocmClient = c 131 } 132 133 // SetKubeClient is for test only 134 func SetKubeClient(k kubernetes.Interface) { 135 kubeClient = k 136 } 137 138 func setOCMClusterInformer(c ocmclient.Interface, stopCh <-chan struct{}) error { 139 ocmClusterInformers := clusterinformers.NewSharedInformerFactory(c, 0) 140 clusterInformer = ocmClusterInformers.Cluster().V1().ManagedClusters().Informer() 141 clusterLister = ocmClusterInformers.Cluster().V1().ManagedClusters().Lister() 142 go clusterInformer.Run(stopCh) 143 return wait.PollImmediateUntil(time.Second, func() (done bool, err error) { 144 return clusterInformer.HasSynced(), nil 145 }, stopCh) 146 } 147 148 func GetClusterControl() clusterutil.OCMClusterControl { 149 return clusterControl 150 }