initial commit
Some checks failed
Lint / Run on Ubuntu (push) Has been cancelled
E2E Tests / Run on Ubuntu (push) Has been cancelled
Tests / Run on Ubuntu (push) Has been cancelled

This commit is contained in:
2026-04-13 00:40:35 +02:00
commit 168e9c6db2
63 changed files with 4270 additions and 0 deletions

View File

@@ -0,0 +1,146 @@
/*
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 (
"bytes"
"context"
"fmt"
"net"
"slices"
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"
)
// 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
}
// Snapshot before mutation
patchBase := resolution.DeepCopy()
addrs, err := net.LookupHost(resolution.Spec.Address)
if err != nil {
log.Error(err, "error while resolving resolution")
return ctrl.Result{}, err
}
// Build new desired status (IMPORTANT: no append)
var ips []net.IP
for _, addr := range addrs {
if ip := net.ParseIP(addr); ip != nil {
ips = append(ips, ip)
}
}
slices.SortStableFunc(ips, func(a, b net.IP) int {
for i := range 15 {
if a[i] < b[i] {
return -1
}
if a[i] > b[i] {
return 1
}
}
return 0
})
// check if both ip address slices are the same
if slices.EqualFunc(patchBase.Status.IPAddresses, ips, func(s1, s2 net.IP) bool {
return bytes.Equal(s1, s2)
}) {
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
// if err := r.Patch(ctx, &resolution, client.Merge); err != nil {
// return ctrl.Result{}, err
// }
// r := &net.Resolver{
// PreferGo: true,
// Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
// d := net.Dialer{
// Timeout: time.Millisecond * time.Duration(10000),
// }
// return d.DialContext(ctx, network, "8.8.8.8:53")
// },
// }
// ip, _ := r.LookupHost(context.Background(), "www.google.com")
}
// 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)
}