init hpa controller framework (#141)

Signed-off-by: chenxianpao <chenxianpao@huawei.com>
This commit is contained in:
Xianpao Chen 2021-01-26 10:12:06 +08:00 committed by GitHub
parent aa3d5b1c6a
commit f684ef0371
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 0 deletions

View File

@ -16,6 +16,7 @@ import (
"github.com/karmada-io/karmada/pkg/controllers/binding"
"github.com/karmada-io/karmada/pkg/controllers/cluster"
"github.com/karmada-io/karmada/pkg/controllers/execution"
"github.com/karmada-io/karmada/pkg/controllers/hpa"
"github.com/karmada-io/karmada/pkg/controllers/propagationpolicy"
"github.com/karmada-io/karmada/pkg/controllers/status"
"github.com/karmada-io/karmada/pkg/util/gclient"
@ -101,6 +102,14 @@ func setupControllers(mgr controllerruntime.Manager, stopChan <-chan struct{}) {
klog.Fatalf("Failed to setup clusterstatus controller: %v", err)
}
hpaController := &hpa.HorizontalPodAutoscalerController{
Client: mgr.GetClient(),
EventRecorder: mgr.GetEventRecorderFor(hpa.ControllerName),
}
if err := hpaController.SetupWithManager(mgr); err != nil {
klog.Fatalf("Failed to setup hpa controller: %v", err)
}
policyController := &propagationpolicy.Controller{
Client: mgr.GetClient(),
DynamicClient: dynamicClientSet,

View File

@ -0,0 +1,50 @@
package hpa
import (
"context"
autoscalingv1 "k8s.io/api/autoscaling/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// ControllerName is the controller name that will be used when reporting events.
const ControllerName = "hpa-controller"
// HorizontalPodAutoscalerController is to sync HorizontalPodAutoscaler.
type HorizontalPodAutoscalerController struct {
client.Client // used to operate HorizontalPodAutoscaler resources.
EventRecorder record.EventRecorder
}
// Reconcile performs a full reconciliation for the object referred to by the Request.
// The Controller will requeue the Request to be processed again if an error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (c *HorizontalPodAutoscalerController) Reconcile(req controllerruntime.Request) (controllerruntime.Result, error) {
klog.V(4).Infof("Reconciling HorizontalPodAutoscaler %s.", req.NamespacedName.String())
hpa := &autoscalingv1.HorizontalPodAutoscaler{}
if err := c.Client.Get(context.TODO(), req.NamespacedName, hpa); err != nil {
// The resource may no longer exist, in which case we stop processing.
if errors.IsNotFound(err) {
return controllerruntime.Result{}, nil
}
return controllerruntime.Result{Requeue: true}, err
}
if !hpa.DeletionTimestamp.IsZero() {
// Do nothing, just return.
return controllerruntime.Result{}, nil
}
return controllerruntime.Result{}, nil
}
// SetupWithManager creates a controller and register to controller manager.
func (c *HorizontalPodAutoscalerController) SetupWithManager(mgr controllerruntime.Manager) error {
return controllerruntime.NewControllerManagedBy(mgr).For(&autoscalingv1.HorizontalPodAutoscaler{}).Complete(c)
}