2019-01-01 08:23:01 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-12-16 18:44:13 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-10-27 05:53:25 +00:00
|
|
|
"github.com/rancher/k3s/pkg/cluster"
|
2019-01-09 16:54:15 +00:00
|
|
|
"github.com/rancher/k3s/pkg/daemons/config"
|
2021-03-03 18:14:12 +00:00
|
|
|
"github.com/rancher/k3s/pkg/daemons/control/deps"
|
2020-04-27 17:09:58 +00:00
|
|
|
"github.com/rancher/k3s/pkg/daemons/executor"
|
2021-04-21 22:56:20 +00:00
|
|
|
util2 "github.com/rancher/k3s/pkg/util"
|
2020-05-05 22:09:04 +00:00
|
|
|
"github.com/rancher/k3s/pkg/version"
|
2019-10-15 21:17:26 +00:00
|
|
|
"github.com/rancher/wrangler-api/pkg/generated/controllers/rbac"
|
2019-01-01 08:23:01 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-10-15 21:17:26 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2021-03-18 22:40:29 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2019-01-01 08:23:01 +00:00
|
|
|
"k8s.io/apiserver/pkg/authentication/authenticator"
|
2019-11-09 06:07:12 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2019-10-15 21:17:26 +00:00
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2020-12-01 01:06:26 +00:00
|
|
|
ccm "k8s.io/cloud-provider"
|
2021-03-18 22:40:29 +00:00
|
|
|
cloudprovider "k8s.io/cloud-provider"
|
2020-12-01 01:06:26 +00:00
|
|
|
ccmapp "k8s.io/cloud-provider/app"
|
2021-03-18 22:40:29 +00:00
|
|
|
cloudcontrollerconfig "k8s.io/cloud-provider/app/config"
|
2020-12-01 01:06:26 +00:00
|
|
|
ccmopt "k8s.io/cloud-provider/options"
|
2021-03-18 22:40:29 +00:00
|
|
|
cliflag "k8s.io/component-base/cli/flag"
|
2020-12-01 01:06:26 +00:00
|
|
|
app2 "k8s.io/controller-manager/app"
|
2019-01-01 08:23:01 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
|
2019-02-08 04:14:02 +00:00
|
|
|
"k8s.io/kubernetes/pkg/proxy/util"
|
2020-04-27 17:09:58 +00:00
|
|
|
|
|
|
|
// registering k3s cloud provider
|
|
|
|
_ "github.com/rancher/k3s/pkg/cloudprovider"
|
|
|
|
// for client metric registration
|
|
|
|
_ "k8s.io/component-base/metrics/prometheus/restclient"
|
2019-01-01 08:23:01 +00:00
|
|
|
)
|
|
|
|
|
2021-03-03 18:14:12 +00:00
|
|
|
var localhostIP = net.ParseIP("127.0.0.1")
|
2019-09-03 23:41:54 +00:00
|
|
|
|
2019-01-01 08:23:01 +00:00
|
|
|
func Server(ctx context.Context, cfg *config.Control) error {
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
|
|
|
|
runtime := &config.ControlRuntime{}
|
|
|
|
cfg.Runtime = runtime
|
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
if err := prepare(ctx, cfg, runtime); err != nil {
|
2019-12-16 18:44:13 +00:00
|
|
|
return errors.Wrap(err, "preparing server")
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Runtime.Tunnel = setupTunnel()
|
2019-02-08 04:14:02 +00:00
|
|
|
util.DisableProxyHostnameCheck = true
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2021-02-12 15:35:57 +00:00
|
|
|
var auth authenticator.Request
|
|
|
|
var handler http.Handler
|
|
|
|
var err error
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2021-02-12 15:35:57 +00:00
|
|
|
if !cfg.DisableAPIServer {
|
|
|
|
auth, handler, err = apiServer(ctx, cfg, runtime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-04 11:08:47 +00:00
|
|
|
|
2021-02-12 15:35:57 +00:00
|
|
|
if err := waitForAPIServerInBackground(ctx, runtime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-08-26 19:47:25 +00:00
|
|
|
basicAuth, err := basicAuthenticator(runtime.PasswdFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Authenticator = combineAuthenticators(basicAuth, auth)
|
2019-01-01 08:23:01 +00:00
|
|
|
runtime.Handler = handler
|
|
|
|
|
2021-02-12 15:35:57 +00:00
|
|
|
if !cfg.DisableScheduler {
|
2020-04-27 17:09:58 +00:00
|
|
|
if err := scheduler(cfg, runtime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
2021-02-12 15:35:57 +00:00
|
|
|
if !cfg.DisableControllerManager {
|
|
|
|
if err := controllerManager(cfg, runtime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-27 17:09:58 +00:00
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2019-10-15 21:17:26 +00:00
|
|
|
if !cfg.DisableCCM {
|
2019-11-05 11:44:35 +00:00
|
|
|
cloudControllerManager(ctx, cfg, runtime)
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 08:23:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-27 17:09:58 +00:00
|
|
|
func controllerManager(cfg *config.Control, runtime *config.ControlRuntime) error {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap := map[string]string{
|
2019-05-29 18:53:51 +00:00
|
|
|
"kubeconfig": runtime.KubeConfigController,
|
2019-04-05 00:43:00 +00:00
|
|
|
"service-account-private-key-file": runtime.ServiceKey,
|
|
|
|
"allocate-node-cidrs": "true",
|
2021-04-21 22:56:20 +00:00
|
|
|
"cluster-cidr": util2.JoinIPNets(cfg.ClusterIPRanges),
|
2019-05-29 18:53:51 +00:00
|
|
|
"root-ca-file": runtime.ServerCA,
|
2019-04-05 00:43:00 +00:00
|
|
|
"port": "10252",
|
2020-07-10 20:08:04 +00:00
|
|
|
"profiling": "false",
|
2020-04-29 06:56:27 +00:00
|
|
|
"address": localhostIP.String(),
|
2019-05-29 18:53:51 +00:00
|
|
|
"bind-address": localhostIP.String(),
|
2019-04-05 00:43:00 +00:00
|
|
|
"secure-port": "0",
|
2019-05-29 18:53:51 +00:00
|
|
|
"use-service-account-credentials": "true",
|
2021-05-01 12:58:06 +00:00
|
|
|
"cluster-signing-kube-apiserver-client-cert-file": runtime.ClientCA,
|
|
|
|
"cluster-signing-kube-apiserver-client-key-file": runtime.ClientCAKey,
|
|
|
|
"cluster-signing-kubelet-client-cert-file": runtime.ClientCA,
|
|
|
|
"cluster-signing-kubelet-client-key-file": runtime.ClientCAKey,
|
|
|
|
"cluster-signing-kubelet-serving-cert-file": runtime.ServerCA,
|
|
|
|
"cluster-signing-kubelet-serving-key-file": runtime.ServerCAKey,
|
|
|
|
"cluster-signing-legacy-unknown-cert-file": runtime.ClientCA,
|
|
|
|
"cluster-signing-legacy-unknown-key-file": runtime.ClientCAKey,
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
2019-01-22 21:14:58 +00:00
|
|
|
if cfg.NoLeaderElect {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["leader-elect"] = "false"
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
2020-11-03 17:27:56 +00:00
|
|
|
if !cfg.DisableCCM {
|
|
|
|
argsMap["configure-cloud-routes"] = "false"
|
2020-11-09 20:09:39 +00:00
|
|
|
argsMap["controllers"] = "*,-service,-route,-cloud-node-lifecycle"
|
2020-11-03 17:27:56 +00:00
|
|
|
}
|
2019-04-05 00:43:00 +00:00
|
|
|
|
|
|
|
args := config.GetArgsList(argsMap, cfg.ExtraControllerArgs)
|
2020-04-27 17:09:58 +00:00
|
|
|
logrus.Infof("Running kube-controller-manager %s", config.ArgString(args))
|
2019-04-05 00:43:00 +00:00
|
|
|
|
2020-04-28 22:44:05 +00:00
|
|
|
return executor.ControllerManager(runtime.APIServerReady, args)
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 17:09:58 +00:00
|
|
|
func scheduler(cfg *config.Control, runtime *config.ControlRuntime) error {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap := map[string]string{
|
2019-05-29 18:53:51 +00:00
|
|
|
"kubeconfig": runtime.KubeConfigScheduler,
|
2019-04-05 00:43:00 +00:00
|
|
|
"port": "10251",
|
2020-04-29 06:56:27 +00:00
|
|
|
"address": "127.0.0.1",
|
2019-04-05 00:43:00 +00:00
|
|
|
"bind-address": "127.0.0.1",
|
|
|
|
"secure-port": "0",
|
2020-06-12 19:09:41 +00:00
|
|
|
"profiling": "false",
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
2019-01-22 21:14:58 +00:00
|
|
|
if cfg.NoLeaderElect {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["leader-elect"] = "false"
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
2019-04-05 00:43:00 +00:00
|
|
|
args := config.GetArgsList(argsMap, cfg.ExtraSchedulerAPIArgs)
|
|
|
|
|
2020-04-27 17:09:58 +00:00
|
|
|
logrus.Infof("Running kube-scheduler %s", config.ArgString(args))
|
2020-04-28 22:44:05 +00:00
|
|
|
return executor.Scheduler(runtime.APIServerReady, args)
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
func apiServer(ctx context.Context, cfg *config.Control, runtime *config.ControlRuntime) (authenticator.Request, http.Handler, error) {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap := make(map[string]string)
|
2019-06-11 22:48:47 +00:00
|
|
|
|
|
|
|
setupStorageBackend(argsMap, cfg)
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2020-04-25 07:22:48 +00:00
|
|
|
certDir := filepath.Join(cfg.DataDir, "tls", "temporary-certs")
|
2019-02-08 04:14:20 +00:00
|
|
|
os.MkdirAll(certDir, 0700)
|
|
|
|
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["cert-dir"] = certDir
|
|
|
|
argsMap["allow-privileged"] = "true"
|
|
|
|
argsMap["authorization-mode"] = strings.Join([]string{modes.ModeNode, modes.ModeRBAC}, ",")
|
|
|
|
argsMap["service-account-signing-key-file"] = runtime.ServiceKey
|
2021-04-21 22:56:20 +00:00
|
|
|
argsMap["service-cluster-ip-range"] = util2.JoinIPNets(cfg.ServiceIPRanges)
|
2021-02-01 19:11:17 +00:00
|
|
|
argsMap["service-node-port-range"] = cfg.ServiceNodePortRange.String()
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["advertise-port"] = strconv.Itoa(cfg.AdvertisePort)
|
2019-05-29 18:53:51 +00:00
|
|
|
if cfg.AdvertiseIP != "" {
|
|
|
|
argsMap["advertise-address"] = cfg.AdvertiseIP
|
|
|
|
}
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["insecure-port"] = "0"
|
2020-04-28 22:00:30 +00:00
|
|
|
argsMap["secure-port"] = strconv.Itoa(cfg.APIServerPort)
|
|
|
|
if cfg.APIServerBindAddress == "" {
|
|
|
|
argsMap["bind-address"] = localhostIP.String()
|
|
|
|
} else {
|
|
|
|
argsMap["bind-address"] = cfg.APIServerBindAddress
|
|
|
|
}
|
2019-05-29 18:53:51 +00:00
|
|
|
argsMap["tls-cert-file"] = runtime.ServingKubeAPICert
|
|
|
|
argsMap["tls-private-key-file"] = runtime.ServingKubeAPIKey
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["service-account-key-file"] = runtime.ServiceKey
|
2021-04-28 05:45:33 +00:00
|
|
|
argsMap["service-account-issuer"] = "https://kubernetes.default.svc." + cfg.ClusterDomain
|
|
|
|
argsMap["api-audiences"] = "https://kubernetes.default.svc." + cfg.ClusterDomain + "," + version.Program
|
2019-09-02 17:03:17 +00:00
|
|
|
argsMap["kubelet-certificate-authority"] = runtime.ServerCA
|
2019-05-29 18:53:51 +00:00
|
|
|
argsMap["kubelet-client-certificate"] = runtime.ClientKubeAPICert
|
|
|
|
argsMap["kubelet-client-key"] = runtime.ClientKubeAPIKey
|
2019-04-10 18:09:38 +00:00
|
|
|
argsMap["requestheader-client-ca-file"] = runtime.RequestHeaderCA
|
2021-03-03 18:14:12 +00:00
|
|
|
argsMap["requestheader-allowed-names"] = deps.RequestHeaderCN
|
2019-04-10 18:09:38 +00:00
|
|
|
argsMap["proxy-client-cert-file"] = runtime.ClientAuthProxyCert
|
|
|
|
argsMap["proxy-client-key-file"] = runtime.ClientAuthProxyKey
|
|
|
|
argsMap["requestheader-extra-headers-prefix"] = "X-Remote-Extra-"
|
|
|
|
argsMap["requestheader-group-headers"] = "X-Remote-Group"
|
|
|
|
argsMap["requestheader-username-headers"] = "X-Remote-User"
|
2019-05-29 18:53:51 +00:00
|
|
|
argsMap["client-ca-file"] = runtime.ClientCA
|
2019-09-05 06:34:47 +00:00
|
|
|
argsMap["enable-admission-plugins"] = "NodeRestriction"
|
2019-08-27 04:36:56 +00:00
|
|
|
argsMap["anonymous-auth"] = "false"
|
2020-06-12 19:09:41 +00:00
|
|
|
argsMap["profiling"] = "false"
|
2019-12-12 22:41:10 +00:00
|
|
|
if cfg.EncryptSecrets {
|
|
|
|
argsMap["encryption-provider-config"] = runtime.EncryptionConfig
|
|
|
|
}
|
2019-04-05 00:43:00 +00:00
|
|
|
args := config.GetArgsList(argsMap, cfg.ExtraAPIArgs)
|
|
|
|
|
2020-04-27 17:09:58 +00:00
|
|
|
logrus.Infof("Running kube-apiserver %s", config.ArgString(args))
|
2021-03-03 18:14:12 +00:00
|
|
|
|
2020-05-05 22:02:16 +00:00
|
|
|
return executor.APIServer(ctx, runtime.ETCDReady, args)
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaults(config *config.Control) {
|
|
|
|
if config.ClusterIPRange == nil {
|
|
|
|
_, clusterIPNet, _ := net.ParseCIDR("10.42.0.0/16")
|
|
|
|
config.ClusterIPRange = clusterIPNet
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.ServiceIPRange == nil {
|
|
|
|
_, serviceIPNet, _ := net.ParseCIDR("10.43.0.0/16")
|
|
|
|
config.ServiceIPRange = serviceIPNet
|
|
|
|
}
|
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
if len(config.ClusterDNS) == 0 {
|
|
|
|
config.ClusterDNS = net.ParseIP("10.43.0.10")
|
|
|
|
}
|
|
|
|
|
2019-01-01 08:23:01 +00:00
|
|
|
if config.AdvertisePort == 0 {
|
2019-05-29 18:53:51 +00:00
|
|
|
config.AdvertisePort = config.HTTPSPort
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 22:00:30 +00:00
|
|
|
if config.APIServerPort == 0 {
|
2019-08-27 04:37:47 +00:00
|
|
|
if config.HTTPSPort != 0 {
|
2020-04-28 22:00:30 +00:00
|
|
|
config.APIServerPort = config.HTTPSPort + 1
|
2019-08-27 04:37:47 +00:00
|
|
|
} else {
|
2020-04-28 22:00:30 +00:00
|
|
|
config.APIServerPort = 6444
|
2019-08-27 04:37:47 +00:00
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.DataDir == "" {
|
|
|
|
config.DataDir = "./management-state"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
func prepare(ctx context.Context, config *config.Control, runtime *config.ControlRuntime) error {
|
2019-01-01 08:23:01 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
defaults(config)
|
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
if err := os.MkdirAll(config.DataDir, 0700); err != nil {
|
2019-01-01 08:23:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
config.DataDir, err = filepath.Abs(config.DataDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-25 07:22:48 +00:00
|
|
|
os.MkdirAll(filepath.Join(config.DataDir, "tls"), 0700)
|
|
|
|
os.MkdirAll(filepath.Join(config.DataDir, "cred"), 0700)
|
|
|
|
|
|
|
|
runtime.ClientCA = filepath.Join(config.DataDir, "tls", "client-ca.crt")
|
|
|
|
runtime.ClientCAKey = filepath.Join(config.DataDir, "tls", "client-ca.key")
|
|
|
|
runtime.ServerCA = filepath.Join(config.DataDir, "tls", "server-ca.crt")
|
|
|
|
runtime.ServerCAKey = filepath.Join(config.DataDir, "tls", "server-ca.key")
|
|
|
|
runtime.RequestHeaderCA = filepath.Join(config.DataDir, "tls", "request-header-ca.crt")
|
|
|
|
runtime.RequestHeaderCAKey = filepath.Join(config.DataDir, "tls", "request-header-ca.key")
|
|
|
|
runtime.IPSECKey = filepath.Join(config.DataDir, "cred", "ipsec.psk")
|
|
|
|
|
|
|
|
runtime.ServiceKey = filepath.Join(config.DataDir, "tls", "service.key")
|
|
|
|
runtime.PasswdFile = filepath.Join(config.DataDir, "cred", "passwd")
|
|
|
|
runtime.NodePasswdFile = filepath.Join(config.DataDir, "cred", "node-passwd")
|
|
|
|
|
|
|
|
runtime.KubeConfigAdmin = filepath.Join(config.DataDir, "cred", "admin.kubeconfig")
|
|
|
|
runtime.KubeConfigController = filepath.Join(config.DataDir, "cred", "controller.kubeconfig")
|
|
|
|
runtime.KubeConfigScheduler = filepath.Join(config.DataDir, "cred", "scheduler.kubeconfig")
|
|
|
|
runtime.KubeConfigAPIServer = filepath.Join(config.DataDir, "cred", "api-server.kubeconfig")
|
|
|
|
runtime.KubeConfigCloudController = filepath.Join(config.DataDir, "cred", "cloud-controller.kubeconfig")
|
|
|
|
|
|
|
|
runtime.ClientAdminCert = filepath.Join(config.DataDir, "tls", "client-admin.crt")
|
|
|
|
runtime.ClientAdminKey = filepath.Join(config.DataDir, "tls", "client-admin.key")
|
|
|
|
runtime.ClientControllerCert = filepath.Join(config.DataDir, "tls", "client-controller.crt")
|
|
|
|
runtime.ClientControllerKey = filepath.Join(config.DataDir, "tls", "client-controller.key")
|
|
|
|
runtime.ClientCloudControllerCert = filepath.Join(config.DataDir, "tls", "client-cloud-controller.crt")
|
|
|
|
runtime.ClientCloudControllerKey = filepath.Join(config.DataDir, "tls", "client-cloud-controller.key")
|
|
|
|
runtime.ClientSchedulerCert = filepath.Join(config.DataDir, "tls", "client-scheduler.crt")
|
|
|
|
runtime.ClientSchedulerKey = filepath.Join(config.DataDir, "tls", "client-scheduler.key")
|
|
|
|
runtime.ClientKubeAPICert = filepath.Join(config.DataDir, "tls", "client-kube-apiserver.crt")
|
|
|
|
runtime.ClientKubeAPIKey = filepath.Join(config.DataDir, "tls", "client-kube-apiserver.key")
|
|
|
|
runtime.ClientKubeProxyCert = filepath.Join(config.DataDir, "tls", "client-kube-proxy.crt")
|
|
|
|
runtime.ClientKubeProxyKey = filepath.Join(config.DataDir, "tls", "client-kube-proxy.key")
|
2020-05-05 22:09:04 +00:00
|
|
|
runtime.ClientK3sControllerCert = filepath.Join(config.DataDir, "tls", "client-"+version.Program+"-controller.crt")
|
|
|
|
runtime.ClientK3sControllerKey = filepath.Join(config.DataDir, "tls", "client-"+version.Program+"-controller.key")
|
2020-04-25 07:22:48 +00:00
|
|
|
|
|
|
|
runtime.ServingKubeAPICert = filepath.Join(config.DataDir, "tls", "serving-kube-apiserver.crt")
|
|
|
|
runtime.ServingKubeAPIKey = filepath.Join(config.DataDir, "tls", "serving-kube-apiserver.key")
|
|
|
|
|
|
|
|
runtime.ClientKubeletKey = filepath.Join(config.DataDir, "tls", "client-kubelet.key")
|
|
|
|
runtime.ServingKubeletKey = filepath.Join(config.DataDir, "tls", "serving-kubelet.key")
|
|
|
|
|
|
|
|
runtime.ClientAuthProxyCert = filepath.Join(config.DataDir, "tls", "client-auth-proxy.crt")
|
|
|
|
runtime.ClientAuthProxyKey = filepath.Join(config.DataDir, "tls", "client-auth-proxy.key")
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2020-05-05 22:00:07 +00:00
|
|
|
runtime.ETCDServerCA = filepath.Join(config.DataDir, "tls", "etcd", "server-ca.crt")
|
|
|
|
runtime.ETCDServerCAKey = filepath.Join(config.DataDir, "tls", "etcd", "server-ca.key")
|
|
|
|
runtime.ETCDPeerCA = filepath.Join(config.DataDir, "tls", "etcd", "peer-ca.crt")
|
|
|
|
runtime.ETCDPeerCAKey = filepath.Join(config.DataDir, "tls", "etcd", "peer-ca.key")
|
|
|
|
runtime.ServerETCDCert = filepath.Join(config.DataDir, "tls", "etcd", "server-client.crt")
|
|
|
|
runtime.ServerETCDKey = filepath.Join(config.DataDir, "tls", "etcd", "server-client.key")
|
|
|
|
runtime.PeerServerClientETCDCert = filepath.Join(config.DataDir, "tls", "etcd", "peer-server-client.crt")
|
|
|
|
runtime.PeerServerClientETCDKey = filepath.Join(config.DataDir, "tls", "etcd", "peer-server-client.key")
|
|
|
|
runtime.ClientETCDCert = filepath.Join(config.DataDir, "tls", "etcd", "client.crt")
|
|
|
|
runtime.ClientETCDKey = filepath.Join(config.DataDir, "tls", "etcd", "client.key")
|
|
|
|
|
2019-12-12 22:41:10 +00:00
|
|
|
if config.EncryptSecrets {
|
2020-04-25 07:22:48 +00:00
|
|
|
runtime.EncryptionConfig = filepath.Join(config.DataDir, "cred", "encryption-config.json")
|
2019-12-12 22:41:10 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 22:18:26 +00:00
|
|
|
cluster := cluster.New(config)
|
|
|
|
|
2020-05-05 21:59:15 +00:00
|
|
|
if err := cluster.Bootstrap(ctx); err != nil {
|
2019-06-27 19:00:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-03 18:14:12 +00:00
|
|
|
if err := deps.GenServerDeps(config, runtime); err != nil {
|
2019-10-27 05:53:25 +00:00
|
|
|
return err
|
2019-07-26 21:54:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 21:59:15 +00:00
|
|
|
ready, err := cluster.Start(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.ETCDReady = ready
|
|
|
|
return nil
|
2019-05-29 18:53:51 +00:00
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2019-06-11 22:48:47 +00:00
|
|
|
func setupStorageBackend(argsMap map[string]string, cfg *config.Control) {
|
2019-07-26 21:54:44 +00:00
|
|
|
argsMap["storage-backend"] = "etcd3"
|
2019-06-11 22:48:47 +00:00
|
|
|
// specify the endpoints
|
2019-11-16 00:12:27 +00:00
|
|
|
if len(cfg.Datastore.Endpoint) > 0 {
|
|
|
|
argsMap["etcd-servers"] = cfg.Datastore.Endpoint
|
2019-06-11 22:48:47 +00:00
|
|
|
}
|
|
|
|
// storage backend tls configuration
|
2019-11-16 00:12:27 +00:00
|
|
|
if len(cfg.Datastore.CAFile) > 0 {
|
|
|
|
argsMap["etcd-cafile"] = cfg.Datastore.CAFile
|
2019-06-11 22:48:47 +00:00
|
|
|
}
|
2019-11-16 00:12:27 +00:00
|
|
|
if len(cfg.Datastore.CertFile) > 0 {
|
|
|
|
argsMap["etcd-certfile"] = cfg.Datastore.CertFile
|
2019-06-11 22:48:47 +00:00
|
|
|
}
|
2019-11-16 00:12:27 +00:00
|
|
|
if len(cfg.Datastore.KeyFile) > 0 {
|
|
|
|
argsMap["etcd-keyfile"] = cfg.Datastore.KeyFile
|
2019-06-11 22:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-26 22:38:04 +00:00
|
|
|
|
2019-11-05 11:44:35 +00:00
|
|
|
func cloudControllerManager(ctx context.Context, cfg *config.Control, runtime *config.ControlRuntime) {
|
2020-12-01 01:06:26 +00:00
|
|
|
// Reference: https://github.com/kubernetes/kubernetes/pull/93764
|
|
|
|
// The above-linked change made the in-tree cloud-controller-manager command much more flexible
|
|
|
|
// but much more complicated to wrap. It now validates some of the configuration very early on, before
|
|
|
|
// the CLI args are parsed, so some of the configuration needs to be hardcoded instead of set via flags.
|
2021-03-18 22:40:29 +00:00
|
|
|
// Reference: https://github.com/kubernetes/kubernetes/pull/98210
|
|
|
|
// The above-linked change further clarifies the intent of the example cloud-controller-manager.
|
2020-12-01 01:06:26 +00:00
|
|
|
|
2019-10-15 21:17:26 +00:00
|
|
|
argsMap := map[string]string{
|
2020-12-01 01:06:26 +00:00
|
|
|
"profiling": "false",
|
|
|
|
}
|
|
|
|
args := config.GetArgsList(argsMap, cfg.ExtraCloudControllerArgs)
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
ccmOptions, err := ccmopt.NewCloudControllerManagerOptions()
|
2020-12-01 01:06:26 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("Unable to initialize cloudcontroller options: %v", err)
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
2020-12-01 01:06:26 +00:00
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
ccmOptions.KubeCloudShared.AllocateNodeCIDRs = true
|
|
|
|
ccmOptions.KubeCloudShared.CloudProvider.Name = version.Program
|
2021-04-21 22:56:20 +00:00
|
|
|
ccmOptions.KubeCloudShared.ClusterCIDR = util2.JoinIPNets(cfg.ClusterIPRanges)
|
2021-03-18 22:40:29 +00:00
|
|
|
ccmOptions.KubeCloudShared.ConfigureCloudRoutes = false
|
|
|
|
ccmOptions.Kubeconfig = runtime.KubeConfigCloudController
|
|
|
|
ccmOptions.NodeStatusUpdateFrequency = metav1.Duration{Duration: 1 * time.Minute}
|
|
|
|
ccmOptions.SecureServing.BindAddress = localhostIP
|
|
|
|
ccmOptions.SecureServing.BindPort = 0
|
2020-12-01 01:06:26 +00:00
|
|
|
|
2019-10-15 21:17:26 +00:00
|
|
|
if cfg.NoLeaderElect {
|
2021-03-18 22:40:29 +00:00
|
|
|
ccmOptions.Generic.LeaderElection.LeaderElect = false
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
controllerInitializers := ccmapp.DefaultInitFuncConstructors
|
|
|
|
delete(controllerInitializers, "service")
|
|
|
|
delete(controllerInitializers, "route")
|
2020-12-01 01:06:26 +00:00
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
cloudInitializer := func(config *cloudcontrollerconfig.CompletedConfig) cloudprovider.Interface {
|
|
|
|
cloud, err := ccm.InitCloudProvider(version.Program, runtime.KubeConfigCloudController)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("Cloud provider could not be initialized: %v", err)
|
|
|
|
}
|
|
|
|
if cloud == nil {
|
|
|
|
logrus.Fatalf("Cloud provider is nil")
|
|
|
|
}
|
2020-12-01 01:06:26 +00:00
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
cloud.Initialize(config.ClientBuilder, make(chan struct{}))
|
|
|
|
if informerUserCloud, ok := cloud.(ccm.InformerUser); ok {
|
|
|
|
informerUserCloud.SetInformers(config.SharedInformers)
|
|
|
|
}
|
2020-12-01 01:06:26 +00:00
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
return cloud
|
|
|
|
}
|
2019-10-15 21:17:26 +00:00
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
command := ccmapp.NewCloudControllerManagerCommand(ccmOptions, cloudInitializer, controllerInitializers, cliflag.NamedFlagSets{}, wait.NeverStop)
|
2019-10-15 21:17:26 +00:00
|
|
|
command.SetArgs(args)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
// check for the cloud controller rbac binding
|
|
|
|
if err := checkForCloudControllerPrivileges(runtime); err != nil {
|
|
|
|
logrus.Infof("Waiting for cloudcontroller rbac role to be created")
|
2019-11-05 11:44:35 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
logrus.Fatalf("cloud-controller-manager context canceled: %v", ctx.Err())
|
2021-02-12 15:35:57 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2019-11-05 11:44:35 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
logrus.Infof("Running cloud-controller-manager with args %v", config.ArgString(args))
|
2020-12-01 01:06:26 +00:00
|
|
|
logrus.Fatalf("cloud-controller-manager exited: %v", command.ExecuteContext(ctx))
|
2019-10-15 21:17:26 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkForCloudControllerPrivileges(runtime *config.ControlRuntime) error {
|
|
|
|
restConfig, err := clientcmd.BuildConfigFromFlags("", runtime.KubeConfigAdmin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
crb := rbac.NewFactoryFromConfigOrDie(restConfig).Rbac().V1().ClusterRoleBinding()
|
|
|
|
_, err = crb.Get("cloud-controller-manager", metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-04 11:08:47 +00:00
|
|
|
|
2020-04-28 22:44:05 +00:00
|
|
|
func waitForAPIServerInBackground(ctx context.Context, runtime *config.ControlRuntime) error {
|
2019-11-04 11:08:47 +00:00
|
|
|
restConfig, err := clientcmd.BuildConfigFromFlags("", runtime.KubeConfigAdmin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-09 06:07:12 +00:00
|
|
|
k8sClient, err := kubernetes.NewForConfig(restConfig)
|
2019-11-04 11:08:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-28 22:44:05 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
runtime.APIServerReady = done
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(done)
|
2020-05-05 22:02:16 +00:00
|
|
|
|
|
|
|
etcdLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-runtime.ETCDReady:
|
|
|
|
break etcdLoop
|
|
|
|
case <-time.After(30 * time.Second):
|
|
|
|
logrus.Infof("Waiting for etcd server to become available")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 22:44:05 +00:00
|
|
|
logrus.Infof("Waiting for API server to become available")
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case err := <-promise(func() error { return app2.WaitForAPIServer(k8sClient, 30*time.Second) }):
|
|
|
|
if err != nil {
|
|
|
|
logrus.Infof("Waiting for API server to become available")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
2019-11-09 06:07:12 +00:00
|
|
|
}
|
2019-11-04 11:08:47 +00:00
|
|
|
|
2019-11-09 06:07:12 +00:00
|
|
|
func promise(f func() error) <-chan error {
|
|
|
|
c := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
c <- f()
|
|
|
|
close(c)
|
|
|
|
}()
|
|
|
|
return c
|
2019-11-04 11:08:47 +00:00
|
|
|
}
|