2019-01-01 08:23:01 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-05-29 18:53:51 +00:00
|
|
|
"crypto"
|
2019-01-01 08:23:01 +00:00
|
|
|
cryptorand "crypto/rand"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/csv"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
"github.com/rancher/kine/pkg/client"
|
|
|
|
"github.com/rancher/kine/pkg/endpoint"
|
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
certutil "github.com/rancher/dynamiclistener/cert"
|
2019-01-09 16:54:15 +00:00
|
|
|
"github.com/rancher/k3s/pkg/daemons/config"
|
2019-01-01 08:23:01 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"k8s.io/apiserver/pkg/authentication/authenticator"
|
|
|
|
"k8s.io/kubernetes/cmd/kube-apiserver/app"
|
|
|
|
cmapp "k8s.io/kubernetes/cmd/kube-controller-manager/app"
|
|
|
|
sapp "k8s.io/kubernetes/cmd/kube-scheduler/app"
|
|
|
|
_ "k8s.io/kubernetes/pkg/client/metrics/prometheus" // for client metric registration
|
|
|
|
"k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
|
|
|
|
"k8s.io/kubernetes/pkg/master"
|
2019-02-08 04:14:02 +00:00
|
|
|
"k8s.io/kubernetes/pkg/proxy/util"
|
2019-01-01 08:23:01 +00:00
|
|
|
_ "k8s.io/kubernetes/pkg/util/reflector/prometheus" // for reflector metric registration
|
|
|
|
_ "k8s.io/kubernetes/pkg/util/workqueue/prometheus" // for workqueue metric registration
|
|
|
|
_ "k8s.io/kubernetes/pkg/version/prometheus" // for version metric registration
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
localhostIP = net.ParseIP("127.0.0.1")
|
2019-05-29 18:53:51 +00:00
|
|
|
requestHeaderCN = "system:auth-proxy"
|
2019-01-01 08:23:01 +00:00
|
|
|
kubeconfigTemplate = template.Must(template.New("kubeconfig").Parse(`apiVersion: v1
|
|
|
|
clusters:
|
|
|
|
- cluster:
|
|
|
|
server: {{.URL}}
|
2019-05-29 18:53:51 +00:00
|
|
|
certificate-authority: {{.CACert}}
|
2019-01-01 08:23:01 +00:00
|
|
|
name: local
|
|
|
|
contexts:
|
|
|
|
- context:
|
|
|
|
cluster: local
|
|
|
|
namespace: default
|
|
|
|
user: user
|
|
|
|
name: Default
|
|
|
|
current-context: Default
|
|
|
|
kind: Config
|
|
|
|
preferences: {}
|
|
|
|
users:
|
|
|
|
- name: user
|
|
|
|
user:
|
2019-05-29 18:53:51 +00:00
|
|
|
client-certificate: {{.ClientCert}}
|
|
|
|
client-key: {{.ClientKey}}
|
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-01-01 08:23:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Runtime.Tunnel = setupTunnel()
|
2019-02-08 04:14:02 +00:00
|
|
|
util.DisableProxyHostnameCheck = true
|
2019-01-01 08:23:01 +00:00
|
|
|
|
|
|
|
auth, handler, err := apiServer(ctx, cfg, runtime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Handler = handler
|
|
|
|
runtime.Authenticator = auth
|
|
|
|
|
|
|
|
if !cfg.NoScheduler {
|
|
|
|
scheduler(cfg, runtime)
|
|
|
|
}
|
|
|
|
|
|
|
|
controllerManager(cfg, runtime)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
func controllerManager(cfg *config.Control, runtime *config.ControlRuntime) {
|
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",
|
|
|
|
"cluster-cidr": cfg.ClusterIPRange.String(),
|
2019-05-29 18:53:51 +00:00
|
|
|
"root-ca-file": runtime.ServerCA,
|
2019-04-05 00:43:00 +00:00
|
|
|
"port": "10252",
|
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",
|
|
|
|
"cluster-signing-cert-file": runtime.ServerCA,
|
|
|
|
"cluster-signing-key-file": runtime.ServerCAKey,
|
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.ExtraControllerArgs)
|
|
|
|
|
2019-01-01 08:23:01 +00:00
|
|
|
command := cmapp.NewControllerManagerCommand()
|
|
|
|
command.SetArgs(args)
|
|
|
|
|
|
|
|
go func() {
|
2019-01-09 16:54:15 +00:00
|
|
|
logrus.Infof("Running kube-controller-manager %s", config.ArgString(args))
|
2019-01-01 08:23:01 +00:00
|
|
|
logrus.Fatalf("controller-manager exited: %v", command.Execute())
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
func scheduler(cfg *config.Control, runtime *config.ControlRuntime) {
|
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",
|
|
|
|
"bind-address": "127.0.0.1",
|
|
|
|
"secure-port": "0",
|
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)
|
|
|
|
|
2019-01-01 08:23:01 +00:00
|
|
|
command := sapp.NewSchedulerCommand()
|
|
|
|
command.SetArgs(args)
|
|
|
|
|
|
|
|
go func() {
|
2019-01-09 16:54:15 +00:00
|
|
|
logrus.Infof("Running kube-scheduler %s", config.ArgString(args))
|
2019-01-01 08:23:01 +00:00
|
|
|
logrus.Fatalf("scheduler exited: %v", command.Execute())
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-02-08 04:14:20 +00:00
|
|
|
certDir := filepath.Join(cfg.DataDir, "tls/temporary-certs")
|
|
|
|
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
|
|
|
|
argsMap["service-cluster-ip-range"] = cfg.ServiceIPRange.String()
|
|
|
|
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"
|
|
|
|
argsMap["secure-port"] = strconv.Itoa(cfg.ListenPort)
|
|
|
|
argsMap["bind-address"] = localhostIP.String()
|
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
|
|
|
|
argsMap["service-account-issuer"] = "k3s"
|
|
|
|
argsMap["api-audiences"] = "unknown"
|
|
|
|
argsMap["basic-auth-file"] = runtime.PasswdFile
|
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
|
|
|
|
argsMap["requestheader-allowed-names"] = requestHeaderCN
|
|
|
|
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-08-27 04:36:56 +00:00
|
|
|
argsMap["anonymous-auth"] = "false"
|
2019-04-05 00:43:00 +00:00
|
|
|
|
|
|
|
args := config.GetArgsList(argsMap, cfg.ExtraAPIArgs)
|
|
|
|
|
2019-01-01 08:23:01 +00:00
|
|
|
command := app.NewAPIServerCommand(ctx.Done())
|
|
|
|
command.SetArgs(args)
|
|
|
|
|
|
|
|
go func() {
|
2019-01-09 16:54:15 +00:00
|
|
|
logrus.Infof("Running kube-apiserver %s", config.ArgString(args))
|
2019-01-01 08:23:01 +00:00
|
|
|
logrus.Fatalf("apiserver exited: %v", command.Execute())
|
|
|
|
}()
|
|
|
|
|
|
|
|
startupConfig := <-app.StartupConfig
|
|
|
|
|
|
|
|
return startupConfig.Authenticator, startupConfig.Handler, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if config.ListenPort == 0 {
|
|
|
|
config.ListenPort = 6444
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
os.MkdirAll(path.Join(config.DataDir, "tls"), 0700)
|
|
|
|
os.MkdirAll(path.Join(config.DataDir, "cred"), 0700)
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
runtime.ClientCA = path.Join(config.DataDir, "tls", "client-ca.crt")
|
|
|
|
runtime.ClientCAKey = path.Join(config.DataDir, "tls", "client-ca.key")
|
|
|
|
runtime.ServerCA = path.Join(config.DataDir, "tls", "server-ca.crt")
|
|
|
|
runtime.ServerCAKey = path.Join(config.DataDir, "tls", "server-ca.key")
|
2019-04-10 18:09:38 +00:00
|
|
|
runtime.RequestHeaderCA = path.Join(config.DataDir, "tls", "request-header-ca.crt")
|
|
|
|
runtime.RequestHeaderCAKey = path.Join(config.DataDir, "tls", "request-header-ca.key")
|
2019-05-29 18:53:51 +00:00
|
|
|
|
|
|
|
runtime.ServiceKey = path.Join(config.DataDir, "tls", "service.key")
|
|
|
|
runtime.PasswdFile = path.Join(config.DataDir, "cred", "passwd")
|
2019-06-25 17:42:15 +00:00
|
|
|
runtime.NodePasswdFile = path.Join(config.DataDir, "cred", "node-passwd")
|
2019-05-29 18:53:51 +00:00
|
|
|
|
|
|
|
runtime.KubeConfigAdmin = path.Join(config.DataDir, "cred", "admin.kubeconfig")
|
|
|
|
runtime.KubeConfigController = path.Join(config.DataDir, "cred", "controller.kubeconfig")
|
|
|
|
runtime.KubeConfigScheduler = path.Join(config.DataDir, "cred", "scheduler.kubeconfig")
|
|
|
|
runtime.KubeConfigAPIServer = path.Join(config.DataDir, "cred", "api-server.kubeconfig")
|
|
|
|
|
|
|
|
runtime.ClientAdminCert = path.Join(config.DataDir, "tls", "client-admin.crt")
|
|
|
|
runtime.ClientAdminKey = path.Join(config.DataDir, "tls", "client-admin.key")
|
|
|
|
runtime.ClientControllerCert = path.Join(config.DataDir, "tls", "client-controller.crt")
|
|
|
|
runtime.ClientControllerKey = path.Join(config.DataDir, "tls", "client-controller.key")
|
|
|
|
runtime.ClientSchedulerCert = path.Join(config.DataDir, "tls", "client-scheduler.crt")
|
|
|
|
runtime.ClientSchedulerKey = path.Join(config.DataDir, "tls", "client-scheduler.key")
|
|
|
|
runtime.ClientKubeAPICert = path.Join(config.DataDir, "tls", "client-kube-apiserver.crt")
|
|
|
|
runtime.ClientKubeAPIKey = path.Join(config.DataDir, "tls", "client-kube-apiserver.key")
|
|
|
|
runtime.ClientKubeProxyCert = path.Join(config.DataDir, "tls", "client-kube-proxy.crt")
|
|
|
|
runtime.ClientKubeProxyKey = path.Join(config.DataDir, "tls", "client-kube-proxy.key")
|
|
|
|
|
|
|
|
runtime.ServingKubeAPICert = path.Join(config.DataDir, "tls", "serving-kube-apiserver.crt")
|
|
|
|
runtime.ServingKubeAPIKey = path.Join(config.DataDir, "tls", "serving-kube-apiserver.key")
|
|
|
|
|
|
|
|
runtime.ClientKubeletKey = path.Join(config.DataDir, "tls", "client-kubelet.key")
|
|
|
|
runtime.ServingKubeletKey = path.Join(config.DataDir, "tls", "serving-kubelet.key")
|
|
|
|
|
2019-04-10 18:09:38 +00:00
|
|
|
runtime.ClientAuthProxyCert = path.Join(config.DataDir, "tls", "client-auth-proxy.crt")
|
2019-05-29 18:53:51 +00:00
|
|
|
runtime.ClientAuthProxyKey = path.Join(config.DataDir, "tls", "client-auth-proxy.key")
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
etcdClient, err := prepareStorageBackend(ctx, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer etcdClient.Close()
|
|
|
|
|
|
|
|
if err := fetchBootstrapData(ctx, config, etcdClient); err != nil {
|
2019-06-27 19:00:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:09:38 +00:00
|
|
|
if err := genCerts(config, runtime); err != nil {
|
2019-01-01 08:23:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:09:38 +00:00
|
|
|
if err := genServiceAccount(runtime); err != nil {
|
2019-01-01 08:23:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := genUsers(config, runtime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
if err := storeBootstrapData(ctx, config, etcdClient); err != nil {
|
2019-06-27 19:00:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-01 08:23:01 +00:00
|
|
|
return readTokens(runtime)
|
|
|
|
}
|
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
func prepareStorageBackend(ctx context.Context, config *config.Control) (client.Client, error) {
|
|
|
|
etcdConfig, err := endpoint.Listen(ctx, config.Storage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Storage.Config = etcdConfig.TLSConfig
|
|
|
|
config.Storage.Endpoint = strings.Join(etcdConfig.Endpoints, ",")
|
|
|
|
config.NoLeaderElect = !etcdConfig.LeaderElect
|
|
|
|
return client.New(etcdConfig)
|
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
func readTokenFile(passwdFile string) (map[string]string, error) {
|
|
|
|
f, err := os.Open(passwdFile)
|
2019-01-01 08:23:01 +00:00
|
|
|
if err != nil {
|
2019-05-29 18:53:51 +00:00
|
|
|
return nil, err
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
2019-05-29 18:53:51 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
2019-01-01 08:23:01 +00:00
|
|
|
reader := csv.NewReader(f)
|
|
|
|
reader.FieldsPerRecord = -1
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
tokens := map[string]string{}
|
|
|
|
|
2019-01-01 08:23:01 +00:00
|
|
|
for {
|
|
|
|
record, err := reader.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-05-29 18:53:51 +00:00
|
|
|
return nil, err
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
if len(record) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
2019-05-29 18:53:51 +00:00
|
|
|
tokens[record[1]] = record[0]
|
|
|
|
}
|
|
|
|
return tokens, nil
|
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
func readTokens(runtime *config.ControlRuntime) error {
|
|
|
|
tokens, err := readTokenFile(runtime.PasswdFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodeToken, ok := tokens["node"]; ok {
|
|
|
|
runtime.NodeToken = "node:" + nodeToken
|
|
|
|
}
|
|
|
|
if clientToken, ok := tokens["admin"]; ok {
|
|
|
|
runtime.ClientToken = "admin:" + clientToken
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-22 21:14:58 +00:00
|
|
|
func ensureNodeToken(config *config.Control, runtime *config.ControlRuntime) error {
|
|
|
|
if config.ClusterSecret == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(runtime.PasswdFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
records := [][]string{}
|
|
|
|
reader := csv.NewReader(f)
|
|
|
|
for {
|
|
|
|
record, err := reader.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
2019-05-29 18:53:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(record) < 3 {
|
|
|
|
return fmt.Errorf("password file '%s' must have at least 3 columns (password, user name, user uid), found %d", runtime.PasswdFile, len(record))
|
|
|
|
}
|
|
|
|
if record[1] == "node" {
|
|
|
|
if record[0] == config.ClusterSecret {
|
2019-01-22 21:14:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-05-29 18:53:51 +00:00
|
|
|
record[0] = config.ClusterSecret
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
2019-05-29 18:53:51 +00:00
|
|
|
records = append(records, record)
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
f.Close()
|
|
|
|
return WritePasswords(runtime.PasswdFile, records)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WritePasswords(passwdFile string, records [][]string) error {
|
|
|
|
out, err := os.Create(passwdFile + ".tmp")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
2019-05-29 18:53:51 +00:00
|
|
|
defer out.Close()
|
2019-01-22 21:14:58 +00:00
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
if err := out.Chmod(0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := csv.NewWriter(out).WriteAll(records); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Rename(passwdFile+".tmp", passwdFile)
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 08:23:01 +00:00
|
|
|
func genUsers(config *config.Control, runtime *config.ControlRuntime) error {
|
|
|
|
if s, err := os.Stat(runtime.PasswdFile); err == nil && s.Size() > 0 {
|
2019-01-22 21:14:58 +00:00
|
|
|
return ensureNodeToken(config, runtime)
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
adminToken, err := getToken()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
systemToken, err := getToken()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nodeToken, err := getToken()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-22 21:14:58 +00:00
|
|
|
if config.ClusterSecret != "" {
|
|
|
|
nodeToken = config.ClusterSecret
|
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
return WritePasswords(runtime.PasswdFile, [][]string{
|
|
|
|
{adminToken, "admin", "admin", "system:masters"},
|
|
|
|
{systemToken, "system", "system", "system:masters"},
|
|
|
|
{nodeToken, "node", "node", "system:masters"},
|
|
|
|
})
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getToken() (string, error) {
|
|
|
|
token := make([]byte, 16, 16)
|
|
|
|
_, err := cryptorand.Read(token)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(token), err
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:09:38 +00:00
|
|
|
func genCerts(config *config.Control, runtime *config.ControlRuntime) error {
|
2019-05-29 18:53:51 +00:00
|
|
|
if err := genClientCerts(config, runtime); err != nil {
|
2019-04-10 18:09:38 +00:00
|
|
|
return err
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
2019-05-29 18:53:51 +00:00
|
|
|
if err := genServerCerts(config, runtime); err != nil {
|
2019-01-01 08:23:01 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-04-10 18:09:38 +00:00
|
|
|
if err := genRequestHeaderCerts(config, runtime); err != nil {
|
|
|
|
return err
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
2019-04-10 18:09:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
type signedCertFactory = func(commonName string, organization []string, certFile, keyFile string) (bool, error)
|
|
|
|
|
|
|
|
func getSigningCertFactory(regen bool, altNames *certutil.AltNames, extKeyUsage []x509.ExtKeyUsage, caCertFile, caKeyFile string) signedCertFactory {
|
|
|
|
return func(commonName string, organization []string, certFile, keyFile string) (bool, error) {
|
|
|
|
return createClientCertKey(regen, commonName, organization, altNames, extKeyUsage, caCertFile, caKeyFile, certFile, keyFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func genClientCerts(config *config.Control, runtime *config.ControlRuntime) error {
|
|
|
|
regen, err := createSigningCertKey("k3s-client", runtime.ClientCA, runtime.ClientCAKey)
|
2019-04-10 18:09:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
factory := getSigningCertFactory(regen, nil, []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, runtime.ClientCA, runtime.ClientCAKey)
|
|
|
|
|
|
|
|
var certGen bool
|
2019-08-22 18:56:00 +00:00
|
|
|
apiEndpoint := fmt.Sprintf("https://127.0.0.1:%d", config.ListenPort)
|
2019-05-29 18:53:51 +00:00
|
|
|
|
|
|
|
certGen, err = factory("system:admin", []string{"system:masters"}, runtime.ClientAdminCert, runtime.ClientAdminKey)
|
2019-01-01 08:23:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-29 18:53:51 +00:00
|
|
|
if certGen {
|
|
|
|
if err := KubeConfig(runtime.KubeConfigAdmin, apiEndpoint, runtime.ServerCA, runtime.ClientAdminCert, runtime.ClientAdminKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
certGen, err = factory("system:kube-controller-manager", nil, runtime.ClientControllerCert, runtime.ClientControllerKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if certGen {
|
|
|
|
if err := KubeConfig(runtime.KubeConfigController, apiEndpoint, runtime.ServerCA, runtime.ClientControllerCert, runtime.ClientControllerKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
certGen, err = factory("system:kube-scheduler", nil, runtime.ClientSchedulerCert, runtime.ClientSchedulerKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if certGen {
|
|
|
|
if err := KubeConfig(runtime.KubeConfigScheduler, apiEndpoint, runtime.ServerCA, runtime.ClientSchedulerCert, runtime.ClientSchedulerKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
certGen, err = factory("kube-apiserver", nil, runtime.ClientKubeAPICert, runtime.ClientKubeAPIKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if certGen {
|
|
|
|
if err := KubeConfig(runtime.KubeConfigAPIServer, apiEndpoint, runtime.ServerCA, runtime.ClientKubeAPICert, runtime.ClientKubeAPIKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = factory("system:kube-proxy", []string{"system:nodes"}, runtime.ClientKubeProxyCert, runtime.ClientKubeProxyKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, _, err := certutil.LoadOrGenerateKeyFile(runtime.ClientKubeletKey); err != nil {
|
2019-01-01 08:23:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:09:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-21 17:45:31 +00:00
|
|
|
func createServerSigningCertKey(config *config.Control, runtime *config.ControlRuntime) (bool, error) {
|
|
|
|
TokenCA := path.Join(config.DataDir, "tls", "token-ca.crt")
|
|
|
|
TokenCAKey := path.Join(config.DataDir, "tls", "token-ca.key")
|
|
|
|
|
|
|
|
if exists(TokenCA, TokenCAKey) && !exists(runtime.ServerCA) && !exists(runtime.ServerCAKey) {
|
|
|
|
logrus.Infof("Upgrading token-ca files to server-ca")
|
|
|
|
if err := os.Link(TokenCA, runtime.ServerCA); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if err := os.Link(TokenCAKey, runtime.ServerCAKey); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return createSigningCertKey("k3s-server", runtime.ServerCA, runtime.ServerCAKey)
|
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
func genServerCerts(config *config.Control, runtime *config.ControlRuntime) error {
|
2019-06-21 17:45:31 +00:00
|
|
|
regen, err := createServerSigningCertKey(config, runtime)
|
2019-01-01 08:23:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:09:38 +00:00
|
|
|
_, apiServerServiceIP, err := master.DefaultServiceIPRange(*config.ServiceIPRange)
|
2019-01-01 08:23:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
if _, err := createClientCertKey(regen, "kube-apiserver", nil,
|
2019-04-26 22:02:30 +00:00
|
|
|
&certutil.AltNames{
|
2019-04-10 18:09:38 +00:00
|
|
|
DNSNames: []string{"kubernetes.default.svc", "kubernetes.default", "kubernetes", "localhost"},
|
|
|
|
IPs: []net.IP{apiServerServiceIP, localhostIP},
|
2019-05-29 18:53:51 +00:00
|
|
|
}, []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
runtime.ServerCA, runtime.ServerCAKey,
|
|
|
|
runtime.ServingKubeAPICert, runtime.ServingKubeAPIKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, _, err := certutil.LoadOrGenerateKeyFile(runtime.ServingKubeletKey); err != nil {
|
2019-01-01 08:23:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:09:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func genRequestHeaderCerts(config *config.Control, runtime *config.ControlRuntime) error {
|
|
|
|
regen, err := createSigningCertKey("k3s-request-header", runtime.RequestHeaderCA, runtime.RequestHeaderCAKey)
|
2019-01-01 08:23:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
if _, err := createClientCertKey(regen, requestHeaderCN, nil,
|
|
|
|
nil, []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
2019-04-10 18:09:38 +00:00
|
|
|
runtime.RequestHeaderCA, runtime.RequestHeaderCAKey,
|
|
|
|
runtime.ClientAuthProxyCert, runtime.ClientAuthProxyKey); err != nil {
|
2019-01-01 08:23:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:09:38 +00:00
|
|
|
return nil
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
func createClientCertKey(regen bool, commonName string, organization []string, altNames *certutil.AltNames, extKeyUsage []x509.ExtKeyUsage, caCertFile, caKeyFile, certFile, keyFile string) (bool, error) {
|
2019-01-01 08:23:01 +00:00
|
|
|
if !regen {
|
2019-04-10 18:09:38 +00:00
|
|
|
if exists(certFile, keyFile) {
|
2019-05-29 18:53:51 +00:00
|
|
|
return false, nil
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:09:38 +00:00
|
|
|
caKeyBytes, err := ioutil.ReadFile(caKeyFile)
|
2019-01-01 08:23:01 +00:00
|
|
|
if err != nil {
|
2019-05-29 18:53:51 +00:00
|
|
|
return false, err
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
caKey, err := certutil.ParsePrivateKeyPEM(caKeyBytes)
|
2019-01-01 08:23:01 +00:00
|
|
|
if err != nil {
|
2019-05-29 18:53:51 +00:00
|
|
|
return false, err
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
caBytes, err := ioutil.ReadFile(caCertFile)
|
2019-01-01 08:23:01 +00:00
|
|
|
if err != nil {
|
2019-05-29 18:53:51 +00:00
|
|
|
return false, err
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
caCert, err := certutil.ParseCertsPEM(caBytes)
|
|
|
|
if err != nil {
|
2019-05-29 18:53:51 +00:00
|
|
|
return false, err
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
keyBytes, _, err := certutil.LoadOrGenerateKeyFile(keyFile)
|
2019-01-01 08:23:01 +00:00
|
|
|
if err != nil {
|
2019-05-29 18:53:51 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := certutil.ParsePrivateKeyPEM(keyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 18:09:38 +00:00
|
|
|
cfg := certutil.Config{
|
|
|
|
CommonName: commonName,
|
|
|
|
Organization: organization,
|
|
|
|
Usages: extKeyUsage,
|
|
|
|
}
|
|
|
|
if altNames != nil {
|
|
|
|
cfg.AltNames = *altNames
|
|
|
|
}
|
2019-05-29 18:53:51 +00:00
|
|
|
cert, err := certutil.NewSignedCert(cfg, key.(crypto.Signer), caCert[0], caKey.(crypto.Signer))
|
2019-01-01 08:23:01 +00:00
|
|
|
if err != nil {
|
2019-05-29 18:53:51 +00:00
|
|
|
return false, err
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
return true, certutil.WriteCert(certFile, append(certutil.EncodeCertPEM(cert), certutil.EncodeCertPEM(caCert[0])...))
|
2019-04-10 18:09:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func exists(files ...string) bool {
|
|
|
|
for _, file := range files {
|
|
|
|
if _, err := os.Stat(file); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func genServiceAccount(runtime *config.ControlRuntime) error {
|
|
|
|
_, keyErr := os.Stat(runtime.ServiceKey)
|
|
|
|
if keyErr == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := certutil.NewPrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return certutil.WriteKey(runtime.ServiceKey, certutil.EncodePrivateKeyPEM(key))
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:09:38 +00:00
|
|
|
func createSigningCertKey(prefix, certFile, keyFile string) (bool, error) {
|
|
|
|
if exists(certFile, keyFile) {
|
|
|
|
return false, nil
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
caKeyBytes, _, err := certutil.LoadOrGenerateKeyFile(keyFile)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
caKey, err := certutil.ParsePrivateKeyPEM(caKeyBytes)
|
2019-01-01 08:23:01 +00:00
|
|
|
if err != nil {
|
2019-04-10 18:09:38 +00:00
|
|
|
return false, err
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg := certutil.Config{
|
2019-04-10 18:09:38 +00:00
|
|
|
CommonName: fmt.Sprintf("%s-ca@%d", prefix, time.Now().Unix()),
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
cert, err := certutil.NewSelfSignedCACert(cfg, caKey.(crypto.Signer))
|
2019-01-01 08:23:01 +00:00
|
|
|
if err != nil {
|
2019-04-10 18:09:38 +00:00
|
|
|
return false, err
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 18:09:38 +00:00
|
|
|
if err := certutil.WriteCert(certFile, certutil.EncodeCertPEM(cert)); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
func KubeConfig(dest, url, caCert, clientCert, clientKey string) error {
|
2019-01-01 08:23:01 +00:00
|
|
|
data := struct {
|
2019-05-29 18:53:51 +00:00
|
|
|
URL string
|
|
|
|
CACert string
|
|
|
|
ClientCert string
|
|
|
|
ClientKey string
|
2019-01-01 08:23:01 +00:00
|
|
|
}{
|
2019-05-29 18:53:51 +00:00
|
|
|
URL: url,
|
|
|
|
CACert: caCert,
|
|
|
|
ClientCert: clientCert,
|
|
|
|
ClientKey: clientKey,
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
output, err := os.Create(dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer output.Close()
|
|
|
|
|
|
|
|
return kubeconfigTemplate.Execute(output, &data)
|
|
|
|
}
|
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-07-26 21:54:44 +00:00
|
|
|
if len(cfg.Storage.Endpoint) > 0 {
|
|
|
|
argsMap["etcd-servers"] = cfg.Storage.Endpoint
|
2019-06-11 22:48:47 +00:00
|
|
|
}
|
|
|
|
// storage backend tls configuration
|
2019-07-26 21:54:44 +00:00
|
|
|
if len(cfg.Storage.CAFile) > 0 {
|
|
|
|
argsMap["etcd-cafile"] = cfg.Storage.CAFile
|
2019-06-11 22:48:47 +00:00
|
|
|
}
|
2019-07-26 21:54:44 +00:00
|
|
|
if len(cfg.Storage.CertFile) > 0 {
|
|
|
|
argsMap["etcd-certfile"] = cfg.Storage.CertFile
|
2019-06-11 22:48:47 +00:00
|
|
|
}
|
2019-07-26 21:54:44 +00:00
|
|
|
if len(cfg.Storage.KeyFile) > 0 {
|
|
|
|
argsMap["etcd-keyfile"] = cfg.Storage.KeyFile
|
2019-06-11 22:48:47 +00:00
|
|
|
}
|
|
|
|
}
|