190 lines
5.0 KiB
Go
190 lines
5.0 KiB
Go
/*
|
|
Copyright 2026.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package controller
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"slices"
|
|
"time"
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
logf "sigs.k8s.io/controller-runtime/pkg/log"
|
|
|
|
dnsv1 "stinnesbeck.com/dns/api/v1"
|
|
v1 "stinnesbeck.com/dns/api/v1"
|
|
)
|
|
|
|
// ResolutionReconciler reconciles a Resolution object
|
|
type ResolutionReconciler struct {
|
|
client.Client
|
|
Scheme *runtime.Scheme
|
|
}
|
|
|
|
// +kubebuilder:rbac:groups=dns.stinnesbeck.com,resources=resolutions,verbs=get;list;watch;create;update;patch;delete
|
|
// +kubebuilder:rbac:groups=dns.stinnesbeck.com,resources=resolutions/status,verbs=get;update;patch
|
|
// +kubebuilder:rbac:groups=dns.stinnesbeck.com,resources=resolutions/finalizers,verbs=update
|
|
|
|
// Reconcile is part of the main kubernetes reconciliation loop which aims to
|
|
// move the current state of the cluster closer to the desired state.
|
|
// TODO(user): Modify the Reconcile function to compare the state specified by
|
|
// the Resolution object against the actual cluster state, and then
|
|
// perform operations to make the cluster state reflect the state specified by
|
|
// the user.
|
|
//
|
|
// For more details, check Reconcile and its Result here:
|
|
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.23.3/pkg/reconcile
|
|
func (r *ResolutionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
|
log := logf.FromContext(ctx)
|
|
|
|
var resolution dnsv1.Resolution
|
|
|
|
switch err := r.Get(ctx, req.NamespacedName, &resolution); {
|
|
case apierrors.IsNotFound(err):
|
|
// resolution was deleted, this is expected
|
|
// exit reconcile loop here
|
|
return ctrl.Result{}, nil
|
|
|
|
case err != nil:
|
|
// an error occurred this is not expected
|
|
log.Error(err, "error while retrieving resolution")
|
|
return ctrl.Result{}, err
|
|
}
|
|
|
|
resolver := net.DefaultResolver
|
|
|
|
// check if a resolver was given
|
|
if resolution.Spec.Resolver != nil {
|
|
customResolver, err := r.getResolver(ctx, req, &resolution)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
|
|
resolution.Status.Resolver = resolution.Spec.Resolver
|
|
resolver = customResolver
|
|
}
|
|
|
|
// Snapshot before mutation
|
|
patchBase := resolution.DeepCopy()
|
|
|
|
var addrs []string
|
|
|
|
addrs, err := resolver.LookupHost(ctx, resolution.Spec.Address)
|
|
if err != nil {
|
|
log.Error(err, "error while resolving resolution")
|
|
return ctrl.Result{}, err
|
|
}
|
|
|
|
// Build new desired status
|
|
var ips []net.IP
|
|
for _, addr := range addrs {
|
|
if ip := net.ParseIP(addr); ip != nil {
|
|
ips = append(ips, ip)
|
|
}
|
|
}
|
|
|
|
// sort to make states comparable
|
|
slices.SortStableFunc(ips, stableSortIPs)
|
|
|
|
// check if both ip address slices are the same
|
|
if slices.EqualFunc(patchBase.Status.IPAddresses, ips, equalIPs) {
|
|
return ctrl.Result{}, nil
|
|
}
|
|
|
|
log.Info(fmt.Sprintf("found the following IP addresses: %v", ips))
|
|
|
|
resolution.Status.IPAddresses = ips
|
|
|
|
// Only patch status (clean + minimal diff)
|
|
if err := r.Status().Patch(ctx, &resolution, client.MergeFrom(patchBase)); err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
|
|
return ctrl.Result{}, nil
|
|
}
|
|
|
|
func (r *ResolutionReconciler) getResolver(ctx context.Context, req ctrl.Request, resolution *v1.Resolution) (*net.Resolver, error) {
|
|
var resolver v1.Resolver
|
|
|
|
resolverFQDN := client.ObjectKey{
|
|
Namespace: req.Namespace,
|
|
Name: *resolution.Spec.Resolver,
|
|
}
|
|
|
|
if err := r.Get(ctx, resolverFQDN, &resolver); err != nil {
|
|
// retrieval of resolver failed
|
|
return nil, err
|
|
}
|
|
|
|
netResolver := net.Resolver{
|
|
PreferGo: true,
|
|
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
d := net.Dialer{
|
|
Timeout: 3 * time.Second,
|
|
}
|
|
return d.DialContext(ctx, network, fmt.Sprintf("%s:%d", resolver.Spec.IPAddress, resolver.Spec.Port))
|
|
},
|
|
}
|
|
|
|
return &netResolver, nil
|
|
|
|
// // Snapshot before mutation
|
|
// patchBase := resolution.DeepCopy()
|
|
|
|
// ip, _ := netResolver.LookupHost(context.Background(), resolution.Spec.Address)
|
|
|
|
// if err := r.Status().Patch(ctx, resolution, client.MergeFrom(patchBase)); err != nil {
|
|
// return ctrl.Result{}, err
|
|
// }
|
|
|
|
// return ctrl.Result{}, nil
|
|
}
|
|
|
|
func equalIPs(a, b net.IP) bool {
|
|
return a.Equal(b)
|
|
}
|
|
|
|
func stableSortIPs(a, b net.IP) int {
|
|
if len(a) != len(b) {
|
|
a, b = a.To16(), b.To16()
|
|
}
|
|
|
|
for i := range a {
|
|
if a[i] < b[i] {
|
|
return -1
|
|
}
|
|
|
|
if a[i] > b[i] {
|
|
return 1
|
|
}
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
// SetupWithManager sets up the controller with the Manager.
|
|
func (r *ResolutionReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
|
return ctrl.NewControllerManagedBy(mgr).
|
|
For(&dnsv1.Resolution{}).
|
|
Named("resolution").
|
|
Complete(r)
|
|
}
|