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

Code before:
	"github.com/k3s-io/k3s/pkg/util"
	"github.com/k3s-io/k3s/pkg/version"
	"github.com/rancher/remotedialer"
	"github.com/sirupsen/logrus"
	"github.com/yl2chen/cidranger"
	authorizationv1 "k8s.io/api/authorization/v1"
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/fields"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/util/sets"
	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/apimachinery/pkg/watch"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/cache"
	toolswatch "k8s.io/client-go/tools/watch"
	"k8s.io/kubernetes/pkg/cluster/ports"
)

var (
	endpointDebounceDelay = time.Second
	defaultDialer         = net.Dialer{}
)

type agentTunnel struct {
	client      kubernetes.Interface
	cidrs       cidranger.Ranger
	ports       map[string]bool
	mode        string
	kubeletAddr string
	kubeletPort string
	startTime   time.Time
}

// explicit interface check
var _ cidranger.RangerEntry = &podEntry{}

type podEntry struct {
	cidr    net.IPNet
	hostNet bool
}

func (p *podEntry) Network() net.IPNet {
	return p.cidr
}

func Setup(ctx context.Context, config *daemonconfig.Node, proxy proxy.Proxy) error {
	client, err := util.GetClientSet(config.AgentConfig.KubeConfigK3sController)
	if err != nil {
		return err

Review comment (bug):
1 second was observed to be too short on rke2.
```
time="2025-03-11T18:55:12Z" level=debug msg="Syncing apiserver addresses from tunnel watch: [172.17.0.7:6443 172.17.0.8:6443]"

time="2025-03-11T18:55:15Z" level=debug msg="Settled apiserver addresses sync: [172.17.0.7:6443 172.17.0.8:6443]"
time="2025-03-11T18:55:15Z" level=debug msg="Sync apiserver addresses - connecting: [172.17.0.8:9345], disconnecting: []"

time="2025-03-11T18:55:22Z" level=debug msg="Syncing apiserver addresses from tunnel watch: [172.17.0.7:6443]"

time="2025-03-11T18:55:23Z" level=debug msg="Syncing apiserver addresses from tunnel watch: [172.17.0.7:6443 172.17.0.8:6443]"
time="2025-03-11T18:55:23Z" level=debug msg="Cancelled apiserver addresses sync: [172.17.0.7:6443]"

time="2025-03-11T18:55:26Z" level=debug msg="Settled apiserver addresses sync: [172.17.0.7:6443 172.17.0.8:6443]"

time="2025-03-11T18:55:42Z" level=debug msg="Syncing apiserver addresses from tunnel watch: [172.17.0.7:6443]"

time="2025-03-11T18:55:43Z" level=debug msg="Syncing apiserver addresses from tunnel watch: [172.17.0.7:6443 172.17.0.8:6443]"
time="2025-03-11T18:55:43Z" level=debug msg="Cancelled apiserver addresses sync: [172.17.0.7:6443]"

time="2025-03-11T18:55:46Z" level=debug msg="Settled apiserver addresses sync: [172.17.0.7:6443 172.17.0.8:6443]"
```

Note that the new server is removed, and then re-added about 1 second later. We need to wait longer than 1 second to avoid removing it; observationally 3 appears to be sufficient while also not causing excessive delay.

Code after:
	"github.com/k3s-io/k3s/pkg/util"
	"github.com/k3s-io/k3s/pkg/version"
	"github.com/rancher/remotedialer"
	"github.com/sirupsen/logrus"
	"github.com/yl2chen/cidranger"
	authorizationv1 "k8s.io/api/authorization/v1"
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/fields"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/util/sets"
	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/apimachinery/pkg/watch"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/cache"
	toolswatch "k8s.io/client-go/tools/watch"
	"k8s.io/kubernetes/pkg/cluster/ports"
)

var (
	// endpointDebounceDelay sets how long we wait before updating apiserver
	// addresses when the kubernetes endpoint list changes. When the apiserver is
	// starting up it adds then removes then re-adds itself a few times in quick
	// succession, and we want to avoid closing connections unnecessarily.
	endpointDebounceDelay = 3 * time.Second
	defaultDialer         = net.Dialer{}
)

type agentTunnel struct {
	client      kubernetes.Interface
	cidrs       cidranger.Ranger
	ports       map[string]bool
	mode        string
	kubeletAddr string
	kubeletPort string
	startTime   time.Time
}

// explicit interface check
var _ cidranger.RangerEntry = &podEntry{}

type podEntry struct {
	cidr    net.IPNet
	hostNet bool
}

func (p *podEntry) Network() net.IPNet {
	return p.cidr
}