-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multi-target support v2 #211
Open
gchaviaras-NS1
wants to merge
1
commit into
kubernetes-sigs:master
Choose a base branch
from
gchaviaras-NS1:multi-target-support-rebased
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
1 change: 1 addition & 0 deletions
1
charts/cluster-proportional-autoscaler/templates/clusterrole.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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
--- | ||
kind: ClusterRole | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
metadata: | ||
|
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
--- | ||
kind: Role | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
metadata: | ||
|
1 change: 1 addition & 0 deletions
1
charts/cluster-proportional-autoscaler/templates/serviceaccount.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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{{- if .Values.serviceAccount.create -}} | ||
--- | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
|
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 |
---|---|---|
|
@@ -51,12 +51,12 @@ type K8sClient interface { | |
// GetNamespace returns the namespace of target resource. | ||
GetNamespace() (namespace string) | ||
// UpdateReplicas updates the number of replicas for the resource and return the previous replicas count | ||
UpdateReplicas(expReplicas int32) (prevReplicas int32, err error) | ||
UpdateReplicas(expReplicas int32) (err error) | ||
} | ||
|
||
// k8sClient - Wraps all Kubernetes API client functionalities | ||
type k8sClient struct { | ||
target *scaleTarget | ||
scaleTargets *scaleTargets | ||
clientset *kubernetes.Clientset | ||
clusterStatus *ClusterStatus | ||
nodeStore cache.Store | ||
|
@@ -77,7 +77,7 @@ func NewK8sClient(namespace, target string, nodelabels string) (K8sClient, error | |
return nil, err | ||
} | ||
|
||
scaleTarget, err := getScaleTarget(target, namespace) | ||
scaleTargets, err := getScaleTargets(target, namespace) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -99,33 +99,51 @@ func NewK8sClient(namespace, target string, nodelabels string) (K8sClient, error | |
go reflector.Run(stopCh) | ||
|
||
return &k8sClient{ | ||
target: scaleTarget, | ||
clientset: clientset, | ||
nodeStore: nodeStore, | ||
reflector: reflector, | ||
stopCh: stopCh, | ||
scaleTargets: scaleTargets, | ||
clientset: clientset, | ||
nodeStore: nodeStore, | ||
reflector: reflector, | ||
stopCh: stopCh, | ||
}, nil | ||
} | ||
|
||
func getScaleTarget(target, namespace string) (*scaleTarget, error) { | ||
splits := strings.Split(target, "/") | ||
func getScaleTargets(targets, namespace string) (*scaleTargets, error) { | ||
st := &scaleTargets{targets: []target{}, namespace: namespace} | ||
|
||
for _, el := range strings.Split(targets, ",") { | ||
el := strings.TrimSpace(el) | ||
target, err := getTarget(el) | ||
if err != nil { | ||
return &scaleTargets{}, fmt.Errorf("target format error: %v", targets) | ||
} | ||
st.targets = append(st.targets, target) | ||
} | ||
return st, nil | ||
} | ||
|
||
func getTarget(t string) (target, error) { | ||
splits := strings.Split(t, "/") | ||
if len(splits) != 2 { | ||
return &scaleTarget{}, fmt.Errorf("target format error: %v", target) | ||
return target{}, fmt.Errorf("target format error: %v", t) | ||
} | ||
kind := splits[0] | ||
name := splits[1] | ||
return &scaleTarget{kind, name, namespace}, nil | ||
return target{kind, name}, nil | ||
} | ||
|
||
// scaleTarget stores the scalable target recourse | ||
type scaleTarget struct { | ||
kind string | ||
name string | ||
type target struct { | ||
kind string | ||
name string | ||
} | ||
|
||
// scaleTargets stores the scalable target resources | ||
type scaleTargets struct { | ||
targets []target | ||
namespace string | ||
} | ||
|
||
func (k *k8sClient) GetNamespace() (namespace string) { | ||
return k.target.namespace | ||
return k.scaleTargets.namespace | ||
} | ||
|
||
func (k *k8sClient) FetchConfigMap(namespace, configmap string) (*v1.ConfigMap, error) { | ||
|
@@ -209,15 +227,25 @@ func (k *k8sClient) GetClusterStatus() (clusterStatus *ClusterStatus, err error) | |
return clusterStatus, nil | ||
} | ||
|
||
func (k *k8sClient) UpdateReplicas(expReplicas int32) (prevReplicas int32, err error) { | ||
prevReplicas, err = k.updateReplicasAppsV1(expReplicas) | ||
func (k *k8sClient) UpdateReplicas(expReplicas int32) (err error) { | ||
for _, target := range k.scaleTargets.targets { | ||
_, err := k.UpdateTargetReplicas(expReplicas, target) | ||
if err == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should check for non-nil instead? |
||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (k *k8sClient) UpdateTargetReplicas(expReplicas int32, target target) (prevReplicas int32, err error) { | ||
prevReplicas, err = k.updateReplicasAppsV1(expReplicas, target) | ||
if err == nil || !apierrors.IsForbidden(err) { | ||
return prevReplicas, err | ||
} | ||
glog.V(1).Infof("Falling back to extensions/v1beta1, error using apps/v1: %v", err) | ||
|
||
// Fall back to using the extensions API if we get a forbidden error | ||
scale, err := k.getScaleExtensionsV1beta1(k.target) | ||
scale, err := k.getScaleExtensionsV1beta1(&target) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
@@ -226,39 +254,39 @@ func (k *k8sClient) UpdateReplicas(expReplicas int32) (prevReplicas int32, err e | |
glog.V(0).Infof("Cluster status: SchedulableNodes[%v], TotalNodes[%v], SchedulableCores[%v], TotalCores[%v]", k.clusterStatus.SchedulableNodes, k.clusterStatus.TotalNodes, k.clusterStatus.SchedulableCores, k.clusterStatus.TotalCores) | ||
glog.V(0).Infof("Replicas are not as expected : updating replicas from %d to %d", prevReplicas, expReplicas) | ||
scale.Spec.Replicas = expReplicas | ||
_, err = k.updateScaleExtensionsV1beta1(k.target, scale) | ||
_, err = k.updateScaleExtensionsV1beta1(&target, scale) | ||
if err != nil { | ||
return 0, err | ||
} | ||
} | ||
return prevReplicas, nil | ||
} | ||
|
||
func (k *k8sClient) getScaleExtensionsV1beta1(target *scaleTarget) (*extensionsv1beta1.Scale, error) { | ||
func (k *k8sClient) getScaleExtensionsV1beta1(target *target) (*extensionsv1beta1.Scale, error) { | ||
opt := metav1.GetOptions{} | ||
switch strings.ToLower(target.kind) { | ||
case "deployment", "deployments": | ||
return k.clientset.ExtensionsV1beta1().Deployments(target.namespace).GetScale(context.TODO(), target.name, opt) | ||
return k.clientset.ExtensionsV1beta1().Deployments(k.scaleTargets.namespace).GetScale(context.TODO(), target.name, opt) | ||
case "replicaset", "replicasets": | ||
return k.clientset.ExtensionsV1beta1().ReplicaSets(target.namespace).GetScale(context.TODO(), target.name, opt) | ||
return k.clientset.ExtensionsV1beta1().ReplicaSets(k.scaleTargets.namespace).GetScale(context.TODO(), target.name, opt) | ||
default: | ||
return nil, fmt.Errorf("unsupported target kind: %v", target.kind) | ||
} | ||
} | ||
|
||
func (k *k8sClient) updateScaleExtensionsV1beta1(target *scaleTarget, scale *extensionsv1beta1.Scale) (*extensionsv1beta1.Scale, error) { | ||
func (k *k8sClient) updateScaleExtensionsV1beta1(target *target, scale *extensionsv1beta1.Scale) (*extensionsv1beta1.Scale, error) { | ||
switch strings.ToLower(target.kind) { | ||
case "deployment", "deployments": | ||
return k.clientset.ExtensionsV1beta1().Deployments(target.namespace).UpdateScale(context.TODO(), target.name, scale, metav1.UpdateOptions{}) | ||
return k.clientset.ExtensionsV1beta1().Deployments(k.scaleTargets.namespace).UpdateScale(context.TODO(), target.name, scale, metav1.UpdateOptions{}) | ||
case "replicaset", "replicasets": | ||
return k.clientset.ExtensionsV1beta1().ReplicaSets(target.namespace).UpdateScale(context.TODO(), target.name, scale, metav1.UpdateOptions{}) | ||
return k.clientset.ExtensionsV1beta1().ReplicaSets(k.scaleTargets.namespace).UpdateScale(context.TODO(), target.name, scale, metav1.UpdateOptions{}) | ||
default: | ||
return nil, fmt.Errorf("unsupported target kind: %v", target.kind) | ||
} | ||
} | ||
|
||
func (k *k8sClient) updateReplicasAppsV1(expReplicas int32) (prevReplicas int32, err error) { | ||
req, err := requestForTarget(k.clientset.AppsV1().RESTClient().Get(), k.target) | ||
func (k *k8sClient) updateReplicasAppsV1(expReplicas int32, target target) (prevReplicas int32, err error) { | ||
req, err := requestForTarget(k.clientset.AppsV1().RESTClient().Get(), &target, k.scaleTargets.namespace) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
@@ -270,10 +298,10 @@ func (k *k8sClient) updateReplicasAppsV1(expReplicas int32) (prevReplicas int32, | |
|
||
prevReplicas = scale.Spec.Replicas | ||
if expReplicas != prevReplicas { | ||
glog.V(0).Infof("Cluster status: SchedulableNodes[%v], TotalNodes[%v], SchedulableCores[%v], TotalCores[%v]", k.clusterStatus.SchedulableNodes, k.clusterStatus.TotalNodes, k.clusterStatus.SchedulableCores, k.clusterStatus.TotalCores) | ||
glog.V(0).Infof("Cluster status: SchedulableNodes[%v], SchedulableCores[%v]", k.clusterStatus.SchedulableNodes, k.clusterStatus.SchedulableCores) | ||
glog.V(0).Infof("Replicas are not as expected : updating replicas from %d to %d", prevReplicas, expReplicas) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we have multiple targets now, could we also log what is being scaled here? |
||
scale.Spec.Replicas = expReplicas | ||
req, err = requestForTarget(k.clientset.AppsV1().RESTClient().Put(), k.target) | ||
req, err = requestForTarget(k.clientset.AppsV1().RESTClient().Put(), &target, k.scaleTargets.namespace) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
@@ -285,7 +313,7 @@ func (k *k8sClient) updateReplicasAppsV1(expReplicas int32) (prevReplicas int32, | |
return prevReplicas, nil | ||
} | ||
|
||
func requestForTarget(req *rest.Request, target *scaleTarget) (*rest.Request, error) { | ||
func requestForTarget(req *rest.Request, target *target, namespace string) (*rest.Request, error) { | ||
var absPath, resource string | ||
// Support the kinds we allowed scaling via the extensions API group | ||
// TODO: switch to use the polymorphic scale client once client-go versions are updated | ||
|
@@ -306,5 +334,5 @@ func requestForTarget(req *rest.Request, target *scaleTarget) (*rest.Request, er | |
return nil, fmt.Errorf("unsupported target kind: %v", target.kind) | ||
} | ||
|
||
return req.AbsPath(absPath).Namespace(target.namespace).Resource(resource).Name(target.name).SubResource("scale"), nil | ||
return req.AbsPath(absPath).Namespace(namespace).Resource(resource).Name(target.name).SubResource("scale"), nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Less certain about helm chart version here - I would expect a minor version bump for CPA itself as this is introducing a new feature.
Should we probably bump this later once we have a new CPA version released?