Skip to content

Commit 6768a76

Browse files
authored
Add network router and resource (#189)
This change adds two new resources, NetworkRouter and NetworkResource, which enable clusters to expose Kubernetes services to Netbird. The NetworkRouter is responsible for creating the network, group, setup key and routing peer all of which are unique to the isntance. Along with the deployment of the client in the cluster. The NetworkResource exposes a service by linking to the specific router it wants to expose to. This makes coupling between the resource and network easy to understand. Routers also set a DNS zone which is used to give names to resources based on the name and namespace of the service being exposed. Part of #172 Signed-off-by: Philip Laine <philip.laine@gmail.com>
1 parent af11e31 commit 6768a76

38 files changed

Lines changed: 2973 additions & 55 deletions

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ endif
8989

9090
.PHONY: install
9191
install: manifests ## Install CRDs into the K8s cluster specified in ~/.kube/config.
92-
$(KUBECTL) apply -f helm/kubernetes-operator/crds
92+
$(KUBECTL) apply --server-side -f helm/kubernetes-operator/crds
9393

9494
.PHONY: uninstall
9595
uninstall: manifests ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.

PROJECT

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,20 @@ resources:
9191
kind: Group
9292
path: github.com/netbirdio/kubernetes-operator/api/v1alpha1
9393
version: v1alpha1
94+
- api:
95+
crdVersion: v1
96+
namespaced: true
97+
controller: true
98+
domain: netbird.io
99+
kind: NetworkRouter
100+
path: github.com/netbirdio/kubernetes-operator/api/v1alpha1
101+
version: v1alpha1
102+
- api:
103+
crdVersion: v1
104+
namespaced: true
105+
controller: true
106+
domain: netbird.io
107+
kind: NetworkResource
108+
path: github.com/netbirdio/kubernetes-operator/api/v1alpha1
109+
version: v1alpha1
94110
version: "3"

api/v1alpha1/condition_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ const ReadyCondition = "Ready"
66

77
const (
88
ReconciledReason = "Reconciled"
9+
DependencyReason = "Dependency"
910
)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package v1alpha1
2+
3+
import (
4+
corev1 "k8s.io/api/core/v1"
5+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6+
)
7+
8+
// NetworkResourceSpec defines the desired state of NetworkResource.
9+
type NetworkResourceSpec struct {
10+
// NetworkRouterRef is a reference to the network and router where the resource will be created.
11+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
12+
NetworkRouterRef CrossNamespaceReference `json:"networkRouterRef"`
13+
14+
// ServiceRef is a reference to the service to expose in the Network.
15+
ServiceRef corev1.LocalObjectReference `json:"serviceRef"`
16+
17+
// Groups are references to groups that the resource will be a part of.
18+
// +optional
19+
Groups []ResourceReference `json:"groups,omitempty"`
20+
}
21+
22+
// NetworkResourceStatus defines the observed state of NetworkResource.
23+
type NetworkResourceStatus struct {
24+
// ObservedGeneration is the last reconciled generation.
25+
// +optional
26+
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
27+
28+
// Conditions holds the conditions for the NetworkResource.
29+
// +listType=map
30+
// +listMapKey=type
31+
// +optional
32+
Conditions []metav1.Condition `json:"conditions,omitempty"`
33+
34+
// NetworkID is the id of the network the resource is created in.
35+
// +optional
36+
NetworkID string `json:"networkID,omitempty"`
37+
38+
// ResourceID is the id of the created resource.
39+
// +optional
40+
ResourceID string `json:"resourceID,omitempty"`
41+
42+
// DNSZoneID is the id of the zone the DNS record is created in.
43+
// +optional
44+
DNSZoneID string `json:"dnsZoneID,omitempty"`
45+
46+
// DNSRecordID is the id of the created DNS record.
47+
// +optional
48+
DNSRecordID string `json:"dnsRecordID,omitempty"`
49+
}
50+
51+
// +kubebuilder:object:root=true
52+
// +kubebuilder:subresource:status
53+
// +kubebuilder:resource
54+
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].status",description=""
55+
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description=""
56+
57+
// NetworkResource is the Schema for the networkresources API.
58+
type NetworkResource struct {
59+
metav1.TypeMeta `json:",inline"`
60+
metav1.ObjectMeta `json:"metadata,omitempty"`
61+
62+
// +required
63+
Spec NetworkResourceSpec `json:"spec"`
64+
65+
// +kubebuilder:default={"observedGeneration":-1}
66+
Status NetworkResourceStatus `json:"status,omitempty"`
67+
}
68+
69+
// GetConditions returns the status conditions of the object.
70+
func (n *NetworkResource) GetConditions() []metav1.Condition {
71+
return n.Status.Conditions
72+
}
73+
74+
// SetConditions sets the status conditions on the object.
75+
func (n *NetworkResource) SetConditions(conditions []metav1.Condition) {
76+
n.Status.Conditions = conditions
77+
}
78+
79+
// +kubebuilder:object:root=true
80+
81+
// NetworkResourceList contains a list of NetworkResource.
82+
type NetworkResourceList struct {
83+
metav1.TypeMeta `json:",inline"`
84+
metav1.ListMeta `json:"metadata,omitzero"`
85+
Items []NetworkResource `json:"items"`
86+
}
87+
88+
func init() {
89+
SchemeBuilder.Register(&NetworkResource{}, &NetworkResourceList{})
90+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package v1alpha1
2+
3+
import (
4+
corev1 "k8s.io/api/core/v1"
5+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6+
)
7+
8+
// NetworkRouterSpec defines the desired state of NetworkRouter.
9+
type NetworkRouterSpec struct {
10+
// DNSZoneRef is a reference to the DNS zone used to create records for resources.
11+
// +required
12+
DNSZoneRef DNSZoneReference `json:"dnsZoneRef"`
13+
14+
// WorkloadOverride contains configuration that will override the default workload.
15+
// +optional
16+
WorkloadOverride *WorkloadOverride `json:"workloadOverride,omitempty"`
17+
}
18+
19+
// DNSZoneReference references a Netbird DNS zone by domain name.
20+
type DNSZoneReference struct {
21+
// Name is the domain name of an existing Netbird DNS zone, e.g. "example.com".
22+
// +required
23+
Name string `json:"name"`
24+
}
25+
26+
type WorkloadOverride struct {
27+
// Labels that will be added.
28+
// +optional
29+
Labels map[string]string `json:"labels"`
30+
31+
// Annotations that will be added.
32+
// +optional
33+
Annotations map[string]string `json:"annotations"`
34+
35+
// Replicas sets the amount of client replicas.
36+
// +optional
37+
Replicas *int32 `json:"replicas"`
38+
39+
// PodTemplate overrides the pod template.
40+
// +optional
41+
// +kubebuilder:pruning:PreserveUnknownFields
42+
// +kubebuilder:validation:Schemaless
43+
PodTemplate *corev1.PodTemplateSpec `json:"podTemplate"`
44+
}
45+
46+
// NetworkRouterStatus defines the observed state of NetworkRouter.
47+
type NetworkRouterStatus struct {
48+
// ObservedGeneration is the last reconciled generation.
49+
// +optional
50+
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
51+
52+
// Conditions holds the conditions for the NetworkRouter.
53+
// +listType=map
54+
// +listMapKey=type
55+
// +optional
56+
Conditions []metav1.Condition `json:"conditions,omitempty"`
57+
58+
// RoutingPeerID is the id of the created routing peer.
59+
// +optional
60+
RoutingPeerID string `json:"routingPeerID,omitempty"`
61+
62+
// NetworkID is the id of the network the routing peer was created in.
63+
// +optional
64+
NetworkID string `json:"networkID,omitempty"`
65+
}
66+
67+
// +kubebuilder:object:root=true
68+
// +kubebuilder:subresource:status
69+
// +kubebuilder:resource
70+
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].status",description=""
71+
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description=""
72+
73+
// NetworkRouter is the Schema for the networkrouters API.
74+
type NetworkRouter struct {
75+
metav1.TypeMeta `json:",inline"`
76+
metav1.ObjectMeta `json:"metadata,omitempty"`
77+
78+
// +required
79+
Spec NetworkRouterSpec `json:"spec"`
80+
81+
// +kubebuilder:default={"observedGeneration":-1}
82+
Status NetworkRouterStatus `json:"status,omitempty"`
83+
}
84+
85+
// GetConditions returns the status conditions of the object.
86+
func (n *NetworkRouter) GetConditions() []metav1.Condition {
87+
return n.Status.Conditions
88+
}
89+
90+
// SetConditions sets the status conditions on the object.
91+
func (n *NetworkRouter) SetConditions(conditions []metav1.Condition) {
92+
n.Status.Conditions = conditions
93+
}
94+
95+
// +kubebuilder:object:root=true
96+
97+
// NetworkRouterList contains a list of NetworkRouter.
98+
type NetworkRouterList struct {
99+
metav1.TypeMeta `json:",inline"`
100+
metav1.ListMeta `json:"metadata,omitzero"`
101+
Items []NetworkRouter `json:"items"`
102+
}
103+
104+
func init() {
105+
SchemeBuilder.Register(&NetworkRouter{}, &NetworkRouterList{})
106+
}

