11package informer
22
33import (
4+ "flag"
5+ "os/user"
6+ "path/filepath"
7+ "sync"
8+ "time"
9+
10+ "github.com/devtron-labs/authenticator/client"
411 "github.com/devtron-labs/devtron/api/bean"
512 "go.uber.org/zap"
613 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
714 kubeinformers "k8s.io/client-go/informers"
815 "k8s.io/client-go/kubernetes"
916 "k8s.io/client-go/rest"
1017 "k8s.io/client-go/tools/cache"
11- "sync"
12- "time"
18+ "k8s.io/client-go/tools/clientcmd"
1319)
1420
1521func NewGlobalMapClusterNamespace () map [string ]map [string ]bool {
@@ -22,6 +28,7 @@ type K8sInformerFactoryImpl struct {
2228 globalMapClusterNamespace map [string ]map [string ]bool // {"cluster1":{"ns1":true","ns2":true"}}
2329 mutex sync.Mutex
2430 informerStopper map [string ]chan struct {}
31+ runtimeConfig * client.RuntimeConfig
2532}
2633
2734type K8sInformerFactory interface {
@@ -30,10 +37,11 @@ type K8sInformerFactory interface {
3037 CleanNamespaceInformer (clusterName string )
3138}
3239
33- func NewK8sInformerFactoryImpl (logger * zap.SugaredLogger , globalMapClusterNamespace map [string ]map [string ]bool ) * K8sInformerFactoryImpl {
40+ func NewK8sInformerFactoryImpl (logger * zap.SugaredLogger , globalMapClusterNamespace map [string ]map [string ]bool , runtimeConfig * client. RuntimeConfig ) * K8sInformerFactoryImpl {
3441 informerFactory := & K8sInformerFactoryImpl {
3542 logger : logger ,
3643 globalMapClusterNamespace : globalMapClusterNamespace ,
44+ runtimeConfig : runtimeConfig ,
3745 }
3846 informerFactory .informerStopper = make (map [string ]chan struct {})
3947 return informerFactory
@@ -58,14 +66,29 @@ func (impl *K8sInformerFactoryImpl) GetLatestNamespaceListGroupByCLuster() map[s
5866}
5967
6068func (impl * K8sInformerFactoryImpl ) BuildInformer (clusterInfo []* bean.ClusterInfo ) {
69+ var restConfig * rest.Config
6170 for _ , info := range clusterInfo {
6271 if info .ClusterName == "default_cluster" {
63- c , err := rest .InClusterConfig ()
64- if err != nil {
65- impl .logger .Errorw ("error in fetch default cluster config" , "err" , err )
66- continue
72+ if impl .runtimeConfig .LocalDevMode {
73+ usr , err := user .Current ()
74+ if err != nil {
75+ impl .logger .Errorw ("Error while getting user current env details" , "error" , err )
76+ }
77+ kubeconfig := flag .String ("build-informer" , filepath .Join (usr .HomeDir , ".kube" , "config" ), "(optional) absolute path to the kubeconfig file" )
78+ flag .Parse ()
79+ restConfig , err = clientcmd .BuildConfigFromFlags ("" , * kubeconfig )
80+ if err != nil {
81+ impl .logger .Errorw ("Error while building config from flags" , "error" , err )
82+ }
83+ } else {
84+ restConfig , err := rest .InClusterConfig ()
85+ if err != nil {
86+ impl .logger .Errorw ("error in fetch default cluster config" , "err" , err , "servername" , restConfig .ServerName )
87+ continue
88+ }
6789 }
68- impl .buildInformerAndNamespaceList (info .ClusterName , c , & impl .mutex )
90+
91+ impl .buildInformerAndNamespaceList (info .ClusterName , restConfig , & impl .mutex )
6992 } else {
7093 c := & rest.Config {
7194 Host : info .ServerUrl ,
0 commit comments