Skip to content

Commit

Permalink
Merge pull request #137 from elmiko/openshift/scale-from-0-using-anno…
Browse files Browse the repository at this point in the history
…tations

UPSTREAM: <carry>: openshift: Implement scale from zero
  • Loading branch information
openshift-merge-robot committed Apr 3, 2020
2 parents d7634cf + fe61822 commit ec12030
Show file tree
Hide file tree
Showing 10 changed files with 891 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,13 @@ func (c *machineController) machineSetNodeGroups() ([]*nodegroup, error) {
if err != nil {
return err
}
if ng.MaxSize()-ng.MinSize() > 0 && pointer.Int32PtrDerefOr(machineSet.Spec.Replicas, 0) > 0 {
nodegroups = append(nodegroups, ng)

if ng.MaxSize()-ng.MinSize() > 0 {
if ng.scalableResource.CanScaleFromZero() {
nodegroups = append(nodegroups, ng)
} else if pointer.Int32PtrDerefOr(machineSet.Spec.Replicas, 0) > 0 {
nodegroups = append(nodegroups, ng)
}
}
return nil
}); err != nil {
Expand All @@ -526,8 +531,12 @@ func (c *machineController) machineDeploymentNodeGroups() ([]*nodegroup, error)
return nil, err
}
// add nodegroup iff it has the capacity to scale
if ng.MaxSize()-ng.MinSize() > 0 && pointer.Int32PtrDerefOr(md.Spec.Replicas, 0) > 0 {
nodegroups = append(nodegroups, ng)
if ng.MaxSize()-ng.MinSize() > 0 {
if ng.scalableResource.CanScaleFromZero() {
nodegroups = append(nodegroups, ng)
} else if pointer.Int32PtrDerefOr(md.Spec.Replicas, 0) > 0 {
nodegroups = append(nodegroups, ng)
}
}
}

Expand Down Expand Up @@ -581,9 +590,6 @@ func (c *machineController) nodeGroupForNode(node *corev1.Node) (*nodegroup, err
if err != nil {
return nil, fmt.Errorf("failed to build nodegroup for node %q: %v", node.Name, err)
}
// We don't scale from 0 so nodes must belong
// to a nodegroup that has a scale size of at
// least 1.
if nodegroup.MaxSize()-nodegroup.MinSize() < 1 {
return nil, nil
}
Expand All @@ -596,8 +602,6 @@ func (c *machineController) nodeGroupForNode(node *corev1.Node) (*nodegroup, err
return nil, fmt.Errorf("failed to build nodegroup for node %q: %v", node.Name, err)
}

// We don't scale from 0 so nodes must belong to a nodegroup
// that has a scale size of at least 1.
if nodegroup.MaxSize()-nodegroup.MinSize() < 1 {
return nil, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,38 @@ limitations under the License.
package clusterapi

import (
"time"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/utils/pointer"
)

func newTaintFromInterface(t interface{}) corev1.Taint {
taint := corev1.Taint{}
if tt, ok := t.(map[string]interface{}); ok {
if tmap, found, _ := unstructured.NestedMap(tt); found {
if val, ok := tmap["key"].(string); ok {
taint.Key = val
}
if val, ok := tmap["effect"].(string); ok {
taint.Effect = corev1.TaintEffect(val)
}
if val, ok := tmap["value"].(string); ok {
taint.Value = val
}
if val, ok := tmap["timeAdded"].(string); ok {
ta := time.Time{}
ta.UnmarshalText([]byte(val))
nta := metav1.NewTime(ta)
taint.TimeAdded = &nta
}
}
}
return taint
}

func newMachineDeploymentFromUnstructured(u *unstructured.Unstructured) *MachineDeployment {
machineDeployment := MachineDeployment{
TypeMeta: metav1.TypeMeta{
Expand All @@ -42,6 +68,20 @@ func newMachineDeploymentFromUnstructured(u *unstructured.Unstructured) *Machine
Status: MachineDeploymentStatus{},
}

machineSpec, found, err := unstructured.NestedMap(u.Object, "spec", "template", "spec")
if err == nil && found {
machineSpecUnstructured := unstructured.Unstructured{Object: machineSpec}
machineDeployment.Spec.Template.Spec.Labels = machineSpecUnstructured.GetLabels()
taints, _, _ := unstructured.NestedSlice(machineSpec, "taints")
if taints != nil {
taintobjlist := make([]corev1.Taint, len(taints))
for i, t := range taints {
taintobjlist[i] = newTaintFromInterface(t)
}
machineDeployment.Spec.Template.Spec.Taints = taintobjlist
}
}

replicas, found, err := unstructured.NestedInt64(u.Object, "spec", "replicas")
if err == nil && found {
machineDeployment.Spec.Replicas = pointer.Int32Ptr(int32(replicas))
Expand Down Expand Up @@ -69,6 +109,20 @@ func newMachineSetFromUnstructured(u *unstructured.Unstructured) *MachineSet {
Status: MachineSetStatus{},
}

machineSpec, found, err := unstructured.NestedMap(u.Object, "spec", "template", "spec")
if err == nil && found {
machineSpecUnstructured := unstructured.Unstructured{Object: machineSpec}
machineSet.Spec.Template.Spec.Labels = machineSpecUnstructured.GetLabels()
taints, _, _ := unstructured.NestedSlice(machineSpec, "taints")
if taints != nil {
taintobjlist := make([]corev1.Taint, len(taints))
for i, t := range taints {
taintobjlist[i] = newTaintFromInterface(t)
}
machineSet.Spec.Template.Spec.Taints = taintobjlist
}
}

replicas, found, err := unstructured.NestedInt64(u.Object, "spec", "replicas")
if err == nil && found {
machineSet.Spec.Replicas = pointer.Int32Ptr(int32(replicas))
Expand Down

0 comments on commit ec12030

Please sign in to comment.