2019-10-15 21:17:26 +00:00
|
|
|
package cloudprovider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-17 15:38:29 +00:00
|
|
|
"fmt"
|
2021-04-21 22:56:20 +00:00
|
|
|
"strings"
|
2019-10-15 21:17:26 +00:00
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2021-01-23 00:59:48 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-10-15 21:17:26 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
2022-09-29 19:52:18 +00:00
|
|
|
v1 "k8s.io/api/core/v1"
|
2019-10-15 21:17:26 +00:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2022-09-29 19:52:18 +00:00
|
|
|
var _ cloudprovider.InstancesV2 = &k3s{}
|
2019-10-15 21:17:26 +00:00
|
|
|
|
2022-09-29 19:52:18 +00:00
|
|
|
// 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) {
|
2019-10-15 21:17:26 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-09-29 19:52:18 +00:00
|
|
|
// 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
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 19:52:18 +00:00
|
|
|
// 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 nil, errors.New("address annotations not yet set")
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addresses := []corev1.NodeAddress{}
|
2021-01-23 00:59:48 +00:00
|
|
|
|
2019-10-15 21:17:26 +00:00
|
|
|
// 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 {
|
2022-09-29 19:52:18 +00:00
|
|
|
logrus.Infof("Couldn't find node internal ip annotation or label on node %s", node.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 {
|
2022-09-29 19:52:18 +00:00
|
|
|
logrus.Infof("Couldn't find node hostname annotation or label on node %s", node.Name)
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 19:52:18 +00:00
|
|
|
return &cloudprovider.InstanceMetadata{
|
2022-10-17 15:38:29 +00:00
|
|
|
ProviderID: fmt.Sprintf("%s://%s", version.Program, node.Name),
|
2022-09-29 19:52:18 +00:00
|
|
|
InstanceType: version.Program,
|
|
|
|
NodeAddresses: addresses,
|
|
|
|
Zone: "",
|
|
|
|
Region: "",
|
|
|
|
}, nil
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|