top_pod.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 cmd
  14. import (
  15. "errors"
  16. "fmt"
  17. "io"
  18. "time"
  19. "k8s.io/kubernetes/pkg/api"
  20. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  21. "k8s.io/kubernetes/pkg/kubectl/metricsutil"
  22. "k8s.io/kubernetes/pkg/labels"
  23. "github.com/golang/glog"
  24. "github.com/renstrom/dedent"
  25. "github.com/spf13/cobra"
  26. )
  27. type TopPodOptions struct {
  28. ResourceName string
  29. Namespace string
  30. Selector string
  31. AllNamespaces bool
  32. PrintContainers bool
  33. Client *metricsutil.HeapsterMetricsClient
  34. Printer *metricsutil.TopCmdPrinter
  35. }
  36. const metricsCreationDelay = 2 * time.Minute
  37. var (
  38. topPodLong = dedent.Dedent(`
  39. Display Resource (CPU/Memory/Storage) usage of pods.
  40. The 'top pod' command allows you to see the resource consumption of pods.
  41. Due to the metrics pipeline delay, they may be unavailable for a few minutes
  42. since pod creation.`)
  43. topPodExample = dedent.Dedent(`
  44. # Show metrics for all pods in the default namespace
  45. kubectl top pod
  46. # Show metrics for all pods in the given namespace
  47. kubectl top pod --namespace=NAMESPACE
  48. # Show metrics for a given pod and its containers
  49. kubectl top pod POD_NAME --containers
  50. # Show metrics for the pods defined by label name=myLabel
  51. kubectl top pod -l name=myLabel`)
  52. )
  53. func NewCmdTopPod(f *cmdutil.Factory, out io.Writer) *cobra.Command {
  54. options := &TopPodOptions{}
  55. cmd := &cobra.Command{
  56. Use: "pod [NAME | -l label]",
  57. Short: "Display Resource (CPU/Memory/Storage) usage of pods",
  58. Long: topPodLong,
  59. Example: topPodExample,
  60. Run: func(cmd *cobra.Command, args []string) {
  61. if err := options.Complete(f, cmd, args, out); err != nil {
  62. cmdutil.CheckErr(err)
  63. }
  64. if err := options.RunTopPod(); err != nil {
  65. cmdutil.CheckErr(err)
  66. }
  67. },
  68. Aliases: []string{"pods"},
  69. }
  70. cmd.Flags().StringVarP(&options.Selector, "selector", "l", "", "Selector (label query) to filter on")
  71. cmd.Flags().BoolVar(&options.PrintContainers, "containers", false, "If present, print usage of containers within a pod.")
  72. cmd.Flags().BoolVar(&options.AllNamespaces, "all-namespaces", false, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.")
  73. return cmd
  74. }
  75. func (o *TopPodOptions) Complete(f *cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer) error {
  76. var err error
  77. if len(args) == 1 {
  78. o.ResourceName = args[0]
  79. } else if len(args) > 1 {
  80. return cmdutil.UsageError(cmd, cmd.Use)
  81. }
  82. o.Namespace, _, err = f.DefaultNamespace()
  83. if err != nil {
  84. return err
  85. }
  86. cli, err := f.Client()
  87. if err != nil {
  88. return err
  89. }
  90. o.Client = metricsutil.DefaultHeapsterMetricsClient(cli)
  91. o.Printer = metricsutil.NewTopCmdPrinter(out)
  92. return nil
  93. }
  94. func (o *TopPodOptions) Validate() error {
  95. if len(o.ResourceName) > 0 && len(o.Selector) > 0 {
  96. return errors.New("only one of NAME or --selector can be provided")
  97. }
  98. return nil
  99. }
  100. func (o TopPodOptions) RunTopPod() error {
  101. var err error
  102. selector := labels.Everything()
  103. if len(o.Selector) > 0 {
  104. selector, err = labels.Parse(o.Selector)
  105. if err != nil {
  106. return err
  107. }
  108. }
  109. metrics, err := o.Client.GetPodMetrics(o.Namespace, o.ResourceName, o.AllNamespaces, selector)
  110. // TODO: Refactor this once Heapster becomes the API server.
  111. // First we check why no metrics have been received.
  112. if len(metrics) == 0 {
  113. // If the API server query is successful but all the pods are newly created,
  114. // the metrics are probably not ready yet, so we return the error here in the first place.
  115. e := verifyEmptyMetrics(o, selector)
  116. if e != nil {
  117. return e
  118. }
  119. }
  120. if err != nil {
  121. return err
  122. }
  123. return o.Printer.PrintPodMetrics(metrics, o.PrintContainers, o.AllNamespaces)
  124. }
  125. func verifyEmptyMetrics(o TopPodOptions, selector labels.Selector) error {
  126. if len(o.ResourceName) > 0 {
  127. pod, err := o.Client.Pods(o.Namespace).Get(o.ResourceName)
  128. if err != nil {
  129. return err
  130. }
  131. if err := checkPodAge(pod); err != nil {
  132. return err
  133. }
  134. } else {
  135. pods, err := o.Client.Pods(o.Namespace).List(api.ListOptions{
  136. LabelSelector: selector,
  137. })
  138. if err != nil {
  139. return err
  140. }
  141. if len(pods.Items) == 0 {
  142. return nil
  143. }
  144. for _, pod := range pods.Items {
  145. if err := checkPodAge(&pod); err != nil {
  146. return err
  147. }
  148. }
  149. }
  150. return errors.New("metrics not available yet")
  151. }
  152. func checkPodAge(pod *api.Pod) error {
  153. age := time.Since(pod.CreationTimestamp.Time)
  154. if age > metricsCreationDelay {
  155. message := fmt.Sprintf("Metrics not available for pod %s/%s, age: %s", pod.Namespace, pod.Name, age.String())
  156. glog.Warningf(message)
  157. return errors.New(message)
  158. } else {
  159. glog.V(2).Infof("Metrics not yet available for pod %s/%s, age: %s", pod.Namespace, pod.Name, age.String())
  160. return nil
  161. }
  162. }