Allow references to groups by name (#195)

Group names are unique so we can safely use the name as a reference
method to groups. This makes assigning resources created in the cluster
to groups that already exist a lot easier.
This commit is contained in:
Philip Laine
2026-04-23 13:12:09 +02:00
committed by GitHub
parent 1a593dafc2
commit 99ef70603f
14 changed files with 139 additions and 71 deletions
+16
View File
@@ -1,9 +1,25 @@
package v1alpha1
import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// +kubebuilder:validation:XValidation:rule="(has(self.id)?1:0)+(has(self.name)?1:0)+(has(self.localRef)?1:0)==1",message="Exactly one of id, name, or localRef must be set"
type GroupReference struct {
// Name is the name of the group.
// +optional
Name *string `json:"name,omitempty"`
// ID is the id of the group.
// +optional
ID *string `json:"id,omitempty"`
// LocalReference is a reference to a group in the same namespace.
// +optional
LocalRef *corev1.LocalObjectReference `json:"localRef,omitempty"`
}
// GroupSpec defines the desired state of Group.
type GroupSpec struct {
// Name of the group.
+1 -1
View File
@@ -16,7 +16,7 @@ type NetworkResourceSpec struct {
// Groups are references to groups that the resource will be a part of.
// +optional
Groups []ResourceReference `json:"groups,omitempty"`
Groups []GroupReference `json:"groups,omitempty"`
}
// NetworkResourceStatus defines the observed state of NetworkResource.
-13
View File
@@ -1,18 +1,5 @@
package v1alpha1
import corev1 "k8s.io/api/core/v1"
// +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"
type ResourceReference struct {
// ID is the id of a resource in the Netbird API.
// +optional
ID *string `json:"id,omitempty"`
// LocalReference is a reference to a object in the same namespace.
// +optional
LocalRef *corev1.LocalObjectReference `json:"localRef,omitempty"`
}
type CrossNamespaceReference struct {
// Name of the referent.
// +required
+1 -1
View File
@@ -23,7 +23,7 @@ type SetupKeySpec struct {
// AutoGroups are groups that will be automatically assigned to peers using setup key.
// +optional
AutoGroups []ResourceReference `json:"autoGroups,omitempty"`
AutoGroups []GroupReference `json:"autoGroups,omitempty"`
}
// SetupKeyStatus defines the observed state of SetupKey.
+40 -35
View File
@@ -5,8 +5,8 @@
package v1alpha1
import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
)
@@ -99,6 +99,36 @@ func (in *GroupList) DeepCopyObject() runtime.Object {
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *GroupReference) DeepCopyInto(out *GroupReference) {
*out = *in
if in.Name != nil {
in, out := &in.Name, &out.Name
*out = new(string)
**out = **in
}
if in.ID != nil {
in, out := &in.ID, &out.ID
*out = new(string)
**out = **in
}
if in.LocalRef != nil {
in, out := &in.LocalRef, &out.LocalRef
*out = new(v1.LocalObjectReference)
**out = **in
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GroupReference.
func (in *GroupReference) DeepCopy() *GroupReference {
if in == nil {
return nil
}
out := new(GroupReference)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *GroupSpec) DeepCopyInto(out *GroupSpec) {
*out = *in
@@ -119,7 +149,7 @@ func (in *GroupStatus) DeepCopyInto(out *GroupStatus) {
*out = *in
if in.Conditions != nil {
in, out := &in.Conditions, &out.Conditions
*out = make([]v1.Condition, len(*in))
*out = make([]metav1.Condition, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
@@ -202,7 +232,7 @@ func (in *NetworkResourceSpec) DeepCopyInto(out *NetworkResourceSpec) {
out.ServiceRef = in.ServiceRef
if in.Groups != nil {
in, out := &in.Groups, &out.Groups
*out = make([]ResourceReference, len(*in))
*out = make([]GroupReference, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
@@ -224,7 +254,7 @@ func (in *NetworkResourceStatus) DeepCopyInto(out *NetworkResourceStatus) {
*out = *in
if in.Conditions != nil {
in, out := &in.Conditions, &out.Conditions
*out = make([]v1.Condition, len(*in))
*out = make([]metav1.Condition, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
@@ -326,7 +356,7 @@ func (in *NetworkRouterStatus) DeepCopyInto(out *NetworkRouterStatus) {
*out = *in
if in.Conditions != nil {
in, out := &in.Conditions, &out.Conditions
*out = make([]v1.Condition, len(*in))
*out = make([]metav1.Condition, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
@@ -343,31 +373,6 @@ func (in *NetworkRouterStatus) DeepCopy() *NetworkRouterStatus {
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ResourceReference) DeepCopyInto(out *ResourceReference) {
*out = *in
if in.ID != nil {
in, out := &in.ID, &out.ID
*out = new(string)
**out = **in
}
if in.LocalRef != nil {
in, out := &in.LocalRef, &out.LocalRef
*out = new(corev1.LocalObjectReference)
**out = **in
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ResourceReference.
func (in *ResourceReference) DeepCopy() *ResourceReference {
if in == nil {
return nil
}
out := new(ResourceReference)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *SetupKey) DeepCopyInto(out *SetupKey) {
*out = *in
@@ -432,12 +437,12 @@ func (in *SetupKeySpec) DeepCopyInto(out *SetupKeySpec) {
*out = *in
if in.Duration != nil {
in, out := &in.Duration, &out.Duration
*out = new(v1.Duration)
*out = new(metav1.Duration)
**out = **in
}
if in.AutoGroups != nil {
in, out := &in.AutoGroups, &out.AutoGroups
*out = make([]ResourceReference, len(*in))
*out = make([]GroupReference, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
@@ -459,7 +464,7 @@ func (in *SetupKeyStatus) DeepCopyInto(out *SetupKeyStatus) {
*out = *in
if in.Conditions != nil {
in, out := &in.Conditions, &out.Conditions
*out = make([]v1.Condition, len(*in))
*out = make([]metav1.Condition, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
@@ -500,7 +505,7 @@ func (in *WorkloadOverride) DeepCopyInto(out *WorkloadOverride) {
}
if in.PodTemplate != nil {
in, out := &in.PodTemplate, &out.PodTemplate
*out = new(corev1.PodTemplateSpec)
*out = new(v1.PodTemplateSpec)
(*in).DeepCopyInto(*out)
}
}