2020-04-27 17:09:58 +00:00
|
|
|
// +build !no_embedded_executor
|
|
|
|
|
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-29 00:38:31 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-04-27 17:09:58 +00:00
|
|
|
"net/http"
|
2021-10-29 00:38:31 +00:00
|
|
|
"runtime"
|
2020-04-27 17:09:58 +00:00
|
|
|
|
2021-05-11 19:50:08 +00:00
|
|
|
"github.com/rancher/k3s/pkg/cli/cmds"
|
|
|
|
daemonconfig "github.com/rancher/k3s/pkg/daemons/config"
|
2021-06-29 14:28:38 +00:00
|
|
|
"github.com/rancher/k3s/pkg/version"
|
2020-04-27 17:09:58 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-10-29 00:38:31 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2021-06-29 14:28:38 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
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"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2021-06-29 14:28:38 +00:00
|
|
|
ccm "k8s.io/cloud-provider"
|
|
|
|
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"
|
|
|
|
ccmopt "k8s.io/cloud-provider/options"
|
|
|
|
cliflag "k8s.io/component-base/cli/flag"
|
2020-04-27 17:09:58 +00:00
|
|
|
"k8s.io/kubernetes/cmd/kube-apiserver/app"
|
|
|
|
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
|
|
|
|
_ "github.com/rancher/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
|
|
|
type Embedded struct {
|
|
|
|
nodeConfig *daemonconfig.Node
|
|
|
|
}
|
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
|
2021-05-11 19:50:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:38:31 +00:00
|
|
|
func (*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 {
|
|
|
|
logrus.Fatalf("kubelet panic: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-09-13 22:20:03 +00:00
|
|
|
logrus.Fatalf("kubelet exited: %v", command.ExecuteContext(ctx))
|
2020-04-27 17:09:58 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:38:31 +00:00
|
|
|
func (*Embedded) KubeProxy(ctx context.Context, args []string) error {
|
2020-04-27 17:09:58 +00:00
|
|
|
command := proxy.NewProxyCommand()
|
|
|
|
command.SetArgs(args)
|
|
|
|
|
|
|
|
go func() {
|
2021-09-08 17:56:18 +00:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
logrus.Fatalf("kube-proxy panic: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-09-13 22:20:03 +00:00
|
|
|
logrus.Fatalf("kube-proxy exited: %v", command.ExecuteContext(ctx))
|
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) {
|
2021-10-12 06:13:10 +00:00
|
|
|
startupConfig := <-app.StartupConfig
|
|
|
|
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 {
|
2020-04-27 17:09:58 +00:00
|
|
|
command := app.NewAPIServerCommand(ctx.Done())
|
|
|
|
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 {
|
|
|
|
logrus.Fatalf("apiserver panic: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-09-13 22:20:03 +00:00
|
|
|
logrus.Fatalf("apiserver exited: %v", command.ExecuteContext(ctx))
|
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 {
|
|
|
|
logrus.Fatalf("scheduler panic: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-09-13 22:20:03 +00:00
|
|
|
logrus.Fatalf("scheduler exited: %v", command.ExecuteContext(ctx))
|
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 {
|
|
|
|
logrus.Fatalf("controller-manager panic: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-09-13 22:20:03 +00:00
|
|
|
logrus.Fatalf("controller-manager exited: %v", command.ExecuteContext(ctx))
|
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 {
|
|
|
|
cloud, err := ccm.InitCloudProvider(version.Program, "")
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("Cloud provider could not be initialized: %v", err)
|
|
|
|
}
|
|
|
|
if cloud == nil {
|
|
|
|
logrus.Fatalf("Cloud provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
cloud.Initialize(config.ClientBuilder, make(chan struct{}))
|
|
|
|
if informerUserCloud, ok := cloud.(ccm.InformerUser); ok {
|
|
|
|
informerUserCloud.SetInformers(config.SharedInformers)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cloud
|
|
|
|
}
|
|
|
|
|
|
|
|
controllerInitializers := ccmapp.DefaultInitFuncConstructors
|
|
|
|
delete(controllerInitializers, "service")
|
|
|
|
delete(controllerInitializers, "route")
|
|
|
|
|
|
|
|
command := ccmapp.NewCloudControllerManagerCommand(ccmOptions, cloudInitializer, controllerInitializers, cliflag.NamedFlagSets{}, wait.NeverStop)
|
|
|
|
command.SetArgs(args)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-ccmRBACReady
|
2021-09-08 17:56:18 +00:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
logrus.Fatalf("cloud-controller-manager panic: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-09-13 22:20:03 +00:00
|
|
|
logrus.Errorf("cloud-controller-manager exited: %v", command.ExecuteContext(ctx))
|
2021-06-29 14:28:38 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
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 {
|
|
|
|
restConfig, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
coreClient, err := typedcorev1.NewForConfig(restConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// List first, to see if there's an existing node that will do
|
|
|
|
nodes, err := coreClient.Nodes().List(ctx, metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, node := range nodes.Items {
|
|
|
|
if taint := getCloudTaint(node.Spec.Taints); taint == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// List didn't give us an existing node, start watching at whatever ResourceVersion the list left off at.
|
|
|
|
watcher, err := coreClient.Nodes().Watch(ctx, metav1.ListOptions{ResourceVersion: nodes.ListMeta.ResourceVersion})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer watcher.Stop()
|
|
|
|
|
|
|
|
for ev := range watcher.ResultChan() {
|
|
|
|
if ev.Type == watch.Added || ev.Type == watch.Modified {
|
|
|
|
node, ok := ev.Object.(*corev1.Node)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("could not convert event object to node: %v", ev)
|
|
|
|
}
|
|
|
|
if taint := getCloudTaint(node.Spec.Taints); taint == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("watch channel closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCloudTaint returns the external cloud provider taint, if present.
|
|
|
|
// Cribbed from k8s.io/cloud-provider/controllers/node/node_controller.go
|
|
|
|
func getCloudTaint(taints []corev1.Taint) *corev1.Taint {
|
|
|
|
for _, taint := range taints {
|
|
|
|
if taint.Key == cloudproviderapi.TaintExternalCloudProvider {
|
|
|
|
return &taint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|