Skip to content

Commit

Permalink
Enable scale command to be used for stacks (#174)
Browse files Browse the repository at this point in the history
* Enable scale command to be used for stacks

Signed-off-by: Mikkel Oscar Lyderik Larsen <[email protected]>

* Add labelSelector to stack status

Signed-off-by: Mikkel Oscar Lyderik Larsen <[email protected]>

* Add labelSelector to stack status test

Signed-off-by: Mikkel Oscar Lyderik Larsen <[email protected]>
  • Loading branch information
mikkeloscar authored and szuecs committed Nov 26, 2019
1 parent aee46af commit 62962e2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
5 changes: 5 additions & 0 deletions docs/stack_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ spec:
subresources:
# status enables the status subresource.
status: {}
# scale enables the scale subresource.
scale:
specReplicasPath: .spec.replicas
statusReplicasPath: .status.replicas
labelSelectorPath: .status.labelSelector
# validation depends on Kubernetes >= v1.11.0
validation:
openAPIV3Schema:
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/zalando.org/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ type StackStatus struct {
// NoTrafficSince is the timestamp defining the last time the stack was
// observed getting traffic.
NoTrafficSince *metav1.Time `json:"noTrafficSince,omitempty"`
// LabelSelector is the label selector used to find all pods managed by
// a stack.
LabelSelector string `json:"labelSelector,omitempty"`
}

// Prescaling hold prescaling information
Expand Down
10 changes: 8 additions & 2 deletions pkg/core/stack_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
v1 "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
)

Expand Down Expand Up @@ -135,6 +136,10 @@ func servicePortsFromContainers(containers []v1.Container) []v1.ServicePort {
return ports
}

func (sc *StackContainer) selector() map[string]string {
return limitLabels(sc.Stack.Labels, selectorLabels)
}

func (sc *StackContainer) GenerateDeployment() *appsv1.Deployment {
stack := sc.Stack

Expand Down Expand Up @@ -166,7 +171,7 @@ func (sc *StackContainer) GenerateDeployment() *appsv1.Deployment {
Spec: appsv1.DeploymentSpec{
Replicas: updatedReplicas,
Selector: &metav1.LabelSelector{
MatchLabels: limitLabels(stack.Labels, selectorLabels),
MatchLabels: sc.selector(),
},
Template: *templateInjectLabels(stack.Spec.PodTemplate.DeepCopy(), stack.Labels),
},
Expand Down Expand Up @@ -237,7 +242,7 @@ func (sc *StackContainer) GenerateService() (*v1.Service, error) {
return &v1.Service{
ObjectMeta: sc.resourceMeta(),
Spec: v1.ServiceSpec{
Selector: limitLabels(sc.Stack.Labels, selectorLabels),
Selector: sc.selector(),
Type: v1.ServiceTypeClusterIP,
Ports: servicePorts,
},
Expand Down Expand Up @@ -309,5 +314,6 @@ func (sc *StackContainer) GenerateStackStatus() *zv1.StackStatus {
DesiredReplicas: sc.desiredReplicas,
Prescaling: prescaling,
NoTrafficSince: wrapTime(sc.noTrafficSince),
LabelSelector: labels.Set(sc.selector()).String(),
}
}
26 changes: 21 additions & 5 deletions pkg/core/stack_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,8 @@ func TestGenerateStackStatus(t *testing.T) {

for _, tc := range []struct {
name string
labels map[string]string
expectedLabelSelector string
actualTrafficWeight float64
desiredTrafficWeight float64
noTrafficSince time.Time
Expand All @@ -580,16 +582,24 @@ func TestGenerateStackStatus(t *testing.T) {
prescalingLastTrafficIncrease time.Time
}{
{
name: "with traffic",
actualTrafficWeight: 0.25,
desiredTrafficWeight: 0.75,
name: "with traffic",
labels: map[string]string{},
expectedLabelSelector: "",
actualTrafficWeight: 0.25,
desiredTrafficWeight: 0.75,
},
{
name: "without traffic",
noTrafficSince: hourAgo,
name: "without traffic",
labels: map[string]string{
StacksetHeritageLabelKey: "stackset-x",
},
expectedLabelSelector: "stackset=stackset-x",
noTrafficSince: hourAgo,
},
{
name: "prescaled",
labels: map[string]string{},
expectedLabelSelector: "",
actualTrafficWeight: 0.25,
desiredTrafficWeight: 0.25,
prescalingActive: true,
Expand All @@ -600,6 +610,11 @@ func TestGenerateStackStatus(t *testing.T) {
} {
t.Run(tc.name, func(t *testing.T) {
c := &StackContainer{
Stack: &zv1.Stack{
ObjectMeta: metav1.ObjectMeta{
Labels: tc.labels,
},
},
actualTrafficWeight: tc.actualTrafficWeight,
desiredTrafficWeight: tc.desiredTrafficWeight,
createdReplicas: 3,
Expand All @@ -621,6 +636,7 @@ func TestGenerateStackStatus(t *testing.T) {
UpdatedReplicas: 1,
DesiredReplicas: 4,
NoTrafficSince: wrapTime(tc.noTrafficSince),
LabelSelector: tc.expectedLabelSelector,
Prescaling: zv1.PrescalingStatus{
Active: tc.prescalingActive,
Replicas: tc.prescalingReplicas,
Expand Down

0 comments on commit 62962e2

Please sign in to comment.