-
Notifications
You must be signed in to change notification settings - Fork 917
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add replicas syncer for resources with HPA
Signed-off-by: lxtywypc <[email protected]>
- Loading branch information
Showing
15 changed files
with
403 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
artifacts/deploy/karmada-controller-manager-cert-secret.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: controller-manager-cert | ||
namespace: karmada-system | ||
type: kubernetes.io/tls | ||
data: | ||
tls.crt: | | ||
{{server_certificate}} | ||
tls.key: | | ||
{{server_key}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package replicassyncer | ||
|
||
import ( | ||
"context" | ||
|
||
autoscalingv1 "k8s.io/api/autoscaling/v1" | ||
autoscalingv2 "k8s.io/api/autoscaling/v2" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/scale" | ||
"k8s.io/klog/v2" | ||
controllerruntime "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/builder" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
) | ||
|
||
type ReplicasSyncer struct { | ||
hostClient client.Client | ||
mapper meta.RESTMapper | ||
scaleClient scale.ScalesGetter | ||
|
||
GenericChan chan<- event.GenericEvent | ||
} | ||
|
||
func NewReplicasSyncer(hostClient client.Client, mapper meta.RESTMapper, scaleClient scale.ScalesGetter) *ReplicasSyncer { | ||
return &ReplicasSyncer{ | ||
hostClient: hostClient, | ||
mapper: mapper, | ||
scaleClient: scaleClient, | ||
} | ||
} | ||
|
||
func (r *ReplicasSyncer) SetupWithManager(mgr controllerruntime.Manager) error { | ||
ch := make(chan event.GenericEvent) | ||
r.GenericChan = ch | ||
return controllerruntime.NewControllerManagedBy(mgr).Named("replicas-syncer"). | ||
For(&autoscalingv2.HorizontalPodAutoscaler{}, | ||
builder.WithPredicates(HPAPredicate)). | ||
WatchesRawSource(&source.Channel{Source: ch}, &handler.EnqueueRequestForObject{}, builder.WithPredicates(HPAPredicate)). | ||
Complete(r) | ||
} | ||
|
||
func (r *ReplicasSyncer) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) { | ||
klog.V(4).Infof("Reconciling for HPA %s/%s", req.Namespace, req.Name) | ||
|
||
hpa := &autoscalingv2.HorizontalPodAutoscaler{} | ||
err := r.hostClient.Get(ctx, req.NamespacedName, hpa) | ||
if err != nil { | ||
if errors.IsNotFound(err) { | ||
return controllerruntime.Result{}, nil | ||
} | ||
|
||
return controllerruntime.Result{}, err | ||
} | ||
|
||
workloadGR, scale, err := GetGroupResourceAndScaleForWorkloadFromHPA(ctx, r.mapper, r.scaleClient, hpa) | ||
if err != nil { | ||
return controllerruntime.Result{}, err | ||
} | ||
|
||
// If the scale of workload is not found, skip processing. | ||
if scale == nil { | ||
klog.Infof("Scale of resource(kind=%s, %s/%s) not found, the resource might have been removed, skip", | ||
hpa.Spec.ScaleTargetRef.Kind, hpa.Namespace, hpa.Spec.ScaleTargetRef.Name) | ||
|
||
return controllerruntime.Result{}, nil | ||
} | ||
|
||
err = r.updateScaleIfNeed(ctx, workloadGR, scale, hpa) | ||
if err != nil { | ||
return controllerruntime.Result{}, err | ||
} | ||
|
||
return controllerruntime.Result{}, nil | ||
} | ||
|
||
// updateScaleIfNeed would update the scale of workload on fed-control plane | ||
// if the replicas decared in the workload on fed-control-plane dose not match | ||
// the actual replicas in member clusters effected by HPA. | ||
func (r *ReplicasSyncer) updateScaleIfNeed(ctx context.Context, workloadGR schema.GroupResource, scale *autoscalingv1.Scale, hpa *autoscalingv2.HorizontalPodAutoscaler) error { | ||
if scale.Spec.Replicas != hpa.Status.CurrentReplicas { | ||
oldReplicas := scale.Spec.Replicas | ||
|
||
scale.Spec.Replicas = hpa.Status.CurrentReplicas | ||
_, err := r.scaleClient.Scales(hpa.Namespace).Update(ctx, workloadGR, scale, metav1.UpdateOptions{}) | ||
if err != nil { | ||
klog.Errorf("Failed to try to sync scale for resource(kind=%s, %s/%s) from %d to %d: %v", | ||
hpa.Spec.ScaleTargetRef.Kind, hpa.Namespace, hpa.Spec.ScaleTargetRef.Name, oldReplicas, hpa.Status.CurrentReplicas, err) | ||
return err | ||
} | ||
|
||
klog.Infof("Successfully synced scale for resource(kind=%s, %s/%s) from %d to %d", | ||
hpa.Spec.ScaleTargetRef.Kind, hpa.Namespace, hpa.Spec.ScaleTargetRef.Name, oldReplicas, hpa.Status.CurrentReplicas) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package replicassyncer | ||
|
||
// TODO(@lxtywypc): Add unit test. |
Oops, something went wrong.