|
| 1 | +/* |
| 2 | +Copyright 2025 the Unikorn 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 health |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "iter" |
| 24 | + |
| 25 | + unikornv1 "github.com/unikorn-cloud/core/pkg/apis/unikorn/v1alpha1" |
| 26 | + "github.com/unikorn-cloud/core/pkg/cd" |
| 27 | + |
| 28 | + corev1 "k8s.io/api/core/v1" |
| 29 | + |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | +) |
| 32 | + |
| 33 | +var ( |
| 34 | + ErrTypeConversion = errors.New("type conversion error") |
| 35 | +) |
| 36 | + |
| 37 | +// Lister is a generic inteface for operating on and iterating over resource |
| 38 | +// lists (as no such interface is required by apimachinery. |
| 39 | +type Lister[T unikornv1.ManagableResourceInterface] interface { |
| 40 | + client.ObjectList |
| 41 | + All() iter.Seq[T] |
| 42 | +} |
| 43 | + |
| 44 | +// Checker lists all resources of the specified type and does a health check on it. |
| 45 | +// The type itself is constrained to a manageable resource so we can get the label selector |
| 46 | +// to pass to the CD layer to get all applications for the resource, then once we have |
| 47 | +// checked the status of those applications we can set the condition generically, again |
| 48 | +// as provided by the manageable resource interface. |
| 49 | +type Checker[T unikornv1.ManagableResourceInterface, L Lister[T]] struct { |
| 50 | + // client allows access to Kubernetes resources. |
| 51 | + client client.Client |
| 52 | + // driver is the CD driver. |
| 53 | + driver cd.Driver |
| 54 | + // l is storage for the manageable resource list. |
| 55 | + l L |
| 56 | +} |
| 57 | + |
| 58 | +// New creates a new checker. All types can be inferred, the template parameters |
| 59 | +// are purely for type constraints. |
| 60 | +func New[T unikornv1.ManagableResourceInterface, L Lister[T]](client client.Client, driver cd.Driver, l L) *Checker[T, L] { |
| 61 | + return &Checker[T, L]{ |
| 62 | + client: client, |
| 63 | + driver: driver, |
| 64 | + l: l, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// resourceIdentifierFromResource takes our manageable resource type and returns |
| 69 | +// a CD resource ID to identify its applications. |
| 70 | +func resourceIdentifierFromResource(r unikornv1.ManagableResourceInterface) (*cd.ResourceIdentifier, error) { |
| 71 | + labels, err := r.ResourceLabels() |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + id := &cd.ResourceIdentifier{ |
| 77 | + Labels: make([]cd.ResourceIdentifierLabel, 0, len(labels)), |
| 78 | + } |
| 79 | + |
| 80 | + for k, v := range labels { |
| 81 | + id.Labels = append(id.Labels, cd.ResourceIdentifierLabel{ |
| 82 | + Name: k, |
| 83 | + Value: v, |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + return id, nil |
| 88 | +} |
| 89 | + |
| 90 | +// convertHealthStatus translates from the CD interface to the Kubernetes API. |
| 91 | +func convertHealthStatus(status cd.HealthStatus) (corev1.ConditionStatus, unikornv1.ConditionReason, string) { |
| 92 | + switch status { |
| 93 | + case cd.HealthStatusUnknown: |
| 94 | + return corev1.ConditionUnknown, unikornv1.ConditionReasonUnknown, "unable to poll application status" |
| 95 | + case cd.HealthStatusHealthy: |
| 96 | + return corev1.ConditionTrue, unikornv1.ConditionReasonHealthy, "resource applications healthy" |
| 97 | + case cd.HealthStatusDegraded: |
| 98 | + return corev1.ConditionFalse, unikornv1.ConditionReasonDegraded, "one or more resource applications are degraded" |
| 99 | + } |
| 100 | + |
| 101 | + // NOTE: the linter will warn about non-exhaustive switches. |
| 102 | + return corev1.ConditionUnknown, unikornv1.ConditionReasonUnknown, "unreachable code reached" |
| 103 | +} |
| 104 | + |
| 105 | +// check does the actual check for a resource and updates its status. |
| 106 | +func (c *Checker[T, L]) check(ctx context.Context, r unikornv1.ManagableResourceInterface) error { |
| 107 | + // Grab the overall health status. |
| 108 | + id, err := resourceIdentifierFromResource(r) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + // TODO: we only support argo now, but will need an abstraction down the line. |
| 114 | + // There is precedent in the main controllers. |
| 115 | + healthStatus, err := c.driver.GetHealthStatus(ctx, id) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + updated, ok := r.DeepCopyObject().(unikornv1.ManagableResourceInterface) |
| 121 | + if !ok { |
| 122 | + return fmt.Errorf("%w: unable to deep copy manageable resource", ErrTypeConversion) |
| 123 | + } |
| 124 | + |
| 125 | + // And finally set the status condition. |
| 126 | + status, reason, message := convertHealthStatus(healthStatus) |
| 127 | + |
| 128 | + updated.StatusConditionWrite(unikornv1.ConditionHealthy, status, reason, message) |
| 129 | + |
| 130 | + if err := c.client.Status().Patch(ctx, updated, client.MergeFrom(r)); err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +// Check does the actual check as described for the health checker type. |
| 138 | +func (c *Checker[T, L]) Check(ctx context.Context) error { |
| 139 | + // NOTE: This looks expensive, but it's all cached by controller-runtime. |
| 140 | + if err := c.client.List(ctx, c.l, &client.ListOptions{}); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + for o := range c.l.All() { |
| 145 | + if err := c.check(ctx, o); err != nil { |
| 146 | + return err |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return nil |
| 151 | +} |
0 commit comments