rollout_status.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. Copyright 2016 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 kubectl
  14. import (
  15. "fmt"
  16. "k8s.io/kubernetes/pkg/api/unversioned"
  17. "k8s.io/kubernetes/pkg/apis/extensions"
  18. client "k8s.io/kubernetes/pkg/client/unversioned"
  19. )
  20. // StatusViewer provides an interface for resources that provides rollout status.
  21. type StatusViewer interface {
  22. Status(namespace, name string) (string, bool, error)
  23. }
  24. func StatusViewerFor(kind unversioned.GroupKind, c client.Interface) (StatusViewer, error) {
  25. switch kind {
  26. case extensions.Kind("Deployment"):
  27. return &DeploymentStatusViewer{c.Extensions()}, nil
  28. }
  29. return nil, fmt.Errorf("no status viewer has been implemented for %v", kind)
  30. }
  31. type DeploymentStatusViewer struct {
  32. c client.ExtensionsInterface
  33. }
  34. // Status returns a message describing deployment status, and a bool value indicating if the status is considered done
  35. func (s *DeploymentStatusViewer) Status(namespace, name string) (string, bool, error) {
  36. deployment, err := s.c.Deployments(namespace).Get(name)
  37. if err != nil {
  38. return "", false, err
  39. }
  40. if deployment.Generation <= deployment.Status.ObservedGeneration {
  41. if deployment.Status.UpdatedReplicas == deployment.Spec.Replicas {
  42. return fmt.Sprintf("deployment %s successfully rolled out\n", name), true, nil
  43. }
  44. return fmt.Sprintf("Waiting for rollout to finish: %d out of %d new replicas have been updated...\n", deployment.Status.UpdatedReplicas, deployment.Spec.Replicas), false, nil
  45. }
  46. return fmt.Sprintf("Waiting for deployment spec update to be observed...\n"), false, nil
  47. }