2021-03-03 18:14:12 +00:00
|
|
|
package deps
|
|
|
|
|
|
|
|
import (
|
2022-12-08 04:05:01 +00:00
|
|
|
"bytes"
|
2021-03-03 18:14:12 +00:00
|
|
|
"crypto"
|
|
|
|
cryptorand "crypto/rand"
|
2021-12-07 22:31:32 +00:00
|
|
|
"crypto/sha256"
|
2021-03-03 18:14:12 +00:00
|
|
|
"crypto/x509"
|
|
|
|
b64 "encoding/base64"
|
2021-12-07 22:31:32 +00:00
|
|
|
"encoding/hex"
|
2021-03-03 18:14:12 +00:00
|
|
|
"encoding/json"
|
2022-02-25 16:43:03 +00:00
|
|
|
"errors"
|
2021-03-03 18:14:12 +00:00
|
|
|
"fmt"
|
2022-12-06 23:45:58 +00:00
|
|
|
"io/fs"
|
2021-03-03 18:14:12 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/clientaccess"
|
2022-09-29 20:37:50 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/cloudprovider"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/daemons/config"
|
|
|
|
"github.com/k3s-io/k3s/pkg/passwd"
|
2023-03-07 23:05:52 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/util"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2021-03-03 18:14:12 +00:00
|
|
|
certutil "github.com/rancher/dynamiclistener/cert"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2022-04-04 21:54:50 +00:00
|
|
|
"k8s.io/apiserver/pkg/apis/apiserver"
|
2021-03-03 18:14:12 +00:00
|
|
|
apiserverconfigv1 "k8s.io/apiserver/pkg/apis/config/v1"
|
2022-04-04 21:53:12 +00:00
|
|
|
"k8s.io/apiserver/pkg/authentication/user"
|
2022-12-06 23:45:58 +00:00
|
|
|
"k8s.io/client-go/util/keyutil"
|
2021-03-03 18:14:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ipsecTokenSize = 48
|
|
|
|
aescbcKeySize = 32
|
|
|
|
|
|
|
|
RequestHeaderCN = "system:auth-proxy"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
kubeconfigTemplate = template.Must(template.New("kubeconfig").Parse(`apiVersion: v1
|
|
|
|
clusters:
|
|
|
|
- cluster:
|
|
|
|
server: {{.URL}}
|
|
|
|
certificate-authority: {{.CACert}}
|
|
|
|
name: local
|
|
|
|
contexts:
|
|
|
|
- context:
|
|
|
|
cluster: local
|
|
|
|
namespace: default
|
|
|
|
user: user
|
|
|
|
name: Default
|
|
|
|
current-context: Default
|
|
|
|
kind: Config
|
|
|
|
preferences: {}
|
|
|
|
users:
|
|
|
|
- name: user
|
|
|
|
user:
|
|
|
|
client-certificate: {{.ClientCert}}
|
|
|
|
client-key: {{.ClientKey}}
|
|
|
|
`))
|
|
|
|
)
|
|
|
|
|
|
|
|
func migratePassword(p *passwd.Passwd) error {
|
|
|
|
server, _ := p.Pass("server")
|
|
|
|
node, _ := p.Pass("node")
|
|
|
|
if server == "" && node != "" {
|
|
|
|
return p.EnsureUser("server", version.Program+":server", node)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func KubeConfig(dest, url, caCert, clientCert, clientKey string) error {
|
|
|
|
data := struct {
|
|
|
|
URL string
|
|
|
|
CACert string
|
|
|
|
ClientCert string
|
|
|
|
ClientKey string
|
|
|
|
}{
|
|
|
|
URL: url,
|
|
|
|
CACert: caCert,
|
|
|
|
ClientCert: clientCert,
|
|
|
|
ClientKey: clientKey,
|
|
|
|
}
|
|
|
|
|
2023-07-14 22:00:27 +00:00
|
|
|
// cis-1.24 and newer require kubeconfigs to be 0600
|
|
|
|
output, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer output.Close()
|
|
|
|
|
|
|
|
return kubeconfigTemplate.Execute(output, &data)
|
|
|
|
}
|
|
|
|
|
2021-10-07 19:47:00 +00:00
|
|
|
// CreateRuntimeCertFiles is responsible for filling out all the
|
2021-07-01 23:08:35 +00:00
|
|
|
// .crt and .key filenames for a ControlRuntime.
|
2022-02-24 19:01:14 +00:00
|
|
|
func CreateRuntimeCertFiles(config *config.Control) {
|
|
|
|
runtime := config.Runtime
|
2021-07-01 23:08:35 +00:00
|
|
|
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")
|
|
|
|
|
2022-12-06 23:45:58 +00:00
|
|
|
runtime.SigningClientCA = filepath.Join(config.DataDir, "tls", "client-ca.nochain.crt")
|
|
|
|
runtime.SigningServerCA = filepath.Join(config.DataDir, "tls", "server-ca.nochain.crt")
|
|
|
|
runtime.ServiceCurrentKey = filepath.Join(config.DataDir, "tls", "service.current.key")
|
|
|
|
|
2021-07-01 23:08:35 +00:00
|
|
|
runtime.KubeConfigAdmin = filepath.Join(config.DataDir, "cred", "admin.kubeconfig")
|
2023-05-26 20:35:17 +00:00
|
|
|
runtime.KubeConfigSupervisor = filepath.Join(config.DataDir, "cred", "supervisor.kubeconfig")
|
2021-07-01 23:08:35 +00:00
|
|
|
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")
|
2023-05-26 20:35:17 +00:00
|
|
|
runtime.ClientSupervisorCert = filepath.Join(config.DataDir, "tls", "client-supervisor.crt")
|
|
|
|
runtime.ClientSupervisorKey = filepath.Join(config.DataDir, "tls", "client-supervisor.key")
|
2021-07-01 23:08:35 +00:00
|
|
|
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-"+version.Program+"-cloud-controller.crt")
|
|
|
|
runtime.ClientCloudControllerKey = filepath.Join(config.DataDir, "tls", "client-"+version.Program+"-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")
|
|
|
|
runtime.ClientK3sControllerCert = filepath.Join(config.DataDir, "tls", "client-"+version.Program+"-controller.crt")
|
|
|
|
runtime.ClientK3sControllerKey = filepath.Join(config.DataDir, "tls", "client-"+version.Program+"-controller.key")
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2022-04-04 21:54:50 +00:00
|
|
|
runtime.EgressSelectorConfig = filepath.Join(config.DataDir, "etc", "egress-selector-config.yaml")
|
2022-09-29 20:37:50 +00:00
|
|
|
runtime.CloudControllerConfig = filepath.Join(config.DataDir, "etc", "cloud-config.yaml")
|
2022-04-04 21:54:50 +00:00
|
|
|
|
2021-07-01 23:08:35 +00:00
|
|
|
runtime.ClientAuthProxyCert = filepath.Join(config.DataDir, "tls", "client-auth-proxy.crt")
|
|
|
|
runtime.ClientAuthProxyKey = filepath.Join(config.DataDir, "tls", "client-auth-proxy.key")
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
if config.EncryptSecrets {
|
|
|
|
runtime.EncryptionConfig = filepath.Join(config.DataDir, "cred", "encryption-config.json")
|
2021-12-07 22:31:32 +00:00
|
|
|
runtime.EncryptionHash = filepath.Join(config.DataDir, "cred", "encryption-state.json")
|
2021-07-01 23:08:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 18:14:12 +00:00
|
|
|
// GenServerDeps is responsible for generating the cluster dependencies
|
|
|
|
// needed to successfully bootstrap a cluster.
|
2022-02-24 19:01:14 +00:00
|
|
|
func GenServerDeps(config *config.Control) error {
|
|
|
|
runtime := config.Runtime
|
|
|
|
if err := genCerts(config); err != nil {
|
2021-03-03 18:14:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := genServiceAccount(runtime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
if err := genUsers(config); err != nil {
|
2021-03-03 18:14:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
if err := genEncryptedNetworkInfo(config); err != nil {
|
2021-03-03 18:14:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
if err := genEncryptionConfigAndState(config); err != nil {
|
2021-03-03 18:14:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:54:50 +00:00
|
|
|
if err := genEgressSelectorConfig(config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-29 20:37:50 +00:00
|
|
|
if err := genCloudConfig(config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-03 18:14:12 +00:00
|
|
|
return readTokens(runtime)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readTokens(runtime *config.ControlRuntime) error {
|
|
|
|
tokens, err := passwd.Read(runtime.PasswdFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodeToken, ok := tokens.Pass("node"); ok {
|
|
|
|
runtime.AgentToken = "node:" + nodeToken
|
|
|
|
}
|
|
|
|
if serverToken, ok := tokens.Pass("server"); ok {
|
|
|
|
runtime.ServerToken = "server:" + serverToken
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNodePass(config *config.Control, serverPass string) string {
|
|
|
|
if config.AgentToken == "" {
|
|
|
|
if _, passwd, ok := clientaccess.ParseUsernamePassword(serverPass); ok {
|
|
|
|
return passwd
|
|
|
|
}
|
|
|
|
return serverPass
|
|
|
|
}
|
|
|
|
return config.AgentToken
|
|
|
|
}
|
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
func genUsers(config *config.Control) error {
|
|
|
|
runtime := config.Runtime
|
2021-03-03 18:14:12 +00:00
|
|
|
passwd, err := passwd.Read(runtime.PasswdFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := migratePassword(passwd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-09 17:58:49 +00:00
|
|
|
// if no token is provided on bootstrap, we generate a random token
|
2021-03-03 18:14:12 +00:00
|
|
|
serverPass, err := getServerPass(passwd, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nodePass := getNodePass(config, serverPass)
|
|
|
|
|
|
|
|
if err := passwd.EnsureUser("node", version.Program+":agent", nodePass); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := passwd.EnsureUser("server", version.Program+":server", serverPass); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return passwd.Write(runtime.PasswdFile)
|
|
|
|
}
|
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
func genEncryptedNetworkInfo(controlConfig *config.Control) error {
|
|
|
|
runtime := controlConfig.Runtime
|
2021-03-03 18:14:12 +00:00
|
|
|
if s, err := os.Stat(runtime.IPSECKey); err == nil && s.Size() > 0 {
|
2022-10-08 00:36:57 +00:00
|
|
|
psk, err := os.ReadFile(runtime.IPSECKey)
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
controlConfig.IPSECPSK = strings.TrimSpace(string(psk))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-01 17:20:32 +00:00
|
|
|
psk, err := util.Random(ipsecTokenSize)
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
controlConfig.IPSECPSK = psk
|
2022-10-08 00:36:57 +00:00
|
|
|
return os.WriteFile(runtime.IPSECKey, []byte(psk+"\n"), 0600)
|
2021-03-03 18:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getServerPass(passwd *passwd.Passwd, config *config.Control) (string, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
serverPass := config.Token
|
|
|
|
if serverPass == "" {
|
|
|
|
serverPass, _ = passwd.Pass("server")
|
|
|
|
}
|
|
|
|
if serverPass == "" {
|
2022-09-01 17:20:32 +00:00
|
|
|
serverPass, err = util.Random(16)
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return serverPass, nil
|
|
|
|
}
|
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
func genCerts(config *config.Control) error {
|
|
|
|
if err := genClientCerts(config); err != nil {
|
2021-03-03 18:14:12 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-02-24 19:01:14 +00:00
|
|
|
if err := genServerCerts(config); err != nil {
|
2021-03-03 18:14:12 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-02-24 19:01:14 +00:00
|
|
|
if err := genRequestHeaderCerts(config); err != nil {
|
2021-03-03 18:14:12 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-02-24 19:01:14 +00:00
|
|
|
return genETCDCerts(config)
|
2021-03-03 18:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
func genClientCerts(config *config.Control) error {
|
|
|
|
runtime := config.Runtime
|
2021-03-03 18:14:12 +00:00
|
|
|
regen, err := createSigningCertKey(version.Program+"-client", runtime.ClientCA, runtime.ClientCAKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-06 23:45:58 +00:00
|
|
|
certs, err := certutil.CertsFromFile(runtime.ClientCA)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If our CA certs are signed by a root or intermediate CA, ClientCA will contain a chain.
|
|
|
|
// The controller-manager's signer wants just a single cert, not a full chain; so create a file
|
|
|
|
// that is guaranteed to contain only a single certificate.
|
|
|
|
if err := certutil.WriteCert(runtime.SigningClientCA, certutil.EncodeCertPEM(certs[0])); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-03 18:14:12 +00:00
|
|
|
factory := getSigningCertFactory(regen, nil, []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, runtime.ClientCA, runtime.ClientCAKey)
|
|
|
|
|
|
|
|
var certGen bool
|
2022-03-29 07:35:54 +00:00
|
|
|
|
2022-07-21 21:40:09 +00:00
|
|
|
apiEndpoint := fmt.Sprintf("https://%s:%d", config.Loopback(true), config.APIServerPort)
|
2021-03-03 18:14:12 +00:00
|
|
|
|
2022-04-04 21:53:12 +00:00
|
|
|
certGen, err = factory("system:admin", []string{user.SystemPrivilegedGroup}, runtime.ClientAdminCert, runtime.ClientAdminKey)
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if certGen {
|
|
|
|
if err := KubeConfig(runtime.KubeConfigAdmin, apiEndpoint, runtime.ServerCA, runtime.ClientAdminCert, runtime.ClientAdminKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 20:35:17 +00:00
|
|
|
certGen, err = factory("system:"+version.Program+"-supervisor", []string{user.SystemPrivilegedGroup}, runtime.ClientSupervisorCert, runtime.ClientSupervisorKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if certGen {
|
|
|
|
if err := KubeConfig(runtime.KubeConfigSupervisor, apiEndpoint, runtime.ServerCA, runtime.ClientSupervisorCert, runtime.ClientSupervisorKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:53:12 +00:00
|
|
|
certGen, err = factory(user.KubeControllerManager, nil, runtime.ClientControllerCert, runtime.ClientControllerKey)
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if certGen {
|
|
|
|
if err := KubeConfig(runtime.KubeConfigController, apiEndpoint, runtime.ServerCA, runtime.ClientControllerCert, runtime.ClientControllerKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:53:12 +00:00
|
|
|
certGen, err = factory(user.KubeScheduler, nil, runtime.ClientSchedulerCert, runtime.ClientSchedulerKey)
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if certGen {
|
|
|
|
if err := KubeConfig(runtime.KubeConfigScheduler, apiEndpoint, runtime.ServerCA, runtime.ClientSchedulerCert, runtime.ClientSchedulerKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:53:12 +00:00
|
|
|
certGen, err = factory(user.APIServerUser, []string{user.SystemPrivilegedGroup}, runtime.ClientKubeAPICert, runtime.ClientKubeAPIKey)
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if certGen {
|
|
|
|
if err := KubeConfig(runtime.KubeConfigAPIServer, apiEndpoint, runtime.ServerCA, runtime.ClientKubeAPICert, runtime.ClientKubeAPIKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:53:12 +00:00
|
|
|
if _, err = factory(user.KubeProxy, nil, runtime.ClientKubeProxyCert, runtime.ClientKubeProxyKey); err != nil {
|
2021-03-03 18:14:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// This user (system:k3s-controller by default) must be bound to a role in rolebindings.yaml or the downstream equivalent
|
|
|
|
if _, err = factory("system:"+version.Program+"-controller", nil, runtime.ClientK3sControllerCert, runtime.ClientK3sControllerKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, _, err := certutil.LoadOrGenerateKeyFile(runtime.ClientKubeletKey, regen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-02 21:50:11 +00:00
|
|
|
certGen, err = factory(version.Program+"-cloud-controller-manager", nil, runtime.ClientCloudControllerCert, runtime.ClientCloudControllerKey)
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if certGen {
|
|
|
|
if err := KubeConfig(runtime.KubeConfigCloudController, apiEndpoint, runtime.ServerCA, runtime.ClientCloudControllerCert, runtime.ClientCloudControllerKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
func genServerCerts(config *config.Control) error {
|
|
|
|
runtime := config.Runtime
|
|
|
|
regen, err := createServerSigningCertKey(config)
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
altNames := &certutil.AltNames{
|
2021-09-01 06:50:23 +00:00
|
|
|
DNSNames: []string{"kubernetes", "kubernetes.default", "kubernetes.default.svc", "kubernetes.default.svc." + config.ClusterDomain},
|
2021-03-03 18:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addSANs(altNames, config.SANs)
|
|
|
|
|
|
|
|
if _, err := createClientCertKey(regen, "kube-apiserver", nil,
|
|
|
|
altNames, []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
runtime.ServerCA, runtime.ServerCAKey,
|
|
|
|
runtime.ServingKubeAPICert, runtime.ServingKubeAPIKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, _, err := certutil.LoadOrGenerateKeyFile(runtime.ServingKubeletKey, regen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
func genETCDCerts(config *config.Control) error {
|
2023-07-11 17:24:35 +00:00
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
runtime := config.Runtime
|
2021-03-03 18:14:12 +00:00
|
|
|
regen, err := createSigningCertKey("etcd-server", runtime.ETCDServerCA, runtime.ETCDServerCAKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-01 06:50:23 +00:00
|
|
|
altNames := &certutil.AltNames{}
|
2021-03-03 18:14:12 +00:00
|
|
|
addSANs(altNames, config.SANs)
|
|
|
|
|
|
|
|
if _, err := createClientCertKey(regen, "etcd-client", nil,
|
|
|
|
nil, []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
|
|
|
runtime.ETCDServerCA, runtime.ETCDServerCAKey,
|
|
|
|
runtime.ClientETCDCert, runtime.ClientETCDKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
regen, err = createSigningCertKey("etcd-peer", runtime.ETCDPeerCA, runtime.ETCDPeerCAKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := createClientCertKey(regen, "etcd-peer", nil,
|
|
|
|
altNames, []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
|
|
|
|
runtime.ETCDPeerCA, runtime.ETCDPeerCAKey,
|
|
|
|
runtime.PeerServerClientETCDCert, runtime.PeerServerClientETCDKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-11 17:24:35 +00:00
|
|
|
if config.DisableETCD {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := createClientCertKey(regen, "etcd-server", nil,
|
|
|
|
altNames, []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
|
|
|
|
runtime.ETCDServerCA, runtime.ETCDServerCAKey,
|
|
|
|
runtime.ServerETCDCert, runtime.ServerETCDKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-03 18:14:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
func genRequestHeaderCerts(config *config.Control) error {
|
|
|
|
runtime := config.Runtime
|
2021-03-03 18:14:12 +00:00
|
|
|
regen, err := createSigningCertKey(version.Program+"-request-header", runtime.RequestHeaderCA, runtime.RequestHeaderCAKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := createClientCertKey(regen, RequestHeaderCN, nil,
|
|
|
|
nil, []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
|
|
|
runtime.RequestHeaderCA, runtime.RequestHeaderCAKey,
|
|
|
|
runtime.ClientAuthProxyCert, runtime.ClientAuthProxyKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type signedCertFactory = func(commonName string, organization []string, certFile, keyFile string) (bool, error)
|
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
func createServerSigningCertKey(config *config.Control) (bool, error) {
|
|
|
|
runtime := config.Runtime
|
2021-03-03 18:14:12 +00:00
|
|
|
TokenCA := filepath.Join(config.DataDir, "tls", "token-ca.crt")
|
|
|
|
TokenCAKey := filepath.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
|
|
|
|
}
|
2022-12-06 23:45:58 +00:00
|
|
|
regen, err := createSigningCertKey(version.Program+"-server", runtime.ServerCA, runtime.ServerCAKey)
|
|
|
|
if err != nil {
|
|
|
|
return regen, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If our CA certs are signed by a root or intermediate CA, ServerCA will contain a chain.
|
|
|
|
// The controller-manager's signer wants just a single cert, not a full chain; so create a file
|
|
|
|
// that is guaranteed to contain only a single certificate.
|
|
|
|
certs, err := certutil.CertsFromFile(runtime.ServerCA)
|
|
|
|
if err != nil {
|
|
|
|
return regen, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := certutil.WriteCert(runtime.SigningServerCA, certutil.EncodeCertPEM(certs[0])); err != nil {
|
|
|
|
return regen, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return regen, nil
|
2021-03-03 18:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addSANs(altNames *certutil.AltNames, sans []string) {
|
|
|
|
for _, san := range sans {
|
|
|
|
ip := net.ParseIP(san)
|
|
|
|
if ip == nil {
|
|
|
|
altNames.DNSNames = append(altNames.DNSNames, san)
|
|
|
|
} else {
|
|
|
|
altNames.IPs = append(altNames.IPs, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:53:12 +00:00
|
|
|
func fieldsChanged(certFile string, commonName string, organization []string, sans *certutil.AltNames, caCertFile string) bool {
|
2021-03-03 18:14:12 +00:00
|
|
|
if sans == nil {
|
2022-04-04 21:53:12 +00:00
|
|
|
sans = &certutil.AltNames{}
|
2021-03-03 18:14:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 21:53:12 +00:00
|
|
|
certificates, err := certutil.CertsFromFile(certFile)
|
|
|
|
if err != nil || len(certificates) == 0 {
|
2021-03-03 18:14:12 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:53:12 +00:00
|
|
|
if certificates[0].Subject.CommonName != commonName {
|
|
|
|
return true
|
2021-03-03 18:14:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 21:53:12 +00:00
|
|
|
if !sets.NewString(certificates[0].Subject.Organization...).Equal(sets.NewString(organization...)) {
|
|
|
|
return true
|
2021-03-03 18:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !sets.NewString(certificates[0].DNSNames...).HasAll(sans.DNSNames...) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
ips := sets.NewString()
|
|
|
|
for _, ip := range certificates[0].IPAddresses {
|
|
|
|
ips.Insert(ip.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ip := range sans.IPs {
|
|
|
|
if !ips.Has(ip.String()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:53:12 +00:00
|
|
|
caCertificates, err := certutil.CertsFromFile(caCertFile)
|
|
|
|
if err != nil || len(caCertificates) == 0 {
|
|
|
|
return false
|
2021-03-03 18:14:12 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 04:05:01 +00:00
|
|
|
return !bytes.Equal(certificates[0].AuthorityKeyId, caCertificates[0].SubjectKeyId)
|
2022-04-04 21:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createClientCertKey(regen bool, commonName string, organization []string, altNames *certutil.AltNames, extKeyUsage []x509.ExtKeyUsage, caCertFile, caKeyFile, certFile, keyFile string) (bool, error) {
|
|
|
|
// check for reasons to renew the certificate even if not manually requested.
|
|
|
|
regen = regen || expired(certFile) || fieldsChanged(certFile, commonName, organization, altNames, caCertFile)
|
|
|
|
|
2021-03-03 18:14:12 +00:00
|
|
|
if !regen {
|
|
|
|
if exists(certFile, keyFile) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:53:12 +00:00
|
|
|
caKey, err := certutil.PrivateKeyFromFile(caKeyFile)
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-03-07 23:05:52 +00:00
|
|
|
caCerts, err := certutil.CertsFromFile(caCertFile)
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keyBytes, _, err := certutil.LoadOrGenerateKeyFile(keyFile, regen)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := certutil.ParsePrivateKeyPEM(keyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := certutil.Config{
|
|
|
|
CommonName: commonName,
|
|
|
|
Organization: organization,
|
|
|
|
Usages: extKeyUsage,
|
|
|
|
}
|
|
|
|
if altNames != nil {
|
|
|
|
cfg.AltNames = *altNames
|
|
|
|
}
|
2023-03-07 23:05:52 +00:00
|
|
|
cert, err := certutil.NewSignedCert(cfg, key.(crypto.Signer), caCerts[0], caKey.(crypto.Signer))
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-03-07 23:05:52 +00:00
|
|
|
return true, certutil.WriteCert(certFile, util.EncodeCertsPEM(cert, caCerts))
|
2021-03-03 18:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func exists(files ...string) bool {
|
|
|
|
for _, file := range files {
|
|
|
|
if _, err := os.Stat(file); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func genServiceAccount(runtime *config.ControlRuntime) error {
|
2022-12-06 23:45:58 +00:00
|
|
|
if _, err := os.Stat(runtime.ServiceKey); err != nil {
|
|
|
|
if !errors.Is(err, fs.ErrNotExist) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key, err := certutil.NewPrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := certutil.WriteKey(runtime.ServiceKey, certutil.EncodePrivateKeyPEM(key)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// When rotating the ServiceAccount signing key, it is necessary to keep the old keys in ServiceKey so that
|
|
|
|
// old ServiceAccount tokens can be validated during the switchover process. The first key in the file
|
|
|
|
// should be the current key used to sign ServiceAccount tokens; others are old keys used for verification
|
|
|
|
// only. Create a file containing just the first key in the list, which will be used to configure the
|
|
|
|
// signing controller.
|
|
|
|
key, err := keyutil.PrivateKeyFromFile(runtime.ServiceKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-03-03 18:14:12 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 23:45:58 +00:00
|
|
|
keyData, err := keyutil.MarshalPrivateKeyToPEM(key)
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-06 23:45:58 +00:00
|
|
|
return certutil.WriteKey(runtime.ServiceCurrentKey, keyData)
|
2021-03-03 18:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createSigningCertKey(prefix, certFile, keyFile string) (bool, error) {
|
|
|
|
if exists(certFile, keyFile) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
caKeyBytes, _, err := certutil.LoadOrGenerateKeyFile(keyFile, false)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
caKey, err := certutil.ParsePrivateKeyPEM(caKeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := certutil.Config{
|
|
|
|
CommonName: fmt.Sprintf("%s-ca@%d", prefix, time.Now().Unix()),
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := certutil.NewSelfSignedCACert(cfg, caKey.(crypto.Signer))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := certutil.WriteCert(certFile, certutil.EncodeCertPEM(cert)); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:53:12 +00:00
|
|
|
func expired(certFile string) bool {
|
|
|
|
certificates, err := certutil.CertsFromFile(certFile)
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return certutil.IsCertExpired(certificates[0], config.CertificateRenewDays)
|
|
|
|
}
|
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
func genEncryptionConfigAndState(controlConfig *config.Control) error {
|
|
|
|
runtime := controlConfig.Runtime
|
2021-03-03 18:14:12 +00:00
|
|
|
if !controlConfig.EncryptSecrets {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if s, err := os.Stat(runtime.EncryptionConfig); err == nil && s.Size() > 0 {
|
2022-02-25 16:43:03 +00:00
|
|
|
// On upgrade from older versions, the encryption hash may not exist, create it
|
|
|
|
if _, err := os.Stat(runtime.EncryptionHash); errors.Is(err, os.ErrNotExist) {
|
2022-10-08 00:36:57 +00:00
|
|
|
curEncryptionByte, err := os.ReadFile(runtime.EncryptionConfig)
|
2022-02-25 16:43:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
encryptionConfigHash := sha256.Sum256(curEncryptionByte)
|
|
|
|
ann := "start-" + hex.EncodeToString(encryptionConfigHash[:])
|
2022-10-08 00:36:57 +00:00
|
|
|
return os.WriteFile(controlConfig.Runtime.EncryptionHash, []byte(ann), 0600)
|
2022-02-25 16:43:03 +00:00
|
|
|
}
|
2021-03-03 18:14:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-09 19:37:37 +00:00
|
|
|
aescbcKey := make([]byte, aescbcKeySize)
|
2021-03-03 18:14:12 +00:00
|
|
|
_, err := cryptorand.Read(aescbcKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
encodedKey := b64.StdEncoding.EncodeToString(aescbcKey)
|
|
|
|
|
|
|
|
encConfig := apiserverconfigv1.EncryptionConfiguration{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "EncryptionConfiguration",
|
|
|
|
APIVersion: "apiserver.config.k8s.io/v1",
|
|
|
|
},
|
|
|
|
Resources: []apiserverconfigv1.ResourceConfiguration{
|
|
|
|
{
|
|
|
|
Resources: []string{"secrets"},
|
|
|
|
Providers: []apiserverconfigv1.ProviderConfiguration{
|
|
|
|
{
|
|
|
|
AESCBC: &apiserverconfigv1.AESConfiguration{
|
|
|
|
Keys: []apiserverconfigv1.Key{
|
|
|
|
{
|
|
|
|
Name: "aescbckey",
|
|
|
|
Secret: encodedKey,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Identity: &apiserverconfigv1.IdentityConfiguration{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-12-07 22:31:32 +00:00
|
|
|
b, err := json.Marshal(encConfig)
|
2021-03-03 18:14:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-01 17:01:54 +00:00
|
|
|
if err := util.AtomicWrite(runtime.EncryptionConfig, b, 0600); err != nil {
|
2021-12-07 22:31:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
encryptionConfigHash := sha256.Sum256(b)
|
|
|
|
ann := "start-" + hex.EncodeToString(encryptionConfigHash[:])
|
2022-10-08 00:36:57 +00:00
|
|
|
return os.WriteFile(controlConfig.Runtime.EncryptionHash, []byte(ann), 0600)
|
2021-03-03 18:14:12 +00:00
|
|
|
}
|
2022-04-04 21:54:50 +00:00
|
|
|
|
|
|
|
func genEgressSelectorConfig(controlConfig *config.Control) error {
|
2022-06-09 21:05:24 +00:00
|
|
|
var clusterConn apiserver.Connection
|
|
|
|
|
|
|
|
if controlConfig.EgressSelectorMode == config.EgressSelectorModeDisabled {
|
|
|
|
clusterConn = apiserver.Connection{
|
|
|
|
ProxyProtocol: apiserver.ProtocolDirect,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
clusterConn = apiserver.Connection{
|
|
|
|
ProxyProtocol: apiserver.ProtocolHTTPConnect,
|
|
|
|
Transport: &apiserver.Transport{
|
|
|
|
TCP: &apiserver.TCPTransport{
|
2022-07-21 21:40:09 +00:00
|
|
|
URL: fmt.Sprintf("https://%s:%d", controlConfig.BindAddressOrLoopback(false, true), controlConfig.SupervisorPort),
|
2022-06-09 21:05:24 +00:00
|
|
|
TLSConfig: &apiserver.TLSConfig{
|
|
|
|
CABundle: controlConfig.Runtime.ServerCA,
|
|
|
|
ClientKey: controlConfig.Runtime.ClientKubeAPIKey,
|
|
|
|
ClientCert: controlConfig.Runtime.ClientKubeAPICert,
|
|
|
|
},
|
2022-04-04 21:54:50 +00:00
|
|
|
},
|
|
|
|
},
|
2022-06-09 21:05:24 +00:00
|
|
|
}
|
2022-05-17 19:25:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 21:54:50 +00:00
|
|
|
egressConfig := apiserver.EgressSelectorConfiguration{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "EgressSelectorConfiguration",
|
|
|
|
APIVersion: "apiserver.k8s.io/v1beta1",
|
|
|
|
},
|
|
|
|
EgressSelections: []apiserver.EgressSelection{
|
|
|
|
{
|
|
|
|
Name: "cluster",
|
2022-05-17 19:25:43 +00:00
|
|
|
Connection: clusterConn,
|
2022-04-04 21:54:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(egressConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-08 00:36:57 +00:00
|
|
|
return os.WriteFile(controlConfig.Runtime.EgressSelectorConfig, b, 0600)
|
2022-04-04 21:54:50 +00:00
|
|
|
}
|
2022-09-29 20:37:50 +00:00
|
|
|
|
|
|
|
func genCloudConfig(controlConfig *config.Control) error {
|
|
|
|
cloudConfig := cloudprovider.Config{
|
|
|
|
LBEnabled: !controlConfig.DisableServiceLB,
|
|
|
|
LBNamespace: controlConfig.ServiceLBNamespace,
|
|
|
|
LBImage: cloudprovider.DefaultLBImage,
|
|
|
|
Rootless: controlConfig.Rootless,
|
2022-09-29 20:50:05 +00:00
|
|
|
NodeEnabled: !controlConfig.DisableCCM,
|
2022-09-29 20:37:50 +00:00
|
|
|
}
|
|
|
|
if controlConfig.SystemDefaultRegistry != "" {
|
|
|
|
cloudConfig.LBImage = controlConfig.SystemDefaultRegistry + "/" + cloudConfig.LBImage
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(cloudConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-08 00:36:57 +00:00
|
|
|
return os.WriteFile(controlConfig.Runtime.CloudControllerConfig, b, 0600)
|
2022-09-29 20:37:50 +00:00
|
|
|
|
|
|
|
}
|