Skip to content

Commit 7a825c1

Browse files
author
tazhate
committed
fix(dashboard): resolve --kubeconfig flag conflict with controller-runtime
Context: discovered the binary panicked on startup with "flag redefined: kubeconfig" when testing locally. controller-runtime/pkg/client/config registers --kubeconfig in its init() function; our manual flag.String("kubeconfig") caused a duplicate. Fixed by removing the manual flag and using ctrlcfg.GetConfig() instead, which already reads the controller-runtime-registered flag.
1 parent 947dc2c commit 7a825c1

1 file changed

Lines changed: 9 additions & 25 deletions

File tree

cmd/dashboard/main.go

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
// --port int HTTP listen port (default 8090)
88
// --namespace string Namespace to watch; empty = all namespaces
9-
// --kubeconfig string Path to kubeconfig; empty = in-cluster
9+
// --kubeconfig string Path to kubeconfig (registered by controller-runtime)
1010
// --refresh duration Cache refresh interval (default 15s)
1111
package main
1212

@@ -22,30 +22,29 @@ import (
2222
"time"
2323

2424
k8sruntime "k8s.io/apimachinery/pkg/runtime"
25-
"k8s.io/client-go/rest"
26-
"k8s.io/client-go/tools/clientcmd"
2725
"sigs.k8s.io/controller-runtime/pkg/client"
26+
ctrlcfg "sigs.k8s.io/controller-runtime/pkg/client/config"
2827

2928
v1alpha1 "github.com/tazhate/blockchain-node-operator/api/v1alpha1"
3029
"github.com/tazhate/blockchain-node-operator/internal/dashboard"
3130
)
3231

3332
func main() {
3433
var (
35-
port = flag.Int("port", 8090, "HTTP listen port")
36-
namespace = flag.String("namespace", "", "Namespace to watch (empty = all)")
37-
kubeconfig = flag.String("kubeconfig", "", "Path to kubeconfig (empty = in-cluster)")
38-
refresh = flag.Duration("refresh", 15*time.Second, "Cache refresh interval")
34+
port = flag.Int("port", 8090, "HTTP listen port")
35+
namespace = flag.String("namespace", "", "Namespace to watch (empty = all)")
36+
refresh = flag.Duration("refresh", 15*time.Second, "Cache refresh interval")
3937
)
4038
flag.Parse()
4139

42-
cfg, err := buildRestConfig(*kubeconfig)
40+
// --kubeconfig is registered by sigs.k8s.io/controller-runtime/pkg/client/config init().
41+
// ctrlcfg.GetConfig() reads that flag automatically.
42+
restCfg, err := ctrlcfg.GetConfig()
4343
if err != nil {
4444
log.Fatalf("build rest config: %v", err)
4545
}
4646

47-
scheme := buildScheme()
48-
k8sClient, err := client.New(cfg, client.Options{Scheme: scheme})
47+
k8sClient, err := client.New(restCfg, client.Options{Scheme: buildScheme()})
4948
if err != nil {
5049
log.Fatalf("build k8s client: %v", err)
5150
}
@@ -59,7 +58,6 @@ func main() {
5958
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
6059
defer stop()
6160

62-
// Start cache refresh loop in background.
6361
go srv.Start(ctx)
6462

6563
httpSrv := &http.Server{
@@ -86,20 +84,6 @@ func main() {
8684
}
8785
}
8886

89-
func buildRestConfig(kubeconfig string) (*rest.Config, error) {
90-
if kubeconfig != "" {
91-
return clientcmd.BuildConfigFromFlags("", kubeconfig)
92-
}
93-
cfg, err := rest.InClusterConfig()
94-
if err == nil {
95-
return cfg, nil
96-
}
97-
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
98-
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
99-
loadingRules, &clientcmd.ConfigOverrides{},
100-
).ClientConfig()
101-
}
102-
10387
func buildScheme() *k8sruntime.Scheme {
10488
s := k8sruntime.NewScheme()
10589
if err := v1alpha1.AddToScheme(s); err != nil {

0 commit comments

Comments
 (0)