k3s/pkg/daemons/agent/agent.go

73 lines
2.1 KiB
Go
Raw Normal View History

2019-01-01 08:23:01 +00:00
package agent
import (
"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"
"github.com/rancher/k3s/pkg/agent/config"
"github.com/rancher/k3s/pkg/agent/proxy"
daemonconfig "github.com/rancher/k3s/pkg/daemons/config"
"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
)
const (
unixPrefix = "unix://"
windowsPrefix = "npipe://"
)
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()
if err := startKubelet(&nodeConfig.AgentConfig); err != nil {
return err
}
2020-04-27 16:31:25 +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
}
func startKubeProxy(cfg *daemonconfig.Agent) error {
argsMap := kubeProxyArgs(cfg)
args := daemonconfig.GetArgs(argsMap, cfg.ExtraKubeProxyArgs)
logrus.Infof("Running kube-proxy %s", daemonconfig.ArgString(args))
return executor.KubeProxy(args)
2019-01-01 08:23:01 +00:00
}
func startKubelet(cfg *daemonconfig.Agent) error {
argsMap := kubeletArgs(cfg)
args := daemonconfig.GetArgs(argsMap, cfg.ExtraKubeletArgs)
logrus.Infof("Running kubelet %s", daemonconfig.ArgString(args))
2019-01-01 08:23:01 +00:00
return executor.Kubelet(args)
2019-01-01 08:23:01 +00:00
}
2019-01-22 21:14:58 +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.
func ImageCredProvAvailable(cfg *daemonconfig.Agent) bool {
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
}