formatting

This commit is contained in:
2025-04-10 00:28:40 +02:00
parent 2f4ddc132e
commit 2485002584
4 changed files with 162 additions and 167 deletions

View File

@@ -46,7 +46,6 @@ type NetworkconfigurationSpec struct {
SettingPreference string `json:"setting_preference,omitempty"` SettingPreference string `json:"setting_preference,omitempty"`
Vlan int64 `json:"vlan,omitempty"` Vlan int64 `json:"vlan,omitempty"`
VlanEnabled bool `json:"vlan_enabled,omitempty"` VlanEnabled bool `json:"vlan_enabled,omitempty"`
} }
// NetworkconfigurationStatus defines the observed state of Networkconfiguration. // NetworkconfigurationStatus defines the observed state of Networkconfiguration.

View File

@@ -18,8 +18,8 @@ package controller
import ( import (
"context" "context"
"net"
"fmt" "fmt"
"net"
"reflect" "reflect"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
@@ -27,8 +27,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log"
unifiv1beta1 "github.com/vegardengen/unifi-network-operator/api/v1beta1"
goUnifi "github.com/vegardengen/go-unifi/unifi" goUnifi "github.com/vegardengen/go-unifi/unifi"
unifiv1beta1 "github.com/vegardengen/unifi-network-operator/api/v1beta1"
"github.com/vegardengen/unifi-network-operator/internal/unifi" "github.com/vegardengen/unifi-network-operator/internal/unifi"
) )
@@ -53,8 +53,6 @@ type FirewallGroupReconciler struct {
// For more details, check Reconcile and its Result here: // For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.20.2/pkg/reconcile // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.20.2/pkg/reconcile
func (r *FirewallGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { func (r *FirewallGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx) log := log.FromContext(ctx)
var nwObj unifiv1beta1.FirewallGroup var nwObj unifiv1beta1.FirewallGroup
@@ -64,7 +62,7 @@ func (r *FirewallGroupReconciler) Reconcile(ctx context.Context, req ctrl.Reques
log.Info(nwObj.Spec.Name) log.Info(nwObj.Spec.Name)
var ipv4, ipv6 []string var ipv4, ipv6 []string
for _,addressEntry := range nwObj.Spec.ManualAddresses { for _, addressEntry := range nwObj.Spec.ManualAddresses {
ip := net.ParseIP(addressEntry) ip := net.ParseIP(addressEntry)
if ip != nil { if ip != nil {
@@ -82,32 +80,32 @@ func (r *FirewallGroupReconciler) Reconcile(ctx context.Context, req ctrl.Reques
log.Info(fmt.Sprintf("Ipv4 Net: %s", net)) log.Info(fmt.Sprintf("Ipv4 Net: %s", net))
ipv4 = append(ipv4, addressEntry) ipv4 = append(ipv4, addressEntry)
} else { } else {
mask,_ := net.Mask.Size() mask, _ := net.Mask.Size()
log.Info(fmt.Sprintf("Ipv6 Net: %s", net)) log.Info(fmt.Sprintf("Ipv6 Net: %s", net))
ipv6 = append(ipv6, addr.Mask(net.Mask).String() + "/" + fmt.Sprint(mask)) ipv6 = append(ipv6, addr.Mask(net.Mask).String()+"/"+fmt.Sprint(mask))
} }
} else { } else {
log.Error(err,fmt.Sprintf("Could not parse: %s", addressEntry)) log.Error(err, fmt.Sprintf("Could not parse: %s", addressEntry))
return ctrl.Result{}, err return ctrl.Result{}, err
} }
} }
} }
firewall_groups, err := r.UnifiClient.Client.ListFirewallGroup(context.Background(), r.UnifiClient.SiteID) firewall_groups, err := r.UnifiClient.Client.ListFirewallGroup(context.Background(), r.UnifiClient.SiteID)
if err != nil { if err != nil {
log.Error(err,"Could not list network objects") log.Error(err, "Could not list network objects")
return ctrl.Result{}, err return ctrl.Result{}, err
} }
ipv4_name := "k8s-"+nwObj.Spec.Name+"-ipv4" ipv4_name := "k8s-" + nwObj.Spec.Name + "-ipv4"
ipv6_name := "k8s-"+nwObj.Spec.Name+"-ipv6" ipv6_name := "k8s-" + nwObj.Spec.Name + "-ipv6"
ipv4_done := false ipv4_done := false
ipv6_done := false ipv6_done := false
for _,firewall_group := range firewall_groups { for _, firewall_group := range firewall_groups {
if firewall_group.Name == ipv4_name { if firewall_group.Name == ipv4_name {
if(len(ipv4) == 0) { if len(ipv4) == 0 {
log.Info(fmt.Sprintf("Delete %s", ipv4_name)) log.Info(fmt.Sprintf("Delete %s", ipv4_name))
err := r.UnifiClient.Client.DeleteFirewallGroup(context.Background(), r.UnifiClient.SiteID, firewall_group.ID) err := r.UnifiClient.Client.DeleteFirewallGroup(context.Background(), r.UnifiClient.SiteID, firewall_group.ID)
if err != nil { if err != nil {
log.Error(err,"Could not delete firewall group") log.Error(err, "Could not delete firewall group")
return ctrl.Result{}, err return ctrl.Result{}, err
} }
ipv4_done = true ipv4_done = true
@@ -117,7 +115,7 @@ func (r *FirewallGroupReconciler) Reconcile(ctx context.Context, req ctrl.Reques
log.Info(fmt.Sprintf("Updating %s", ipv4_name)) log.Info(fmt.Sprintf("Updating %s", ipv4_name))
_, err := r.UnifiClient.Client.UpdateFirewallGroup(context.Background(), r.UnifiClient.SiteID, &firewall_group) _, err := r.UnifiClient.Client.UpdateFirewallGroup(context.Background(), r.UnifiClient.SiteID, &firewall_group)
if err != nil { if err != nil {
log.Error(err,"Could not update firewall group") log.Error(err, "Could not update firewall group")
return ctrl.Result{}, err return ctrl.Result{}, err
} }
} }
@@ -125,11 +123,11 @@ func (r *FirewallGroupReconciler) Reconcile(ctx context.Context, req ctrl.Reques
} }
} }
if firewall_group.Name == ipv6_name { if firewall_group.Name == ipv6_name {
if(len(ipv6) == 0) { if len(ipv6) == 0 {
log.Info(fmt.Sprintf("Delete %s", ipv6_name)) log.Info(fmt.Sprintf("Delete %s", ipv6_name))
err := r.UnifiClient.Client.DeleteFirewallGroup(context.Background(), r.UnifiClient.SiteID, firewall_group.ID) err := r.UnifiClient.Client.DeleteFirewallGroup(context.Background(), r.UnifiClient.SiteID, firewall_group.ID)
if err != nil { if err != nil {
log.Error(err,"Could not delete firewall group") log.Error(err, "Could not delete firewall group")
return ctrl.Result{}, err return ctrl.Result{}, err
} }
ipv6_done = true ipv6_done = true
@@ -139,7 +137,7 @@ func (r *FirewallGroupReconciler) Reconcile(ctx context.Context, req ctrl.Reques
log.Info(fmt.Sprintf("Updating %s", ipv6_name)) log.Info(fmt.Sprintf("Updating %s", ipv6_name))
_, err := r.UnifiClient.Client.UpdateFirewallGroup(context.Background(), r.UnifiClient.SiteID, &firewall_group) _, err := r.UnifiClient.Client.UpdateFirewallGroup(context.Background(), r.UnifiClient.SiteID, &firewall_group)
if err != nil { if err != nil {
log.Error(err,"Could not update firewall group") log.Error(err, "Could not update firewall group")
return ctrl.Result{}, err return ctrl.Result{}, err
} }
} }
@@ -150,26 +148,26 @@ func (r *FirewallGroupReconciler) Reconcile(ctx context.Context, req ctrl.Reques
if len(ipv4) > 0 && !ipv4_done { if len(ipv4) > 0 && !ipv4_done {
log.Info(fmt.Sprintf("Creating %s", ipv4_name)) log.Info(fmt.Sprintf("Creating %s", ipv4_name))
var firewall_group goUnifi.FirewallGroup var firewall_group goUnifi.FirewallGroup
firewall_group.Name=ipv4_name firewall_group.Name = ipv4_name
firewall_group.SiteID=r.UnifiClient.SiteID firewall_group.SiteID = r.UnifiClient.SiteID
firewall_group.GroupMembers = ipv4 firewall_group.GroupMembers = ipv4
firewall_group.GroupType = "address-group" firewall_group.GroupType = "address-group"
_, err := r.UnifiClient.Client.CreateFirewallGroup(context.Background(), r.UnifiClient.SiteID, &firewall_group) _, err := r.UnifiClient.Client.CreateFirewallGroup(context.Background(), r.UnifiClient.SiteID, &firewall_group)
if err != nil { if err != nil {
log.Error(err,"Could not create firewall group") log.Error(err, "Could not create firewall group")
return ctrl.Result{}, err return ctrl.Result{}, err
} }
} }
if len(ipv6) > 0 && !ipv6_done { if len(ipv6) > 0 && !ipv6_done {
log.Info(fmt.Sprintf("Creating %s", ipv6_name)) log.Info(fmt.Sprintf("Creating %s", ipv6_name))
var firewall_group goUnifi.FirewallGroup var firewall_group goUnifi.FirewallGroup
firewall_group.Name=ipv6_name firewall_group.Name = ipv6_name
firewall_group.SiteID=r.UnifiClient.SiteID firewall_group.SiteID = r.UnifiClient.SiteID
firewall_group.GroupMembers = ipv6 firewall_group.GroupMembers = ipv6
firewall_group.GroupType = "ipv6-address-group" firewall_group.GroupType = "ipv6-address-group"
_, err := r.UnifiClient.Client.CreateFirewallGroup(context.Background(), r.UnifiClient.SiteID, &firewall_group) _, err := r.UnifiClient.Client.CreateFirewallGroup(context.Background(), r.UnifiClient.SiteID, &firewall_group)
if err != nil { if err != nil {
log.Error(err,"Could not create firewall group") log.Error(err, "Could not create firewall group")
return ctrl.Result{}, err return ctrl.Result{}, err
} }
} }

View File

@@ -62,23 +62,21 @@ func (r *NetworkconfigurationReconciler) Reconcile(ctx context.Context, req ctrl
k8sNetworks[networkCRDs.Items[i].Spec.NetworkID] = &networkCRDs.Items[i] k8sNetworks[networkCRDs.Items[i].Spec.NetworkID] = &networkCRDs.Items[i]
} }
networks, err := r.UnifiClient.Client.ListNetwork(context.Background(), r.UnifiClient.SiteID) networks, err := r.UnifiClient.Client.ListNetwork(context.Background(), r.UnifiClient.SiteID)
if err != nil { if err != nil {
log.Error(err,"Failed to list Unifi Networks") log.Error(err, "Failed to list Unifi Networks")
return ctrl.Result{}, err return ctrl.Result{}, err
} }
seenNetworks := map[string]bool{} seenNetworks := map[string]bool{}
for _,network := range networks { for _, network := range networks {
networkID := network.ID networkID := network.ID
seenNetworks[networkID] = true seenNetworks[networkID] = true
log.Info(fmt.Sprintf("Searching for %s\n",networkID)) log.Info(fmt.Sprintf("Searching for %s\n", networkID))
if existing, found := k8sNetworks[networkID]; found { if existing, found := k8sNetworks[networkID]; found {
log.Info(fmt.Sprintf("Found network match: %s/%s", existing.Spec.NetworkID,networkID)) log.Info(fmt.Sprintf("Found network match: %s/%s", existing.Spec.NetworkID, networkID))
} else { } else {
log.Info(fmt.Sprintf("New network: %s with ID %s", network.Name, network.ID)) log.Info(fmt.Sprintf("New network: %s with ID %s", network.Name, network.ID))
} }