2019-10-15 21:17:26 +00:00
|
|
|
package cloudprovider
|
|
|
|
|
|
|
|
import (
|
2022-09-29 20:37:50 +00:00
|
|
|
"encoding/json"
|
2019-10-15 21:17:26 +00:00
|
|
|
"io"
|
2022-09-29 20:37:50 +00:00
|
|
|
"io/ioutil"
|
2019-10-15 21:17:26 +00:00
|
|
|
|
2022-09-29 20:37:50 +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-09-29 20:37:50 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/apply"
|
|
|
|
"github.com/rancher/wrangler/pkg/generated/controllers/apps"
|
|
|
|
appsclient "github.com/rancher/wrangler/pkg/generated/controllers/apps/v1"
|
|
|
|
"github.com/rancher/wrangler/pkg/generated/controllers/core"
|
|
|
|
coreclient "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
|
|
|
|
"github.com/rancher/wrangler/pkg/generic"
|
|
|
|
"github.com/rancher/wrangler/pkg/start"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/tools/record"
|
|
|
|
"k8s.io/client-go/util/workqueue"
|
2019-10-15 21:17:26 +00:00
|
|
|
cloudprovider "k8s.io/cloud-provider"
|
|
|
|
)
|
|
|
|
|
2022-09-29 20:37:50 +00:00
|
|
|
// Config describes externally-configurable cloud provider configuration.
|
|
|
|
// This is normally unmarshalled from a JSON config file.
|
|
|
|
type Config struct {
|
|
|
|
LBEnabled bool `json:"lbEnabled"`
|
|
|
|
LBImage string `json:"lbImage"`
|
|
|
|
LBNamespace string `json:"lbNamespace"`
|
|
|
|
Rootless bool `json:"rootless"`
|
|
|
|
}
|
|
|
|
|
2019-10-15 21:17:26 +00:00
|
|
|
type k3s struct {
|
2022-09-29 20:37:50 +00:00
|
|
|
Config
|
|
|
|
|
|
|
|
client kubernetes.Interface
|
|
|
|
recorder record.EventRecorder
|
|
|
|
|
|
|
|
processor apply.Apply
|
|
|
|
daemonsetCache appsclient.DaemonSetCache
|
|
|
|
nodeCache coreclient.NodeCache
|
|
|
|
podCache coreclient.PodCache
|
|
|
|
workqueue workqueue.RateLimitingInterface
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 00:59:48 +00:00
|
|
|
var _ cloudprovider.Interface = &k3s{}
|
|
|
|
|
2019-10-15 21:17:26 +00:00
|
|
|
func init() {
|
2020-05-05 22:09:04 +00:00
|
|
|
cloudprovider.RegisterCloudProvider(version.Program, func(config io.Reader) (cloudprovider.Interface, error) {
|
2022-09-29 20:37:50 +00:00
|
|
|
var err error
|
|
|
|
k := k3s{
|
|
|
|
Config: Config{
|
|
|
|
LBEnabled: true,
|
|
|
|
LBImage: DefaultLBImage,
|
|
|
|
LBNamespace: DefaultLBNS,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if config != nil {
|
|
|
|
var bytes []byte
|
|
|
|
bytes, err = ioutil.ReadAll(config)
|
|
|
|
if err == nil {
|
|
|
|
err = json.Unmarshal(bytes, &k.Config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &k, err
|
2019-10-15 21:17:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *k3s) Initialize(clientBuilder cloudprovider.ControllerClientBuilder, stop <-chan struct{}) {
|
2022-09-29 20:37:50 +00:00
|
|
|
ctx, _ := wait.ContextForChannel(stop)
|
|
|
|
config := clientBuilder.ConfigOrDie(controllerName)
|
|
|
|
k.client = kubernetes.NewForConfigOrDie(config)
|
|
|
|
|
|
|
|
if k.LBEnabled {
|
|
|
|
// Wrangler controller and caches are only needed if the load balancer controller is enabled.
|
|
|
|
k.recorder = util.BuildControllerEventRecorder(k.client, controllerName, meta.NamespaceAll)
|
|
|
|
coreFactory := core.NewFactoryFromConfigOrDie(config)
|
|
|
|
k.nodeCache = coreFactory.Core().V1().Node().Cache()
|
|
|
|
|
|
|
|
lbCoreFactory := core.NewFactoryFromConfigWithOptionsOrDie(config, &generic.FactoryOptions{Namespace: k.LBNamespace})
|
|
|
|
lbAppsFactory := apps.NewFactoryFromConfigWithOptionsOrDie(config, &generic.FactoryOptions{Namespace: k.LBNamespace})
|
|
|
|
|
|
|
|
processor, err := apply.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("Failed to create apply processor for %s: %v", controllerName, err)
|
|
|
|
}
|
|
|
|
k.processor = processor.WithDynamicLookup().WithCacheTypes(lbAppsFactory.Apps().V1().DaemonSet())
|
|
|
|
k.daemonsetCache = lbAppsFactory.Apps().V1().DaemonSet().Cache()
|
|
|
|
k.podCache = lbCoreFactory.Core().V1().Pod().Cache()
|
|
|
|
k.workqueue = workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
|
|
|
|
|
|
|
|
if err := k.Register(ctx, coreFactory.Core().V1().Node(), lbCoreFactory.Core().V1().Pod()); err != nil {
|
|
|
|
logrus.Fatalf("Failed to register %s handlers: %v", controllerName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := start.All(ctx, 1, coreFactory, lbCoreFactory, lbAppsFactory); err != nil {
|
|
|
|
logrus.Fatalf("Failed to start %s controllers: %v", controllerName, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If load-balancer functionality has not been enabled, delete managed daemonsets.
|
|
|
|
// This uses the raw kubernetes client, as the controllers are not started when the load balancer controller is disabled.
|
|
|
|
if err := k.deleteAllDaemonsets(ctx); err != nil {
|
|
|
|
logrus.Fatalf("Failed to clean up %s daemonsets: %v", controllerName, err)
|
|
|
|
}
|
|
|
|
}
|
2021-01-23 00:59:48 +00:00
|
|
|
}
|
2019-10-15 21:17:26 +00:00
|
|
|
|
|
|
|
func (k *k3s) Instances() (cloudprovider.Instances, bool) {
|
2022-09-29 19:52:18 +00:00
|
|
|
return nil, false
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 16:26:36 +00:00
|
|
|
func (k *k3s) InstancesV2() (cloudprovider.InstancesV2, bool) {
|
2022-09-29 19:52:18 +00:00
|
|
|
return k, true
|
2020-08-05 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 21:17:26 +00:00
|
|
|
func (k *k3s) LoadBalancer() (cloudprovider.LoadBalancer, bool) {
|
2022-09-29 20:37:50 +00:00
|
|
|
return k, k.LBEnabled
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *k3s) Zones() (cloudprovider.Zones, bool) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *k3s) Clusters() (cloudprovider.Clusters, bool) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *k3s) Routes() (cloudprovider.Routes, bool) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *k3s) ProviderName() string {
|
2020-05-05 22:09:04 +00:00
|
|
|
return version.Program
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *k3s) HasClusterID() bool {
|
2022-09-29 19:52:18 +00:00
|
|
|
return false
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|