diff --git a/api/v1beta1/firewallgroup_types.go b/api/v1beta1/firewallgroup_types.go index d6b93e9..7430711 100644 --- a/api/v1beta1/firewallgroup_types.go +++ b/api/v1beta1/firewallgroup_types.go @@ -36,7 +36,7 @@ type FirewallGroupSpec struct { // ManualAddresses is a list of manual IPs or CIDRs (IPv4 or IPv6) // +optional ManualAddresses []string `json:"manualAddresses,omitempty"` - ManualPorts []string `json:"manualPorts,omitempty"` + ManualPorts []string `json:"manualPorts,omitempty"` // AutoIncludeSelector defines which services to extract addresses from // +optional diff --git a/api/v1beta1/firewallzone_types.go b/api/v1beta1/firewallzone_types.go index 6290e5e..34dc4e7 100644 --- a/api/v1beta1/firewallzone_types.go +++ b/api/v1beta1/firewallzone_types.go @@ -28,11 +28,11 @@ type FirewallZoneSpec struct { // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster // Important: Run "make" to regenerate code after modifying this file - Name string `json:"name,omitempty"` - ID string `json:"_id,omitempty"` - DefaultZone bool `json:"default_zone,omitempty"` - ZoneKey string `json:"zone_key,omitempty"` - NetworkIDs []string `json:"network_ids,omitempty"` + Name string `json:"name,omitempty"` + ID string `json:"_id,omitempty"` + DefaultZone bool `json:"default_zone,omitempty"` + ZoneKey string `json:"zone_key,omitempty"` + NetworkIDs []string `json:"network_ids,omitempty"` } // FirewallZoneStatus defines the observed state of FirewallZone. diff --git a/api/v1beta1/networkconfiguration_types.go b/api/v1beta1/networkconfiguration_types.go index 8db86f5..23eaee9 100644 --- a/api/v1beta1/networkconfiguration_types.go +++ b/api/v1beta1/networkconfiguration_types.go @@ -51,16 +51,16 @@ type NetworkconfigurationSpec struct { type NetworkconfigurationStatus struct { // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster // Important: Run "make" to regenerate code after modifying this file - FirewallZoneID string `json:"firewall_zone_id,omitempty"` - Ipv6SubnetStatus string `json:"ipv6_subnet_status,omitempty"` + FirewallZoneID string `json:"firewall_zone_id,omitempty"` + Ipv6SubnetStatus string `json:"ipv6_subnet_status,omitempty"` // SyncedWithUnifi indicates whether the addresses are successfully pushed - // +optional - SyncedWithUnifi bool `json:"syncedWithUnifi,omitempty"` + // +optional + SyncedWithUnifi bool `json:"syncedWithUnifi,omitempty"` // LastSyncTime is the last time the object was synced - // +optional - LastSyncTime *metav1.Time `json:"lastSyncTime,omitempty"` + // +optional + LastSyncTime *metav1.Time `json:"lastSyncTime,omitempty"` } // +kubebuilder:object:root=true diff --git a/cmd/main.go b/cmd/main.go index fd19fe3..23ce8e1 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -38,9 +38,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/webhook" unifiv1beta1 "github.com/vegardengen/unifi-network-operator/api/v1beta1" + "github.com/vegardengen/unifi-network-operator/internal/config" "github.com/vegardengen/unifi-network-operator/internal/controller" "github.com/vegardengen/unifi-network-operator/internal/unifi" - "github.com/vegardengen/unifi-network-operator/internal/config" // +kubebuilder:scaffold:imports ) @@ -204,7 +204,7 @@ func main() { os.Exit(1) } - configLoader := config.NewConfigLoader(mgr.GetClient()) + configLoader := config.NewConfigLoader(mgr.GetClient()) // Unifi client setupLog.Info("Setting up UniFi client") @@ -216,27 +216,27 @@ func main() { setupLog.Info("Finished Setting up UniFi client") if err = (&controller.NetworkconfigurationReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - UnifiClient: unifiClient, + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + UnifiClient: unifiClient, ConfigLoader: configLoader, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Networkconfiguration") os.Exit(1) } if err = (&controller.FirewallZoneReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - UnifiClient: unifiClient, + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + UnifiClient: unifiClient, ConfigLoader: configLoader, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "FirewallZone") os.Exit(1) } if err = (&controller.FirewallRuleReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - UnifiClient: unifiClient, + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + UnifiClient: unifiClient, ConfigLoader: configLoader, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "FirewallRule") @@ -245,9 +245,9 @@ func main() { // +kubebuilder:scaffold:builder if err = (&controller.FirewallGroupReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - UnifiClient: unifiClient, + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + UnifiClient: unifiClient, ConfigLoader: configLoader, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "FirewallGroup") diff --git a/internal/config/config.go b/internal/config/config.go index 61a28f9..37bc9d5 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,45 +1,44 @@ package config import ( - "context" - "sync" + "context" + "sync" - corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/client" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" ) type ConfigLoaderType struct { - Client client.Client + Client client.Client - mu sync.Mutex - loaded bool - config *corev1.ConfigMap - err error + mu sync.Mutex + loaded bool + config *corev1.ConfigMap + err error } func NewConfigLoader(k8sClient client.Client) *ConfigLoaderType { - return &ConfigLoaderType{Client: k8sClient} + return &ConfigLoaderType{Client: k8sClient} } func (c *ConfigLoaderType) GetConfig(ctx context.Context, name string) (*corev1.ConfigMap, error) { - c.mu.Lock() - defer c.mu.Unlock() + c.mu.Lock() + defer c.mu.Unlock() - if c.loaded { - return c.config, c.err - } + if c.loaded { + return c.config, c.err + } - cm := &corev1.ConfigMap{} - err := c.Client.Get(ctx, types.NamespacedName{ - Name: name, - Namespace: "unifi-network-operator-system", - }, cm) + cm := &corev1.ConfigMap{} + err := c.Client.Get(ctx, types.NamespacedName{ + Name: name, + Namespace: "unifi-network-operator-system", + }, cm) - c.loaded = true - c.config = cm - c.err = err + c.loaded = true + c.config = cm + c.err = err - return cm, err + return cm, err } - diff --git a/internal/unifi_network_operator_utils/unifi_network_operator_utils.go b/internal/unifi_network_operator_utils/unifi_network_operator_utils.go new file mode 100644 index 0000000..40ad783 --- /dev/null +++ b/internal/unifi_network_operator_utils/unifi_network_operator_utils.go @@ -0,0 +1,33 @@ +/* https://github.com/clbx/kube-port-forward-controller */ + +package unifi_network_operator_utils + +import ( + "regexp" + "strings" +) + +func isIPv6(ip string) bool { + return strings.Contains(ip, ":") +} + +func toKubeName(input string) string { + // Lowercase the input + name := strings.ToLower(input) + + // Replace any non-alphanumeric characters with dashes + re := regexp.MustCompile(`[^a-z0-9\-\.]+`) + name = re.ReplaceAllString(name, "-") + + // Trim leading and trailing non-alphanumerics + name = strings.Trim(name, "-.") + + // Ensure it's not empty and doesn't exceed 253 characters + if len(name) == 0 { + name = "default" + } else if len(name) > 253 { + name = name[:253] + } + + return name +}