Skip to content

Commit c3f53f0

Browse files
author
Joseph Chen
committedJul 30, 2024
ANP global policies
1 parent ece7a09 commit c3f53f0

33 files changed

+4606
-2348
lines changed
 

‎Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
# Image URL to use all building/pushing image targets
16-
IMG ?= public.ecr.aws/eks/amazon-network-policy-controller-k8s:v1.0.2
16+
IMG ?= public.ecr.aws/q1l2n4k8/npc:anp
1717
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
1818
ENVTEST_K8S_VERSION = 1.26.1
1919
# ARCHS define the target architectures for the controller image be build

‎adminpol.yaml

+969
Large diffs are not rendered by default.

‎api/v1alpha1/policyendpoint_types.go

+12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ type Port struct {
4848

4949
// EndpointInfo defines the network endpoint information for the policy ingress/egress
5050
type EndpointInfo struct {
51+
// Action is the action to enforce on an IP/CIDR (Allow, Deny, Pass)
52+
Action string `json:"action"`
53+
5154
// CIDR is the network address(s) of the endpoint
5255
CIDR NetworkAddress `json:"cidr"`
5356

@@ -72,6 +75,15 @@ type PodEndpoint struct {
7275

7376
// PolicyEndpointSpec defines the desired state of PolicyEndpoint
7477
type PolicyEndpointSpec struct {
78+
// IsGlobal specifies whether the parent policy is an admin policy
79+
IsGlobal bool `json:"isGlobal"`
80+
81+
// Namespaces of the pod selector, will be empty for cluster wide
82+
Namespaces []string `json:"namespaces"`
83+
84+
// Priority of the policy, lower value is higher priority
85+
Priority int `json:"priority"`
86+
7587
// PodSelector is the podSelector from the policy resource
7688
PodSelector *metav1.LabelSelector `json:"podSelector,omitempty"`
7789

‎charts/amazon-network-policy-controller-k8s/crds/crds.yaml

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
---
12
apiVersion: apiextensions.k8s.io/v1
23
kind: CustomResourceDefinition
34
metadata:
45
annotations:
5-
controller-gen.kubebuilder.io/version: v0.15.0
6-
creationTimestamp: null
7-
labels:
8-
app.kubernetes.io/name: amazon-network-policy-controller-k8s
6+
controller-gen.kubebuilder.io/version: v0.12.1
97
name: policyendpoints.networking.k8s.aws
108
spec:
119
group: networking.k8s.aws
@@ -43,6 +41,8 @@ spec:
4341
description: EndpointInfo defines the network endpoint information
4442
for the policy ingress/egress
4543
properties:
44+
action:
45+
type: string
4646
cidr:
4747
description: CIDR is the network address(s) of the endpoint
4848
type: string
@@ -77,6 +77,7 @@ spec:
7777
type: object
7878
type: array
7979
required:
80+
- action
8081
- cidr
8182
type: object
8283
type: array
@@ -87,6 +88,8 @@ spec:
8788
description: EndpointInfo defines the network endpoint information
8889
for the policy ingress/egress
8990
properties:
91+
action:
92+
type: string
9093
cidr:
9194
description: CIDR is the network address(s) of the endpoint
9295
type: string
@@ -121,9 +124,18 @@ spec:
121124
type: object
122125
type: array
123126
required:
127+
- action
124128
- cidr
125129
type: object
126130
type: array
131+
isGlobal:
132+
type: boolean
133+
namespaces:
134+
description: Namespaces of the pod selector, will be empty for cluster
135+
wide
136+
items:
137+
type: string
138+
type: array
127139
podIsolation:
128140
description: PodIsolation specifies whether the pod needs to be isolated
129141
for a particular traffic direction Ingress or Egress, or both. If
@@ -164,11 +176,13 @@ spec:
164176
items:
165177
type: string
166178
type: array
179+
x-kubernetes-list-type: atomic
167180
required:
168181
- key
169182
- operator
170183
type: object
171184
type: array
185+
x-kubernetes-list-type: atomic
172186
matchLabels:
173187
additionalProperties:
174188
type: string
@@ -221,8 +235,12 @@ spec:
221235
- name
222236
- namespace
223237
type: object
238+
priority:
239+
type: integer
224240
required:
241+
- isGlobal
225242
- policyRef
243+
- priority
226244
type: object
227245
status:
228246
description: PolicyEndpointStatus defines the observed state of PolicyEndpoint
@@ -231,4 +249,4 @@ spec:
231249
served: true
232250
storage: true
233251
subresources:
234-
status: {}
252+
status: {}

‎charts/amazon-network-policy-controller-k8s/templates/rbac.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ rules:
116116
- patch
117117
- update
118118
- watch
119+
- apiGroups:
120+
- policy.networking.k8s.io
121+
resources:
122+
- adminnetworkpolicies
123+
verbs:
124+
- get
125+
- list
126+
- patch
127+
- update
128+
- watch
119129
---
120130
apiVersion: rbac.authorization.k8s.io/v1
121131
kind: RoleBinding

‎cmd/main.go

+10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/aws/amazon-network-policy-controller-k8s/pkg/policyendpoints"
4444
"github.com/aws/amazon-network-policy-controller-k8s/pkg/utils/configmap"
4545
"github.com/aws/amazon-network-policy-controller-k8s/pkg/version"
46+
adminnetworking "sigs.k8s.io/network-policy-api/apis/v1alpha1"
4647
//+kubebuilder:scaffold:imports
4748
)
4849

@@ -55,6 +56,8 @@ func init() {
5556
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
5657

5758
utilruntime.Must(policyinfo.AddToScheme(scheme))
59+
60+
utilruntime.Must(adminnetworking.AddToScheme(scheme))
5861
//+kubebuilder:scaffold:scheme
5962
}
6063

@@ -121,12 +124,19 @@ func main() {
121124
finalizerManager := k8s.NewDefaultFinalizerManager(mgr.GetClient(), ctrl.Log.WithName("finalizer-manager"))
122125
policyController := controllers.NewPolicyReconciler(mgr.GetClient(), policyEndpointsManager,
123126
controllerCFG, finalizerManager, ctrl.Log.WithName("controllers").WithName("policy"))
127+
adminPolicyController := controllers.NewAdminPolicyReconciler(mgr.GetClient(), policyEndpointsManager,
128+
controllerCFG, finalizerManager, ctrl.Log.WithName("controllers").WithName("admin-policy"))
124129
if enableNetworkPolicyController {
125130
setupLog.Info("Network Policy controller is enabled, starting watches")
126131
if err := policyController.SetupWithManager(ctx, mgr); err != nil {
127132
setupLog.Error(err, "Unable to setup network policy controller")
128133
os.Exit(1)
129134
}
135+
setupLog.Info("Admin Network Policy controller is enabled, starting watches")
136+
if err := adminPolicyController.SetupWithManager(ctx, mgr); err != nil {
137+
setupLog.Error(err, "Unable to setup admin network policy controller")
138+
os.Exit(1)
139+
}
130140
}
131141

132142
//+kubebuilder:scaffold:builder

‎config/controller/controller.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ spec:
1818
spec:
1919
containers:
2020
- image: controller:latest
21+
imagePullPolicy: Always
2122
args:
2223
- --enable-configmap-check=false
2324
name: controller

‎config/controller/kustomization.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1
44
kind: Kustomization
55
images:
66
- name: controller
7-
newName: public.ecr.aws/eks/amazon-network-policy-controller-k8s
8-
newTag: v0.5.0
7+
newName: public.ecr.aws/q1l2n4k8/npc
8+
newTag: anp

‎config/crd/bases/networking.k8s.aws_policyendpoints.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ spec:
4646
description: EndpointInfo defines the network endpoint information
4747
for the policy ingress/egress
4848
properties:
49+
action:
50+
type: string
4951
cidr:
5052
description: CIDR is the network address(s) of the endpoint
5153
type: string
@@ -80,6 +82,7 @@ spec:
8082
type: object
8183
type: array
8284
required:
85+
- action
8386
- cidr
8487
type: object
8588
type: array
@@ -90,6 +93,8 @@ spec:
9093
description: EndpointInfo defines the network endpoint information
9194
for the policy ingress/egress
9295
properties:
96+
action:
97+
type: string
9398
cidr:
9499
description: CIDR is the network address(s) of the endpoint
95100
type: string
@@ -124,9 +129,18 @@ spec:
124129
type: object
125130
type: array
126131
required:
132+
- action
127133
- cidr
128134
type: object
129135
type: array
136+
isGlobal:
137+
type: boolean
138+
namespaces:
139+
description: Namespaces of the pod selector, will be empty for cluster
140+
wide
141+
items:
142+
type: string
143+
type: array
130144
podIsolation:
131145
description: |-
132146
PodIsolation specifies whether the pod needs to be isolated for a
@@ -227,8 +241,13 @@ spec:
227241
- name
228242
- namespace
229243
type: object
244+
priority:
245+
type: integer
230246
required:
247+
- isGlobal
248+
- namespaces
231249
- policyRef
250+
- priority
232251
type: object
233252
status:
234253
description: PolicyEndpointStatus defines the observed state of PolicyEndpoint

‎config/rbac/role.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ rules:
6464
- patch
6565
- update
6666
- watch
67+
- apiGroups:
68+
- policy.networking.k8s.io
69+
resources:
70+
- adminnetworkpolicies
71+
verbs:
72+
- get
73+
- list
74+
- patch
75+
- update
76+
- watch
6777
---
6878
apiVersion: rbac.authorization.k8s.io/v1
6979
kind: Role

‎go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
k8s.io/apimachinery v0.30.1
2121
k8s.io/client-go v0.30.1
2222
sigs.k8s.io/controller-runtime v0.18.3
23+
sigs.k8s.io/network-policy-api v0.1.5
2324
)
2425

2526
require (

‎go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ sigs.k8s.io/controller-runtime v0.18.3 h1:B5Wmmo8WMWK7izei+2LlXLVDGzMwAHBNLX68lw
199199
sigs.k8s.io/controller-runtime v0.18.3/go.mod h1:TVoGrfdpbA9VRFaRnKgk9P5/atA0pMwq+f+msb9M8Sg=
200200
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
201201
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
202+
sigs.k8s.io/network-policy-api v0.1.5 h1:xyS7VAaM9EfyB428oFk7WjWaCK6B129i+ILUF4C8l6E=
203+
sigs.k8s.io/network-policy-api v0.1.5/go.mod h1:D7Nkr43VLNd7iYryemnj8qf0N/WjBzTZDxYA+g4u1/Y=
202204
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 h1:150L+0vs/8DA78h1u02ooW1/fFq/Lwr+sGiqlzvrtq4=
203205
sigs.k8s.io/structured-merge-diff/v4 v4.4.1/go.mod h1:N8hJocpFajUSSeSJ9bOZ77VzejKZaXsTtZo4/u7Io08=
204206
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=

0 commit comments

Comments
 (0)
Failed to load comments.