|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Package app does all of the work necessary to create a Kubernetes |
| 18 | +// APIServer by binding together the API, master and APIServer infrastructure. |
| 19 | +// It can be configured and called directly or via the hyperkube framework. |
| 20 | +package origin |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "io/ioutil" |
| 25 | + "net/http" |
| 26 | + "strings" |
| 27 | + |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "k8s.io/apimachinery/pkg/labels" |
| 30 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 31 | + genericapiserver "k8s.io/apiserver/pkg/server" |
| 32 | + "k8s.io/apiserver/pkg/server/healthz" |
| 33 | + kubeclientset "k8s.io/client-go/kubernetes" |
| 34 | + "k8s.io/kube-aggregator/pkg/apis/apiregistration" |
| 35 | + "k8s.io/kube-aggregator/pkg/apis/apiregistration/install" |
| 36 | + aggregatorapiserver "k8s.io/kube-aggregator/pkg/apiserver" |
| 37 | + apiregistrationclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/internalclientset/typed/apiregistration/internalversion" |
| 38 | + "k8s.io/kube-aggregator/pkg/controllers/autoregister" |
| 39 | + kapi "k8s.io/kubernetes/pkg/api" |
| 40 | + informers "k8s.io/kubernetes/pkg/client/informers/informers_generated/internalversion" |
| 41 | +) |
| 42 | + |
| 43 | +func (c *MasterConfig) createAggregatorConfig(kubeAPIServerConfig genericapiserver.Config) (*aggregatorapiserver.Config, error) { |
| 44 | + // make a shallow copy to let us twiddle a few things |
| 45 | + // most of the config actually remains the same. We only need to mess with a couple items related to the particulars of the aggregator |
| 46 | + genericConfig := kubeAPIServerConfig |
| 47 | + |
| 48 | + // the aggregator doesn't wire these up. It just delegates them to the kubeapiserver |
| 49 | + genericConfig.EnableSwaggerUI = false |
| 50 | + genericConfig.OpenAPIConfig = nil |
| 51 | + genericConfig.SwaggerConfig = nil |
| 52 | + genericConfig.FallThroughHandler = nil |
| 53 | + |
| 54 | + // install our types into the scheme so that "normal" RESTOptionsGetters can work for us |
| 55 | + install.Install(kapi.GroupFactoryRegistry, kapi.Registry, kapi.Scheme) |
| 56 | + |
| 57 | + client, err := kubeclientset.NewForConfig(genericConfig.LoopbackClientConfig) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + |
| 62 | + certBytes, err := ioutil.ReadFile(c.Options.AggregatorConfig.ProxyClientInfo.CertFile) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + keyBytes, err := ioutil.ReadFile(c.Options.AggregatorConfig.ProxyClientInfo.KeyFile) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + return &aggregatorapiserver.Config{ |
| 71 | + GenericConfig: &genericConfig, |
| 72 | + CoreAPIServerClient: client, |
| 73 | + ProxyClientCert: certBytes, |
| 74 | + ProxyClientKey: keyBytes, |
| 75 | + }, nil |
| 76 | +} |
| 77 | + |
| 78 | +func createAggregatorServer(aggregatorConfig *aggregatorapiserver.Config, delegateAPIServer genericapiserver.DelegationTarget, sharedInformers informers.SharedInformerFactory, stopCh <-chan struct{}) (*aggregatorapiserver.APIAggregator, error) { |
| 79 | + aggregatorServer, err := aggregatorConfig.Complete().NewWithDelegate(delegateAPIServer, stopCh) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + // create controllers for auto-registration |
| 85 | + apiRegistrationClient, err := apiregistrationclient.NewForConfig(aggregatorConfig.GenericConfig.LoopbackClientConfig) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + autoRegistrationController := autoregister.NewAutoRegisterController(aggregatorServer.APIRegistrationInformers.Apiregistration().InternalVersion().APIServices(), apiRegistrationClient) |
| 90 | + apiServices := apiServicesToRegister(delegateAPIServer, autoRegistrationController) |
| 91 | + |
| 92 | + aggregatorServer.GenericAPIServer.AddPostStartHook("kube-apiserver-autoregistration", func(context genericapiserver.PostStartHookContext) error { |
| 93 | + go autoRegistrationController.Run(5, stopCh) |
| 94 | + return nil |
| 95 | + }) |
| 96 | + aggregatorServer.GenericAPIServer.AddHealthzChecks(healthz.NamedCheck("autoregister-completion", func(r *http.Request) error { |
| 97 | + items, err := aggregatorServer.APIRegistrationInformers.Apiregistration().InternalVersion().APIServices().Lister().List(labels.Everything()) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + missing := []apiregistration.APIService{} |
| 103 | + for _, apiService := range apiServices { |
| 104 | + found := false |
| 105 | + for _, item := range items { |
| 106 | + if item.Name == apiService.Name { |
| 107 | + found = true |
| 108 | + break |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if !found { |
| 113 | + missing = append(missing, *apiService) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if len(missing) > 0 { |
| 118 | + return fmt.Errorf("missing APIService: %v", missing) |
| 119 | + } |
| 120 | + return nil |
| 121 | + })) |
| 122 | + |
| 123 | + return aggregatorServer, nil |
| 124 | +} |
| 125 | + |
| 126 | +func makeAPIService(gv schema.GroupVersion) *apiregistration.APIService { |
| 127 | + return &apiregistration.APIService{ |
| 128 | + ObjectMeta: metav1.ObjectMeta{Name: gv.Version + "." + gv.Group}, |
| 129 | + Spec: apiregistration.APIServiceSpec{ |
| 130 | + Group: gv.Group, |
| 131 | + Version: gv.Version, |
| 132 | + Priority: 100, |
| 133 | + }, |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func apiServicesToRegister(delegateAPIServer genericapiserver.DelegationTarget, registration autoregister.AutoAPIServiceRegistration) []*apiregistration.APIService { |
| 138 | + apiServices := []*apiregistration.APIService{} |
| 139 | + |
| 140 | + for _, curr := range delegateAPIServer.ListedPaths() { |
| 141 | + if curr == "/api/v1" { |
| 142 | + apiService := makeAPIService(schema.GroupVersion{Group: "", Version: "v1"}) |
| 143 | + registration.AddAPIServiceToSync(apiService) |
| 144 | + apiServices = append(apiServices, apiService) |
| 145 | + continue |
| 146 | + } |
| 147 | + |
| 148 | + if !strings.HasPrefix(curr, "/apis/") { |
| 149 | + continue |
| 150 | + } |
| 151 | + // this comes back in a list that looks like /apis/rbac.authorization.k8s.io/v1alpha1 |
| 152 | + tokens := strings.Split(curr, "/") |
| 153 | + if len(tokens) != 4 { |
| 154 | + continue |
| 155 | + } |
| 156 | + |
| 157 | + apiService := makeAPIService(schema.GroupVersion{Group: tokens[2], Version: tokens[3]}) |
| 158 | + |
| 159 | + // TODO this is probably an indication that we need explicit and precise control over the discovery chain |
| 160 | + // but for now its a special case |
| 161 | + // apps has to come last for compatibility with 1.5 kubectl clients |
| 162 | + if apiService.Spec.Group == "apps" { |
| 163 | + apiService.Spec.Priority = 110 |
| 164 | + } |
| 165 | + |
| 166 | + registration.AddAPIServiceToSync(apiService) |
| 167 | + apiServices = append(apiServices, apiService) |
| 168 | + } |
| 169 | + |
| 170 | + return apiServices |
| 171 | +} |
0 commit comments