Compare commits

..

3 Commits

Author SHA1 Message Date
3f59db13f0 Use reauthentication 2025-04-14 17:47:26 +02:00
a023204d6c Merge pull request #21 from vegardengen/19-create-configmap-with-default-namespace
Fix configmap role
2025-04-14 15:14:21 +02:00
72c13517b0 Merge pull request #20 from vegardengen/19-create-configmap-with-default-namespace
19 create configmap with default namespace
2025-04-14 15:08:32 +02:00
4 changed files with 107 additions and 92 deletions

View File

@@ -21,10 +21,10 @@ import (
"fmt" "fmt"
"net" "net"
"reflect" "reflect"
"regexp"
"slices" "slices"
"strconv" "strconv"
"strings" "strings"
"regexp"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -39,15 +39,15 @@ import (
goUnifi "github.com/vegardengen/go-unifi/unifi" goUnifi "github.com/vegardengen/go-unifi/unifi"
unifiv1beta1 "github.com/vegardengen/unifi-network-operator/api/v1beta1" unifiv1beta1 "github.com/vegardengen/unifi-network-operator/api/v1beta1"
"github.com/vegardengen/unifi-network-operator/internal/unifi"
"github.com/vegardengen/unifi-network-operator/internal/config" "github.com/vegardengen/unifi-network-operator/internal/config"
"github.com/vegardengen/unifi-network-operator/internal/unifi"
) )
// FirewallGroupReconciler reconciles a FirewallGroup object // FirewallGroupReconciler reconciles a FirewallGroup object
type FirewallGroupReconciler struct { type FirewallGroupReconciler struct {
client.Client client.Client
Scheme *runtime.Scheme Scheme *runtime.Scheme
UnifiClient *unifi.UnifiClient UnifiClient *unifi.UnifiClient
ConfigLoader *config.ConfigLoaderType ConfigLoader *config.ConfigLoaderType
} }
@@ -69,13 +69,13 @@ type FirewallGroupReconciler struct {
func (r *FirewallGroupReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { func (r *FirewallGroupReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
log := log.FromContext(ctx) log := log.FromContext(ctx)
cfg, err := r.ConfigLoader.GetConfig(ctx, "unifi-operator-config")
if err != nil {
return ctrl.Result{}, err
}
defaultNs := cfg.Data["defaultNamespace"] cfg, err := r.ConfigLoader.GetConfig(ctx, "unifi-operator-config")
if err != nil {
return ctrl.Result{}, err
}
defaultNs := cfg.Data["defaultNamespace"]
log.Info(defaultNs) log.Info(defaultNs)
var nwObj unifiv1beta1.FirewallGroup var nwObj unifiv1beta1.FirewallGroup
@@ -118,19 +118,19 @@ func (r *FirewallGroupReconciler) Reconcile(ctx context.Context, req reconcile.R
port_type := "tcp" port_type := "tcp"
port := portEntry port := portEntry
if match, _ := regexp.MatchString("(?:tcp|udp)\\/?)\\d+", string(portEntry)); match { if match, _ := regexp.MatchString("(?:tcp|udp)\\/?)\\d+", string(portEntry)); match {
fields := strings.Split("/",portEntry) fields := strings.Split("/", portEntry)
port_type = fields[0] port_type = fields[0]
port = fields[1] port = fields[1]
} }
if(port_type == "tcp") { if port_type == "tcp" {
if !slices.Contains(tcpports, port) { if !slices.Contains(tcpports, port) {
tcpports = append(tcpports, port) tcpports = append(tcpports, port)
} }
} }
if(port_type == "udp") { if port_type == "udp" {
if !slices.Contains(udpports, port) { if !slices.Contains(udpports, port) {
tcpports = append(udpports, port) tcpports = append(udpports, port)
} }
} }
} }
var services corev1.ServiceList var services corev1.ServiceList
@@ -162,15 +162,15 @@ func (r *FirewallGroupReconciler) Reconcile(ctx context.Context, req reconcile.R
for _, portSpec := range service.Spec.Ports { for _, portSpec := range service.Spec.Ports {
log.Info(fmt.Sprintf("portSpec: %+v", portSpec)) log.Info(fmt.Sprintf("portSpec: %+v", portSpec))
log.Info(fmt.Sprintf("Port: %s %d", strconv.Itoa(int(portSpec.Port)), portSpec.Port)) log.Info(fmt.Sprintf("Port: %s %d", strconv.Itoa(int(portSpec.Port)), portSpec.Port))
if(portSpec.Protocol == "TCP") { if portSpec.Protocol == "TCP" {
if !slices.Contains(tcpports, strconv.Itoa(int(portSpec.Port))) { if !slices.Contains(tcpports, strconv.Itoa(int(portSpec.Port))) {
tcpports = append(tcpports, strconv.Itoa(int(portSpec.Port))) tcpports = append(tcpports, strconv.Itoa(int(portSpec.Port)))
} }
} }
if(portSpec.Protocol == "UDP") { if portSpec.Protocol == "UDP" {
if !slices.Contains(udpports, strconv.Itoa(int(portSpec.Port))) { if !slices.Contains(udpports, strconv.Itoa(int(portSpec.Port))) {
udpports = append(udpports, strconv.Itoa(int(portSpec.Port))) udpports = append(udpports, strconv.Itoa(int(portSpec.Port)))
} }
} }
} }
} }

View File

@@ -25,15 +25,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log"
unifiv1beta1 "github.com/vegardengen/unifi-network-operator/api/v1beta1" unifiv1beta1 "github.com/vegardengen/unifi-network-operator/api/v1beta1"
"github.com/vegardengen/unifi-network-operator/internal/unifi"
"github.com/vegardengen/unifi-network-operator/internal/config" "github.com/vegardengen/unifi-network-operator/internal/config"
"github.com/vegardengen/unifi-network-operator/internal/unifi"
) )
// FirewallRuleReconciler reconciles a FirewallRule object // FirewallRuleReconciler reconciles a FirewallRule object
type FirewallRuleReconciler struct { type FirewallRuleReconciler struct {
client.Client client.Client
Scheme *runtime.Scheme Scheme *runtime.Scheme
UnifiClient *unifi.UnifiClient UnifiClient *unifi.UnifiClient
ConfigLoader *config.ConfigLoaderType ConfigLoader *config.ConfigLoaderType
} }
@@ -57,13 +57,18 @@ func (r *FirewallRuleReconciler) Reconcile(ctx context.Context, req ctrl.Request
// TODO(user): your logic here // TODO(user): your logic here
cfg, err := r.ConfigLoader.GetConfig(ctx, "unifi-operator-config") cfg, err := r.ConfigLoader.GetConfig(ctx, "unifi-operator-config")
if err != nil { if err != nil {
return ctrl.Result{}, err return ctrl.Result{}, err
} }
defaultNs := cfg.Data["defaultNamespace"] defaultNs := cfg.Data["defaultNamespace"]
log.Info(defaultNs) log.Info(defaultNs)
err = r.UnifiClient.Reauthenticate()
if err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil return ctrl.Result{}, nil
} }

