top.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 top
  14. import (
  15. "github.com/spf13/cobra"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/cli-runtime/pkg/genericclioptions"
  18. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  19. "k8s.io/kubernetes/pkg/kubectl/util/i18n"
  20. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  21. metricsapi "k8s.io/metrics/pkg/apis/metrics"
  22. )
  23. const (
  24. sortByCPU = "cpu"
  25. sortByMemory = "memory"
  26. )
  27. var (
  28. supportedMetricsAPIVersions = []string{
  29. "v1beta1",
  30. }
  31. topLong = templates.LongDesc(i18n.T(`
  32. Display Resource (CPU/Memory/Storage) usage.
  33. The top command allows you to see the resource consumption for nodes or pods.
  34. This command requires Heapster to be correctly configured and working on the server. `))
  35. )
  36. func NewCmdTop(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
  37. cmd := &cobra.Command{
  38. Use: "top",
  39. Short: i18n.T("Display Resource (CPU/Memory/Storage) usage."),
  40. Long: topLong,
  41. Run: cmdutil.DefaultSubCommandRun(streams.ErrOut),
  42. }
  43. // create subcommands
  44. cmd.AddCommand(NewCmdTopNode(f, nil, streams))
  45. cmd.AddCommand(NewCmdTopPod(f, nil, streams))
  46. return cmd
  47. }
  48. func SupportedMetricsAPIVersionAvailable(discoveredAPIGroups *metav1.APIGroupList) bool {
  49. for _, discoveredAPIGroup := range discoveredAPIGroups.Groups {
  50. if discoveredAPIGroup.Name != metricsapi.GroupName {
  51. continue
  52. }
  53. for _, version := range discoveredAPIGroup.Versions {
  54. for _, supportedVersion := range supportedMetricsAPIVersions {
  55. if version.Version == supportedVersion {
  56. return true
  57. }
  58. }
  59. }
  60. }
  61. return false
  62. }