2019-01-09 16:54:15 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2019-03-04 05:25:02 +00:00
|
|
|
net2 "net"
|
2019-01-09 16:54:15 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-01-22 21:14:58 +00:00
|
|
|
"strings"
|
2019-05-08 21:34:12 +00:00
|
|
|
"time"
|
2019-01-09 16:54:15 +00:00
|
|
|
|
2019-03-07 03:49:17 +00:00
|
|
|
systemd "github.com/coreos/go-systemd/daemon"
|
2019-01-09 16:54:15 +00:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
|
|
|
"github.com/natefinch/lumberjack"
|
2019-03-04 05:25:02 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-01-09 16:54:15 +00:00
|
|
|
"github.com/rancher/k3s/pkg/agent"
|
|
|
|
"github.com/rancher/k3s/pkg/cli/cmds"
|
2019-03-08 22:47:44 +00:00
|
|
|
"github.com/rancher/k3s/pkg/datadir"
|
|
|
|
"github.com/rancher/k3s/pkg/rootless"
|
2019-01-09 16:54:15 +00:00
|
|
|
"github.com/rancher/k3s/pkg/server"
|
2019-05-09 22:05:51 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/signals"
|
2019-01-09 16:54:15 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli"
|
2019-02-08 04:12:49 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/net"
|
2019-05-29 18:53:51 +00:00
|
|
|
"k8s.io/kubernetes/pkg/master"
|
2019-03-08 22:47:44 +00:00
|
|
|
"k8s.io/kubernetes/pkg/volume/csi"
|
2019-01-22 21:14:58 +00:00
|
|
|
|
2019-05-15 23:05:24 +00:00
|
|
|
_ "github.com/go-sql-driver/mysql" // ensure we have mysql
|
|
|
|
_ "github.com/lib/pq" // ensure we have postgres
|
|
|
|
_ "github.com/mattn/go-sqlite3" // ensure we have sqlite
|
2019-01-09 16:54:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setupLogging(app *cli.Context) {
|
|
|
|
if !app.GlobalBool("debug") {
|
2019-05-01 03:23:32 +00:00
|
|
|
flag.Set("stderrthreshold", "WARNING")
|
2019-01-09 16:54:15 +00:00
|
|
|
flag.Set("alsologtostderr", "false")
|
|
|
|
flag.Set("logtostderr", "false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runWithLogging(app *cli.Context, cfg *cmds.Server) error {
|
|
|
|
l := &lumberjack.Logger{
|
|
|
|
Filename: cfg.Log,
|
|
|
|
MaxSize: 50,
|
|
|
|
MaxBackups: 3,
|
|
|
|
MaxAge: 28,
|
|
|
|
Compress: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
args := append([]string{"k3s"}, os.Args[1:]...)
|
|
|
|
cmd := reexec.Command(args...)
|
|
|
|
cmd.Env = os.Environ()
|
|
|
|
cmd.Env = append(cmd.Env, "_RIO_REEXEC_=true")
|
|
|
|
cmd.Stderr = l
|
|
|
|
cmd.Stdout = l
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Run(app *cli.Context) error {
|
|
|
|
return run(app, &cmds.ServerConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(app *cli.Context, cfg *cmds.Server) error {
|
2019-03-04 05:25:02 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
if cfg.Log != "" && os.Getenv("_RIO_REEXEC_") == "" {
|
|
|
|
return runWithLogging(app, cfg)
|
|
|
|
}
|
|
|
|
|
2019-05-08 21:34:12 +00:00
|
|
|
if err := checkUnixTimestamp(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
setupLogging(app)
|
|
|
|
|
2019-03-08 22:47:44 +00:00
|
|
|
if !cfg.DisableAgent && os.Getuid() != 0 && !cfg.Rootless {
|
2019-01-09 16:54:15 +00:00
|
|
|
return fmt.Errorf("must run as root unless --disable-agent is specified")
|
|
|
|
}
|
|
|
|
|
2019-03-08 22:47:44 +00:00
|
|
|
if cfg.Rootless {
|
|
|
|
dataDir, err := datadir.LocalHome(cfg.DataDir, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cfg.DataDir = dataDir
|
|
|
|
if err := rootless.Rootless(dataDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:54:59 +00:00
|
|
|
// If running agent in server, set this so that CSI initializes properly
|
2019-03-08 22:47:44 +00:00
|
|
|
csi.WaitForValidHostName = !cfg.DisableAgent
|
2019-04-08 17:54:59 +00:00
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
serverConfig := server.Config{}
|
2019-01-22 21:14:58 +00:00
|
|
|
serverConfig.ControlConfig.ClusterSecret = cfg.ClusterSecret
|
2019-01-09 16:54:15 +00:00
|
|
|
serverConfig.ControlConfig.DataDir = cfg.DataDir
|
2019-01-22 21:14:58 +00:00
|
|
|
serverConfig.ControlConfig.KubeConfigOutput = cfg.KubeConfigOutput
|
|
|
|
serverConfig.ControlConfig.KubeConfigMode = cfg.KubeConfigMode
|
2019-03-08 22:47:44 +00:00
|
|
|
serverConfig.Rootless = cfg.Rootless
|
2019-01-09 16:54:15 +00:00
|
|
|
serverConfig.TLSConfig.HTTPSPort = cfg.HTTPSPort
|
|
|
|
serverConfig.TLSConfig.HTTPPort = cfg.HTTPPort
|
2019-05-29 18:53:51 +00:00
|
|
|
for _, san := range knownIPs(cfg.TLSSan) {
|
|
|
|
addr := net2.ParseIP(san)
|
|
|
|
if addr != nil {
|
|
|
|
serverConfig.TLSConfig.KnownIPs = append(serverConfig.TLSConfig.KnownIPs, san)
|
|
|
|
} else {
|
|
|
|
serverConfig.TLSConfig.Domains = append(serverConfig.TLSConfig.Domains, san)
|
|
|
|
}
|
|
|
|
}
|
2019-03-31 00:10:23 +00:00
|
|
|
serverConfig.TLSConfig.BindAddress = cfg.BindAddress
|
2019-05-29 18:53:51 +00:00
|
|
|
serverConfig.ControlConfig.HTTPSPort = cfg.HTTPSPort
|
2019-04-05 00:43:00 +00:00
|
|
|
serverConfig.ControlConfig.ExtraAPIArgs = cfg.ExtraAPIArgs
|
|
|
|
serverConfig.ControlConfig.ExtraControllerArgs = cfg.ExtraControllerArgs
|
|
|
|
serverConfig.ControlConfig.ExtraSchedulerAPIArgs = cfg.ExtraSchedulerArgs
|
2019-04-12 06:06:35 +00:00
|
|
|
serverConfig.ControlConfig.ClusterDomain = cfg.ClusterDomain
|
2019-05-15 23:05:24 +00:00
|
|
|
serverConfig.ControlConfig.StorageEndpoint = cfg.StorageEndpoint
|
2019-06-11 22:48:47 +00:00
|
|
|
serverConfig.ControlConfig.StorageBackend = cfg.StorageBackend
|
|
|
|
serverConfig.ControlConfig.StorageCAFile = cfg.StorageCAFile
|
|
|
|
serverConfig.ControlConfig.StorageCertFile = cfg.StorageCertFile
|
|
|
|
serverConfig.ControlConfig.StorageKeyFile = cfg.StorageKeyFile
|
2019-05-29 18:53:51 +00:00
|
|
|
serverConfig.ControlConfig.AdvertiseIP = cfg.AdvertiseIP
|
|
|
|
serverConfig.ControlConfig.AdvertisePort = cfg.AdvertisePort
|
|
|
|
|
|
|
|
if serverConfig.ControlConfig.AdvertiseIP == "" && cmds.AgentConfig.NodeIP != "" {
|
|
|
|
serverConfig.ControlConfig.AdvertiseIP = cmds.AgentConfig.NodeIP
|
|
|
|
}
|
|
|
|
if serverConfig.ControlConfig.AdvertiseIP != "" {
|
|
|
|
serverConfig.TLSConfig.KnownIPs = append(serverConfig.TLSConfig.KnownIPs, serverConfig.ControlConfig.AdvertiseIP)
|
|
|
|
}
|
2019-01-09 16:54:15 +00:00
|
|
|
|
2019-03-04 05:25:02 +00:00
|
|
|
_, serverConfig.ControlConfig.ClusterIPRange, err = net2.ParseCIDR(cfg.ClusterCIDR)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "Invalid CIDR %s: %v", cfg.ClusterCIDR, err)
|
|
|
|
}
|
2019-03-06 10:37:03 +00:00
|
|
|
_, serverConfig.ControlConfig.ServiceIPRange, err = net2.ParseCIDR(cfg.ServiceCIDR)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "Invalid CIDR %s: %v", cfg.ServiceCIDR, err)
|
|
|
|
}
|
2019-03-06 18:41:07 +00:00
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
_, apiServerServiceIP, err := master.DefaultServiceIPRange(*serverConfig.ControlConfig.ServiceIPRange)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
serverConfig.TLSConfig.KnownIPs = append(serverConfig.TLSConfig.KnownIPs, apiServerServiceIP.String())
|
|
|
|
|
2019-03-06 18:41:07 +00:00
|
|
|
// If cluster-dns CLI arg is not set, we set ClusterDNS address to be ServiceCIDR network + 10,
|
|
|
|
// i.e. when you set service-cidr to 192.168.0.0/16 and don't provide cluster-dns, it will be set to 192.168.0.10
|
|
|
|
if cfg.ClusterDNS == "" {
|
|
|
|
serverConfig.ControlConfig.ClusterDNS = make(net2.IP, 4)
|
|
|
|
copy(serverConfig.ControlConfig.ClusterDNS, serverConfig.ControlConfig.ServiceIPRange.IP.To4())
|
|
|
|
serverConfig.ControlConfig.ClusterDNS[3] = 10
|
|
|
|
} else {
|
|
|
|
serverConfig.ControlConfig.ClusterDNS = net2.ParseIP(cfg.ClusterDNS)
|
|
|
|
}
|
|
|
|
|
2019-01-22 21:14:58 +00:00
|
|
|
// TODO: support etcd
|
|
|
|
serverConfig.ControlConfig.NoLeaderElect = true
|
|
|
|
|
|
|
|
for _, noDeploy := range app.StringSlice("no-deploy") {
|
2019-02-02 05:09:11 +00:00
|
|
|
if noDeploy == "servicelb" {
|
|
|
|
serverConfig.DisableServiceLB = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-01-22 21:14:58 +00:00
|
|
|
if !strings.HasSuffix(noDeploy, ".yaml") {
|
|
|
|
noDeploy = noDeploy + ".yaml"
|
|
|
|
}
|
|
|
|
serverConfig.ControlConfig.Skips = append(serverConfig.ControlConfig.Skips, noDeploy)
|
|
|
|
}
|
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
logrus.Info("Starting k3s ", app.App.Version)
|
2019-03-07 03:49:17 +00:00
|
|
|
notifySocket := os.Getenv("NOTIFY_SOCKET")
|
|
|
|
os.Unsetenv("NOTIFY_SOCKET")
|
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
ctx := signals.SetupSignalHandler(context.Background())
|
2019-01-09 16:54:15 +00:00
|
|
|
certs, err := server.StartServer(ctx, &serverConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-08 04:12:49 +00:00
|
|
|
logrus.Info("k3s is up and running")
|
2019-03-07 03:49:17 +00:00
|
|
|
if notifySocket != "" {
|
|
|
|
os.Setenv("NOTIFY_SOCKET", notifySocket)
|
|
|
|
systemd.SdNotify(true, "READY=1")
|
|
|
|
}
|
2019-02-08 04:12:49 +00:00
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
if cfg.DisableAgent {
|
|
|
|
<-ctx.Done()
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-31 00:10:23 +00:00
|
|
|
ip := serverConfig.TLSConfig.BindAddress
|
|
|
|
if ip == "" {
|
|
|
|
ip = "localhost"
|
|
|
|
}
|
|
|
|
url := fmt.Sprintf("https://%s:%d", ip, serverConfig.TLSConfig.HTTPSPort)
|
2019-01-09 16:54:15 +00:00
|
|
|
token := server.FormatToken(serverConfig.ControlConfig.Runtime.NodeToken, certs)
|
|
|
|
|
|
|
|
agentConfig := cmds.AgentConfig
|
2019-02-08 04:12:49 +00:00
|
|
|
agentConfig.Debug = app.GlobalBool("bool")
|
2019-01-22 21:14:58 +00:00
|
|
|
agentConfig.DataDir = filepath.Dir(serverConfig.ControlConfig.DataDir)
|
2019-01-09 16:54:15 +00:00
|
|
|
agentConfig.ServerURL = url
|
|
|
|
agentConfig.Token = token
|
2019-05-07 23:47:07 +00:00
|
|
|
agentConfig.Labels = append(agentConfig.Labels, "node-role.kubernetes.io/master=true")
|
2019-01-09 16:54:15 +00:00
|
|
|
|
|
|
|
return agent.Run(ctx, agentConfig)
|
|
|
|
}
|
2019-01-31 23:57:40 +00:00
|
|
|
|
2019-03-23 17:34:55 +00:00
|
|
|
func knownIPs(ips []string) []string {
|
|
|
|
ips = append(ips, "127.0.0.1")
|
2019-01-31 23:57:40 +00:00
|
|
|
ip, err := net.ChooseHostInterface()
|
|
|
|
if err == nil {
|
|
|
|
ips = append(ips, ip.String())
|
|
|
|
}
|
|
|
|
return ips
|
|
|
|
}
|
2019-05-08 21:34:12 +00:00
|
|
|
|
|
|
|
func checkUnixTimestamp() error {
|
|
|
|
timeNow := time.Now()
|
|
|
|
// check if time before 01/01/1980
|
|
|
|
if timeNow.Before(time.Unix(315532800, 0)) {
|
|
|
|
return fmt.Errorf("server time isn't set properly: %v", timeNow)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|