Skip to content

Commit 38445c5

Browse files
committed
Add support for policy auto-creation
1 parent 924f17f commit 38445c5

File tree

12 files changed

+690
-190
lines changed

12 files changed

+690
-190
lines changed

api/v1/nbresource_types.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package v1
22

33
import (
4+
"maps"
5+
46
"github.com/netbirdio/kubernetes-operator/internal/util"
57
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
68
)
@@ -18,6 +20,10 @@ type NBResourceSpec struct {
1820
// +optional
1921
PolicyName string `json:"policyName,omitempty"`
2022
// +optional
23+
PolicySourceGroups []string `json:"policySourceGroups,omitempty"`
24+
// +optional
25+
PolicyFriendlyName map[string]string `json:"policyFriendlyName,omitempty"`
26+
// +optional
2127
TCPPorts []int32 `json:"tcpPorts,omitempty"`
2228
// +optional
2329
UDPPorts []int32 `json:"udpPorts,omitempty"`
@@ -31,7 +37,8 @@ func (a NBResourceSpec) Equal(b NBResourceSpec) bool {
3137
util.Equivalent(a.Groups, b.Groups) &&
3238
a.PolicyName == b.PolicyName &&
3339
util.Equivalent(a.TCPPorts, b.TCPPorts) &&
34-
util.Equivalent(a.UDPPorts, b.UDPPorts)
40+
util.Equivalent(a.UDPPorts, b.UDPPorts) &&
41+
util.Equivalent(a.PolicySourceGroups, b.PolicySourceGroups)
3542
}
3643

3744
// NBResourceStatus defines the observed state of NBResource.
@@ -47,7 +54,13 @@ type NBResourceStatus struct {
4754
// +optional
4855
Groups []string `json:"groups,omitempty"`
4956
// +optional
57+
PolicySourceGroups []string `json:"policySourceGroups,omitempty"`
58+
// +optional
59+
PolicyFriendlyName map[string]string `json:"policyFriendlyName,omitempty"`
60+
// +optional
5061
Conditions []NBCondition `json:"conditions,omitempty"`
62+
// +optional
63+
PolicyNameMapping map[string]string `json:"policyNameMapping"`
5164
}
5265

5366
// Equal returns if NBResourceStatus is equal to this one
@@ -57,7 +70,10 @@ func (a NBResourceStatus) Equal(b NBResourceStatus) bool {
5770
util.Equivalent(a.TCPPorts, b.TCPPorts) &&
5871
util.Equivalent(a.UDPPorts, b.UDPPorts) &&
5972
util.Equivalent(a.Groups, b.Groups) &&
60-
util.Equivalent(a.Conditions, b.Conditions)
73+
util.Equivalent(a.Conditions, b.Conditions) &&
74+
util.Equivalent(a.PolicySourceGroups, b.PolicySourceGroups) &&
75+
maps.Equal(a.PolicyFriendlyName, b.PolicyFriendlyName) &&
76+
maps.Equal(a.PolicyNameMapping, b.PolicyNameMapping)
6177
}
6278

6379
// +kubebuilder:object:root=true

api/v1/zz_generated.deepcopy.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/main.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ func init() {
6767
func main() {
6868
// NB Specific flags
6969
var (
70-
managementURL string
71-
clientImage string
72-
clusterName string
73-
namespacedNetworks bool
74-
clusterDNS string
75-
netbirdAPIKey string
70+
managementURL string
71+
clientImage string
72+
clusterName string
73+
namespacedNetworks bool
74+
clusterDNS string
75+
netbirdAPIKey string
76+
allowAutomaticPolicyCreation bool
7677
)
7778
flag.StringVar(&managementURL, "netbird-management-url", "https://api.netbird.io", "Management service URL")
7879
flag.StringVar(&clientImage, "netbird-client-image", "netbirdio/netbird:latest", "Image for netbird client container")
@@ -90,6 +91,12 @@ func main() {
9091
)
9192
flag.StringVar(&clusterDNS, "cluster-dns", "svc.cluster.local", "Cluster DNS name")
9293
flag.StringVar(&netbirdAPIKey, "netbird-api-key", "", "API key for NetBird API operations")
94+
flag.BoolVar(
95+
&allowAutomaticPolicyCreation,
96+
"allow-automatic-policy-creation",
97+
false,
98+
"Allow creating NBPolicy resources from annotations on Services",
99+
)
93100

94101
// Controller generic flags
95102
var (
@@ -233,10 +240,12 @@ func main() {
233240
}
234241

235242
if err = (&controller.NBResourceReconciler{
236-
Client: mgr.GetClient(),
237-
Scheme: mgr.GetScheme(),
238-
APIKey: netbirdAPIKey,
239-
ManagementURL: managementURL,
243+
Client: mgr.GetClient(),
244+
Scheme: mgr.GetScheme(),
245+
APIKey: netbirdAPIKey,
246+
ManagementURL: managementURL,
247+
AllowAutomaticPolicyCreation: allowAutomaticPolicyCreation,
248+
ClusterName: clusterName,
240249
}).SetupWithManager(mgr); err != nil {
241250
setupLog.Error(err, "unable to create controller", "controller", "NBResource")
242251
os.Exit(1)

helm/kubernetes-operator/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ apiVersion: v2
22
name: kubernetes-operator
33
description: NetBird Kubernetes Operator
44
type: application
5-
version: 0.1.7
6-
appVersion: "0.1.2"
5+
version: 0.1.8
6+
appVersion: "0.1.3"

helm/kubernetes-operator/crds/netbird.io_nbresources.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,16 @@ spec:
5555
x-kubernetes-validations:
5656
- message: Value is immutable
5757
rule: self == oldSelf
58+
policyFriendlyName:
59+
additionalProperties:
60+
type: string
61+
type: object
5862
policyName:
5963
type: string
64+
policySourceGroups:
65+
items:
66+
type: string
67+
type: array
6068
tcpPorts:
6169
items:
6270
format: int32
@@ -116,8 +124,20 @@ spec:
116124
type: array
117125
networkResourceID:
118126
type: string
127+
policyFriendlyName:
128+
additionalProperties:
129+
type: string
130+
type: object
119131
policyName:
120132
type: string
133+
policyNameMapping:
134+
additionalProperties:
135+
type: string
136+
type: object
137+
policySourceGroups:
138+
items:
139+
type: string
140+
type: array
121141
tcpPorts:
122142
items:
123143
format: int32

helm/kubernetes-operator/templates/deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ spec:
6060
{{- if or .Values.netbirdAPI.key .Values.netbirdAPI.keyFromSecret }}
6161
- --netbird-api-key=$(NB_API_KEY)
6262
{{- end }}
63+
{{- if .Values.ingress.allowAutomaticPolicyCreation }}
64+
- --allow-automatic-policy-creation
65+
{{- end }}
6366
ports:
6467
- name: webhook-server
6568
containerPort: {{ .Values.webhook.service.port }}

helm/kubernetes-operator/values.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ ingress:
135135
enabled: false
136136
# Create router per namespace, useful for strict networking requirements
137137
namespacedNetworks: false
138+
# Allow creating policies through Service annotations
139+
allowAutomaticPolicyCreation: false
138140
kubernetesAPI:
139141
enabled: false
140142
groups: []

internal/controller/nbpolicy_controller.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var (
3333
errUnknownProtocol = fmt.Errorf("Unknown protocol")
3434
errKubernetesAPI = fmt.Errorf("kubernetes API error")
3535
errNetBirdAPI = fmt.Errorf("netbird API error")
36+
errInvalidValue = fmt.Errorf("invalid value")
3637
)
3738

3839
const (
@@ -77,16 +78,28 @@ func (r *NBPolicyReconciler) mapResources(ctx context.Context, nbPolicy *netbird
7778
}
7879

7980
for _, resource := range resources {
80-
if resource.Status.PolicyName != nil && util.Contains(util.SplitTrim(*resource.Status.PolicyName, ","), nbPolicy.Name) {
81-
// Groups
82-
groups = append(groups, resource.Status.Groups...)
81+
generatedBy := nbPolicy.Annotations["netbird.io/generated-by"]
82+
generatedBy = strings.ReplaceAll(generatedBy, "/", "-")
83+
if resource.Status.PolicyName == nil {
84+
continue
85+
}
86+
resourcePolicies := util.SplitTrim(*resource.Status.PolicyName, ",")
8387

84-
for _, p := range resource.Spec.TCPPorts {
85-
portMapping[protocolTCP][p] = nil
86-
}
87-
for _, p := range resource.Spec.UDPPorts {
88-
portMapping[protocolUDP][p] = nil
89-
}
88+
if generatedBy == "" && !util.Contains(resourcePolicies, nbPolicy.Name) {
89+
continue
90+
}
91+
92+
if generatedBy != "" && !util.Contains(resourcePolicies, strings.ReplaceAll(nbPolicy.Name, "-"+generatedBy, "")) {
93+
continue
94+
}
95+
// Groups
96+
groups = append(groups, resource.Status.Groups...)
97+
98+
for _, p := range resource.Spec.TCPPorts {
99+
portMapping[protocolTCP][p] = nil
100+
}
101+
for _, p := range resource.Spec.UDPPorts {
102+
portMapping[protocolUDP][p] = nil
90103
}
91104
}
92105

0 commit comments

Comments
 (0)