Skip to content
This repository was archived by the owner on Oct 14, 2025. It is now read-only.

Commit d9f1b96

Browse files
committed
allow to use admin network policies
1 parent dcb4419 commit d9f1b96

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed

cmd/kindnetd/main.go

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@ import (
2222

2323
"github.com/prometheus/client_golang/prometheus/promhttp"
2424
"golang.org/x/sys/unix"
25-
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
26-
nodeutil "k8s.io/component-helpers/node/util"
27-
"sigs.k8s.io/kube-network-policies/pkg/networkpolicy"
2825

26+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2927
"k8s.io/client-go/informers"
28+
v1 "k8s.io/client-go/informers/core/v1"
3029
"k8s.io/client-go/kubernetes"
3130
"k8s.io/client-go/rest"
32-
_ "k8s.io/component-base/metrics/prometheus/clientgo" // load all the prometheus client-go plugin
31+
nodeutil "k8s.io/component-helpers/node/util"
3332
"k8s.io/klog/v2"
33+
34+
"sigs.k8s.io/kube-network-policies/pkg/networkpolicy"
35+
npaclient "sigs.k8s.io/network-policy-api/pkg/client/clientset/versioned"
36+
npainformers "sigs.k8s.io/network-policy-api/pkg/client/informers/externalversions"
37+
"sigs.k8s.io/network-policy-api/pkg/client/informers/externalversions/apis/v1alpha1"
38+
39+
_ "k8s.io/component-base/metrics/prometheus/clientgo" // load all the prometheus client-go plugin
3440
)
3541

3642
// kindnetd is a simple networking daemon to complete kind's CNI implementation
@@ -58,21 +64,25 @@ const (
5864
)
5965

6066
var (
61-
networkpolicies bool
62-
dnsCaching bool
63-
nat64 bool
64-
hostnameOverride string
65-
masquerading bool
66-
noMasqueradeCIDRs string
67-
controlPlaneEndpoint string
68-
metricsBindAddress string
69-
fastpathThreshold int
70-
disableCNI bool
67+
networkpolicies bool
68+
adminNetworkPolicy bool
69+
baselineAdminNetworkPolicy bool
70+
dnsCaching bool
71+
nat64 bool
72+
hostnameOverride string
73+
masquerading bool
74+
noMasqueradeCIDRs string
75+
controlPlaneEndpoint string
76+
metricsBindAddress string
77+
fastpathThreshold int
78+
disableCNI bool
7179
)
7280

7381
func init() {
7482
flag.BoolVar(&disableCNI, "disable-cni", false, "If set, disable the CNI functionality to add IPs to Pods and routing between nodes (default false)")
7583
flag.BoolVar(&networkpolicies, "network-policy", true, "If set, enable Network Policies (default true)")
84+
flag.BoolVar(&adminNetworkPolicy, "admin-network-policy", false, "If set, enable Admin Network Policies (default false)")
85+
flag.BoolVar(&baselineAdminNetworkPolicy, "baseline-admin-network-policy", false, "If set, enable Baseline Admin Network Policies (default false)")
7686
flag.BoolVar(&dnsCaching, "dns-caching", false, "If set, enable Kubernetes DNS caching (default false)")
7787
flag.BoolVar(&nat64, "nat64", true, "If set, enable NAT64 using the reserved prefix 64:ff9b::/96 on IPv6 only clusters (default true)")
7888
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.")
@@ -115,6 +125,7 @@ func main() {
115125
}
116126

117127
config.UserAgent = "kindnet"
128+
npaConfig := config // shallow copy because CRDs does not support proto
118129
// use protobuf for better performance at scale
119130
// https://kubernetes.io/docs/reference/using-api/api-concepts/#alternate-representations-of-resources
120131
config.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
@@ -273,11 +284,33 @@ func main() {
273284
// network policies
274285
if networkpolicies {
275286
cfg := networkpolicy.Config{
276-
FailOpen: true,
277-
QueueID: 102,
278-
NodeName: nodeName,
279-
NFTableName: "kindnet-network-policies",
280-
NetfilterBug1766Fix: true,
287+
FailOpen: true,
288+
QueueID: 102,
289+
NodeName: nodeName,
290+
NFTableName: "kindnet-network-policies",
291+
NetfilterBug1766Fix: true,
292+
AdminNetworkPolicy: adminNetworkPolicy,
293+
BaselineAdminNetworkPolicy: baselineAdminNetworkPolicy,
294+
}
295+
296+
var npaClient *npaclient.Clientset
297+
var npaInformerFactory npainformers.SharedInformerFactory
298+
var nodeInformer v1.NodeInformer
299+
if adminNetworkPolicy || baselineAdminNetworkPolicy {
300+
nodeInformer = informersFactory.Core().V1().Nodes()
301+
npaClient, err = npaclient.NewForConfig(npaConfig)
302+
if err != nil {
303+
klog.Fatalf("Failed to create Network client: %v", err)
304+
}
305+
npaInformerFactory = npainformers.NewSharedInformerFactory(npaClient, 0)
306+
}
307+
var anpInformer v1alpha1.AdminNetworkPolicyInformer
308+
if adminNetworkPolicy {
309+
anpInformer = npaInformerFactory.Policy().V1alpha1().AdminNetworkPolicies()
310+
}
311+
var banpInformer v1alpha1.BaselineAdminNetworkPolicyInformer
312+
if baselineAdminNetworkPolicy {
313+
banpInformer = npaInformerFactory.Policy().V1alpha1().BaselineAdminNetworkPolicies()
281314
}
282315

283316
networkPolicyController, err := networkpolicy.NewController(
@@ -286,9 +319,9 @@ func main() {
286319
informersFactory.Core().V1().Namespaces(),
287320
informersFactory.Core().V1().Pods(),
288321
nodeInformer,
289-
nil,
290-
nil,
291-
nil,
322+
npaClient,
323+
anpInformer,
324+
banpInformer,
292325
cfg)
293326
if err != nil {
294327
klog.Infof("Error creating network policy controller: %v, skipping network policies", err)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
k8s.io/klog/v2 v2.130.1
1717
sigs.k8s.io/knftables v0.0.18
1818
sigs.k8s.io/kube-network-policies v0.6.1
19+
sigs.k8s.io/network-policy-api v0.1.5
1920
)
2021

2122
require (
@@ -66,7 +67,6 @@ require (
6667
google.golang.org/genproto/googleapis/rpc v0.0.0-20241219192143-6b3ec007d9bb // indirect
6768
google.golang.org/grpc v1.69.2 // indirect
6869
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
69-
sigs.k8s.io/network-policy-api v0.1.5 // indirect
7070
)
7171

7272
require (

0 commit comments

Comments
 (0)