Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto add private ip annotation to nodes #182

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading