2015-01-30 17:04:39 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2015-01-30 17:04:39 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-02-07 22:13:22 +00:00
|
|
|
// Package app does all of the work necessary to create a Kubernetes
|
2015-01-30 17:04:39 +00:00
|
|
|
// APIServer by binding together the API, master and APIServer infrastructure.
|
|
|
|
// It can be configured and called directly or via the hyperkube framework.
|
2015-02-07 22:13:22 +00:00
|
|
|
package app
|
2015-01-30 17:04:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
2015-12-05 02:01:29 +00:00
|
|
|
"net/url"
|
2015-01-30 17:04:39 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2015-12-25 00:08:15 +00:00
|
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/cmd/kube-apiserver/app/options"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/admission"
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-11-13 21:20:54 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2016-02-15 14:00:40 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/autoscaling"
|
2016-02-17 17:11:31 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/batch"
|
2015-12-08 14:21:04 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apiserver"
|
2015-12-24 21:05:04 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apiserver/authenticator"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/capabilities"
|
|
|
|
"k8s.io/kubernetes/pkg/cloudprovider"
|
2015-12-24 21:54:40 +00:00
|
|
|
serviceaccountcontroller "k8s.io/kubernetes/pkg/controller/serviceaccount"
|
2015-11-16 21:46:00 +00:00
|
|
|
"k8s.io/kubernetes/pkg/genericapiserver"
|
2015-10-27 13:18:45 +00:00
|
|
|
kubeletclient "k8s.io/kubernetes/pkg/kubelet/client"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/master"
|
2016-02-05 07:47:27 +00:00
|
|
|
"k8s.io/kubernetes/pkg/registry/cachesize"
|
2015-12-24 21:54:40 +00:00
|
|
|
"k8s.io/kubernetes/pkg/serviceaccount"
|
2015-05-12 02:41:13 +00:00
|
|
|
)
|
|
|
|
|
2015-10-12 14:33:39 +00:00
|
|
|
// NewAPIServerCommand creates a *cobra.Command object with default parameters
|
|
|
|
func NewAPIServerCommand() *cobra.Command {
|
2015-12-25 00:08:15 +00:00
|
|
|
s := options.NewAPIServer()
|
2015-10-12 14:33:39 +00:00
|
|
|
s.AddFlags(pflag.CommandLine)
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "kube-apiserver",
|
|
|
|
Long: `The Kubernetes API server validates and configures data
|
|
|
|
for the api objects which include pods, services, replicationcontrollers, and
|
|
|
|
others. The API Server services REST operations and provides the frontend to the
|
|
|
|
cluster's shared state through which all other components interact.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2015-01-30 17:04:39 +00:00
|
|
|
// Run runs the specified APIServer. This should never exit.
|
2015-12-25 00:08:15 +00:00
|
|
|
func Run(s *options.APIServer) error {
|
2016-04-15 01:01:31 +00:00
|
|
|
genericapiserver.DefaultAndValidateRunOptions(s.ServerRunOptions)
|
2015-05-22 06:12:57 +00:00
|
|
|
|
2015-01-30 17:04:39 +00:00
|
|
|
capabilities.Initialize(capabilities.Capabilities{
|
|
|
|
AllowPrivileged: s.AllowPrivileged,
|
2015-03-24 23:09:16 +00:00
|
|
|
// TODO(vmarmol): Implement support for HostNetworkSources.
|
2015-08-24 17:41:51 +00:00
|
|
|
PrivilegedSources: capabilities.PrivilegedSources{
|
|
|
|
HostNetworkSources: []string{},
|
2015-09-15 16:43:59 +00:00
|
|
|
HostPIDSources: []string{},
|
2015-08-10 08:14:01 +00:00
|
|
|
HostIPCSources: []string{},
|
2015-08-24 17:41:51 +00:00
|
|
|
},
|
2015-07-29 05:00:15 +00:00
|
|
|
PerConnectionBandwidthLimitBytesPerSec: s.MaxConnectionBytesPerSec,
|
2015-01-30 17:04:39 +00:00
|
|
|
})
|
|
|
|
|
2015-10-09 05:18:16 +00:00
|
|
|
// Setup tunneler if needed
|
2016-04-22 00:48:35 +00:00
|
|
|
var tunneler genericapiserver.Tunneler
|
2015-10-09 05:18:16 +00:00
|
|
|
var proxyDialerFn apiserver.ProxyDialerFunc
|
|
|
|
if len(s.SSHUser) > 0 {
|
|
|
|
// Get ssh key distribution func, if supported
|
2016-04-22 00:48:35 +00:00
|
|
|
var installSSH genericapiserver.InstallSSHKey
|
2016-04-27 21:25:59 +00:00
|
|
|
cloud, err := cloudprovider.InitCloudProvider(s.CloudProvider, s.CloudConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("Cloud provider could not be initialized: %v", err)
|
|
|
|
}
|
2015-10-09 05:18:16 +00:00
|
|
|
if cloud != nil {
|
|
|
|
if instances, supported := cloud.Instances(); supported {
|
|
|
|
installSSH = instances.AddSSHKeyToAllInstances
|
|
|
|
}
|
|
|
|
}
|
2015-12-05 02:01:29 +00:00
|
|
|
if s.KubeletConfig.Port == 0 {
|
|
|
|
glog.Fatalf("Must enable kubelet port if proxy ssh-tunneling is specified.")
|
|
|
|
}
|
2015-10-09 05:18:16 +00:00
|
|
|
// Set up the tunneler
|
2015-12-05 02:01:29 +00:00
|
|
|
// TODO(cjcullen): If we want this to handle per-kubelet ports or other
|
|
|
|
// kubelet listen-addresses, we need to plumb through options.
|
|
|
|
healthCheckPath := &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: net.JoinHostPort("127.0.0.1", strconv.FormatUint(uint64(s.KubeletConfig.Port), 10)),
|
|
|
|
Path: "healthz",
|
|
|
|
}
|
2016-04-22 00:48:35 +00:00
|
|
|
tunneler = genericapiserver.NewSSHTunneler(s.SSHUser, s.SSHKeyfile, healthCheckPath, installSSH)
|
2015-10-09 05:18:16 +00:00
|
|
|
|
|
|
|
// Use the tunneler's dialer to connect to the kubelet
|
|
|
|
s.KubeletConfig.Dial = tunneler.Dial
|
|
|
|
// Use the tunneler's dialer when proxying to pods, services, and nodes
|
|
|
|
proxyDialerFn = tunneler.Dial
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proxying to pods and services is IP-based... don't expect to be able to verify the hostname
|
|
|
|
proxyTLSClientConfig := &tls.Config{InsecureSkipVerify: true}
|
|
|
|
|
2015-10-27 13:18:45 +00:00
|
|
|
kubeletClient, err := kubeletclient.NewStaticKubeletClient(&s.KubeletConfig)
|
2015-01-30 17:04:39 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("Failure to start kubelet client: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-04-26 07:43:56 +00:00
|
|
|
storageGroupsToEncodingVersion, err := s.StorageGroupsToEncodingVersion()
|
2015-10-13 00:40:37 +00:00
|
|
|
if err != nil {
|
2016-04-26 07:43:56 +00:00
|
|
|
glog.Fatalf("error generating storage version map: %s", err)
|
2015-05-11 23:09:25 +00:00
|
|
|
}
|
2016-04-26 07:43:56 +00:00
|
|
|
storageFactory, err := genericapiserver.BuildDefaultStorageFactory(
|
|
|
|
s.StorageConfig, s.DefaultStorageMediaType, api.Codecs,
|
|
|
|
genericapiserver.NewDefaultResourceEncodingConfig(), storageGroupsToEncodingVersion,
|
|
|
|
master.DefaultAPIResourceConfigSource(), s.RuntimeConfig)
|
2015-01-30 17:04:39 +00:00
|
|
|
if err != nil {
|
2016-04-26 07:43:56 +00:00
|
|
|
glog.Fatalf("error in initializing storage factory: %s", err)
|
2015-01-30 17:04:39 +00:00
|
|
|
}
|
2016-03-16 14:17:04 +00:00
|
|
|
storageFactory.AddCohabitatingResources(batch.Resource("jobs"), extensions.Resource("jobs"))
|
|
|
|
storageFactory.AddCohabitatingResources(autoscaling.Resource("horizontalpodautoscalers"), extensions.Resource("horizontalpodautoscalers"))
|
|
|
|
for _, override := range s.EtcdServersOverrides {
|
|
|
|
tokens := strings.Split(override, "#")
|
|
|
|
if len(tokens) != 2 {
|
|
|
|
glog.Errorf("invalid value of etcd server overrides: %s", override)
|
|
|
|
continue
|
2016-02-17 17:11:31 +00:00
|
|
|
}
|
|
|
|
|
2016-03-16 14:17:04 +00:00
|
|
|
apiresource := strings.Split(tokens[0], "/")
|
|
|
|
if len(apiresource) != 2 {
|
|
|
|
glog.Errorf("invalid resource definition: %s", tokens[0])
|
|
|
|
continue
|
2016-04-15 22:30:15 +00:00
|
|
|
}
|
2016-03-16 14:17:04 +00:00
|
|
|
group := apiresource[0]
|
|
|
|
resource := apiresource[1]
|
|
|
|
groupResource := unversioned.GroupResource{Group: group, Resource: resource}
|
2016-04-15 22:30:15 +00:00
|
|
|
|
2016-03-16 14:17:04 +00:00
|
|
|
servers := strings.Split(tokens[1], ";")
|
|
|
|
storageFactory.SetEtcdLocation(groupResource, servers)
|
2016-04-15 22:30:15 +00:00
|
|
|
}
|
|
|
|
|
2015-05-01 16:02:38 +00:00
|
|
|
// Default to the private server key for service account token signing
|
|
|
|
if s.ServiceAccountKeyFile == "" && s.TLSPrivateKeyFile != "" {
|
2015-12-24 21:05:04 +00:00
|
|
|
if authenticator.IsValidServiceAccountKeyFile(s.TLSPrivateKeyFile) {
|
2015-06-17 03:02:54 +00:00
|
|
|
s.ServiceAccountKeyFile = s.TLSPrivateKeyFile
|
|
|
|
} else {
|
2015-10-01 12:00:46 +00:00
|
|
|
glog.Warning("No RSA key provided, service account token authentication disabled")
|
2015-06-17 03:02:54 +00:00
|
|
|
}
|
2015-05-01 16:02:38 +00:00
|
|
|
}
|
2015-12-24 21:54:40 +00:00
|
|
|
|
|
|
|
var serviceAccountGetter serviceaccount.ServiceAccountTokenGetter
|
|
|
|
if s.ServiceAccountLookup {
|
|
|
|
// If we need to look up service accounts and tokens,
|
|
|
|
// go directly to etcd to avoid recursive auth insanity
|
2016-03-16 14:17:04 +00:00
|
|
|
storage, err := storageFactory.New(api.Resource("serviceaccounts"))
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("Unable to get serviceaccounts storage: %v", err)
|
|
|
|
}
|
|
|
|
serviceAccountGetter = serviceaccountcontroller.NewGetterFromStorageInterface(storage)
|
2015-12-24 21:54:40 +00:00
|
|
|
}
|
|
|
|
|
2015-12-24 21:05:04 +00:00
|
|
|
authenticator, err := authenticator.New(authenticator.AuthenticatorConfig{
|
2015-12-24 21:54:40 +00:00
|
|
|
BasicAuthFile: s.BasicAuthFile,
|
|
|
|
ClientCAFile: s.ClientCAFile,
|
|
|
|
TokenAuthFile: s.TokenAuthFile,
|
|
|
|
OIDCIssuerURL: s.OIDCIssuerURL,
|
|
|
|
OIDCClientID: s.OIDCClientID,
|
|
|
|
OIDCCAFile: s.OIDCCAFile,
|
|
|
|
OIDCUsernameClaim: s.OIDCUsernameClaim,
|
2016-02-10 21:44:05 +00:00
|
|
|
OIDCGroupsClaim: s.OIDCGroupsClaim,
|
2015-12-24 21:54:40 +00:00
|
|
|
ServiceAccountKeyFile: s.ServiceAccountKeyFile,
|
|
|
|
ServiceAccountLookup: s.ServiceAccountLookup,
|
|
|
|
ServiceAccountTokenGetter: serviceAccountGetter,
|
|
|
|
KeystoneURL: s.KeystoneURL,
|
2015-08-19 00:36:22 +00:00
|
|
|
})
|
|
|
|
|
2015-01-30 17:04:39 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("Invalid Authentication Config: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-08-27 23:18:26 +00:00
|
|
|
authorizationModeNames := strings.Split(s.AuthorizationMode, ",")
|
2016-02-18 17:26:36 +00:00
|
|
|
authorizer, err := apiserver.NewAuthorizerFromAuthorizationConfig(authorizationModeNames, s.AuthorizationConfig)
|
2015-01-30 17:04:39 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("Invalid Authorization Config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
admissionControlPluginNames := strings.Split(s.AdmissionControl, ",")
|
2016-04-26 07:43:56 +00:00
|
|
|
client, err := s.NewSelfClient()
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Failed to create clientset: %v", err)
|
|
|
|
}
|
2015-01-30 17:04:39 +00:00
|
|
|
admissionController := admission.NewFromPlugins(client, admissionControlPluginNames, s.AdmissionControlConfigFile)
|
|
|
|
|
2016-04-15 01:01:31 +00:00
|
|
|
genericConfig := genericapiserver.NewConfig(s.ServerRunOptions)
|
|
|
|
// TODO: Move the following to generic api server as well.
|
2016-03-16 14:17:04 +00:00
|
|
|
genericConfig.StorageFactory = storageFactory
|
2016-04-15 01:01:31 +00:00
|
|
|
genericConfig.Authenticator = authenticator
|
|
|
|
genericConfig.SupportsBasicAuth = len(s.BasicAuthFile) > 0
|
|
|
|
genericConfig.Authorizer = authorizer
|
|
|
|
genericConfig.AdmissionControl = admissionController
|
2016-04-26 07:43:56 +00:00
|
|
|
genericConfig.APIResourceConfigSource = storageFactory.APIResourceConfigSource
|
2016-04-15 01:01:31 +00:00
|
|
|
genericConfig.MasterServiceNamespace = s.MasterServiceNamespace
|
|
|
|
genericConfig.ProxyDialer = proxyDialerFn
|
|
|
|
genericConfig.ProxyTLSClientConfig = proxyTLSClientConfig
|
|
|
|
genericConfig.Serializer = api.Codecs
|
|
|
|
|
2015-01-30 17:04:39 +00:00
|
|
|
config := &master.Config{
|
2016-04-15 01:01:31 +00:00
|
|
|
Config: genericConfig,
|
2016-02-18 13:50:43 +00:00
|
|
|
EnableCoreControllers: true,
|
|
|
|
DeleteCollectionWorkers: s.DeleteCollectionWorkers,
|
|
|
|
EventTTL: s.EventTTL,
|
|
|
|
KubeletClient: kubeletClient,
|
2015-11-16 21:46:00 +00:00
|
|
|
|
|
|
|
Tunneler: tunneler,
|
2015-01-30 17:04:39 +00:00
|
|
|
}
|
2016-02-05 07:47:27 +00:00
|
|
|
|
|
|
|
if s.EnableWatchCache {
|
|
|
|
cachesize.SetWatchCacheSizes(s.WatchCacheSizes)
|
|
|
|
}
|
|
|
|
|
2016-02-03 22:26:11 +00:00
|
|
|
m, err := master.New(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-05 07:47:27 +00:00
|
|
|
|
2016-01-09 02:30:33 +00:00
|
|
|
m.Run(s.ServerRunOptions)
|
2015-01-30 17:04:39 +00:00
|
|
|
return nil
|
|
|
|
}
|