serving.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright 2017 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 and options for initializing kube-apiserver
  14. package options
  15. import (
  16. "fmt"
  17. "net"
  18. utilnet "k8s.io/apimachinery/pkg/util/net"
  19. genericoptions "k8s.io/apiserver/pkg/server/options"
  20. )
  21. // NewSecureServingOptions gives default values for the kube-apiserver which are not the options wanted by
  22. // "normal" API servers running on the platform
  23. func NewSecureServingOptions() *genericoptions.SecureServingOptionsWithLoopback {
  24. o := genericoptions.SecureServingOptions{
  25. BindAddress: net.ParseIP("0.0.0.0"),
  26. BindPort: 6443,
  27. Required: true,
  28. ServerCert: genericoptions.GeneratableKeyCert{
  29. PairName: "apiserver",
  30. CertDirectory: "/var/run/kubernetes",
  31. },
  32. }
  33. return o.WithLoopback()
  34. }
  35. // NewInsecureServingOptions gives default values for the kube-apiserver.
  36. // TODO: switch insecure serving off by default
  37. func NewInsecureServingOptions() *genericoptions.DeprecatedInsecureServingOptionsWithLoopback {
  38. o := genericoptions.DeprecatedInsecureServingOptions{
  39. BindAddress: net.ParseIP("127.0.0.1"),
  40. BindPort: 8080,
  41. }
  42. return o.WithLoopback()
  43. }
  44. // DefaultAdvertiseAddress sets the field AdvertiseAddress if
  45. // unset. The field will be set based on the SecureServingOptions. If
  46. // the SecureServingOptions is not present, DefaultExternalAddress
  47. // will fall back to the insecure ServingOptions.
  48. func DefaultAdvertiseAddress(s *genericoptions.ServerRunOptions, insecure *genericoptions.DeprecatedInsecureServingOptions) error {
  49. if insecure == nil {
  50. return nil
  51. }
  52. if s.AdvertiseAddress == nil || s.AdvertiseAddress.IsUnspecified() {
  53. hostIP, err := utilnet.ChooseBindAddress(insecure.BindAddress)
  54. if err != nil {
  55. return fmt.Errorf("unable to find suitable network address.error='%v'. "+
  56. "Try to set the AdvertiseAddress directly or provide a valid BindAddress to fix this", err)
  57. }
  58. s.AdvertiseAddress = hostIP
  59. }
  60. return nil
  61. }