2019-10-15 21:17:26 +00:00
|
|
|
package cloudprovider
|
|
|
|
|
|
|
|
import (
|
2022-09-29 20:37:50 +00:00
|
|
|
"encoding/json"
|
2022-09-29 20:50:05 +00:00
|
|
|
"fmt"
|
2019-10-15 21:17:26 +00:00
|
|
|
"io"
|
|
|
|
|
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"
|
2024-05-06 16:42:27 +00:00
|
|
|
"github.com/rancher/wrangler/v3/pkg/apply"
|
|
|
|
"github.com/rancher/wrangler/v3/pkg/generated/controllers/apps"
|
|
|
|
appsclient "github.com/rancher/wrangler/v3/pkg/generated/controllers/apps/v1"
|
|
|
|
"github.com/rancher/wrangler/v3/pkg/generated/controllers/core"
|
|
|
|
coreclient "github.com/rancher/wrangler/v3/pkg/generated/controllers/core/v1"
|
|
|
|
"github.com/rancher/wrangler/v3/pkg/generated/controllers/discovery"
|
|
|
|
discoveryclient "github.com/rancher/wrangler/v3/pkg/generated/controllers/discovery/v1"
|
|
|
|
"github.com/rancher/wrangler/v3/pkg/generic"
|
|
|
|
"github.com/rancher/wrangler/v3/pkg/start"
|
2022-09-29 20:37:50 +00:00
|
|
|
"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 {
|
2024-04-29 19:39:35 +00:00
|
|
|
LBDefaultPriorityClassName string `json:"lbDefaultPriorityClassName"`
|
|
|
|
LBEnabled bool `json:"lbEnabled"`
|
|
|
|
LBImage string `json:"lbImage"`
|
|
|
|
LBNamespace string `json:"lbNamespace"`
|
|
|
|
NodeEnabled bool `json:"nodeEnabled"`
|
|
|
|
Rootless bool `json:"rootless"`
|
2022-09-29 20:37:50 +00:00
|
|
|
}
|
|
|
|
|
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
|
2023-01-26 23:48:12 +00:00
|
|
|
endpointsCache discoveryclient.EndpointSliceCache
|
2022-09-29 20:37:50 +00:00
|
|
|
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{
|
2024-04-29 19:39:35 +00:00
|
|
|
LBDefaultPriorityClassName: DefaultLBPriorityClassName,
|
|
|
|
LBEnabled: true,
|
|
|
|
LBImage: DefaultLBImage,
|
|
|
|
LBNamespace: DefaultLBNS,
|
|
|
|
NodeEnabled: true,
|
2022-09-29 20:37:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if config != nil {
|
|
|
|
var bytes []byte
|
2022-10-08 00:36:57 +00:00
|
|
|
bytes, err = io.ReadAll(config)
|
2022-09-29 20:37:50 +00:00
|
|
|
if err == nil {
|
|
|
|
err = json.Unmarshal(bytes, &k.Config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 20:50:05 +00:00
|
|
|
if !k.LBEnabled && !k.NodeEnabled {
|
|
|
|
return nil, fmt.Errorf("all cloud-provider functionality disabled by config")
|
|
|
|
}
|
|
|
|
|
2022-09-29 20:37:50 +00:00
|
|
|
return &k, err
|
2019-10-15 21:17:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *k3s) Initialize(clientBuilder cloudprovider.ControllerClientBuilder, stop <-chan struct{}) {
|
2023-04-19 04:48:36 +00:00
|
|
|
ctx := wait.ContextForChannel(stop)
|
2022-09-29 20:37:50 +00:00
|
|
|
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})
|
2023-01-26 23:48:12 +00:00
|
|
|
lbDiscFactory := discovery.NewFactoryFromConfigOrDie(config)
|
2022-09-29 20:37:50 +00:00
|
|
|
|
|
|
|
processor, err := apply.NewForConfig(config)
|
|
|
|
if err != nil {
|
2023-02-07 01:34:02 +00:00
|
|
|
logrus.Panicf("failed to create apply processor for %s: %v", controllerName, err)
|
2022-09-29 20:37:50 +00:00
|
|
|
}
|
|
|
|
k.processor = processor.WithDynamicLookup().WithCacheTypes(lbAppsFactory.Apps().V1().DaemonSet())
|
|
|
|
k.daemonsetCache = lbAppsFactory.Apps().V1().DaemonSet().Cache()
|
2023-01-26 23:48:12 +00:00
|
|
|
k.endpointsCache = lbDiscFactory.Discovery().V1().EndpointSlice().Cache()
|
2022-09-29 20:37:50 +00:00
|
|
|
k.podCache = lbCoreFactory.Core().V1().Pod().Cache()
|
|
|
|
k.workqueue = workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
|
|
|
|
|
2023-01-26 23:48:12 +00:00
|
|
|
if err := k.Register(ctx, coreFactory.Core().V1().Node(), lbCoreFactory.Core().V1().Pod(), lbDiscFactory.Discovery().V1().EndpointSlice()); err != nil {
|
2023-02-07 01:34:02 +00:00
|
|
|
logrus.Panicf("failed to register %s handlers: %v", controllerName, err)
|
2022-09-29 20:37:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-26 23:48:12 +00:00
|
|
|
if err := start.All(ctx, 1, coreFactory, lbCoreFactory, lbAppsFactory, lbDiscFactory); err != nil {
|
2023-02-07 01:34:02 +00:00
|
|
|
logrus.Panicf("failed to start %s controllers: %v", controllerName, err)
|
2022-09-29 20:37:50 +00:00
|
|
|
}
|
|
|
|
} 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 {
|
2023-02-07 01:34:02 +00:00
|
|
|
logrus.Panicf("failed to clean up %s daemonsets: %v", controllerName, err)
|
2022-09-29 20:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
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 20:50:05 +00:00
|
|
|
return k, k.NodeEnabled
|
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
|
|
|
}
|