clusterinfo.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 cmd
  14. import (
  15. "fmt"
  16. "io"
  17. "os"
  18. "strconv"
  19. "k8s.io/kubernetes/pkg/api"
  20. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  21. "k8s.io/kubernetes/pkg/kubectl/resource"
  22. "github.com/daviddengcn/go-colortext"
  23. "github.com/spf13/cobra"
  24. )
  25. var longDescr = `Display addresses of the master and services with label kubernetes.io/cluster-service=true
  26. To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.`
  27. func NewCmdClusterInfo(f *cmdutil.Factory, out io.Writer) *cobra.Command {
  28. cmd := &cobra.Command{
  29. Use: "cluster-info",
  30. // clusterinfo is deprecated.
  31. Aliases: []string{"clusterinfo"},
  32. Short: "Display cluster info",
  33. Long: longDescr,
  34. Run: func(cmd *cobra.Command, args []string) {
  35. err := RunClusterInfo(f, out, cmd)
  36. cmdutil.CheckErr(err)
  37. },
  38. }
  39. cmdutil.AddInclude3rdPartyFlags(cmd)
  40. cmd.AddCommand(NewCmdClusterInfoDump(f, out))
  41. return cmd
  42. }
  43. func RunClusterInfo(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command) error {
  44. if len(os.Args) > 1 && os.Args[1] == "clusterinfo" {
  45. printDeprecationWarning("cluster-info", "clusterinfo")
  46. }
  47. client, err := f.ClientConfig()
  48. if err != nil {
  49. return err
  50. }
  51. printService(out, "Kubernetes master", client.Host)
  52. mapper, typer := f.Object(cmdutil.GetIncludeThirdPartyAPIs(cmd))
  53. cmdNamespace := cmdutil.GetFlagString(cmd, "namespace")
  54. if cmdNamespace == "" {
  55. cmdNamespace = api.NamespaceSystem
  56. }
  57. // TODO use generalized labels once they are implemented (#341)
  58. b := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
  59. NamespaceParam(cmdNamespace).DefaultNamespace().
  60. SelectorParam("kubernetes.io/cluster-service=true").
  61. ResourceTypeOrNameArgs(false, []string{"services"}...).
  62. Latest()
  63. b.Do().Visit(func(r *resource.Info, err error) error {
  64. if err != nil {
  65. return err
  66. }
  67. services := r.Object.(*api.ServiceList).Items
  68. for _, service := range services {
  69. var link string
  70. if len(service.Status.LoadBalancer.Ingress) > 0 {
  71. ingress := service.Status.LoadBalancer.Ingress[0]
  72. ip := ingress.IP
  73. if ip == "" {
  74. ip = ingress.Hostname
  75. }
  76. for _, port := range service.Spec.Ports {
  77. link += "http://" + ip + ":" + strconv.Itoa(int(port.Port)) + " "
  78. }
  79. } else {
  80. if len(client.GroupVersion.Group) == 0 {
  81. link = client.Host + "/api/" + client.GroupVersion.Version + "/proxy/namespaces/" + service.ObjectMeta.Namespace + "/services/" + service.ObjectMeta.Name
  82. } else {
  83. link = client.Host + "/api/" + client.GroupVersion.Group + "/" + client.GroupVersion.Version + "/proxy/namespaces/" + service.ObjectMeta.Namespace + "/services/" + service.ObjectMeta.Name
  84. }
  85. }
  86. name := service.ObjectMeta.Labels["kubernetes.io/name"]
  87. if len(name) == 0 {
  88. name = service.ObjectMeta.Name
  89. }
  90. printService(out, name, link)
  91. }
  92. return nil
  93. })
  94. out.Write([]byte("\nTo further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.\n"))
  95. return nil
  96. // TODO consider printing more information about cluster
  97. }
  98. func printService(out io.Writer, name, link string) {
  99. ct.ChangeColor(ct.Green, false, ct.None, false)
  100. fmt.Fprint(out, name)
  101. ct.ResetColor()
  102. fmt.Fprintf(out, " is running at ")
  103. ct.ChangeColor(ct.Yellow, false, ct.None, false)
  104. fmt.Fprint(out, link)
  105. ct.ResetColor()
  106. fmt.Fprintln(out, "")
  107. }