2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-29 08:48:40 -04:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-01-08 07:19:38 -05:00
|
|
|
node "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/internal/node"
|
2022-06-29 08:48:40 -04:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/event"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/predicate"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
|
|
|
|
2023-02-14 12:46:48 -05:00
|
|
|
nodemaintenancev1beta1 "github.com/edgelesssys/constellation/v2/3rdparty/node-maintenance-operator/api/v1beta1"
|
2024-01-08 07:19:38 -05:00
|
|
|
updatev1alpha1 "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/api/v1alpha1"
|
2022-06-29 08:48:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// scalingGroupImageChangedPredicate checks if a scaling group has adopted a new node image for future nodes.
|
|
|
|
func scalingGroupImageChangedPredicate() predicate.Predicate {
|
|
|
|
return predicate.Funcs{
|
|
|
|
UpdateFunc: func(e event.UpdateEvent) bool {
|
|
|
|
oldScalingGroup, ok := e.ObjectOld.(*updatev1alpha1.ScalingGroup)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
newScalingGroup, ok := e.ObjectNew.(*updatev1alpha1.ScalingGroup)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return oldScalingGroup.Status.ImageReference != newScalingGroup.Status.ImageReference
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// autoscalerEnabledStatusChangedPredicate checks if the autoscaler was either enabled or disabled.
|
|
|
|
func autoscalerEnabledStatusChangedPredicate() predicate.Predicate {
|
|
|
|
return predicate.Funcs{
|
|
|
|
UpdateFunc: func(e event.UpdateEvent) bool {
|
|
|
|
oldAutoscalingStrat, ok := e.ObjectOld.(*updatev1alpha1.AutoscalingStrategy)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
newAutoscalingStrat, ok := e.ObjectNew.(*updatev1alpha1.AutoscalingStrategy)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return oldAutoscalingStrat.Status.Enabled != newAutoscalingStrat.Status.Enabled
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nodeReadyPredicate checks if a node became ready or acquired a providerID.
|
|
|
|
func nodeReadyPredicate() predicate.Predicate {
|
|
|
|
return predicate.Funcs{
|
|
|
|
UpdateFunc: func(e event.UpdateEvent) bool {
|
|
|
|
oldNode, ok := e.ObjectOld.(*corev1.Node)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
newNode, ok := e.ObjectNew.(*corev1.Node)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
becameReady := !node.Ready(oldNode) && node.Ready(newNode)
|
|
|
|
receivedProviderID := len(oldNode.Spec.ProviderID) == 0 && len(newNode.Spec.ProviderID) != 0
|
|
|
|
return becameReady || receivedProviderID
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nodeMaintenanceSucceededPredicate checks if a node maintenance resource switched its status to "maintenance succeeded".
|
|
|
|
func nodeMaintenanceSucceededPredicate() predicate.Predicate {
|
|
|
|
return predicate.Funcs{
|
|
|
|
UpdateFunc: func(e event.UpdateEvent) bool {
|
|
|
|
oldNode, ok := e.ObjectOld.(*nodemaintenancev1beta1.NodeMaintenance)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
newNode, ok := e.ObjectNew.(*nodemaintenancev1beta1.NodeMaintenance)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
succeeded := oldNode.Status.Phase != nodemaintenancev1beta1.MaintenanceSucceeded &&
|
|
|
|
newNode.Status.Phase == nodemaintenancev1beta1.MaintenanceSucceeded
|
|
|
|
return succeeded
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 06:08:25 -05:00
|
|
|
// joiningNodeDeletedPredicate checks if a joining node was deleted.
|
|
|
|
func joiningNodeDeletedPredicate() predicate.Predicate {
|
|
|
|
return predicate.Funcs{
|
|
|
|
DeleteFunc: func(e event.DeleteEvent) bool {
|
|
|
|
_, ok := e.Object.(*updatev1alpha1.JoiningNode)
|
|
|
|
return ok
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 08:48:40 -04:00
|
|
|
// findObjectsForScalingGroup requests a reconcile call for the node image referenced by a scaling group.
|
2023-05-24 12:57:45 -04:00
|
|
|
func (r *NodeVersionReconciler) findObjectsForScalingGroup(_ context.Context, rawScalingGroup client.Object) []reconcile.Request {
|
2022-06-29 08:48:40 -04:00
|
|
|
scalingGroup := rawScalingGroup.(*updatev1alpha1.ScalingGroup)
|
|
|
|
return []reconcile.Request{
|
2023-01-03 06:09:53 -05:00
|
|
|
{NamespacedName: types.NamespacedName{Name: scalingGroup.Spec.NodeVersion}},
|
2022-06-29 08:48:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 06:09:53 -05:00
|
|
|
// findAllNodeVersions requests a reconcile call for all node versions.
|
2023-05-24 12:57:45 -04:00
|
|
|
func (r *NodeVersionReconciler) findAllNodeVersions(ctx context.Context, _ client.Object) []reconcile.Request {
|
2023-01-03 06:09:53 -05:00
|
|
|
var nodeVersionList updatev1alpha1.NodeVersionList
|
2023-05-24 12:57:45 -04:00
|
|
|
err := r.List(ctx, &nodeVersionList)
|
2022-06-29 08:48:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return []reconcile.Request{}
|
|
|
|
}
|
2023-01-03 06:09:53 -05:00
|
|
|
requests := make([]reconcile.Request, len(nodeVersionList.Items))
|
|
|
|
for i, item := range nodeVersionList.Items {
|
2022-06-29 08:48:40 -04:00
|
|
|
requests[i] = reconcile.Request{
|
|
|
|
NamespacedName: types.NamespacedName{Name: item.GetName()},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return requests
|
|
|
|
}
|