Skip to content

Commit

Permalink
Merge pull request #44 from aojea/flags_better_than_env
Browse files Browse the repository at this point in the history
don't use obscure env variables and use flags
  • Loading branch information
aojea authored Jun 26, 2024
2 parents ac15c29 + 639cb81 commit 1b5734b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
9 changes: 6 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand All @@ -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")
Expand All @@ -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{
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion install-anp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1b5734b

Please sign in to comment.