Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't use obscure env variables and use flags #44

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
aojea marked this conversation as resolved.
Show resolved Hide resolved
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")
aojea marked this conversation as resolved.
Show resolved Hide resolved
}

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}"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the main problem because I added the env variables is because I was not sure you can do this, of using the env variable as the flag value and I was too lazy ... now that you mentioned I can see this is exactly how most people do https://grep.app/search?q=hostname-override%3D%24&filter[lang][0]=YAML

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ups , something is wrong

2024-06-26T10:58:00.54696576Z stderr F I0626 10:58:00.546903       1 controller.go:89] Creating controller: networkpolicy.Config{FailOpen:false, AdminNetworkPolicy:false, BaselineAdminNetworkPolicy:false, QueueID:100, NodeName:"${my_node_name}"}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Loading