2022-03-02 23:47:27 +00:00
|
|
|
//go:build !no_embedded_executor
|
2020-04-27 17:09:58 +00:00
|
|
|
// +build !no_embedded_executor
|
|
|
|
|
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-21 12:41:48 +00:00
|
|
|
"flag"
|
2020-04-27 17:09:58 +00:00
|
|
|
"net/http"
|
2024-01-25 00:44:12 +00:00
|
|
|
"os"
|
2021-10-29 00:38:31 +00:00
|
|
|
"runtime"
|
2022-07-29 20:38:10 +00:00
|
|
|
"runtime/debug"
|
2023-04-21 12:41:48 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2020-04-27 17:09:58 +00:00
|
|
|
|
2024-01-08 22:38:36 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/agent/containerd"
|
|
|
|
"github.com/k3s-io/k3s/pkg/agent/cridockerd"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/cli/cmds"
|
|
|
|
daemonconfig "github.com/k3s-io/k3s/pkg/daemons/config"
|
2022-04-27 19:03:47 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/util"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2022-04-21 20:56:39 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-04-27 17:09:58 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-04-21 20:56:39 +00:00
|
|
|
v1 "k8s.io/api/core/v1"
|
2021-10-29 00:38:31 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2022-04-21 20:56:39 +00:00
|
|
|
k8sruntime "k8s.io/apimachinery/pkg/runtime"
|
2021-10-29 00:38:31 +00:00
|
|
|
"k8s.io/apimachinery/pkg/watch"
|
2021-06-29 14:28:38 +00:00
|
|
|
"k8s.io/apiserver/pkg/authentication/authenticator"
|
2021-10-29 00:38:31 +00:00
|
|
|
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
2022-04-21 20:56:39 +00:00
|
|
|
"k8s.io/client-go/tools/cache"
|
|
|
|
toolswatch "k8s.io/client-go/tools/watch"
|
2021-06-29 14:28:38 +00:00
|
|
|
cloudprovider "k8s.io/cloud-provider"
|
2021-10-29 00:38:31 +00:00
|
|
|
cloudproviderapi "k8s.io/cloud-provider/api"
|
2021-06-29 14:28:38 +00:00
|
|
|
ccmapp "k8s.io/cloud-provider/app"
|
|
|
|
cloudcontrollerconfig "k8s.io/cloud-provider/app/config"
|
2023-08-22 21:09:31 +00:00
|
|
|
"k8s.io/cloud-provider/names"
|
2021-06-29 14:28:38 +00:00
|
|
|
ccmopt "k8s.io/cloud-provider/options"
|
|
|
|
cliflag "k8s.io/component-base/cli/flag"
|
2023-04-21 12:41:48 +00:00
|
|
|
"k8s.io/klog/v2"
|
2023-08-22 21:09:31 +00:00
|
|
|
apiapp "k8s.io/kubernetes/cmd/kube-apiserver/app"
|
2020-04-27 17:09:58 +00:00
|
|
|
cmapp "k8s.io/kubernetes/cmd/kube-controller-manager/app"
|
2021-06-29 14:28:38 +00:00
|
|
|
proxy "k8s.io/kubernetes/cmd/kube-proxy/app"
|
2020-04-27 17:09:58 +00:00
|
|
|
sapp "k8s.io/kubernetes/cmd/kube-scheduler/app"
|
2021-06-29 14:28:38 +00:00
|
|
|
kubelet "k8s.io/kubernetes/cmd/kubelet/app"
|
|
|
|
|
|
|
|
// registering k3s cloud provider
|
2022-03-02 23:47:27 +00:00
|
|
|
_ "github.com/k3s-io/k3s/pkg/cloudprovider"
|
2020-04-27 17:09:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-10-29 00:38:31 +00:00
|
|
|
executor = &Embedded{}
|
2020-04-27 17:09:58 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 00:38:31 +00:00
|
|
|
func (e *Embedded) Bootstrap(ctx context.Context, nodeConfig *daemonconfig.Node, cfg cmds.Agent) error {
|
|
|
|
e.nodeConfig = nodeConfig
|
2023-04-21 12:41:48 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Ensure that the log verbosity remains set to the configured level by resetting it at 1-second intervals
|
|
|
|
// for the first 2 minutes that K3s is starting up. This is necessary because each of the Kubernetes
|
|
|
|
// components will initialize klog and reset the verbosity flag when they are starting.
|
|
|
|
logCtx, cancel := context.WithTimeout(ctx, time.Second*120)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
klog.InitFlags(nil)
|
|
|
|
for {
|
|
|
|
flag.Set("v", strconv.Itoa(cmds.LogConfig.VLevel))
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
case <-logCtx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-05-11 19:50:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-27 19:03:47 +00:00
|
|
|
func (e *Embedded) Kubelet(ctx context.Context, args []string) error {
|
2020-08-05 16:26:36 +00:00
|
|
|
command := kubelet.NewKubeletCommand(context.Background())
|
2020-04-27 17:09:58 +00:00
|
|
|
command.SetArgs(args)
|
|
|
|
|
|
|
|
go func() {
|
2021-09-08 17:56:18 +00:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2023-04-21 19:44:08 +00:00
|
|
|
logrus.WithField("stack", string(debug.Stack())).Fatalf("kubelet panic: %v", err)
|
2021-09-08 17:56:18 +00:00
|
|
|
}
|
|
|
|
}()
|
2022-04-27 19:03:47 +00:00
|
|
|
// The embedded executor doesn't need the kubelet to come up to host any components, and
|
|
|
|
// having it come up on servers before the apiserver is available causes a lot of log spew.
|
|
|
|
// Agents don't have access to the server's apiReady channel, so just wait directly.
|
2022-04-29 19:57:38 +00:00
|
|
|
if err := util.WaitForAPIServerReady(ctx, e.nodeConfig.AgentConfig.KubeConfigKubelet, util.DefaultAPIServerReadyTimeout); err != nil {
|
2022-04-27 19:03:47 +00:00
|
|
|
logrus.Fatalf("Kubelet failed to wait for apiserver ready: %v", err)
|
|
|
|
}
|
2024-01-25 00:44:12 +00:00
|
|
|
err := command.ExecuteContext(ctx)
|
|
|
|
if err != nil && !errors.Is(err, context.Canceled) {
|
|
|
|
logrus.Errorf("kubelet exited: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
2020-04-27 17:09:58 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-16 18:53:09 +00:00
|
|
|
func (e *Embedded) KubeProxy(ctx context.Context, args []string) error {
|
2020-04-27 17:09:58 +00:00
|
|
|
command := proxy.NewProxyCommand()
|
2023-10-16 18:53:09 +00:00
|
|
|
command.SetArgs(daemonconfig.GetArgs(platformKubeProxyArgs(e.nodeConfig), args))
|
2020-04-27 17:09:58 +00:00
|
|
|
|
|
|
|
go func() {
|
2021-09-08 17:56:18 +00:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2023-04-21 19:44:08 +00:00
|
|
|
logrus.WithField("stack", string(debug.Stack())).Fatalf("kube-proxy panic: %v", err)
|
2021-09-08 17:56:18 +00:00
|
|
|
}
|
|
|
|
}()
|
2024-01-25 00:44:12 +00:00
|
|
|
err := command.ExecuteContext(ctx)
|
|
|
|
if err != nil && !errors.Is(err, context.Canceled) {
|
|
|
|
logrus.Errorf("kube-proxy exited: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
2020-04-27 17:09:58 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:38:31 +00:00
|
|
|
func (*Embedded) APIServerHandlers(ctx context.Context) (authenticator.Request, http.Handler, error) {
|
2023-08-22 21:09:31 +00:00
|
|
|
startupConfig := <-apiapp.StartupConfig
|
2021-10-12 06:13:10 +00:00
|
|
|
return startupConfig.Authenticator, startupConfig.Handler, nil
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:38:31 +00:00
|
|
|
func (*Embedded) APIServer(ctx context.Context, etcdReady <-chan struct{}, args []string) error {
|
2023-08-22 21:09:31 +00:00
|
|
|
command := apiapp.NewAPIServerCommand(ctx.Done())
|
2020-04-27 17:09:58 +00:00
|
|
|
command.SetArgs(args)
|
|
|
|
|
|
|
|
go func() {
|
2021-10-12 06:13:10 +00:00
|
|
|
<-etcdReady
|
2021-09-08 17:56:18 +00:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2023-04-21 19:44:08 +00:00
|
|
|
logrus.WithField("stack", string(debug.Stack())).Fatalf("apiserver panic: %v", err)
|
2021-09-08 17:56:18 +00:00
|
|
|
}
|
|
|
|
}()
|
2024-01-25 00:44:12 +00:00
|
|
|
err := command.ExecuteContext(ctx)
|
|
|
|
if err != nil && !errors.Is(err, context.Canceled) {
|
|
|
|
logrus.Errorf("apiserver exited: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
2020-04-27 17:09:58 +00:00
|
|
|
}()
|
|
|
|
|
2021-10-12 06:13:10 +00:00
|
|
|
return nil
|
2020-04-27 17:09:58 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 00:38:31 +00:00
|
|
|
func (e *Embedded) Scheduler(ctx context.Context, apiReady <-chan struct{}, args []string) error {
|
2020-04-27 17:09:58 +00:00
|
|
|
command := sapp.NewSchedulerCommand()
|
|
|
|
command.SetArgs(args)
|
|
|
|
|
|
|
|
go func() {
|
2020-04-28 22:44:05 +00:00
|
|
|
<-apiReady
|
2021-10-29 00:38:31 +00:00
|
|
|
// wait for Bootstrap to set nodeConfig
|
|
|
|
for e.nodeConfig == nil {
|
|
|
|
runtime.Gosched()
|
|
|
|
}
|
|
|
|
// If we're running the embedded cloud controller, wait for it to untaint at least one
|
|
|
|
// node (usually, the local node) before starting the scheduler to ensure that it
|
|
|
|
// finds a node that is ready to run pods during its initial scheduling loop.
|
|
|
|
if !e.nodeConfig.AgentConfig.DisableCCM {
|
|
|
|
if err := waitForUntaintedNode(ctx, e.nodeConfig.AgentConfig.KubeConfigKubelet); err != nil {
|
|
|
|
logrus.Fatalf("failed to wait for untained node: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2021-09-08 17:56:18 +00:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2023-04-21 19:44:08 +00:00
|
|
|
logrus.WithField("stack", string(debug.Stack())).Fatalf("scheduler panic: %v", err)
|
2021-09-08 17:56:18 +00:00
|
|
|
}
|
|
|
|
}()
|
2024-01-25 00:44:12 +00:00
|
|
|
err := command.ExecuteContext(ctx)
|
|
|
|
if err != nil && !errors.Is(err, context.Canceled) {
|
|
|
|
logrus.Errorf("scheduler exited: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
2020-04-27 17:09:58 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:38:31 +00:00
|
|
|
func (*Embedded) ControllerManager(ctx context.Context, apiReady <-chan struct{}, args []string) error {
|
2020-04-27 17:09:58 +00:00
|
|
|
command := cmapp.NewControllerManagerCommand()
|
|
|
|
command.SetArgs(args)
|
|
|
|
|
|
|
|
go func() {
|
2020-04-28 22:44:05 +00:00
|
|
|
<-apiReady
|
2021-09-08 17:56:18 +00:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2023-04-21 19:44:08 +00:00
|
|
|
logrus.WithField("stack", string(debug.Stack())).Fatalf("controller-manager panic: %v", err)
|
2021-09-08 17:56:18 +00:00
|
|
|
}
|
|
|
|
}()
|
2024-01-25 00:44:12 +00:00
|
|
|
err := command.ExecuteContext(ctx)
|
|
|
|
if err != nil && !errors.Is(err, context.Canceled) {
|
|
|
|
logrus.Errorf("controller-manager exited: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
2020-04-27 17:09:58 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-29 14:28:38 +00:00
|
|
|
|
2021-10-29 00:38:31 +00:00
|
|
|
func (*Embedded) CloudControllerManager(ctx context.Context, ccmRBACReady <-chan struct{}, args []string) error {
|
2021-06-29 14:28:38 +00:00
|
|
|
ccmOptions, err := ccmopt.NewCloudControllerManagerOptions()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("unable to initialize command options: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cloudInitializer := func(config *cloudcontrollerconfig.CompletedConfig) cloudprovider.Interface {
|
2023-06-20 07:08:08 +00:00
|
|
|
cloud, err := cloudprovider.InitCloudProvider(version.Program, config.ComponentConfig.KubeCloudShared.CloudProvider.CloudConfigFile)
|
2021-06-29 14:28:38 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("Cloud provider could not be initialized: %v", err)
|
|
|
|
}
|
|
|
|
if cloud == nil {
|
|
|
|
logrus.Fatalf("Cloud provider is nil")
|
|
|
|
}
|
|
|
|
return cloud
|
|
|
|
}
|
|
|
|
|
2023-08-22 21:09:31 +00:00
|
|
|
controllerAliases := names.CCMControllerAliases()
|
|
|
|
|
|
|
|
command := ccmapp.NewCloudControllerManagerCommand(
|
|
|
|
ccmOptions,
|
|
|
|
cloudInitializer,
|
|
|
|
ccmapp.DefaultInitFuncConstructors,
|
|
|
|
controllerAliases,
|
|
|
|
cliflag.NamedFlagSets{},
|
|
|
|
ctx.Done())
|
2021-06-29 14:28:38 +00:00
|
|
|
command.SetArgs(args)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-ccmRBACReady
|
2021-09-08 17:56:18 +00:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2023-04-21 19:44:08 +00:00
|
|
|
logrus.WithField("stack", string(debug.Stack())).Fatalf("cloud-controller-manager panic: %v", err)
|
2021-09-08 17:56:18 +00:00
|
|
|
}
|
|
|
|
}()
|
2024-01-25 00:44:12 +00:00
|
|
|
err := command.ExecuteContext(ctx)
|
|
|
|
if err != nil && !errors.Is(err, context.Canceled) {
|
|
|
|
logrus.Errorf("cloud-controller-manager exited: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
2021-06-29 14:28:38 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-29 00:38:31 +00:00
|
|
|
|
2022-02-24 22:35:08 +00:00
|
|
|
func (e *Embedded) CurrentETCDOptions() (InitialOptions, error) {
|
|
|
|
return InitialOptions{}, nil
|
|
|
|
}
|
|
|
|
|
2024-01-08 22:38:36 +00:00
|
|
|
func (e *Embedded) Containerd(ctx context.Context, cfg *daemonconfig.Node) error {
|
|
|
|
return containerd.Run(ctx, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Embedded) Docker(ctx context.Context, cfg *daemonconfig.Node) error {
|
|
|
|
return cridockerd.Run(ctx, cfg)
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:38:31 +00:00
|
|
|
// waitForUntaintedNode watches nodes, waiting to find one not tainted as
|
|
|
|
// uninitialized by the external cloud provider.
|
|
|
|
func waitForUntaintedNode(ctx context.Context, kubeConfig string) error {
|
2024-11-21 23:48:49 +00:00
|
|
|
restConfig, err := util.GetRESTConfig(kubeConfig)
|
2021-10-29 00:38:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
coreClient, err := typedcorev1.NewForConfig(restConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-21 20:56:39 +00:00
|
|
|
nodes := coreClient.Nodes()
|
|
|
|
|
|
|
|
lw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (object k8sruntime.Object, e error) {
|
|
|
|
return nodes.List(ctx, options)
|
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (i watch.Interface, e error) {
|
|
|
|
return nodes.Watch(ctx, options)
|
|
|
|
},
|
2021-10-29 00:38:31 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 20:56:39 +00:00
|
|
|
condition := func(ev watch.Event) (bool, error) {
|
|
|
|
if node, ok := ev.Object.(*v1.Node); ok {
|
|
|
|
return getCloudTaint(node.Spec.Taints) == nil, nil
|
2021-10-29 00:38:31 +00:00
|
|
|
}
|
2022-04-21 20:56:39 +00:00
|
|
|
return false, errors.New("event object not of type v1.Node")
|
2021-10-29 00:38:31 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 20:56:39 +00:00
|
|
|
if _, err := toolswatch.UntilWithSync(ctx, lw, &v1.Node{}, nil, condition); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to wait for untainted node")
|
|
|
|
}
|
|
|
|
return nil
|
2021-10-29 00:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getCloudTaint returns the external cloud provider taint, if present.
|
|
|
|
// Cribbed from k8s.io/cloud-provider/controllers/node/node_controller.go
|
2022-04-21 20:56:39 +00:00
|
|
|
func getCloudTaint(taints []v1.Taint) *v1.Taint {
|
2021-10-29 00:38:31 +00:00
|
|
|
for _, taint := range taints {
|
|
|
|
if taint.Key == cloudproviderapi.TaintExternalCloudProvider {
|
|
|
|
return &taint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|