View File

@@ -19,8 +19,8 @@ package controller
import ( import (
"context" "context"
"fmt" "fmt"
"strings"
"regexp" "regexp"
"strings"
"time" "time"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
@@ -29,40 +29,39 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log"
unifiv1beta1 "github.com/vegardengen/unifi-network-operator/api/v1beta1" unifiv1beta1 "github.com/vegardengen/unifi-network-operator/api/v1beta1"
"github.com/vegardengen/unifi-network-operator/internal/unifi"
"github.com/vegardengen/unifi-network-operator/internal/config" "github.com/vegardengen/unifi-network-operator/internal/config"
"github.com/vegardengen/unifi-network-operator/internal/unifi"
) )
// FirewallZoneReconciler reconciles a FirewallZone object // FirewallZoneReconciler reconciles a FirewallZone object
type FirewallZoneReconciler struct { type FirewallZoneReconciler struct {
client.Client client.Client
Scheme *runtime.Scheme Scheme *runtime.Scheme
UnifiClient *unifi.UnifiClient UnifiClient *unifi.UnifiClient
ConfigLoader *config.ConfigLoaderType ConfigLoader *config.ConfigLoaderType
} }
func toKubeName(input string) string { func toKubeName(input string) string {
// Lowercase the input // Lowercase the input
name := strings.ToLower(input) name := strings.ToLower(input)
// Replace any non-alphanumeric characters with dashes // Replace any non-alphanumeric characters with dashes
re := regexp.MustCompile(`[^a-z0-9\-\.]+`) re := regexp.MustCompile(`[^a-z0-9\-\.]+`)
name = re.ReplaceAllString(name, "-") name = re.ReplaceAllString(name, "-")
// Trim leading and trailing non-alphanumerics // Trim leading and trailing non-alphanumerics
name = strings.Trim(name, "-.") name = strings.Trim(name, "-.")
// Ensure it's not empty and doesn't exceed 253 characters // Ensure it's not empty and doesn't exceed 253 characters
if len(name) == 0 { if len(name) == 0 {
name = "default" name = "default"
} else if len(name) > 253 { } else if len(name) > 253 {
name = name[:253] name = name[:253]
} }
return name return name
} }
// +kubebuilder:rbac:groups=unifi.engen.priv.no,resources=firewallzones,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=unifi.engen.priv.no,resources=firewallzones,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=unifi.engen.priv.no,resources=firewallzones/status,verbs=get;update;patch // +kubebuilder:rbac:groups=unifi.engen.priv.no,resources=firewallzones/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=unifi.engen.priv.no,resources=firewallzones/finalizers,verbs=update // +kubebuilder:rbac:groups=unifi.engen.priv.no,resources=firewallzones/finalizers,verbs=update
@@ -81,11 +80,16 @@ func (r *FirewallZoneReconciler) Reconcile(ctx context.Context, req ctrl.Request
log := log.FromContext(ctx) log := log.FromContext(ctx)
cfg, err := r.ConfigLoader.GetConfig(ctx, "unifi-operator-config") cfg, err := r.ConfigLoader.GetConfig(ctx, "unifi-operator-config")
if err != nil { if err != nil {
return ctrl.Result{}, err return ctrl.Result{}, err
} }
defaultNs := cfg.Data["defaultNamespace"] defaultNs := cfg.Data["defaultNamespace"]
err = r.UnifiClient.Reauthenticate()
if err != nil {
return ctrl.Result{}, err
}
var fwzCRDs unifiv1beta1.FirewallZoneList var fwzCRDs unifiv1beta1.FirewallZoneList
_ = r.List(ctx, &fwzCRDs, client.InNamespace(defaultNs)) _ = r.List(ctx, &fwzCRDs, client.InNamespace(defaultNs))
@@ -118,17 +122,17 @@ func (r *FirewallZoneReconciler) Reconcile(ctx context.Context, req ctrl.Request
for _, unifizone := range firewall_zones { for _, unifizone := range firewall_zones {
log.Info(fmt.Sprintf("%+v\n", unifizone)) log.Info(fmt.Sprintf("%+v\n", unifizone))
if _, found := firewallZoneNamesCRDs[unifizone.Name]; !found { if _, found := firewallZoneNamesCRDs[unifizone.Name]; !found {
zoneCRD := &unifiv1beta1.FirewallZone { zoneCRD := &unifiv1beta1.FirewallZone{
ObjectMeta : ctrl.ObjectMeta { ObjectMeta: ctrl.ObjectMeta{
Name: toKubeName(unifizone.Name), Name: toKubeName(unifizone.Name),
Namespace: defaultNs, Namespace: defaultNs,
}, },
Spec: unifiv1beta1.FirewallZoneSpec { Spec: unifiv1beta1.FirewallZoneSpec{
Name : unifizone.Name, Name: unifizone.Name,
ID : unifizone.ID, ID: unifizone.ID,
DefaultZone: unifizone.DefaultZone, DefaultZone: unifizone.DefaultZone,
ZoneKey : unifizone.ZoneKey, ZoneKey: unifizone.ZoneKey,
NetworkIDs : unifizone.NetworkIDs, NetworkIDs: unifizone.NetworkIDs,
}, },
} }
err := r.Create(ctx, zoneCRD) err := r.Create(ctx, zoneCRD)
@@ -136,22 +140,22 @@ func (r *FirewallZoneReconciler) Reconcile(ctx context.Context, req ctrl.Request
return ctrl.Result{RequeueAfter: 10 * time.Minute}, err return ctrl.Result{RequeueAfter: 10 * time.Minute}, err
} }
} else { } else {
for _, zoneCRD := range fwzCRDs.Items { for _, zoneCRD := range fwzCRDs.Items {
if zoneCRD.Spec.Name == unifizone.Name { if zoneCRD.Spec.Name == unifizone.Name {
zoneCRD.Spec = unifiv1beta1.FirewallZoneSpec { zoneCRD.Spec = unifiv1beta1.FirewallZoneSpec{
Name : unifizone.Name, Name: unifizone.Name,
ID : unifizone.ID, ID: unifizone.ID,
DefaultZone: unifizone.DefaultZone, DefaultZone: unifizone.DefaultZone,
ZoneKey : unifizone.ZoneKey, ZoneKey: unifizone.ZoneKey,
NetworkIDs : unifizone.NetworkIDs, NetworkIDs: unifizone.NetworkIDs,
} }
err := r.Update(ctx, &zoneCRD) err := r.Update(ctx, &zoneCRD)
if err != nil { if err != nil {
return ctrl.Result{RequeueAfter: 10 * time.Minute}, err return ctrl.Result{RequeueAfter: 10 * time.Minute}, err
} }
} }
} }
} }
} }
return ctrl.Result{RequeueAfter: 10 * time.Minute}, nil return ctrl.Result{RequeueAfter: 10 * time.Minute}, nil

View File

@@ -26,15 +26,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log"
unifiv1 "github.com/vegardengen/unifi-network-operator/api/v1beta1" unifiv1 "github.com/vegardengen/unifi-network-operator/api/v1beta1"
"github.com/vegardengen/unifi-network-operator/internal/unifi"
"github.com/vegardengen/unifi-network-operator/internal/config" "github.com/vegardengen/unifi-network-operator/internal/config"
"github.com/vegardengen/unifi-network-operator/internal/unifi"
) )
// NetworkconfigurationReconciler reconciles a Networkconfiguration object // NetworkconfigurationReconciler reconciles a Networkconfiguration object
type NetworkconfigurationReconciler struct { type NetworkconfigurationReconciler struct {
client.Client client.Client
Scheme *runtime.Scheme Scheme *runtime.Scheme
UnifiClient *unifi.UnifiClient UnifiClient *unifi.UnifiClient
ConfigLoader *config.ConfigLoaderType ConfigLoader *config.ConfigLoaderType
} }
@@ -55,17 +55,23 @@ type NetworkconfigurationReconciler struct {
func (r *NetworkconfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { func (r *NetworkconfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx) log := log.FromContext(ctx)
cfg, err := r.ConfigLoader.GetConfig(ctx, "unifi-operator-config") cfg, err := r.ConfigLoader.GetConfig(ctx, "unifi-operator-config")
if err != nil { if err != nil {
return ctrl.Result{}, err return ctrl.Result{}, err
} }
defaultNs := cfg.Data["defaultNamespace"] defaultNs := cfg.Data["defaultNamespace"]
log.Info(defaultNs) log.Info(defaultNs)
var networkCRDs unifiv1.NetworkconfigurationList var networkCRDs unifiv1.NetworkconfigurationList
if err := r.List(ctx, &networkCRDs); err != nil { if err := r.List(ctx, &networkCRDs); err != nil {
return ctrl.Result{}, err return ctrl.Result{}, err
} }
err = r.UnifiClient.Reauthenticate()
if err != nil {
return ctrl.Result{}, err
}
k8sNetworks := make(map[string]*unifiv1.Networkconfiguration) k8sNetworks := make(map[string]*unifiv1.Networkconfiguration)
for i := range networkCRDs.Items { for i := range networkCRDs.Items {
log.Info(fmt.Sprintf("Inserting network %s\n", networkCRDs.Items[i].Spec.NetworkID)) log.Info(fmt.Sprintf("Inserting network %s\n", networkCRDs.Items[i].Spec.NetworkID))