k3s/pkg/cloudprovider/instances.go

93 lines
3.2 KiB
Go
Raw Normal View History

2019-10-15 21:17:26 +00:00
package cloudprovider
import (
"context"
"fmt"
"strings"
2019-10-15 21:17:26 +00:00
"github.com/k3s-io/k3s/pkg/version"
"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"
)
var (
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
)
var _ cloudprovider.InstancesV2 = &k3s{}
2019-10-15 21:17:26 +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
}
// 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 *corev1.Node) (bool, error) {
return false, nil
2019-10-15 21:17:26 +00:00
}
// InstanceMetadata returns the instance's metadata.
func (k *k3s) InstanceMetadata(ctx context.Context, node *corev1.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
}
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
}
2019-10-15 21:17:26 +00:00
// check internal address
if address := node.Annotations[InternalIPKey]; address != "" {
for _, v := range strings.Split(address, ",") {
metadata.NodeAddresses = append(metadata.NodeAddresses, corev1.NodeAddress{Type: corev1.NodeInternalIP, Address: v})
}
} else if address = node.Labels[InternalIPKey]; address != "" {
metadata.NodeAddresses = append(metadata.NodeAddresses, corev1.NodeAddress{Type: corev1.NodeInternalIP, Address: address})
2019-10-15 21:17:26 +00:00
} else {
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
if address := node.Annotations[ExternalIPKey]; address != "" {
for _, v := range strings.Split(address, ",") {
metadata.NodeAddresses = append(metadata.NodeAddresses, corev1.NodeAddress{Type: corev1.NodeExternalIP, Address: v})
}
} else if address = node.Labels[ExternalIPKey]; address != "" {
metadata.NodeAddresses = append(metadata.NodeAddresses, corev1.NodeAddress{Type: corev1.NodeExternalIP, Address: address})
2019-10-15 21:17:26 +00:00
}
// check hostname
if address := node.Annotations[HostnameKey]; address != "" {
metadata.NodeAddresses = append(metadata.NodeAddresses, corev1.NodeAddress{Type: corev1.NodeHostName, Address: address})
} else if address = node.Labels[HostnameKey]; address != "" {
metadata.NodeAddresses = append(metadata.NodeAddresses, corev1.NodeAddress{Type: corev1.NodeHostName, Address: address})
2019-10-15 21:17:26 +00:00
} else {
logrus.Infof("Couldn't find node hostname annotation or label on node %s", node.Name)
2019-10-15 21:17:26 +00:00
}
return metadata, nil
2019-10-15 21:17:26 +00:00
}