use controller runtime createorupdate
This commit is contained in:
parent
8dcc2cf71a
commit
b09fc9028d
1 changed files with 12 additions and 21 deletions
|
|
@ -21,7 +21,6 @@ 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"
|
||||||
|
|
@ -100,6 +99,7 @@ func (r *ExternalDNSWatcherReconciler) Reconcile(ctx context.Context, req ctrl.R
|
||||||
Namespace: route.Namespace,
|
Namespace: route.Namespace,
|
||||||
Name: recordName,
|
Name: recordName,
|
||||||
}, &existingRecord)
|
}, &existingRecord)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("Creating new Record", "recordName", recordName)
|
log.Info("Creating new Record", "recordName", recordName)
|
||||||
|
|
||||||
|
|
@ -132,32 +132,23 @@ func (r *ExternalDNSWatcherReconciler) Reconcile(ctx context.Context, req ctrl.R
|
||||||
} else {
|
} else {
|
||||||
log.Info("Created new Record", "name", record.Name)
|
log.Info("Created new Record", "name", record.Name)
|
||||||
}
|
}
|
||||||
|
return ctrl.Result{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 6: Update existing TechnitiumRecord
|
// 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)
|
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
|
// Add the new network and IPs
|
||||||
|
_, err = ctrl.CreateOrUpdate(ctx, r.Client, &existingRecord, func() error {
|
||||||
existingRecord.Spec.RecordData[network] = gatewayIPs
|
existingRecord.Spec.RecordData[network] = gatewayIPs
|
||||||
if err := r.Update(ctx, &existingRecord); err != nil {
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
log.Error(err, "Failed to update TechnitiumRecord")
|
log.Error(err, "Failed to update TechnitiumRecord")
|
||||||
} else {
|
} else {
|
||||||
log.Info("Updated new network in existing Record", "name", recordName)
|
log.Info("Updated existing Record", "name", recordName)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue