apiserver.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 services
  14. import (
  15. "fmt"
  16. "net"
  17. "k8s.io/apiserver/pkg/storage/storagebackend"
  18. apiserver "k8s.io/kubernetes/cmd/kube-apiserver/app"
  19. "k8s.io/kubernetes/cmd/kube-apiserver/app/options"
  20. )
  21. const (
  22. clusterIPRange = "10.0.0.1/24"
  23. apiserverClientURL = "http://localhost:8080"
  24. apiserverHealthCheckURL = apiserverClientURL + "/healthz"
  25. )
  26. // APIServer is a server which manages apiserver.
  27. type APIServer struct {
  28. storageConfig storagebackend.Config
  29. stopCh chan struct{}
  30. }
  31. // NewAPIServer creates an apiserver.
  32. func NewAPIServer(storageConfig storagebackend.Config) *APIServer {
  33. return &APIServer{
  34. storageConfig: storageConfig,
  35. stopCh: make(chan struct{}),
  36. }
  37. }
  38. // Start starts the apiserver, returns when apiserver is ready.
  39. func (a *APIServer) Start() error {
  40. o := options.NewServerRunOptions()
  41. o.Etcd.StorageConfig = a.storageConfig
  42. _, ipnet, err := net.ParseCIDR(clusterIPRange)
  43. if err != nil {
  44. return err
  45. }
  46. o.ServiceClusterIPRanges = ipnet.String()
  47. o.AllowPrivileged = true
  48. o.Admission.GenericAdmission.DisablePlugins = []string{"ServiceAccount", "TaintNodesByCondition"}
  49. errCh := make(chan error)
  50. go func() {
  51. defer close(errCh)
  52. completedOptions, err := apiserver.Complete(o)
  53. if err != nil {
  54. errCh <- fmt.Errorf("set apiserver default options error: %v", err)
  55. return
  56. }
  57. err = apiserver.Run(completedOptions, a.stopCh)
  58. if err != nil {
  59. errCh <- fmt.Errorf("run apiserver error: %v", err)
  60. return
  61. }
  62. }()
  63. err = readinessCheck("apiserver", []string{apiserverHealthCheckURL}, errCh)
  64. if err != nil {
  65. return err
  66. }
  67. return nil
  68. }
  69. // Stop stops the apiserver. Currently, there is no way to stop the apiserver.
  70. // The function is here only for completion.
  71. func (a *APIServer) Stop() error {
  72. if a.stopCh != nil {
  73. close(a.stopCh)
  74. a.stopCh = nil
  75. }
  76. return nil
  77. }
  78. const apiserverName = "apiserver"
  79. // Name returns the name of APIServer.
  80. func (a *APIServer) Name() string {
  81. return apiserverName
  82. }
  83. func getAPIServerClientURL() string {
  84. return apiserverClientURL
  85. }
  86. func getAPIServerHealthCheckURL() string {
  87. return apiserverHealthCheckURL
  88. }