diff --git a/cluster/cluster.go b/cluster/cluster.go index ef7e4096..d1a5ddff 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -281,6 +281,9 @@ func (c *Cluster) SyncLabelsAndTaints(ctx context.Context) error { return fmt.Errorf("Failed to initialize new kubernetes client: %v", err) } for _, host := range hosts.GetUniqueHostList(c.EtcdHosts, c.ControlPlaneHosts, c.WorkerHosts) { + if err := k8s.SetAddressesAnnotations(k8sClient, host.HostnameOverride, host.InternalAddress, host.Address); err != nil { + return err + } if err := k8s.SyncLabels(k8sClient, host.HostnameOverride, host.ToAddLabels, host.ToDelLabels); err != nil { return err } diff --git a/cluster/plan.go b/cluster/plan.go index 496a10db..dabda3e3 100644 --- a/cluster/plan.go +++ b/cluster/plan.go @@ -246,7 +246,9 @@ func (c *Cluster) BuildKubeletProcess(host *hosts.Host) v3.Process { "require-kubeconfig": "True", "fail-swap-on": strconv.FormatBool(c.Services.Kubelet.FailSwapOn), } - + if host.Address != host.InternalAddress { + CommandArgs["node-ip"] = host.InternalAddress + } VolumesFrom := []string{ services.SidekickContainerName, } diff --git a/k8s/node.go b/k8s/node.go index 557c785d..b0057be2 100644 --- a/k8s/node.go +++ b/k8s/node.go @@ -6,6 +6,7 @@ import ( "strings" "time" + "github.com/pkg/errors" "github.com/sirupsen/logrus" "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -15,7 +16,9 @@ import ( ) const ( - HostnameLabel = "kubernetes.io/hostname" + HostnameLabel = "kubernetes.io/hostname" + InternalAddressAnnotation = "rke.io/internal-ip" + ExternalAddressAnnotation = "rke.io/external-ip" ) func DeleteNode(k8sClient *kubernetes.Clientset, nodeName string) error { @@ -244,3 +247,30 @@ func toTaint(taintStr string) v1.Taint { Effect: effect, } } + +func SetAddressesAnnotations(k8sClient *kubernetes.Clientset, nodeName, internalAddress, externalAddress string) error { + var listErr error + for retries := 0; retries <= 5; retries++ { + node, err := GetNode(k8sClient, nodeName) + if err != nil { + listErr = errors.Wrapf(err, "Failed to get kubernetes node [%s]", nodeName) + time.Sleep(time.Second * 5) + continue + } + currentExternalAnnotation := node.Annotations[ExternalAddressAnnotation] + currentInternalAnnotation := node.Annotations[ExternalAddressAnnotation] + if currentExternalAnnotation == externalAddress && currentInternalAnnotation == internalAddress { + return nil + } + node.Annotations[ExternalAddressAnnotation] = externalAddress + node.Annotations[InternalAddressAnnotation] = internalAddress + _, err = k8sClient.CoreV1().Nodes().Update(node) + if err != nil { + listErr = errors.Wrapf(err, "Error updating node [%s] with address annotations: %v", nodeName, err) + time.Sleep(time.Second * 5) + continue + } + return nil + } + return listErr +}