|
| 1 | +/* |
| 2 | +Copyright 2024 IBM Corporation. |
| 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 appwrapper |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + v1 "k8s.io/api/core/v1" |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 25 | + "k8s.io/apimachinery/pkg/util/sets" |
| 26 | + |
| 27 | + ctrl "sigs.k8s.io/controller-runtime" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 31 | + |
| 32 | + "github.com/project-codeflare/appwrapper/pkg/config" |
| 33 | +) |
| 34 | + |
| 35 | +// NodeHealthMonitor maintains the set of nodes that Autopilot has labelled as unhealthy |
| 36 | +type NodeHealthMonitor struct { |
| 37 | + client.Client |
| 38 | + Config *config.AppWrapperConfig |
| 39 | +} |
| 40 | + |
| 41 | +// unhealthyNodes is a mapping from Node names to a set of resources that Autopilot has labeled as unhealthy on that Node |
| 42 | +var unhealthyNodes = make(map[string]sets.Set[string]) |
| 43 | + |
| 44 | +// permission to watch nodes |
| 45 | +//+kubebuilder:rbac:groups="",resources=nodes,verbs=get;list;watch |
| 46 | + |
| 47 | +func (r *NodeHealthMonitor) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 48 | + node := &metav1.PartialObjectMetadata{} |
| 49 | + node.SetGroupVersionKind(schema.GroupVersionKind{ |
| 50 | + Group: "", |
| 51 | + Version: "v1", |
| 52 | + Kind: "Node", |
| 53 | + }) |
| 54 | + if err := r.Get(ctx, req.NamespacedName, node); err != nil { |
| 55 | + return ctrl.Result{}, nil |
| 56 | + } |
| 57 | + |
| 58 | + log.FromContext(ctx).V(2).Info("Reconcilling", "node", req.NamespacedName) |
| 59 | + |
| 60 | + flaggedResources := make(sets.Set[string]) |
| 61 | + for key, value := range node.GetLabels() { |
| 62 | + for resource, apLabels := range r.Config.Autopilot.ResourceUnhealthyConfig { |
| 63 | + if apValue, ok := apLabels[key]; ok && apValue == value { |
| 64 | + flaggedResources.Insert(resource) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + hadEntries := len(flaggedResources) > 0 |
| 70 | + |
| 71 | + if len(flaggedResources) == 0 { |
| 72 | + delete(unhealthyNodes, node.GetName()) |
| 73 | + } else { |
| 74 | + unhealthyNodes[node.GetName()] = flaggedResources |
| 75 | + } |
| 76 | + |
| 77 | + if len(unhealthyNodes) == 0 { |
| 78 | + if hadEntries { |
| 79 | + log.FromContext(ctx).Info("All nodes now healthy") |
| 80 | + } else { |
| 81 | + log.FromContext(ctx).V(2).Info("All nodes now healthy") |
| 82 | + } |
| 83 | + } else { |
| 84 | + log.FromContext(ctx).Info("Some nodes unhealthy", "number", len(unhealthyNodes), "details", unhealthyNodes) |
| 85 | + } |
| 86 | + |
| 87 | + return ctrl.Result{}, nil |
| 88 | +} |
| 89 | + |
| 90 | +// SetupWithManager sets up the controller with the Manager. |
| 91 | +func (r *NodeHealthMonitor) SetupWithManager(mgr ctrl.Manager) error { |
| 92 | + return ctrl.NewControllerManagedBy(mgr). |
| 93 | + WatchesMetadata(&v1.Node{}, &handler.EnqueueRequestForObject{}). |
| 94 | + Named("NodeMonitor"). |
| 95 | + Complete(r) |
| 96 | +} |
0 commit comments