diff --git a/cmd/main.go b/cmd/main.go index 53f9ef6..489b3b5 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -22,6 +22,7 @@ import ( v1 "k8s.io/client-go/informers/core/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" + nodeutil "k8s.io/component-helpers/node/util" "k8s.io/klog/v2" "golang.org/x/sys/unix" @@ -33,6 +34,7 @@ var ( baselineAdminNetworkPolicy bool // BaselineAdminNetworkPolicy is alpha so keep it feature gated behind a flag queueID int metricsBindAddress string + hostnameOverride string ) func init() { @@ -41,6 +43,7 @@ func init() { flag.BoolVar(&baselineAdminNetworkPolicy, "baseline-admin-network-policy", false, "If set, enable Baseline Admin Network Policy API") flag.IntVar(&queueID, "nfqueue-id", 100, "Number of the nfqueue used") flag.StringVar(&metricsBindAddress, "metrics-bind-address", ":9080", "The IP address and port for the metrics server to serve on") + flag.StringVar(&hostnameOverride, "hostname-override", "", "If non-empty, will be used as the name of the Node that kube-network-policies is running on. If unset, the node name is assumed to be the same as the node's hostname.") flag.Usage = func() { fmt.Fprint(os.Stderr, "Usage: kube-network-policies [options]\n\n") @@ -64,9 +67,9 @@ func main() { klog.Fatalf("error parsing metrics bind address %s : %v", metricsBindAddress, err) } - nodeName := os.Getenv("MY_NODE_NAME") - if nodeName == "" { - klog.Fatalf("node name not set, please set the environment variable using the Downward API") + nodeName, err := nodeutil.GetHostname(hostnameOverride) + if err != nil { + klog.Fatalf("can not obtain the node name, use the hostname-override flag if you want to set it to a specific value: %v", err) } cfg := networkpolicy.Config{ diff --git a/go.mod b/go.mod index 14fa5d0..6f4c7be 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( k8s.io/apimachinery v0.30.2 k8s.io/client-go v0.30.2 k8s.io/component-base v0.30.2 + k8s.io/component-helpers v0.30.2 k8s.io/klog/v2 v2.130.1 k8s.io/utils v0.0.0-20240310230437-4693a0247e57 sigs.k8s.io/knftables v0.0.16 diff --git a/go.sum b/go.sum index e9773f8..5e3dd67 100644 --- a/go.sum +++ b/go.sum @@ -185,6 +185,8 @@ k8s.io/client-go v0.30.2 h1:sBIVJdojUNPDU/jObC+18tXWcTJVcwyqS9diGdWHk50= k8s.io/client-go v0.30.2/go.mod h1:JglKSWULm9xlJLx4KCkfLLQ7XwtlbflV6uFFSHTMgVs= k8s.io/component-base v0.30.2 h1:pqGBczYoW1sno8q9ObExUqrYSKhtE5rW3y6gX88GZII= k8s.io/component-base v0.30.2/go.mod h1:yQLkQDrkK8J6NtP+MGJOws+/PPeEXNpwFixsUI7h/OE= +k8s.io/component-helpers v0.30.2 h1:kDMYLiWEYeWU7H6jBI+Ua1i2hqNh0DzqDHNIppFC3po= +k8s.io/component-helpers v0.30.2/go.mod h1:tI0anfS6AbRqooaICkGg7UVAQLedOauVSQW9srDBnJw= k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk= k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= k8s.io/kube-openapi v0.0.0-20240411171206-dc4e619f62f3 h1:SbdLaI6mM6ffDSJCadEaD4IkuPzepLDGlkd2xV0t1uA= diff --git a/install-anp.yaml b/install-anp.yaml index b51f48b..f1daba7 100644 --- a/install-anp.yaml +++ b/install-anp.yaml @@ -80,7 +80,7 @@ spec: - name: kube-network-policies image: registry.k8s.io/networking/kube-network-policies:v0.3.0 command: ["/bin/netpol"] - args: ["-v=2", "-admin-network-policy=true", "-baseline-admin-network-policy=true"] + args: ["-v=2", "-admin-network-policy=true", "-baseline-admin-network-policy=true","--hostname-override=$(MY_NODE_NAME)"] volumeMounts: - name: lib-modules mountPath: /lib/modules diff --git a/install.yaml b/install.yaml index b7c3086..4f66c5b 100644 --- a/install.yaml +++ b/install.yaml @@ -72,8 +72,8 @@ spec: image: registry.k8s.io/networking/kube-network-policies:v0.3.0 args: - /bin/netpol - - -v - - "2" + - --hostname-override=$(MY_NODE_NAME) + - --v=2 volumeMounts: - name: lib-modules mountPath: /lib/modules