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"
|
|
|
|
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.
|
2023-06-20 07:08:08 +00:00
|
|
|
func (k *k3s) InstanceShutdown(ctx context.Context, node *corev1.Node) (bool, error) {
|
2022-09-29 19:52:18 +00:00
|
|
|
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.
|
2023-06-20 07:08:08 +00:00
|
|
|
func (k *k3s) InstanceMetadata(ctx context.Context, node *corev1.Node) (*cloudprovider.InstanceMetadata, error) {
|
2022-09-29 19:52:18 +00:00
|
|
|
if (node.Annotations[InternalIPKey] == "") && (node.Labels[InternalIPKey] == "") {
|
|
|
|
return nil, errors.New("address annotations not yet set")
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 18:41:03 +00:00
|
|
|
metadata := &cloudprovider.InstanceMetadata{
|
|
|
|
ProviderID: fmt.Sprintf("%s://%s", version.Program, node.Name),
|
|
|
|
InstanceType: version.Program,
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Spec.ProviderID != "" {
|
|
|
|
metadata.ProviderID = node.Spec.ProviderID
|
|
|
|
}
|
|
|
|
|
|
|
|
if instanceType := node.Labels[corev1.LabelInstanceTypeStable]; instanceType != "" {
|
|
|
|
metadata.InstanceType = instanceType
|
|
|
|
}
|
|
|
|
|
|
|
|
if region := node.Labels[corev1.LabelTopologyRegion]; region != "" {
|
|
|
|
metadata.Region = region
|
|
|
|
}
|
|
|
|
|
|
|
|
if zone := node.Labels[corev1.LabelTopologyZone]; zone != "" {
|
|
|
|
metadata.Zone = zone
|
|
|
|
}
|
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, ",") {
|
2024-03-11 18:41:03 +00:00
|
|
|
metadata.NodeAddresses = append(metadata.NodeAddresses, corev1.NodeAddress{Type: corev1.NodeInternalIP, Address: v})
|
2021-04-21 22:56:20 +00:00
|
|
|
}
|
|
|
|
} else if address = node.Labels[InternalIPKey]; address != "" {
|
2024-03-11 18:41:03 +00:00
|
|
|
metadata.NodeAddresses = append(metadata.NodeAddresses, 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, ",") {
|
2024-03-11 18:41:03 +00:00
|
|
|
metadata.NodeAddresses = append(metadata.NodeAddresses, corev1.NodeAddress{Type: corev1.NodeExternalIP, Address: v})
|
2021-04-21 22:56:20 +00:00
|
|
|
}
|
|
|
|
} else if address = node.Labels[ExternalIPKey]; address != "" {
|
2024-03-11 18:41:03 +00:00
|
|
|
metadata.NodeAddresses = append(metadata.NodeAddresses, 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 != "" {
|
2024-03-11 18:41:03 +00:00
|
|
|
metadata.NodeAddresses = append(metadata.NodeAddresses, corev1.NodeAddress{Type: corev1.NodeHostName, Address: address})
|
2021-04-21 22:56:20 +00:00
|
|
|
} else if address = node.Labels[HostnameKey]; address != "" {
|
2024-03-11 18:41:03 +00:00
|
|
|
metadata.NodeAddresses = append(metadata.NodeAddresses, 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
|
|
|
}
|
|
|
|
|
2024-03-11 18:41:03 +00:00
|
|
|
return metadata, nil
|
2019-10-15 21:17:26 +00:00
|
|
|
}
|