api/v1alpha1/reference_type.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@ import corev1 "k8s.io/api/core/v1"
44

55
// +kubebuilder:validation:XValidation:rule="(has(self.id) && !has(self.localRef)) || (!has(self.id) && has(self.localRef))",message="exactly one of id or localRef must be set"
66
type ResourceReference struct {
7-
// id of the resource in the Netbird API.
7+
// ID is the id of a resource in the Netbird API.
88
// +optional
99
ID *string `json:"id,omitempty"`
1010

11-
// local reference to the object in the same namespace.
11+
// LocalReference is a reference to a object in the same namespace.
1212
// +optional
1313
LocalRef *corev1.LocalObjectReference `json:"localRef,omitempty"`
1414
}
15+
16+
type CrossNamespaceReference struct {
17+
// Name of the referent.
18+
// +required
19+
Name string `json:"name"`
20+
21+
// Namespace of the referent.
22+
// +required
23+
Namespace string `json:"namespace"`
24+
}

api/v1alpha1/setupkey_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import (
66

77
// SetupKeySpec defines the desired state of SetupKey.
88
type SetupKeySpec struct {
9+
// Name of the setup key.
10+
// +kubebuilder:validation:MinLength=1
11+
Name string `json:"name"`
12+
913
// Ephemeral decides if peers added with the key are ephemeral or not.
1014
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="ephemeral is immutable"
1115
Ephemeral bool `json:"ephemeral"`

0 commit comments

Comments
 (0)