-
Notifications
You must be signed in to change notification settings - Fork 473
/
tunnel.go
61 lines (49 loc) · 2.02 KB
/
tunnel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package botanist
import (
"context"
"fmt"
"time"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/utils/retry"
)
// SetupPortForwarder is an alias for kubernetes.SetupPortForwarder, exposed for testing
var SetupPortForwarder = kubernetes.SetupPortForwarder
// CheckTunnelConnection checks if the tunnel connection between the control plane and the shoot networks
// is established.
func CheckTunnelConnection(ctx context.Context, log logr.Logger, shootClient kubernetes.Interface, tunnelName string) (bool, error) {
podList := &corev1.PodList{}
if err := shootClient.Client().List(ctx, podList, client.InNamespace(metav1.NamespaceSystem), client.MatchingLabels{"app": tunnelName}); err != nil {
return retry.SevereError(err)
}
var tunnelPod *corev1.Pod
for _, p := range podList.Items {
pod := p
if pod.Status.Phase == corev1.PodRunning {
tunnelPod = &pod
break
}
}
if tunnelPod == nil {
log.Info("Waiting until a running pod exists in the Shoot cluster", "tunnelName", tunnelName)
return retry.MinorError(fmt.Errorf("no running %s pod found yet in the shoot cluster", tunnelName))
}
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
fw, err := SetupPortForwarder(timeoutCtx, shootClient.RESTConfig(), tunnelPod.Namespace, tunnelPod.Name, 0, 22)
if err != nil {
return retry.MinorError(fmt.Errorf("could not setup pod port forwarding: %w", err))
}
if err := kubernetes.CheckForwardPodPort(fw); err != nil {
log.Info("Waiting until the tunnel connection has been established")
return retry.MinorError(fmt.Errorf("could not forward to %s pod (timeout after 5 seconds): %v", tunnelName, err))
}
log.Info("Tunnel connection has been established")
return retry.Ok()
}