Repository: k3s-io/k3s
File: pkg/agent/run.go (Go)

Code before:
func updateAddressAnnotations(nodeConfig *daemonconfig.Node, nodeAnnotations map[string]string) (map[string]string, bool) {
	agentConfig := &nodeConfig.AgentConfig
	result := map[string]string{
		cp.InternalIPKey: util.JoinIPs(agentConfig.NodeIPs),
		cp.HostnameKey:   getHostname(agentConfig),
	}

	if agentConfig.NodeExternalIP != "" {
		result[cp.ExternalIPKey] = util.JoinIPs(agentConfig.NodeExternalIPs)
		if nodeConfig.FlannelExternalIP {
			for _, ipAddress := range agentConfig.NodeExternalIPs {
				if utilsnet.IsIPv4(ipAddress) {
					result[flannel.FlannelExternalIPv4Annotation] = ipAddress.String()
				}
				if utilsnet.IsIPv6(ipAddress) {
					result[flannel.FlannelExternalIPv6Annotation] = ipAddress.String()
				}
			}
		}
	}

	if agentConfig.NodeExternalDNS != "" {
		result[cp.ExternalDNSKey] = strings.Join(agentConfig.NodeExternalDNSs, ",")
		if nodeConfig.FlannelExternalIP {
			for _, dns := range agentConfig.NodeExternalDNSs {
				result[flannel.FlannelExternalDNSAnnotation] = dns
			}
		}
	}

	result = labels.Merge(nodeAnnotations, result)
	return result, !equality.Semantic.DeepEqual(nodeAnnotations, result)
}

// setupTunnelAndRunAgent should start the setup tunnel before starting kubelet and kubeproxy
// there are special case for etcd agents, it will wait until it can find the apiaddress from
// the address channel and update the proxy with the servers addresses, if in rke2 we need to
// start the agent before the tunnel is setup to allow kubelet to start first and start the pods
func setupTunnelAndRunAgent(ctx context.Context, nodeConfig *daemonconfig.Node, cfg cmds.Agent, proxy proxy.Proxy) error {
	var agentRan bool
	// IsAPIServerLBEnabled is used as a shortcut for detecting RKE2, where the kubelet needs to
	// be run earlier in order to manage static pods. This should probably instead query a
	// flag on the executor or something.
	if !cfg.ClusterReset && cfg.ETCDAgent {
		// ETCDAgent is only set to true on servers that are started with --disable-apiserver.
		// In this case, we may be running without an apiserver available in the cluster, and need
		// to wait for one to register and post it's address into APIAddressCh so that we can update
		// the LB proxy with its address.
		if proxy.IsAPIServerLBEnabled() {
			// On RKE2, the agent needs to be started early to run the etcd static pod.
			if err := agent.Agent(ctx, nodeConfig, proxy); err != nil {

Review comment (suggestion):
I'm not sure why you're adding flannel annotations? Flannel does not care about the DNS addresses, and will not make use of these annotations. You should be adding annotations with `cloudprovider.ExternalDNSKey` since that is what the cloud provider sets the addresses from.

And do the same for InternalDNSKey.

Code after:
func updateAddressAnnotations(nodeConfig *daemonconfig.Node, nodeAnnotations map[string]string) (map[string]string, bool) {
	agentConfig := &nodeConfig.AgentConfig
	result := map[string]string{
		cp.InternalIPKey: util.JoinIPs(agentConfig.NodeIPs),
		cp.HostnameKey:   getHostname(agentConfig),
	}

	if agentConfig.NodeExternalIP != "" {
		result[cp.ExternalIPKey] = util.JoinIPs(agentConfig.NodeExternalIPs)
		if nodeConfig.FlannelExternalIP {
			for _, ipAddress := range agentConfig.NodeExternalIPs {
				if utilsnet.IsIPv4(ipAddress) {
					result[flannel.FlannelExternalIPv4Annotation] = ipAddress.String()
				}
				if utilsnet.IsIPv6(ipAddress) {
					result[flannel.FlannelExternalIPv6Annotation] = ipAddress.String()
				}
			}
		}
	}

	if len(agentConfig.NodeInternalDNSs) > 0 {
		result[cp.InternalDNSKey] = strings.Join(agentConfig.NodeInternalDNSs, ",")
	} else {
		delete(result, cp.InternalDNSKey)
	}
	if len(agentConfig.NodeExternalDNSs) > 0 {
		result[cp.ExternalDNSKey] = strings.Join(agentConfig.NodeExternalDNSs, ",")
	} else {
		delete(result, cp.ExternalDNSKey)
	}

	result = labels.Merge(nodeAnnotations, result)
	return result, !equality.Semantic.DeepEqual(nodeAnnotations, result)
}

// setupTunnelAndRunAgent should start the setup tunnel before starting kubelet and kubeproxy
// there are special case for etcd agents, it will wait until it can find the apiaddress from
// the address channel and update the proxy with the servers addresses, if in rke2 we need to
// start the agent before the tunnel is setup to allow kubelet to start first and start the pods
func setupTunnelAndRunAgent(ctx context.Context, nodeConfig *daemonconfig.Node, cfg cmds.Agent, proxy proxy.Proxy) error {
	var agentRan bool
	// IsAPIServerLBEnabled is used as a shortcut for detecting RKE2, where the kubelet needs to
	// be run earlier in order to manage static pods. This should probably instead query a
	// flag on the executor or something.
	if !cfg.ClusterReset && cfg.ETCDAgent {
		// ETCDAgent is only set to true on servers that are started with --disable-apiserver.
		// In this case, we may be running without an apiserver available in the cluster, and need
		// to wait for one to register and post it's address into APIAddressCh so that we can update
		// the LB proxy with its address.
		if proxy.IsAPIServerLBEnabled() {