2019-01-01 08:23:01 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
2019-01-22 21:14:58 +00:00
|
|
|
"os"
|
2019-01-01 08:23:01 +00:00
|
|
|
"time"
|
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
"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://"
|
|
|
|
windowsPrefix = "npipe:"
|
|
|
|
)
|
2021-01-21 01:03:22 +00:00
|
|
|
|
2019-01-01 08:23:01 +00:00
|
|
|
func Agent(config *config.Agent) error {
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
|
2019-11-05 09:45:07 +00:00
|
|
|
logs.InitLogs()
|
|
|
|
defer logs.FlushLogs()
|
2020-04-27 17:09:58 +00:00
|
|
|
if err := startKubelet(config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-27 16:31:25 +00:00
|
|
|
|
|
|
|
if !config.DisableKubeProxy {
|
|
|
|
return startKubeProxy(config)
|
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-27 16:31:25 +00:00
|
|
|
func startKubeProxy(cfg *config.Agent) error {
|
2021-06-10 19:27:00 +00:00
|
|
|
argsMap := kubeProxyArgs(cfg)
|
2019-11-05 09:45:07 +00:00
|
|
|
args := config.GetArgsList(argsMap, cfg.ExtraKubeProxyArgs)
|
2020-04-27 17:09:58 +00:00
|
|
|
logrus.Infof("Running kube-proxy %s", config.ArgString(args))
|
|
|
|
return executor.KubeProxy(args)
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 17:09:58 +00:00
|
|
|
func startKubelet(cfg *config.Agent) error {
|
2021-06-10 19:27:00 +00:00
|
|
|
argsMap := kubeletArgs(cfg)
|
2020-07-20 23:31:56 +00:00
|
|
|
|
2019-03-08 22:47:44 +00:00
|
|
|
args := config.GetArgsList(argsMap, cfg.ExtraKubeletArgs)
|
2020-04-27 17:09:58 +00:00
|
|
|
logrus.Infof("Running kubelet %s", config.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
|
|
|
|
2019-04-12 23:45:59 +00:00
|
|
|
func addFeatureGate(current, new string) string {
|
|
|
|
if current == "" {
|
|
|
|
return new
|
|
|
|
}
|
|
|
|
return current + "," + new
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func ImageCredProvAvailable(cfg *config.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
|
|
|
|
}
|