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"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/source"
|
2022-06-27 05:00:54 -04:00
|
|
|
|
|
|
|
updatev1alpha1 "github.com/edgelesssys/constellation/operators/constellation-node-operator/api/v1alpha1"
|
|
|
|
)
|
|
|
|
|
2022-06-27 07:48:40 -04:00
|
|
|
const (
|
|
|
|
nodeImageField = ".spec.nodeImage"
|
|
|
|
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-06-27 05:00:54 -04:00
|
|
|
// ScalingGroupReconciler reconciles a ScalingGroup object
|
|
|
|
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
|
2022-06-27 07:48:40 -04:00
|
|
|
//+kubebuilder:rbac:groups=update.edgeless.systems,resources=nodeimage,verbs=get;list;watch
|
2022-07-20 04:53:03 -04:00
|
|
|
//+kubebuilder:rbac:groups=update.edgeless.systems,resources=nodeimages/status,verbs=get
|
2022-06-27 05:00:54 -04:00
|
|
|
|
2022-06-27 07:48:40 -04:00
|
|
|
// Reconcile reads the latest node image from the referenced NodeImage 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)
|
|
|
|
}
|
|
|
|
var desiredNodeImage updatev1alpha1.NodeImage
|
|
|
|
if err := r.Get(ctx, client.ObjectKey{Name: desiredScalingGroup.Spec.NodeImage}, &desiredNodeImage); err != nil {
|
|
|
|
logr.Error(err, "Unable to fetch NodeImage")
|
|
|
|
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,
|
|
|
|
}
|
2022-08-05 06:17:53 -04:00
|
|
|
imagesMatch := strings.EqualFold(nodeImage, desiredNodeImage.Spec.ImageReference)
|
|
|
|
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")
|
|
|
|
if err := r.scalingGroupUpdater.SetScalingGroupImage(ctx, desiredScalingGroup.Spec.GroupID, desiredNodeImage.Spec.ImageReference); err != nil {
|
|
|
|
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 {
|
2022-06-27 07:48:40 -04:00
|
|
|
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &updatev1alpha1.ScalingGroup{}, nodeImageField, func(rawObj client.Object) []string {
|
|
|
|
// Extract the NodeImage name from the ScalingGroup Spec, if one is provided
|
|
|
|
scalingGroup := rawObj.(*updatev1alpha1.ScalingGroup)
|
|
|
|
if scalingGroup.Spec.NodeImage == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return []string{scalingGroup.Spec.NodeImage}
|
|
|
|
}); 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(
|
|
|
|
&source.Kind{Type: &updatev1alpha1.NodeImage{}},
|
|
|
|
handler.EnqueueRequestsFromMapFunc(r.findObjectsForNodeImage),
|
|
|
|
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
|
|
|
|
).
|
2022-06-27 05:00:54 -04:00
|
|
|
Complete(r)
|
|
|
|
}
|
2022-06-27 07:48:40 -04:00
|
|
|
|
|
|
|
// findObjectsForNodeImage requests reconcile calls for every scaling group referencing the node image.
|
|
|
|
func (r *ScalingGroupReconciler) findObjectsForNodeImage(nodeImage client.Object) []reconcile.Request {
|
|
|
|
attachedScalingGroups := &updatev1alpha1.ScalingGroupList{}
|
|
|
|
listOps := &client.ListOptions{
|
|
|
|
FieldSelector: fields.OneTermEqualSelector(nodeImageField, nodeImage.GetName()),
|
|
|
|
}
|
|
|
|
if err := r.List(context.TODO(), attachedScalingGroups, listOps); err != nil {
|
|
|
|
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
|
|
|
|
}
|