2019-10-15 21:17:26 +00:00
|
|
|
package cloudprovider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-04-21 22:56:20 +00:00
|
|
|
"strings"
|
2019-10-15 21:17:26 +00:00
|
|
|
|
2021-01-23 00:59:48 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-05-05 22:09:04 +00:00
|
|
|
"github.com/rancher/k3s/pkg/version"
|
2019-10-15 21:17:26 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
cloudprovider "k8s.io/cloud-provider"
|
|
|
|
)
|
|
|
|
|
2020-05-05 22:09:04 +00:00
|
|
|
var (
|
2021-04-21 22:56:20 +00:00
|
|
|
InternalIPKey = version.Program + ".io/internal-ip"
|
|
|
|
ExternalIPKey = version.Program + ".io/external-ip"
|
|
|
|
HostnameKey = version.Program + ".io/hostname"
|
2019-10-15 21:17:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (k *k3s) AddSSHKeyToAllInstances(ctx context.Context, user string, keyData []byte) error {
|
|
|
|
return cloudprovider.NotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *k3s) InstanceID(ctx context.Context, nodeName types.NodeName) (string, error) {
|
2021-01-23 00:59:48 +00:00
|
|
|
if k.nodeInformerHasSynced == nil || !k.nodeInformerHasSynced() {
|
|
|
|
return "", errors.New("Node informer has not synced yet")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := k.nodeInformer.Lister().Get(string(nodeName))
|
2019-10-15 21:17:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to find node %s: %v", nodeName, err)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2020-05-05 22:09:04 +00:00
|
|
|
return version.Program, nil
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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{}
|
2021-01-23 00:59:48 +00:00
|
|
|
if k.nodeInformerHasSynced == nil || !k.nodeInformerHasSynced() {
|
|
|
|
return nil, errors.New("Node informer has not synced yet")
|
|
|
|
}
|
|
|
|
|
|
|
|
node, err := k.nodeInformer.Lister().Get(string(name))
|
2019-10-15 21:17:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to find node %s: %v", name, err)
|
|
|
|
}
|
|
|
|
// check internal address
|
2021-04-21 22:56:20 +00:00
|
|
|
if address := node.Annotations[InternalIPKey]; address != "" {
|
|
|
|
for _, v := range strings.Split(address, ",") {
|
|
|
|
addresses = append(addresses, corev1.NodeAddress{Type: corev1.NodeInternalIP, Address: v})
|
|
|
|
}
|
|
|
|
} else if address = node.Labels[InternalIPKey]; address != "" {
|
|
|
|
addresses = append(addresses, corev1.NodeAddress{Type: corev1.NodeInternalIP, Address: address})
|
2019-10-15 21:17:26 +00:00
|
|
|
} else {
|
2021-04-21 22:56:20 +00:00
|
|
|
logrus.Infof("Couldn't find node internal ip annotation or label on node %s", name)
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check external address
|
2021-04-21 22:56:20 +00:00
|
|
|
if address := node.Annotations[ExternalIPKey]; address != "" {
|
|
|
|
for _, v := range strings.Split(address, ",") {
|
|
|
|
addresses = append(addresses, corev1.NodeAddress{Type: corev1.NodeExternalIP, Address: v})
|
|
|
|
}
|
|
|
|
} else if address = node.Labels[ExternalIPKey]; address != "" {
|
|
|
|
addresses = append(addresses, corev1.NodeAddress{Type: corev1.NodeExternalIP, Address: address})
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check hostname
|
2021-04-21 22:56:20 +00:00
|
|
|
if address := node.Annotations[HostnameKey]; address != "" {
|
|
|
|
addresses = append(addresses, corev1.NodeAddress{Type: corev1.NodeHostName, Address: address})
|
|
|
|
} else if address = node.Labels[HostnameKey]; address != "" {
|
|
|
|
addresses = append(addresses, corev1.NodeAddress{Type: corev1.NodeHostName, Address: address})
|
2019-10-15 21:17:26 +00:00
|
|
|
} else {
|
2021-04-21 22:56:20 +00:00
|
|
|
logrus.Infof("Couldn't find node hostname annotation or label on node %s", name)
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return addresses, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *k3s) NodeAddressesByProviderID(ctx context.Context, providerID string) ([]corev1.NodeAddress, error) {
|
|
|
|
return nil, cloudprovider.NotImplemented
|
|
|
|
}
|