options.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 options contains flags for initializing a proxy.
  14. package options
  15. import (
  16. "net/url"
  17. "os"
  18. "fmt"
  19. _ "net/http/pprof"
  20. "strings"
  21. "github.com/spf13/pflag"
  22. "k8s.io/kubernetes/pkg/util/validation"
  23. )
  24. type KubeDNSConfig struct {
  25. ClusterDomain string
  26. KubeConfigFile string
  27. KubeMasterURL string
  28. HealthzPort int
  29. DNSPort int
  30. // Federations maps federation names to their registered domain names.
  31. Federations map[string]string
  32. }
  33. func NewKubeDNSConfig() *KubeDNSConfig {
  34. return &KubeDNSConfig{
  35. ClusterDomain: "cluster.local.",
  36. KubeConfigFile: "",
  37. KubeMasterURL: "",
  38. HealthzPort: 8081,
  39. DNSPort: 53,
  40. Federations: make(map[string]string),
  41. }
  42. }
  43. type clusterDomainVar struct {
  44. val *string
  45. }
  46. func (m clusterDomainVar) Set(v string) error {
  47. v = strings.TrimSuffix(v, ".")
  48. segments := strings.Split(v, ".")
  49. for _, segment := range segments {
  50. if errs := validation.IsDNS1123Label(segment); len(errs) > 0 {
  51. return fmt.Errorf("Not a valid DNS label. %v", errs)
  52. }
  53. }
  54. if !strings.HasSuffix(v, ".") {
  55. v = fmt.Sprintf("%s.", v)
  56. }
  57. *m.val = v
  58. return nil
  59. }
  60. func (m clusterDomainVar) String() string {
  61. return *m.val
  62. }
  63. func (m clusterDomainVar) Type() string {
  64. return "string"
  65. }
  66. type kubeMasterURLVar struct {
  67. val *string
  68. }
  69. func (m kubeMasterURLVar) Set(v string) error {
  70. parsedURL, err := url.Parse(os.ExpandEnv(v))
  71. if err != nil {
  72. return fmt.Errorf("failed to parse kube-master-url")
  73. }
  74. if parsedURL.Scheme == "" || parsedURL.Host == "" || parsedURL.Host == ":" {
  75. return fmt.Errorf("invalid kube-master-url specified")
  76. }
  77. *m.val = v
  78. return nil
  79. }
  80. func (m kubeMasterURLVar) String() string {
  81. return *m.val
  82. }
  83. func (m kubeMasterURLVar) Type() string {
  84. return "string"
  85. }
  86. type federationsVar struct {
  87. nameDomainMap map[string]string
  88. }
  89. // Set deserializes the input string in the format
  90. // "myfederation1=example.com,myfederation2=second.example.com,myfederation3=example.com"
  91. // into a map of key-value pairs of federation names to domain names.
  92. func (fv federationsVar) Set(keyVal string) error {
  93. for _, val := range strings.Split(keyVal, ",") {
  94. splits := strings.SplitN(strings.TrimSpace(val), "=", 2)
  95. name := strings.TrimSpace(splits[0])
  96. domain := strings.TrimSpace(splits[1])
  97. if errs := validation.IsDNS1123Label(name); len(errs) != 0 {
  98. return fmt.Errorf("%q not a valid federation name: %q", name, errs)
  99. }
  100. // The federation domain name need not strictly be domain names, we
  101. // accept valid dns names with subdomain components.
  102. if errs := validation.IsDNS1123Subdomain(domain); len(errs) != 0 {
  103. return fmt.Errorf("%q not a valid domain name: %q", domain, errs)
  104. }
  105. fv.nameDomainMap[name] = domain
  106. }
  107. return nil
  108. }
  109. func (fv federationsVar) String() string {
  110. var splits []string
  111. for name, domain := range fv.nameDomainMap {
  112. splits = append(splits, fmt.Sprintf("%s=%s", name, domain))
  113. }
  114. return strings.Join(splits, ",")
  115. }
  116. func (fv federationsVar) Type() string {
  117. return "[]string"
  118. }
  119. func (s *KubeDNSConfig) AddFlags(fs *pflag.FlagSet) {
  120. fs.Var(clusterDomainVar{&s.ClusterDomain}, "domain", "domain under which to create names")
  121. fs.StringVar(&s.KubeConfigFile, "kubecfg-file", s.KubeConfigFile, "Location of kubecfg file for access to kubernetes master service; --kube-master-url overrides the URL part of this; if neither this nor --kube-master-url are provided, defaults to service account tokens")
  122. fs.Var(kubeMasterURLVar{&s.KubeMasterURL}, "kube-master-url", "URL to reach kubernetes master. Env variables in this flag will be expanded.")
  123. fs.IntVar(&s.HealthzPort, "healthz-port", s.HealthzPort, "port on which to serve a kube-dns HTTP readiness probe.")
  124. fs.IntVar(&s.DNSPort, "dns-port", s.DNSPort, "port on which to serve DNS requests.")
  125. fs.Var(federationsVar{s.Federations}, "federations", "a comma separated list of the federation names and their corresponding domain names to which this cluster belongs. Example: \"myfederation1=example.com,myfederation2=example2.com,myfederation3=example.com\"")
  126. }