2019-01-01 08:23:01 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2021-07-27 21:56:05 +00:00
|
|
|
"context"
|
2019-01-01 08:23:01 +00:00
|
|
|
"math/rand"
|
2019-01-22 21:14:58 +00:00
|
|
|
"os"
|
2019-01-01 08:23:01 +00:00
|
|
|
"time"
|
|
|
|
|
2021-07-27 21:56:05 +00:00
|
|
|
"github.com/rancher/k3s/pkg/agent/config"
|
|
|
|
"github.com/rancher/k3s/pkg/agent/proxy"
|
|
|
|
daemonconfig "github.com/rancher/k3s/pkg/daemons/config"
|
2020-04-27 17:09:58 +00:00
|
|
|
"github.com/rancher/k3s/pkg/daemons/executor"
|
2019-01-01 08:23:01 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-04-08 17:53:52 +00:00
|
|
|
"k8s.io/component-base/logs"
|
2019-12-12 01:23:55 +00:00
|
|
|
_ "k8s.io/component-base/metrics/prometheus/restclient" // for client metric registration
|
|
|
|
_ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
|
2019-01-01 08:23:01 +00:00
|
|
|
)
|
|
|
|
|
2021-06-10 19:27:00 +00:00
|
|
|
const (
|
|
|
|
unixPrefix = "unix://"
|
2021-07-07 19:50:18 +00:00
|
|
|
windowsPrefix = "npipe://"
|
2021-06-10 19:27:00 +00:00
|
|
|
)
|
2021-01-21 01:03:22 +00:00
|
|
|
|
2021-07-27 21:56:05 +00:00
|
|
|
func Agent(ctx context.Context, nodeConfig *daemonconfig.Node, proxy proxy.Proxy) error {
|
2019-01-01 08:23:01 +00:00
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
|
2019-11-05 09:45:07 +00:00
|
|
|
logs.InitLogs()
|
|
|
|
defer logs.FlushLogs()
|
2021-07-27 21:56:05 +00:00
|
|
|
if err := startKubelet(&nodeConfig.AgentConfig); err != nil {
|
2020-04-27 17:09:58 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-04-27 16:31:25 +00:00
|
|
|
|
2021-07-27 21:56:05 +00:00
|
|
|
go func() {
|
|
|
|
if !config.KubeProxyDisabled(ctx, nodeConfig, proxy) {
|
|
|
|
if err := startKubeProxy(&nodeConfig.AgentConfig); err != nil {
|
|
|
|
logrus.Fatalf("Failed to start kube-proxy: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2019-01-01 08:23:01 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-27 21:56:05 +00:00
|
|
|
func startKubeProxy(cfg *daemonconfig.Agent) error {
|
2021-06-10 19:27:00 +00:00
|
|
|
argsMap := kubeProxyArgs(cfg)
|
2021-07-28 20:04:19 +00:00
|
|
|
args := daemonconfig.GetArgs(argsMap, cfg.ExtraKubeProxyArgs)
|
2021-07-27 21:56:05 +00:00
|
|
|
logrus.Infof("Running kube-proxy %s", daemonconfig.ArgString(args))
|
2020-04-27 17:09:58 +00:00
|
|
|
return executor.KubeProxy(args)
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 21:56:05 +00:00
|
|
|
func startKubelet(cfg *daemonconfig.Agent) error {
|
2021-06-10 19:27:00 +00:00
|
|
|
argsMap := kubeletArgs(cfg)
|
2020-07-20 23:31:56 +00:00
|
|
|
|
2021-07-28 20:04:19 +00:00
|
|
|
args := daemonconfig.GetArgs(argsMap, cfg.ExtraKubeletArgs)
|
2021-07-27 21:56:05 +00:00
|
|
|
logrus.Infof("Running kubelet %s", daemonconfig.ArgString(args))
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2020-04-27 17:09:58 +00:00
|
|
|
return executor.Kubelet(args)
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
2019-01-22 21:14:58 +00:00
|
|
|
|
2021-05-10 22:58:41 +00:00
|
|
|
// ImageCredProvAvailable checks to see if the kubelet image credential provider bin dir and config
|
|
|
|
// files exist and are of the correct types. This is exported so that it may be used by downstream projects.
|
2021-07-27 21:56:05 +00:00
|
|
|
func ImageCredProvAvailable(cfg *daemonconfig.Agent) bool {
|
2021-05-10 22:58:41 +00:00
|
|
|
if info, err := os.Stat(cfg.ImageCredProvBinDir); err != nil || !info.IsDir() {
|
|
|
|
logrus.Debugf("Kubelet image credential provider bin directory check failed: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if info, err := os.Stat(cfg.ImageCredProvConfig); err != nil || info.IsDir() {
|
|
|
|
logrus.Debugf("Kubelet image credential provider config file check failed: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|