mirror of
https://github.com/rancher/k3k.git
synced 2026-03-03 18:20:53 +00:00
* Adding cluster set types Adds types for cluster sets, which allows constraining a few elements of clusters including: overall resource usage, and which nodes it can use. * Add networkpolicy to clustersets Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * Fix comments Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * Fix linting issues Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * fixing node controller logic and nit fixes Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * more fixes Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * fix main cli Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> * Comment the resource quota for clustersets Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> --------- Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com> Co-authored-by: Michael Bolot <michael.bolot@suse.com>
79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
package clusterset
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/rancher/k3k/pkg/apis/k3k.io/v1alpha1"
|
|
"github.com/rancher/k3k/pkg/controller/util"
|
|
v1 "k8s.io/api/core/v1"
|
|
networkingv1 "k8s.io/api/networking/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
|
|
"sigs.k8s.io/controller-runtime/pkg/controller"
|
|
"sigs.k8s.io/controller-runtime/pkg/manager"
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
|
)
|
|
|
|
const (
|
|
nodeController = "k3k-node-controller"
|
|
)
|
|
|
|
type NodeReconciler struct {
|
|
Client ctrlruntimeclient.Client
|
|
Scheme *runtime.Scheme
|
|
ClusterCIDR string
|
|
}
|
|
|
|
// AddNodeController adds a new controller to the manager
|
|
func AddNodeController(ctx context.Context, mgr manager.Manager) error {
|
|
// initialize a new Reconciler
|
|
reconciler := NodeReconciler{
|
|
Client: mgr.GetClient(),
|
|
Scheme: mgr.GetScheme(),
|
|
}
|
|
|
|
return ctrl.NewControllerManagedBy(mgr).
|
|
For(&v1.Node{}).
|
|
WithOptions(controller.Options{
|
|
MaxConcurrentReconciles: maxConcurrentReconciles,
|
|
}).
|
|
Named(nodeController).
|
|
Complete(&reconciler)
|
|
}
|
|
|
|
func (n *NodeReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
|
|
var clusterSetList v1alpha1.ClusterSetList
|
|
if err := n.Client.List(ctx, &clusterSetList); err != nil {
|
|
return reconcile.Result{}, util.LogAndReturnErr("failed to list clusterSets", err)
|
|
}
|
|
|
|
if len(clusterSetList.Items) <= 0 {
|
|
return reconcile.Result{}, nil
|
|
}
|
|
|
|
if err := n.ensureNetworkPolicies(ctx, clusterSetList); err != nil {
|
|
return reconcile.Result{}, err
|
|
}
|
|
|
|
return reconcile.Result{}, nil
|
|
}
|
|
|
|
func (n *NodeReconciler) ensureNetworkPolicies(ctx context.Context, clusterSetList v1alpha1.ClusterSetList) error {
|
|
var setNetworkPolicy *networkingv1.NetworkPolicy
|
|
for _, cs := range clusterSetList.Items {
|
|
if cs.Spec.DisableNetworkPolicy {
|
|
continue
|
|
}
|
|
var err error
|
|
setNetworkPolicy, err = netpol(ctx, "", &cs, n.Client)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := n.Client.Update(ctx, setNetworkPolicy); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|