k3s/pkg/agent/config/config.go

212 lines
5.4 KiB
Go
Raw Normal View History

2019-01-01 08:23:01 +00:00
package config
import (
2019-01-09 16:54:15 +00:00
"context"
2019-01-01 08:23:01 +00:00
"crypto/tls"
"encoding/pem"
"fmt"
"io/ioutil"
"net/url"
"os"
2019-01-09 16:54:15 +00:00
"os/exec"
2019-01-01 08:23:01 +00:00
"path/filepath"
"time"
"github.com/pkg/errors"
2019-01-09 16:54:15 +00:00
"github.com/rancher/k3s/pkg/cli/cmds"
"github.com/rancher/k3s/pkg/daemons/config"
2019-01-01 08:23:01 +00:00
"github.com/rancher/norman/pkg/clientaccess"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/json"
2019-01-09 16:54:15 +00:00
"k8s.io/apimachinery/pkg/util/net"
2019-01-01 08:23:01 +00:00
"k8s.io/client-go/util/cert"
2019-01-22 21:14:58 +00:00
"k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1"
2019-01-01 08:23:01 +00:00
)
2019-01-09 16:54:15 +00:00
func Get(ctx context.Context, agent cmds.Agent) *config.Node {
2019-01-01 08:23:01 +00:00
for {
2019-01-09 16:54:15 +00:00
agentConfig, err := get(&agent)
2019-01-01 08:23:01 +00:00
if err != nil {
logrus.Error(err)
2019-01-09 16:54:15 +00:00
select {
case <-time.After(5 * time.Second):
continue
case <-ctx.Done():
logrus.Fatalf("Interrupted")
}
2019-01-01 08:23:01 +00:00
}
return agentConfig
}
}
func getNodeCert(info *clientaccess.Info) (*tls.Certificate, error) {
2019-01-09 16:54:15 +00:00
nodeCert, err := clientaccess.Get("/v1-k3s/node.crt", info)
2019-01-01 08:23:01 +00:00
if err != nil {
return nil, err
}
nodeKey, err := clientaccess.Get("/v1-k3s/node.key", info)
if err != nil {
return nil, err
}
cert, err := tls.X509KeyPair(nodeCert, nodeKey)
if err != nil {
return nil, err
}
return &cert, nil
}
func writeNodeCA(dataDir string, nodeCert *tls.Certificate) (string, error) {
clientCABytes := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: nodeCert.Certificate[1],
})
clientCA := filepath.Join(dataDir, "client-ca.pem")
if err := ioutil.WriteFile(clientCA, clientCABytes, 0600); err != nil {
return "", errors.Wrapf(err, "failed to write client CA")
}
return clientCA, nil
}
2019-01-09 16:54:15 +00:00
func getHostnameAndIP(info cmds.Agent) (string, string, error) {
2019-01-01 08:23:01 +00:00
ip := info.NodeIP
if ip == "" {
2019-01-09 16:54:15 +00:00
hostIP, err := net.ChooseHostInterface()
2019-01-01 08:23:01 +00:00
if err != nil {
return "", "", err
}
ip = hostIP.String()
}
name := info.NodeName
if name == "" {
hostname, err := os.Hostname()
if err != nil {
return "", "", err
}
2019-01-09 16:54:15 +00:00
name = hostname
2019-01-01 08:23:01 +00:00
}
return name, ip, nil
}
func localAddress(controlConfig *config.Control) string {
return fmt.Sprintf("127.0.0.1:%d", controlConfig.AdvertisePort)
}
2019-01-09 16:54:15 +00:00
func writeKubeConfig(envInfo *cmds.Agent, info clientaccess.Info, controlConfig *config.Control, nodeCert *tls.Certificate) (string, error) {
2019-01-01 08:23:01 +00:00
os.MkdirAll(envInfo.DataDir, 0700)
kubeConfigPath := filepath.Join(envInfo.DataDir, "kubeconfig.yaml")
info.URL = "https://" + localAddress(controlConfig)
info.CACerts = pem.EncodeToMemory(&pem.Block{
Type: cert.CertificateBlockType,
Bytes: nodeCert.Certificate[1],
})
return kubeConfigPath, info.WriteKubeConfig(kubeConfigPath)
}
2019-01-09 16:54:15 +00:00
func get(envInfo *cmds.Agent) (*config.Node, error) {
if envInfo.Debug {
logrus.SetLevel(logrus.DebugLevel)
2019-01-01 08:23:01 +00:00
}
serverURLParsed, err := url.Parse(envInfo.ServerURL)
if err != nil {
return nil, err
}
info, err := clientaccess.ParseAndValidateToken(envInfo.ServerURL, envInfo.Token)
if err != nil {
return nil, err
}
controlConfig, err := getConfig(info)
if err != nil {
return nil, err
}
nodeCert, err := getNodeCert(info)
if err != nil {
return nil, err
}
clientCA, err := writeNodeCA(envInfo.DataDir, nodeCert)
if err != nil {
return nil, err
}
nodeName, nodeIP, err := getHostnameAndIP(*envInfo)
if err != nil {
return nil, err
}
kubeConfig, err := writeKubeConfig(envInfo, *info, controlConfig, nodeCert)
if err != nil {
return nil, err
}
2019-01-09 16:54:15 +00:00
hostLocal, err := exec.LookPath("host-local")
if err != nil {
return nil, errors.Wrapf(err, "failed to find host-local")
}
nodeConfig := &config.Node{
Docker: envInfo.Docker,
NoFlannel: envInfo.NoFlannel,
}
2019-01-01 08:23:01 +00:00
nodeConfig.LocalAddress = localAddress(controlConfig)
2019-01-09 16:54:15 +00:00
nodeConfig.AgentConfig.NodeIP = nodeIP
nodeConfig.AgentConfig.NodeName = nodeName
nodeConfig.AgentConfig.ClusterDNS = controlConfig.ClusterDNS
2019-01-01 08:23:01 +00:00
nodeConfig.AgentConfig.CACertPath = clientCA
2019-01-09 16:54:15 +00:00
nodeConfig.AgentConfig.ListenAddress = "127.0.0.1"
2019-01-01 08:23:01 +00:00
nodeConfig.AgentConfig.KubeConfig = kubeConfig
2019-01-09 16:54:15 +00:00
nodeConfig.AgentConfig.RootDir = filepath.Join(envInfo.DataDir, "kubelet")
2019-01-01 08:23:01 +00:00
nodeConfig.CACerts = info.CACerts
2019-01-09 16:54:15 +00:00
nodeConfig.Containerd.Config = filepath.Join(envInfo.DataDir, "etc/containerd/config.toml")
nodeConfig.Containerd.Root = filepath.Join(envInfo.DataDir, "containerd")
nodeConfig.Containerd.Opt = filepath.Join(envInfo.DataDir, "containerd")
nodeConfig.Containerd.State = "/run/k3s/containerd"
nodeConfig.Containerd.Address = filepath.Join(nodeConfig.Containerd.State, "containerd.sock")
2019-01-01 08:23:01 +00:00
nodeConfig.ServerAddress = serverURLParsed.Host
nodeConfig.Certificate = nodeCert
2019-01-09 16:54:15 +00:00
if !nodeConfig.NoFlannel {
nodeConfig.FlannelConf = filepath.Join(envInfo.DataDir, "etc/flannel/net-conf.json")
nodeConfig.AgentConfig.CNIBinDir = filepath.Dir(hostLocal)
nodeConfig.AgentConfig.CNIConfDir = filepath.Join(envInfo.DataDir, "etc/cni/net.d")
}
2019-01-01 08:23:01 +00:00
if !nodeConfig.Docker {
2019-01-09 16:54:15 +00:00
nodeConfig.AgentConfig.RuntimeSocket = "unix://" + nodeConfig.Containerd.Address
}
if controlConfig.ClusterIPRange != nil {
nodeConfig.AgentConfig.ClusterCIDR = *controlConfig.ClusterIPRange
2019-01-01 08:23:01 +00:00
}
2019-01-09 16:54:15 +00:00
os.Setenv("NODE_NAME", nodeConfig.AgentConfig.NodeName)
v1beta1.KubeletSocket = filepath.Join(envInfo.DataDir, "kubelet/device-plugins/kubelet.sock")
2019-01-01 08:23:01 +00:00
return nodeConfig, nil
}
func defString(val, newVal string) string {
if val == "" {
return newVal
}
return val
}
func getConfig(info *clientaccess.Info) (*config.Control, error) {
data, err := clientaccess.Get("/v1-k3s/config", info)
if err != nil {
return nil, err
}
controlControl := &config.Control{}
return controlControl, json.Unmarshal(data, controlControl)
}