2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-27 05:00:54 -04:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-05 06:17:53 -04:00
|
|
|
"strings"
|
2022-06-27 05:00:54 -04:00
|
|
|
|
2022-06-27 07:48:40 -04:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2022-06-27 05:00:54 -04:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2022-06-27 07:48:40 -04:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2022-06-27 05:00:54 -04:00
|
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
2022-06-27 07:48:40 -04:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/builder"
|
2022-06-27 05:00:54 -04:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
2022-06-27 07:48:40 -04:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/handler"
|
2022-06-27 05:00:54 -04:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
2022-06-27 07:48:40 -04:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/predicate"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
2022-06-27 05:00:54 -04:00
|
|
|
|
2023-01-04 13:04:28 -05:00
|
|
|
updatev1alpha1 "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/api/v1alpha1"
|
2022-06-27 05:00:54 -04:00
|
|
|
)
|
|
|
|
|
2022-06-27 07:48:40 -04:00
|
|
|
const (
|
2023-01-03 06:09:53 -05:00
|
|
|
nodeVersionField = ".spec.nodeVersion"
|
2022-06-27 07:48:40 -04:00
|
|
|
conditionScalingGroupUpToDateReason = "ScalingGroupNodeImageUpToDate"
|
|
|
|
conditionScalingGroupUpToDateMessage = "Scaling group will use the latest image when creating new nodes"
|
|
|
|
conditionScalingGroupOutOfDateReason = "ScalingGroupNodeImageOutOfDate"
|
|
|
|
conditionScalingGroupOutOfDateMessage = "Scaling group will not use the latest image when creating new nodes"
|
|
|
|
)
|
|
|
|
|
2022-09-12 07:45:31 -04:00
|
|
|
// ScalingGroupReconciler reconciles a ScalingGroup object.
|
2022-06-27 05:00:54 -04:00
|
|
|
type ScalingGroupReconciler struct {
|
2022-06-27 07:48:40 -04:00
|
|
|
scalingGroupUpdater
|
2022-06-27 05:00:54 -04:00
|
|
|
client.Client
|
|
|
|
Scheme *runtime.Scheme
|
|
|
|
}
|
|
|
|
|
2022-06-27 07:48:40 -04:00
|
|
|
// NewScalingGroupReconciler returns a new ScalingGroupReconciler.
|
|
|
|
func NewScalingGroupReconciler(scalingGroupUpdater scalingGroupUpdater, client client.Client, scheme *runtime.Scheme) *ScalingGroupReconciler {
|
|
|
|
return &ScalingGroupReconciler{
|
|
|
|
scalingGroupUpdater: scalingGroupUpdater,
|
|
|
|
Client: client,
|
|
|
|
Scheme: scheme,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 05:00:54 -04:00
|
|
|
//+kubebuilder:rbac:groups=update.edgeless.systems,resources=scalinggroups,verbs=get;list;watch;create;update;patch;delete
|
|
|
|
//+kubebuilder:rbac:groups=update.edgeless.systems,resources=scalinggroups/status,verbs=get;update;patch
|
|
|
|
//+kubebuilder:rbac:groups=update.edgeless.systems,resources=scalinggroups/finalizers,verbs=update
|
2023-01-03 06:09:53 -05:00
|
|
|
//+kubebuilder:rbac:groups=update.edgeless.systems,resources=nodeversion,verbs=get;list;watch
|
|
|
|
//+kubebuilder:rbac:groups=update.edgeless.systems,resources=nodeversion/status,verbs=get
|
2022-06-27 05:00:54 -04:00
|
|
|
|
2023-01-03 06:09:53 -05:00
|
|
|
// Reconcile reads the latest node image from the referenced NodeVersion spec and updates the scaling group to match.
|
2022-06-27 05:00:54 -04:00
|
|
|
func (r *ScalingGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
2022-06-27 07:48:40 -04:00
|
|
|
logr := log.FromContext(ctx)
|
|
|
|
|
|
|
|
var desiredScalingGroup updatev1alpha1.ScalingGroup
|
|
|
|
if err := r.Get(ctx, req.NamespacedName, &desiredScalingGroup); err != nil {
|
|
|
|
logr.Error(err, "Unable to fetch ScalingGroup")
|
|
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
|
|
}
|
2023-01-03 06:09:53 -05:00
|
|
|
var desiredNodeVersion updatev1alpha1.NodeVersion
|
|
|
|
if err := r.Get(ctx, client.ObjectKey{Name: desiredScalingGroup.Spec.NodeVersion}, &desiredNodeVersion); err != nil {
|
|
|
|
logr.Error(err, "Unable to fetch NodeVersion")
|
2022-06-27 07:48:40 -04:00
|
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
|
|
}
|
|
|
|
nodeImage, err := r.scalingGroupUpdater.GetScalingGroupImage(ctx, desiredScalingGroup.Spec.GroupID)
|
|
|
|
if err != nil {
|
|
|
|
logr.Error(err, "Unable to get ScalingGroup NodeImage")
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
}
|
2022-06-27 05:00:54 -04:00
|
|
|
|
2022-06-27 07:48:40 -04:00
|
|
|
desiredScalingGroup.Status.ImageReference = nodeImage
|
|
|
|
outdatedCondition := metav1.Condition{
|
|
|
|
Type: updatev1alpha1.ConditionOutdated,
|
|
|
|
}
|
2023-01-03 06:09:53 -05:00
|
|
|
imagesMatch := strings.EqualFold(nodeImage, desiredNodeVersion.Spec.ImageReference)
|
2022-08-05 06:17:53 -04:00
|
|
|
if imagesMatch {
|
2022-06-27 07:48:40 -04:00
|
|
|
outdatedCondition.Status = metav1.ConditionFalse
|
|
|
|
outdatedCondition.Reason = conditionScalingGroupUpToDateReason
|
|
|
|
outdatedCondition.Message = conditionScalingGroupUpToDateMessage
|
|
|
|
} else {
|
|
|
|
outdatedCondition.Status = metav1.ConditionTrue
|
|
|
|
outdatedCondition.Reason = conditionScalingGroupOutOfDateReason
|
|
|
|
outdatedCondition.Message = conditionScalingGroupOutOfDateMessage
|
|
|
|
}
|
|
|
|
meta.SetStatusCondition(&desiredScalingGroup.Status.Conditions, outdatedCondition)
|
|
|
|
if err := r.Status().Update(ctx, &desiredScalingGroup); err != nil {
|
|
|
|
logr.Error(err, "Unable to update AutoscalingStrategy status")
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
}
|
2022-06-27 05:00:54 -04:00
|
|
|
|
2022-08-05 06:17:53 -04:00
|
|
|
if !imagesMatch {
|
2022-06-27 07:48:40 -04:00
|
|
|
logr.Info("ScalingGroup NodeImage is out of date")
|
2023-01-03 06:09:53 -05:00
|
|
|
if err := r.scalingGroupUpdater.SetScalingGroupImage(ctx, desiredScalingGroup.Spec.GroupID, desiredNodeVersion.Spec.ImageReference); err != nil {
|
2022-06-27 07:48:40 -04:00
|
|
|
logr.Error(err, "Unable to set ScalingGroup NodeImage")
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
}
|
|
|
|
// requeue to update status
|
|
|
|
return ctrl.Result{Requeue: true}, nil
|
|
|
|
}
|
2022-06-27 05:00:54 -04:00
|
|
|
return ctrl.Result{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupWithManager sets up the controller with the Manager.
|
|
|
|
func (r *ScalingGroupReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
2023-01-03 06:09:53 -05:00
|
|
|
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &updatev1alpha1.ScalingGroup{}, nodeVersionField, func(rawObj client.Object) []string {
|
|
|
|
// Extract the NodeVersion name from the ScalingGroup Spec, if one is provided
|
2022-06-27 07:48:40 -04:00
|
|
|
scalingGroup := rawObj.(*updatev1alpha1.ScalingGroup)
|
2023-01-03 06:09:53 -05:00
|
|
|
if scalingGroup.Spec.NodeVersion == "" {
|
2022-06-27 07:48:40 -04:00
|
|
|
return nil
|
|
|
|
}
|
2023-01-03 06:09:53 -05:00
|
|
|
return []string{scalingGroup.Spec.NodeVersion}
|
2022-06-27 07:48:40 -04:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-27 05:00:54 -04:00
|
|
|
return ctrl.NewControllerManagedBy(mgr).
|
|
|
|
For(&updatev1alpha1.ScalingGroup{}).
|
2022-06-27 07:48:40 -04:00
|
|
|
Watches(
|
2023-05-24 12:57:45 -04:00
|
|
|
client.Object(&updatev1alpha1.NodeVersion{}),
|
2023-01-03 06:09:53 -05:00
|
|
|
handler.EnqueueRequestsFromMapFunc(r.findObjectsForNodeVersion),
|
2022-06-27 07:48:40 -04:00
|
|
|
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
|
|
|
|
).
|
2022-06-27 05:00:54 -04:00
|
|
|
Complete(r)
|
|
|
|
}
|
2022-06-27 07:48:40 -04:00
|
|
|
|
2023-01-03 06:09:53 -05:00
|
|
|
// findObjectsForNodeVersion requests reconcile calls for every scaling group referencing the node image.
|
2023-05-24 12:57:45 -04:00
|
|
|
func (r *ScalingGroupReconciler) findObjectsForNodeVersion(ctx context.Context, nodeVersion client.Object) []reconcile.Request {
|
2022-06-27 07:48:40 -04:00
|
|
|
attachedScalingGroups := &updatev1alpha1.ScalingGroupList{}
|
|
|
|
listOps := &client.ListOptions{
|
2023-01-03 06:09:53 -05:00
|
|
|
FieldSelector: fields.OneTermEqualSelector(nodeVersionField, nodeVersion.GetName()),
|
2022-06-27 07:48:40 -04:00
|
|
|
}
|
2023-05-24 12:57:45 -04:00
|
|
|
if err := r.List(ctx, attachedScalingGroups, listOps); err != nil {
|
2022-06-27 07:48:40 -04:00
|
|
|
return []reconcile.Request{}
|
|
|
|
}
|
|
|
|
|
|
|
|
requests := make([]reconcile.Request, len(attachedScalingGroups.Items))
|
|
|
|
for i, item := range attachedScalingGroups.Items {
|
|
|
|
requests[i] = reconcile.Request{
|
|
|
|
NamespacedName: types.NamespacedName{Name: item.GetName()},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return requests
|
|
|
|
}
|
|
|
|
|
|
|
|
type scalingGroupUpdater interface {
|
|
|
|
GetScalingGroupImage(ctx context.Context, scalingGroupID string) (string, error)
|
|
|
|
SetScalingGroupImage(ctx context.Context, scalingGroupID, imageURI string) error
|
|
|
|
}
|