deployment_controller_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package deployment
  14. import (
  15. "fmt"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/api/testapi"
  19. "k8s.io/kubernetes/pkg/api/unversioned"
  20. exp "k8s.io/kubernetes/pkg/apis/extensions"
  21. "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
  22. "k8s.io/kubernetes/pkg/client/record"
  23. "k8s.io/kubernetes/pkg/client/testing/core"
  24. "k8s.io/kubernetes/pkg/controller"
  25. "k8s.io/kubernetes/pkg/runtime"
  26. "k8s.io/kubernetes/pkg/util/intstr"
  27. "k8s.io/kubernetes/pkg/util/uuid"
  28. )
  29. var (
  30. alwaysReady = func() bool { return true }
  31. noTimestamp = unversioned.Time{}
  32. )
  33. func rs(name string, replicas int, selector map[string]string, timestamp unversioned.Time) *exp.ReplicaSet {
  34. return &exp.ReplicaSet{
  35. ObjectMeta: api.ObjectMeta{
  36. Name: name,
  37. CreationTimestamp: timestamp,
  38. Namespace: api.NamespaceDefault,
  39. },
  40. Spec: exp.ReplicaSetSpec{
  41. Replicas: int32(replicas),
  42. Selector: &unversioned.LabelSelector{MatchLabels: selector},
  43. Template: api.PodTemplateSpec{},
  44. },
  45. }
  46. }
  47. func newRSWithStatus(name string, specReplicas, statusReplicas int, selector map[string]string) *exp.ReplicaSet {
  48. rs := rs(name, specReplicas, selector, noTimestamp)
  49. rs.Status = exp.ReplicaSetStatus{
  50. Replicas: int32(statusReplicas),
  51. }
  52. return rs
  53. }
  54. func deployment(name string, replicas int, maxSurge, maxUnavailable intstr.IntOrString, selector map[string]string) exp.Deployment {
  55. return exp.Deployment{
  56. ObjectMeta: api.ObjectMeta{
  57. Name: name,
  58. Namespace: api.NamespaceDefault,
  59. },
  60. Spec: exp.DeploymentSpec{
  61. Replicas: int32(replicas),
  62. Selector: &unversioned.LabelSelector{MatchLabels: selector},
  63. Strategy: exp.DeploymentStrategy{
  64. Type: exp.RollingUpdateDeploymentStrategyType,
  65. RollingUpdate: &exp.RollingUpdateDeployment{
  66. MaxSurge: maxSurge,
  67. MaxUnavailable: maxUnavailable,
  68. },
  69. },
  70. },
  71. }
  72. }
  73. func newDeployment(replicas int, revisionHistoryLimit *int) *exp.Deployment {
  74. var v *int32
  75. if revisionHistoryLimit != nil {
  76. v = new(int32)
  77. *v = int32(*revisionHistoryLimit)
  78. }
  79. d := exp.Deployment{
  80. TypeMeta: unversioned.TypeMeta{APIVersion: testapi.Default.GroupVersion().String()},
  81. ObjectMeta: api.ObjectMeta{
  82. UID: uuid.NewUUID(),
  83. Name: "foobar",
  84. Namespace: api.NamespaceDefault,
  85. ResourceVersion: "18",
  86. },
  87. Spec: exp.DeploymentSpec{
  88. Strategy: exp.DeploymentStrategy{
  89. Type: exp.RollingUpdateDeploymentStrategyType,
  90. RollingUpdate: &exp.RollingUpdateDeployment{},
  91. },
  92. Replicas: int32(replicas),
  93. Selector: &unversioned.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
  94. Template: api.PodTemplateSpec{
  95. ObjectMeta: api.ObjectMeta{
  96. Labels: map[string]string{
  97. "name": "foo",
  98. "type": "production",
  99. },
  100. },
  101. Spec: api.PodSpec{
  102. Containers: []api.Container{
  103. {
  104. Image: "foo/bar",
  105. },
  106. },
  107. },
  108. },
  109. RevisionHistoryLimit: v,
  110. },
  111. }
  112. return &d
  113. }
  114. // TODO: Consolidate all deployment helpers into one.
  115. func newDeploymentEnhanced(replicas int, maxSurge intstr.IntOrString) *exp.Deployment {
  116. d := newDeployment(replicas, nil)
  117. d.Spec.Strategy.RollingUpdate.MaxSurge = maxSurge
  118. return d
  119. }
  120. func newReplicaSet(d *exp.Deployment, name string, replicas int) *exp.ReplicaSet {
  121. return &exp.ReplicaSet{
  122. ObjectMeta: api.ObjectMeta{
  123. Name: name,
  124. Namespace: api.NamespaceDefault,
  125. },
  126. Spec: exp.ReplicaSetSpec{
  127. Replicas: int32(replicas),
  128. Template: d.Spec.Template,
  129. },
  130. }
  131. }
  132. func getKey(d *exp.Deployment, t *testing.T) string {
  133. if key, err := controller.KeyFunc(d); err != nil {
  134. t.Errorf("Unexpected error getting key for deployment %v: %v", d.Name, err)
  135. return ""
  136. } else {
  137. return key
  138. }
  139. }
  140. type fixture struct {
  141. t *testing.T
  142. client *fake.Clientset
  143. // Objects to put in the store.
  144. dStore []*exp.Deployment
  145. rsStore []*exp.ReplicaSet
  146. podStore []*api.Pod
  147. // Actions expected to happen on the client. Objects from here are also
  148. // preloaded into NewSimpleFake.
  149. actions []core.Action
  150. objects []runtime.Object
  151. }
  152. func (f *fixture) expectUpdateDeploymentAction(d *exp.Deployment) {
  153. f.actions = append(f.actions, core.NewUpdateAction(unversioned.GroupVersionResource{Resource: "deployments"}, d.Namespace, d))
  154. }
  155. func (f *fixture) expectUpdateDeploymentStatusAction(d *exp.Deployment) {
  156. action := core.NewUpdateAction(unversioned.GroupVersionResource{Resource: "deployments"}, d.Namespace, d)
  157. action.Subresource = "status"
  158. f.actions = append(f.actions, action)
  159. }
  160. func (f *fixture) expectCreateRSAction(rs *exp.ReplicaSet) {
  161. f.actions = append(f.actions, core.NewCreateAction(unversioned.GroupVersionResource{Resource: "replicasets"}, rs.Namespace, rs))
  162. }
  163. func (f *fixture) expectUpdateRSAction(rs *exp.ReplicaSet) {
  164. f.actions = append(f.actions, core.NewUpdateAction(unversioned.GroupVersionResource{Resource: "replicasets"}, rs.Namespace, rs))
  165. }
  166. func (f *fixture) expectListPodAction(namespace string, opt api.ListOptions) {
  167. f.actions = append(f.actions, core.NewListAction(unversioned.GroupVersionResource{Resource: "pods"}, namespace, opt))
  168. }
  169. func newFixture(t *testing.T) *fixture {
  170. f := &fixture{}
  171. f.t = t
  172. f.objects = []runtime.Object{}
  173. return f
  174. }
  175. func (f *fixture) run(deploymentName string) {
  176. f.client = fake.NewSimpleClientset(f.objects...)
  177. c := NewDeploymentController(f.client, controller.NoResyncPeriodFunc)
  178. c.eventRecorder = &record.FakeRecorder{}
  179. c.rsStoreSynced = alwaysReady
  180. c.podStoreSynced = alwaysReady
  181. for _, d := range f.dStore {
  182. c.dStore.Indexer.Add(d)
  183. }
  184. for _, rs := range f.rsStore {
  185. c.rsStore.Store.Add(rs)
  186. }
  187. for _, pod := range f.podStore {
  188. c.podStore.Indexer.Add(pod)
  189. }
  190. err := c.syncDeployment(deploymentName)
  191. if err != nil {
  192. f.t.Errorf("error syncing deployment: %v", err)
  193. }
  194. actions := f.client.Actions()
  195. for i, action := range actions {
  196. if len(f.actions) < i+1 {
  197. f.t.Errorf("%d unexpected actions: %+v", len(actions)-len(f.actions), actions[i:])
  198. break
  199. }
  200. expectedAction := f.actions[i]
  201. if !expectedAction.Matches(action.GetVerb(), action.GetResource().Resource) {
  202. f.t.Errorf("Expected\n\t%#v\ngot\n\t%#v", expectedAction, action)
  203. continue
  204. }
  205. }
  206. if len(f.actions) > len(actions) {
  207. f.t.Errorf("%d additional expected actions:%+v", len(f.actions)-len(actions), f.actions[len(actions):])
  208. }
  209. }
  210. func TestSyncDeploymentCreatesReplicaSet(t *testing.T) {
  211. f := newFixture(t)
  212. d := newDeployment(1, nil)
  213. f.dStore = append(f.dStore, d)
  214. f.objects = append(f.objects, d)
  215. rs := newReplicaSet(d, "deploymentrs-4186632231", 1)
  216. f.expectCreateRSAction(rs)
  217. f.expectUpdateDeploymentAction(d)
  218. f.expectUpdateDeploymentStatusAction(d)
  219. f.run(getKey(d, t))
  220. }
  221. func TestSyncDeploymentDontDoAnythingDuringDeletion(t *testing.T) {
  222. f := newFixture(t)
  223. d := newDeployment(1, nil)
  224. now := unversioned.Now()
  225. d.DeletionTimestamp = &now
  226. f.dStore = append(f.dStore, d)
  227. f.run(getKey(d, t))
  228. }
  229. // issue: https://github.com/kubernetes/kubernetes/issues/23218
  230. func TestDeploymentController_dontSyncDeploymentsWithEmptyPodSelector(t *testing.T) {
  231. fake := &fake.Clientset{}
  232. controller := NewDeploymentController(fake, controller.NoResyncPeriodFunc)
  233. controller.eventRecorder = &record.FakeRecorder{}
  234. controller.rsStoreSynced = alwaysReady
  235. controller.podStoreSynced = alwaysReady
  236. d := newDeployment(1, nil)
  237. empty := unversioned.LabelSelector{}
  238. d.Spec.Selector = &empty
  239. controller.dStore.Indexer.Add(d)
  240. // We expect the deployment controller to not take action here since it's configuration
  241. // is invalid, even though no replicasets exist that match it's selector.
  242. controller.syncDeployment(fmt.Sprintf("%s/%s", d.ObjectMeta.Namespace, d.ObjectMeta.Name))
  243. if len(fake.Actions()) == 0 {
  244. return
  245. }
  246. for _, action := range fake.Actions() {
  247. t.Logf("unexpected action: %#v", action)
  248. }
  249. t.Errorf("expected deployment controller to not take action")
  250. }