Implement InstancesV2 instead of Instances

... and drop legacy ClusterID support.

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
pull/6203/head
Brad Davidson 2022-09-29 19:52:18 +00:00 committed by Brad Davidson
parent fac9c99adf
commit 063039471c
2 changed files with 25 additions and 66 deletions

View File

@ -4,19 +4,13 @@ import (
"io"
"github.com/k3s-io/k3s/pkg/version"
"k8s.io/client-go/informers"
informercorev1 "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/tools/cache"
cloudprovider "k8s.io/cloud-provider"
)
type k3s struct {
nodeInformer informercorev1.NodeInformer
nodeInformerHasSynced cache.InformerSynced
}
var _ cloudprovider.Interface = &k3s{}
var _ cloudprovider.InformerUser = &k3s{}
func init() {
cloudprovider.RegisterCloudProvider(version.Program, func(config io.Reader) (cloudprovider.Interface, error) {
@ -27,17 +21,12 @@ func init() {
func (k *k3s) Initialize(clientBuilder cloudprovider.ControllerClientBuilder, stop <-chan struct{}) {
}
func (k *k3s) SetInformers(informerFactory informers.SharedInformerFactory) {
k.nodeInformer = informerFactory.Core().V1().Nodes()
k.nodeInformerHasSynced = k.nodeInformer.Informer().HasSynced
}
func (k *k3s) Instances() (cloudprovider.Instances, bool) {
return k, true
return nil, false
}
func (k *k3s) InstancesV2() (cloudprovider.InstancesV2, bool) {
return nil, false
return k, true
}
func (k *k3s) LoadBalancer() (cloudprovider.LoadBalancer, bool) {
@ -61,5 +50,5 @@ func (k *k3s) ProviderName() string {
}
func (k *k3s) HasClusterID() bool {
return true
return false
}

View File

@ -2,14 +2,13 @@ package cloudprovider
import (
"context"
"fmt"
"strings"
"github.com/k3s-io/k3s/pkg/version"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
v1 "k8s.io/api/core/v1"
cloudprovider "k8s.io/cloud-provider"
)
@ -19,59 +18,28 @@ var (
HostnameKey = version.Program + ".io/hostname"
)
func (k *k3s) AddSSHKeyToAllInstances(ctx context.Context, user string, keyData []byte) error {
return cloudprovider.NotImplemented
}
var _ cloudprovider.InstancesV2 = &k3s{}
func (k *k3s) CurrentNodeName(ctx context.Context, hostname string) (types.NodeName, error) {
return types.NodeName(hostname), nil
}
func (k *k3s) InstanceExistsByProviderID(ctx context.Context, providerID string) (bool, error) {
// InstanceExists returns true if the instance for the given node exists according to the cloud provider.
// K3s nodes always exist.
func (k *k3s) InstanceExists(ctx context.Context, node *corev1.Node) (bool, error) {
return true, nil
}
func (k *k3s) InstanceID(ctx context.Context, nodeName types.NodeName) (string, error) {
if k.nodeInformerHasSynced == nil || !k.nodeInformerHasSynced() {
return "", errors.New("Node informer has not synced yet")
}
// InstanceShutdown returns true if the instance is shutdown according to the cloud provider.
// K3s nodes are never shutdown.
func (k *k3s) InstanceShutdown(ctx context.Context, node *v1.Node) (bool, error) {
return false, nil
}
node, err := k.nodeInformer.Lister().Get(string(nodeName))
if err != nil {
return "", fmt.Errorf("failed to get node %s: %w", nodeName, err)
}
// InstanceMetadata returns the instance's metadata.
func (k *k3s) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {
if (node.Annotations[InternalIPKey] == "") && (node.Labels[InternalIPKey] == "") {
return string(nodeName), errors.New("address annotations not yet set")
return nil, errors.New("address annotations not yet set")
}
return string(nodeName), nil
}
func (k *k3s) InstanceShutdownByProviderID(ctx context.Context, providerID string) (bool, error) {
return true, cloudprovider.NotImplemented
}
func (k *k3s) InstanceType(ctx context.Context, name types.NodeName) (string, error) {
_, err := k.InstanceID(ctx, name)
if err != nil {
return "", err
}
return version.Program, nil
}
func (k *k3s) InstanceTypeByProviderID(ctx context.Context, providerID string) (string, error) {
return "", cloudprovider.NotImplemented
}
func (k *k3s) NodeAddresses(ctx context.Context, name types.NodeName) ([]corev1.NodeAddress, error) {
addresses := []corev1.NodeAddress{}
if k.nodeInformerHasSynced == nil || !k.nodeInformerHasSynced() {
return nil, errors.New("Node informer has not synced yet")
}
node, err := k.nodeInformer.Lister().Get(string(name))
if err != nil {
return nil, fmt.Errorf("Failed to find node %s: %v", name, err)
}
// check internal address
if address := node.Annotations[InternalIPKey]; address != "" {
for _, v := range strings.Split(address, ",") {
@ -80,7 +48,7 @@ func (k *k3s) NodeAddresses(ctx context.Context, name types.NodeName) ([]corev1.
} else if address = node.Labels[InternalIPKey]; address != "" {
addresses = append(addresses, corev1.NodeAddress{Type: corev1.NodeInternalIP, Address: address})
} else {
logrus.Infof("Couldn't find node internal ip annotation or label on node %s", name)
logrus.Infof("Couldn't find node internal ip annotation or label on node %s", node.Name)
}
// check external address
@ -98,12 +66,14 @@ func (k *k3s) NodeAddresses(ctx context.Context, name types.NodeName) ([]corev1.
} else if address = node.Labels[HostnameKey]; address != "" {
addresses = append(addresses, corev1.NodeAddress{Type: corev1.NodeHostName, Address: address})
} else {
logrus.Infof("Couldn't find node hostname annotation or label on node %s", name)
logrus.Infof("Couldn't find node hostname annotation or label on node %s", node.Name)
}
return addresses, nil
}
func (k *k3s) NodeAddressesByProviderID(ctx context.Context, providerID string) ([]corev1.NodeAddress, error) {
return nil, cloudprovider.NotImplemented
return &cloudprovider.InstanceMetadata{
ProviderID: version.Program,
InstanceType: version.Program,
NodeAddresses: addresses,
Zone: "",
Region: "",
}, nil
}