Update networks and patch Ips

This commit is contained in:
superflo22 2025-04-12 13:00:54 +02:00
parent 264fdb756d
commit 8dcc2cf71a

View file

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
dnsv1alpha1 "git.mayers.cloud/superflo22/split-horizon-operator/api/v1alpha1" dnsv1alpha1 "git.mayers.cloud/superflo22/split-horizon-operator/api/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/handler"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
"strings" "strings"
@ -61,16 +62,15 @@ func (r *ExternalDNSWatcherReconciler) Reconcile(ctx context.Context, req ctrl.R
// Step 2: Check annotation // Step 2: Check annotation
annotations := gateway.GetAnnotations() annotations := gateway.GetAnnotations()
if annotations["dns.mayers.cloud/enabled"] != "true" { authority := annotations["dns.mayers.cloud/authority"]
log.Info("ExternalDNS watcher is not enabled") if authority == "" {
log.Info("Missing required authority annotation")
return ctrl.Result{}, nil return ctrl.Result{}, nil
} }
network := annotations["dns.mayers.cloud/network"] network := annotations["dns.mayers.cloud/network"]
authority := annotations["dns.mayers.cloud/authority"] if network == "" {
if network == "" || authority == "" { log.Info("Missing network annotation assuming default network")
log.Info("Missing required annotations") network = "default"
return ctrl.Result{}, nil
} }
// Step 3: Get Gateway IP (if available) // Step 3: Get Gateway IP (if available)
@ -88,19 +88,28 @@ func (r *ExternalDNSWatcherReconciler) Reconcile(ctx context.Context, req ctrl.R
} }
for _, route := range routes.Items { for _, route := range routes.Items {
for _, parent := range route.Spec.ParentRefs {
// Check if the parent is a Gateway and matches the current gateway
if string(*parent.Kind) == "Gateway" && string(parent.Name) == gateway.Name {
// Step 5: Collect hostnames // Step 5: Collect hostnames
for _, hostname := range route.Spec.Hostnames { for _, hostname := range route.Spec.Hostnames {
// Naming convention for TechnitiumRecord
recordName := fmt.Sprintf("%s", strings.ReplaceAll(string(hostname), ".", "-"))
// Bestehendes TechnitiumRecord abrufen
var existingRecord dnsv1alpha1.TechnitiumRecord
err := r.Client.Get(ctx, client.ObjectKey{
Namespace: route.Namespace,
Name: recordName,
}, &existingRecord)
if err != nil {
log.Info("Creating new Record", "recordName", recordName)
record := &dnsv1alpha1.TechnitiumRecord{ record := &dnsv1alpha1.TechnitiumRecord{
TypeMeta: metav1.TypeMeta{ TypeMeta: metav1.TypeMeta{
Kind: "TechnitiumRecord", Kind: "TechnitiumRecord",
APIVersion: "dns.mayers.cloud/v1alpha1", APIVersion: "dns.mayers.cloud/v1alpha1",
}, },
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s", strings.ReplaceAll(string(hostname), ".", "-")), Name: recordName,
Namespace: route.Namespace, Namespace: route.Namespace,
}, },
Spec: dnsv1alpha1.TechnitiumRecordSpec{ Spec: dnsv1alpha1.TechnitiumRecordSpec{
@ -116,14 +125,38 @@ func (r *ExternalDNSWatcherReconciler) Reconcile(ctx context.Context, req ctrl.R
}, },
} }
// Log the record yaml // Log the record yaml
log.Info("TechnitiumRecord", "record", record) log.V(1).Info("TechnitiumRecord Manifest", "record", record)
if err := r.Client.Patch(ctx, record, client.Apply, client.ForceOwnership, client.FieldOwner("external-dns-watcher")); err != nil { if err := r.Client.Patch(ctx, record, client.Apply, client.ForceOwnership, client.FieldOwner("external-dns-watcher")); err != nil {
log.Error(err, "Failed to apply TechnitiumRecord") log.Error(err, "Failed to apply TechnitiumRecord")
} else { } else {
log.Info("Reconciled TechnitiumRecord", "name", record.Name) log.Info("Created new Record", "name", record.Name)
} }
} }
// Step 6: Update existing TechnitiumRecord
if existingRecord.Spec.RecordData[network] != nil {
// Check if the IPs are different
if !reflect.DeepEqual(existingRecord.Spec.RecordData[network], gatewayIPs) {
log.Info("Updating existing Record", "recordName", recordName)
existingRecord.Spec.RecordData[network] = gatewayIPs
if err := r.Update(ctx, &existingRecord); err != nil {
log.Error(err, "Failed to update TechnitiumRecord")
} else {
log.Info("Updated IPs in existing Record", "name", recordName)
}
}
} else {
log.Info("Adding new network to existing Record", "recordName", recordName)
// Add the new network and IPs
existingRecord.Spec.RecordData[network] = gatewayIPs
if err := r.Update(ctx, &existingRecord); err != nil {
log.Error(err, "Failed to update TechnitiumRecord")
} else {
log.Info("Updated new network in existing Record", "name", recordName)
}
} }
} }
} }