|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/go-logr/logr" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 26 | + "k8s.io/apimachinery/pkg/types" |
| 27 | + corev1client "k8s.io/client-go/kubernetes/typed/core/v1" |
| 28 | + "k8s.io/klog/v2" |
| 29 | + kopsapi "k8s.io/kops/pkg/apis/kops/v1alpha2" |
| 30 | + ctrl "sigs.k8s.io/controller-runtime" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 33 | +) |
| 34 | + |
| 35 | +// NewMetalIPAMReconciler is the constructor for a MetalIPAMReconciler |
| 36 | +func NewMetalIPAMReconciler(ctx context.Context, mgr manager.Manager) (*MetalIPAMReconciler, error) { |
| 37 | + klog.Info("starting metal ipam controller") |
| 38 | + r := &MetalIPAMReconciler{ |
| 39 | + client: mgr.GetClient(), |
| 40 | + log: ctrl.Log.WithName("controllers").WithName("metal_ipam"), |
| 41 | + } |
| 42 | + |
| 43 | + coreClient, err := corev1client.NewForConfig(mgr.GetConfig()) |
| 44 | + if err != nil { |
| 45 | + return nil, fmt.Errorf("building corev1 client: %w", err) |
| 46 | + } |
| 47 | + r.coreV1Client = coreClient |
| 48 | + |
| 49 | + return r, nil |
| 50 | +} |
| 51 | + |
| 52 | +// MetalIPAMReconciler observes Node objects, assigning their `PodCIDRs` from the instance's `ExternalIpv6`. |
| 53 | +type MetalIPAMReconciler struct { |
| 54 | + // client is the controller-runtime client |
| 55 | + client client.Client |
| 56 | + |
| 57 | + // log is a logr |
| 58 | + log logr.Logger |
| 59 | + |
| 60 | + // coreV1Client is a client-go client for patching nodes |
| 61 | + coreV1Client *corev1client.CoreV1Client |
| 62 | +} |
| 63 | + |
| 64 | +// +kubebuilder:rbac:groups=,resources=nodes,verbs=get;list;watch;patch |
| 65 | +// Reconcile is the main reconciler function that observes node changes. |
| 66 | +func (r *MetalIPAMReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 67 | + node := &corev1.Node{} |
| 68 | + if err := r.client.Get(ctx, req.NamespacedName, node); err != nil { |
| 69 | + klog.Warningf("unable to fetch node %s: %v", node.Name, err) |
| 70 | + if apierrors.IsNotFound(err) { |
| 71 | + // we'll ignore not-found errors, since they can't be fixed by an immediate |
| 72 | + // requeue (we'll need to wait for a new notification), and we can get them |
| 73 | + // on deleted requests. |
| 74 | + return ctrl.Result{}, nil |
| 75 | + } |
| 76 | + return ctrl.Result{}, err |
| 77 | + } |
| 78 | + |
| 79 | + host := &kopsapi.Host{} |
| 80 | + id := types.NamespacedName{ |
| 81 | + Namespace: "kops-system", |
| 82 | + Name: node.Name, |
| 83 | + } |
| 84 | + if err := r.client.Get(ctx, id, host); err != nil { |
| 85 | + klog.Warningf("unable to fetch host %s: %v", id, err) |
| 86 | + return ctrl.Result{}, err |
| 87 | + } |
| 88 | + |
| 89 | + if len(node.Spec.PodCIDRs) == 0 { |
| 90 | + if err := patchNodePodCIDRs(r.coreV1Client, ctx, node, host.Spec.PodCIDRs); err != nil { |
| 91 | + return ctrl.Result{}, err |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return ctrl.Result{}, nil |
| 96 | +} |
| 97 | + |
| 98 | +func (r *MetalIPAMReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 99 | + return ctrl.NewControllerManagedBy(mgr). |
| 100 | + Named("metal_ipam"). |
| 101 | + For(&corev1.Node{}). |
| 102 | + Complete(r) |
| 103 | +} |
0 commit comments