Skip to content

Commit edc4a89

Browse files
committed
allow to pass kubeconfig as flag
1 parent 832086a commit edc4a89

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

cmd/main.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2626
"k8s.io/client-go/informers"
2727
coreinformers "k8s.io/client-go/informers/core/v1"
28+
"k8s.io/client-go/tools/clientcmd"
2829

2930
"k8s.io/client-go/kubernetes"
3031
"k8s.io/client-go/rest"
@@ -36,6 +37,7 @@ import (
3637
)
3738

3839
var (
40+
kubeconfig string
3941
failOpen bool
4042
adminNetworkPolicy bool // AdminNetworkPolicy is alpha so keep it feature gated behind a flag
4143
baselineAdminNetworkPolicy bool // BaselineAdminNetworkPolicy is alpha so keep it feature gated behind a flag
@@ -47,6 +49,7 @@ var (
4749
)
4850

4951
func init() {
52+
flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
5053
flag.BoolVar(&failOpen, "fail-open", false, "If set, don't drop packets if the controller is not running")
5154
flag.BoolVar(&adminNetworkPolicy, "admin-network-policy", false, "If set, enable Admin Network Policy API")
5255
flag.BoolVar(&baselineAdminNetworkPolicy, "baseline-admin-network-policy", false, "If set, enable Baseline Admin Network Policy API")
@@ -106,10 +109,16 @@ func run() int {
106109
QueueID: queueID,
107110
NetfilterBug1766Fix: netfilterBug1766Fix,
108111
}
109-
// creates the in-cluster config
110-
config, err := rest.InClusterConfig()
112+
113+
var config *rest.Config
114+
if kubeconfig != "" {
115+
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
116+
} else {
117+
// creates the in-cluster config
118+
config, err = rest.InClusterConfig()
119+
}
111120
if err != nil {
112-
panic(err.Error())
121+
klog.Fatalf("can not create client-go configuration: %v", err)
113122
}
114123

115124
// use protobuf for better performance at scale

0 commit comments

Comments
 (0)