2015-10-05 21:28:46 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-10-05 21:28:46 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package deployment
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-09-21 07:06:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2015-10-09 22:04:41 +00:00
|
|
|
exp "k8s.io/kubernetes/pkg/apis/extensions"
|
2016-02-16 22:16:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
|
2015-10-05 21:28:46 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/record"
|
2016-01-15 05:00:58 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/testing/core"
|
2015-09-21 07:06:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller"
|
2015-10-05 21:28:46 +00:00
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2015-11-10 06:28:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/intstr"
|
2016-07-26 15:13:18 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/uuid"
|
2015-10-05 21:28:46 +00:00
|
|
|
)
|
|
|
|
|
2016-01-28 16:35:14 +00:00
|
|
|
var (
|
|
|
|
alwaysReady = func() bool { return true }
|
|
|
|
noTimestamp = unversioned.Time{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func rs(name string, replicas int, selector map[string]string, timestamp unversioned.Time) *exp.ReplicaSet {
|
2016-02-29 23:15:55 +00:00
|
|
|
return &exp.ReplicaSet{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
2016-01-28 16:35:14 +00:00
|
|
|
Name: name,
|
|
|
|
CreationTimestamp: timestamp,
|
2016-06-02 01:44:52 +00:00
|
|
|
Namespace: api.NamespaceDefault,
|
2016-02-29 23:15:55 +00:00
|
|
|
},
|
|
|
|
Spec: exp.ReplicaSetSpec{
|
2016-04-27 04:35:14 +00:00
|
|
|
Replicas: int32(replicas),
|
2016-02-29 23:15:55 +00:00
|
|
|
Selector: &unversioned.LabelSelector{MatchLabels: selector},
|
2016-03-09 21:11:13 +00:00
|
|
|
Template: api.PodTemplateSpec{},
|
2016-02-29 23:15:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRSWithStatus(name string, specReplicas, statusReplicas int, selector map[string]string) *exp.ReplicaSet {
|
2016-01-28 16:35:14 +00:00
|
|
|
rs := rs(name, specReplicas, selector, noTimestamp)
|
2016-02-29 23:15:55 +00:00
|
|
|
rs.Status = exp.ReplicaSetStatus{
|
2016-04-27 04:35:14 +00:00
|
|
|
Replicas: int32(statusReplicas),
|
2016-02-29 23:15:55 +00:00
|
|
|
}
|
|
|
|
return rs
|
|
|
|
}
|
|
|
|
|
2016-06-07 23:58:18 +00:00
|
|
|
func deployment(name string, replicas int, maxSurge, maxUnavailable intstr.IntOrString, selector map[string]string) exp.Deployment {
|
2016-02-29 23:15:55 +00:00
|
|
|
return exp.Deployment{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
2016-06-02 01:44:52 +00:00
|
|
|
Name: name,
|
|
|
|
Namespace: api.NamespaceDefault,
|
2016-02-29 23:15:55 +00:00
|
|
|
},
|
|
|
|
Spec: exp.DeploymentSpec{
|
2016-04-27 04:35:14 +00:00
|
|
|
Replicas: int32(replicas),
|
2016-06-07 23:58:18 +00:00
|
|
|
Selector: &unversioned.LabelSelector{MatchLabels: selector},
|
2016-02-29 23:15:55 +00:00
|
|
|
Strategy: exp.DeploymentStrategy{
|
|
|
|
Type: exp.RollingUpdateDeploymentStrategyType,
|
|
|
|
RollingUpdate: &exp.RollingUpdateDeployment{
|
|
|
|
MaxSurge: maxSurge,
|
|
|
|
MaxUnavailable: maxUnavailable,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDeployment(replicas int, revisionHistoryLimit *int) *exp.Deployment {
|
2016-04-27 04:35:14 +00:00
|
|
|
var v *int32
|
|
|
|
if revisionHistoryLimit != nil {
|
|
|
|
v = new(int32)
|
|
|
|
*v = int32(*revisionHistoryLimit)
|
|
|
|
}
|
2016-02-29 23:15:55 +00:00
|
|
|
d := exp.Deployment{
|
|
|
|
TypeMeta: unversioned.TypeMeta{APIVersion: testapi.Default.GroupVersion().String()},
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
2016-07-26 15:13:18 +00:00
|
|
|
UID: uuid.NewUUID(),
|
2016-02-29 23:15:55 +00:00
|
|
|
Name: "foobar",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
ResourceVersion: "18",
|
|
|
|
},
|
|
|
|
Spec: exp.DeploymentSpec{
|
|
|
|
Strategy: exp.DeploymentStrategy{
|
|
|
|
Type: exp.RollingUpdateDeploymentStrategyType,
|
|
|
|
RollingUpdate: &exp.RollingUpdateDeployment{},
|
|
|
|
},
|
2016-04-27 04:35:14 +00:00
|
|
|
Replicas: int32(replicas),
|
2016-02-29 23:15:55 +00:00
|
|
|
Selector: &unversioned.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
|
|
|
|
Template: api.PodTemplateSpec{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"name": "foo",
|
|
|
|
"type": "production",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo/bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-04-27 04:35:14 +00:00
|
|
|
RevisionHistoryLimit: v,
|
2016-02-29 23:15:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return &d
|
|
|
|
}
|
|
|
|
|
2016-01-28 16:35:14 +00:00
|
|
|
// TODO: Consolidate all deployment helpers into one.
|
|
|
|
func newDeploymentEnhanced(replicas int, maxSurge intstr.IntOrString) *exp.Deployment {
|
|
|
|
d := newDeployment(replicas, nil)
|
|
|
|
d.Spec.Strategy.RollingUpdate.MaxSurge = maxSurge
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2016-02-29 23:15:55 +00:00
|
|
|
func newReplicaSet(d *exp.Deployment, name string, replicas int) *exp.ReplicaSet {
|
|
|
|
return &exp.ReplicaSet{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: name,
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
|
|
|
Spec: exp.ReplicaSetSpec{
|
2016-04-27 04:35:14 +00:00
|
|
|
Replicas: int32(replicas),
|
2016-03-09 21:11:13 +00:00
|
|
|
Template: d.Spec.Template,
|
2016-02-29 23:15:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 07:06:45 +00:00
|
|
|
func getKey(d *exp.Deployment, t *testing.T) string {
|
|
|
|
if key, err := controller.KeyFunc(d); err != nil {
|
|
|
|
t.Errorf("Unexpected error getting key for deployment %v: %v", d.Name, err)
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fixture struct {
|
|
|
|
t *testing.T
|
|
|
|
|
2016-01-15 05:00:58 +00:00
|
|
|
client *fake.Clientset
|
2015-09-21 07:06:45 +00:00
|
|
|
// Objects to put in the store.
|
2016-10-04 17:11:07 +00:00
|
|
|
dLister []*exp.Deployment
|
|
|
|
rsLister []*exp.ReplicaSet
|
|
|
|
podLister []*api.Pod
|
2015-09-21 07:06:45 +00:00
|
|
|
|
|
|
|
// Actions expected to happen on the client. Objects from here are also
|
|
|
|
// preloaded into NewSimpleFake.
|
2016-01-15 05:00:58 +00:00
|
|
|
actions []core.Action
|
2016-06-02 01:44:52 +00:00
|
|
|
objects []runtime.Object
|
2015-09-21 07:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fixture) expectUpdateDeploymentAction(d *exp.Deployment) {
|
2016-04-13 22:33:15 +00:00
|
|
|
f.actions = append(f.actions, core.NewUpdateAction(unversioned.GroupVersionResource{Resource: "deployments"}, d.Namespace, d))
|
2016-06-02 01:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fixture) expectUpdateDeploymentStatusAction(d *exp.Deployment) {
|
|
|
|
action := core.NewUpdateAction(unversioned.GroupVersionResource{Resource: "deployments"}, d.Namespace, d)
|
|
|
|
action.Subresource = "status"
|
|
|
|
f.actions = append(f.actions, action)
|
2015-09-21 07:06:45 +00:00
|
|
|
}
|
|
|
|
|
2016-02-06 02:43:02 +00:00
|
|
|
func (f *fixture) expectCreateRSAction(rs *exp.ReplicaSet) {
|
2016-04-13 22:33:15 +00:00
|
|
|
f.actions = append(f.actions, core.NewCreateAction(unversioned.GroupVersionResource{Resource: "replicasets"}, rs.Namespace, rs))
|
2015-09-21 07:06:45 +00:00
|
|
|
}
|
|
|
|
|
2016-02-06 02:43:02 +00:00
|
|
|
func (f *fixture) expectUpdateRSAction(rs *exp.ReplicaSet) {
|
2016-04-13 22:33:15 +00:00
|
|
|
f.actions = append(f.actions, core.NewUpdateAction(unversioned.GroupVersionResource{Resource: "replicasets"}, rs.Namespace, rs))
|
2015-09-21 07:06:45 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 23:58:52 +00:00
|
|
|
func (f *fixture) expectListPodAction(namespace string, opt api.ListOptions) {
|
2016-04-13 22:33:15 +00:00
|
|
|
f.actions = append(f.actions, core.NewListAction(unversioned.GroupVersionResource{Resource: "pods"}, namespace, opt))
|
2016-01-08 23:58:52 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 07:06:45 +00:00
|
|
|
func newFixture(t *testing.T) *fixture {
|
|
|
|
f := &fixture{}
|
|
|
|
f.t = t
|
2016-06-02 01:44:52 +00:00
|
|
|
f.objects = []runtime.Object{}
|
2015-09-21 07:06:45 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fixture) run(deploymentName string) {
|
2016-06-02 01:44:52 +00:00
|
|
|
f.client = fake.NewSimpleClientset(f.objects...)
|
2015-11-18 23:12:11 +00:00
|
|
|
c := NewDeploymentController(f.client, controller.NoResyncPeriodFunc)
|
2015-12-22 22:28:44 +00:00
|
|
|
c.eventRecorder = &record.FakeRecorder{}
|
2016-10-04 17:11:07 +00:00
|
|
|
c.rsListerSynced = alwaysReady
|
|
|
|
c.podListerSynced = alwaysReady
|
|
|
|
for _, d := range f.dLister {
|
|
|
|
c.dLister.Indexer.Add(d)
|
2015-09-21 07:06:45 +00:00
|
|
|
}
|
2016-10-04 17:11:07 +00:00
|
|
|
for _, rs := range f.rsLister {
|
2016-10-04 17:23:27 +00:00
|
|
|
c.rsLister.Indexer.Add(rs)
|
2015-09-21 07:06:45 +00:00
|
|
|
}
|
2016-10-04 17:11:07 +00:00
|
|
|
for _, pod := range f.podLister {
|
|
|
|
c.podLister.Indexer.Add(pod)
|
2015-09-21 07:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := c.syncDeployment(deploymentName)
|
|
|
|
if err != nil {
|
|
|
|
f.t.Errorf("error syncing deployment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actions := f.client.Actions()
|
|
|
|
for i, action := range actions {
|
|
|
|
if len(f.actions) < i+1 {
|
|
|
|
f.t.Errorf("%d unexpected actions: %+v", len(actions)-len(f.actions), actions[i:])
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedAction := f.actions[i]
|
2016-04-13 22:33:15 +00:00
|
|
|
if !expectedAction.Matches(action.GetVerb(), action.GetResource().Resource) {
|
2015-09-21 07:06:45 +00:00
|
|
|
f.t.Errorf("Expected\n\t%#v\ngot\n\t%#v", expectedAction, action)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(f.actions) > len(actions) {
|
|
|
|
f.t.Errorf("%d additional expected actions:%+v", len(f.actions)-len(actions), f.actions[len(actions):])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:40:18 +00:00
|
|
|
func TestSyncDeploymentCreatesReplicaSet(t *testing.T) {
|
2015-09-21 07:06:45 +00:00
|
|
|
f := newFixture(t)
|
|
|
|
|
2016-01-28 06:13:07 +00:00
|
|
|
d := newDeployment(1, nil)
|
2016-10-04 17:11:07 +00:00
|
|
|
f.dLister = append(f.dLister, d)
|
2016-06-02 01:44:52 +00:00
|
|
|
f.objects = append(f.objects, d)
|
2015-09-21 07:06:45 +00:00
|
|
|
|
2016-06-02 01:44:52 +00:00
|
|
|
rs := newReplicaSet(d, "deploymentrs-4186632231", 1)
|
2015-09-21 07:06:45 +00:00
|
|
|
|
2016-01-20 00:40:18 +00:00
|
|
|
f.expectCreateRSAction(rs)
|
2016-01-13 01:52:18 +00:00
|
|
|
f.expectUpdateDeploymentAction(d)
|
2016-06-02 01:44:52 +00:00
|
|
|
f.expectUpdateDeploymentStatusAction(d)
|
2015-09-21 07:06:45 +00:00
|
|
|
|
|
|
|
f.run(getKey(d, t))
|
|
|
|
}
|
2016-03-25 07:40:12 +00:00
|
|
|
|
2016-06-17 11:46:43 +00:00
|
|
|
func TestSyncDeploymentDontDoAnythingDuringDeletion(t *testing.T) {
|
|
|
|
f := newFixture(t)
|
|
|
|
|
|
|
|
d := newDeployment(1, nil)
|
|
|
|
now := unversioned.Now()
|
|
|
|
d.DeletionTimestamp = &now
|
2016-10-04 17:11:07 +00:00
|
|
|
f.dLister = append(f.dLister, d)
|
2016-06-17 11:46:43 +00:00
|
|
|
|
|
|
|
f.run(getKey(d, t))
|
|
|
|
}
|
|
|
|
|
2016-03-25 07:40:12 +00:00
|
|
|
// issue: https://github.com/kubernetes/kubernetes/issues/23218
|
|
|
|
func TestDeploymentController_dontSyncDeploymentsWithEmptyPodSelector(t *testing.T) {
|
|
|
|
fake := &fake.Clientset{}
|
|
|
|
controller := NewDeploymentController(fake, controller.NoResyncPeriodFunc)
|
|
|
|
|
|
|
|
controller.eventRecorder = &record.FakeRecorder{}
|
2016-10-04 17:11:07 +00:00
|
|
|
controller.rsListerSynced = alwaysReady
|
|
|
|
controller.podListerSynced = alwaysReady
|
2016-03-25 07:40:12 +00:00
|
|
|
|
|
|
|
d := newDeployment(1, nil)
|
|
|
|
empty := unversioned.LabelSelector{}
|
|
|
|
d.Spec.Selector = &empty
|
2016-10-04 17:11:07 +00:00
|
|
|
controller.dLister.Indexer.Add(d)
|
2016-03-25 07:40:12 +00:00
|
|
|
// We expect the deployment controller to not take action here since it's configuration
|
|
|
|
// is invalid, even though no replicasets exist that match it's selector.
|
|
|
|
controller.syncDeployment(fmt.Sprintf("%s/%s", d.ObjectMeta.Namespace, d.ObjectMeta.Name))
|
|
|
|
if len(fake.Actions()) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, action := range fake.Actions() {
|
|
|
|
t.Logf("unexpected action: %#v", action)
|
|
|
|
}
|
|
|
|
t.Errorf("expected deployment controller to not take action")
|
|
|
|
}
|