Skip to content

Commit

Permalink
Auto add private ip annotation to nodes (#182)
Browse files Browse the repository at this point in the history
Co-authored-by: Rahul Sharma <[email protected]>
  • Loading branch information
rahulait and rahulait authored Mar 21, 2024
1 parent 918806c commit 9b6ebcf
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions cloud/linode/node_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ func (s *nodeController) handleNode(ctx context.Context, node *v1.Node) error {

lastUpdate := s.LastMetadataUpdate(node.Name)

uuid, ok := node.Labels[annotations.AnnLinodeHostUUID]
if ok && time.Since(lastUpdate) < s.ttl {
uuid, foundLabel := node.Labels[annotations.AnnLinodeHostUUID]
configuredPrivateIP, foundAnnotation := node.Annotations[annotations.AnnLinodeNodePrivateIP]
if foundLabel && foundAnnotation && time.Since(lastUpdate) < s.ttl {
return nil
}

Expand All @@ -138,7 +139,19 @@ func (s *nodeController) handleNode(ctx context.Context, node *v1.Node) error {
return err
}

if uuid == linode.HostUUID {
expectedPrivateIP := ""
// linode API response for linode will contain only one private ip
// if any private ip is configured. If it changes in future or linode
// supports other subnets with nodebalancer, this logic needs to be updated.
// https://www.linode.com/docs/api/linode-instances/#linode-view
for _, addr := range linode.IPv4 {
if addr.IsPrivate() {
expectedPrivateIP = addr.String()
break
}
}

if uuid == linode.HostUUID && configuredPrivateIP == expectedPrivateIP {
s.SetLastMetadataUpdate(node.Name)
return nil
}
Expand All @@ -150,13 +163,16 @@ func (s *nodeController) handleNode(ctx context.Context, node *v1.Node) error {
return err
}

// It may be that the UUID has been set
if n.Labels[annotations.AnnLinodeHostUUID] == linode.HostUUID {
// It may be that the UUID label and private ip annotation has been set
if n.Labels[annotations.AnnLinodeHostUUID] == linode.HostUUID && n.Annotations[annotations.AnnLinodeNodePrivateIP] == expectedPrivateIP {
return nil
}

// Try to update the node
n.Labels[annotations.AnnLinodeHostUUID] = linode.HostUUID
if expectedPrivateIP != "" {
n.Annotations[annotations.AnnLinodeNodePrivateIP] = expectedPrivateIP
}
_, err = s.kubeclient.CoreV1().Nodes().Update(ctx, n, metav1.UpdateOptions{})
return err
}); err != nil {
Expand Down

0 comments on commit 9b6ebcf

Please sign in to comment.