Skip to content

Commit 5743052

Browse files
authored
[BUGFIX] Fixed local set up issue for cluster config (#1402)
merging
1 parent fbb484d commit 5743052

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

client/k8s/informer/K8sInformerFactory.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package informer
22

33
import (
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

1521
func 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

2734
type 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

6068
func (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,

internal/util/K8sUtil_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import (
2121
"testing"
2222
)
2323

24-
var client *K8sUtil
24+
var k8sUtilClient *K8sUtil
2525
var clusterConfig *ClusterConfig
2626

2727
func init() {
28-
client = NewK8sUtil(NewSugardLogger())
28+
k8sUtilClient = NewK8sUtil(NewSugardLogger())
2929
clusterConfig = &ClusterConfig{
3030
Host: "",
3131
BearerToken: "",
@@ -53,7 +53,7 @@ func TestK8sUtil_checkIfNsExists(t *testing.T) {
5353
}
5454
for _, tt := range tests {
5555
t.Run(tt.name, func(t *testing.T) {
56-
impl := client
56+
impl := k8sUtilClient
5757
k8s, _ := impl.GetClient(clusterConfig)
5858
gotExists, err := impl.checkIfNsExists(tt.namespace, k8s)
5959
if (err != nil) != tt.wantErr {
@@ -81,7 +81,7 @@ func TestK8sUtil_CreateNsIfNotExists(t *testing.T) {
8181
}
8282
for _, tt := range tests {
8383
t.Run(tt.name, func(t *testing.T) {
84-
impl := client
84+
impl := k8sUtilClient
8585
if err := impl.CreateNsIfNotExists(tt.namespace, clusterConfig); (err != nil) != tt.wantErr {
8686
t.Errorf("K8sUtil.CreateNsIfNotExists() error = %v, wantErr %v", err, tt.wantErr)
8787
}

wire_gen.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)