<commit_before>package kic

import (
	"fmt"
	"strings"
	"time"

	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	typed_core "k8s.io/client-go/kubernetes/typed/core/v1"
)

// Tunnel ...
type Tunnel struct {
	sshPort string
	sshKey  string
	v1Core  typed_core.CoreV1Interface
}

// NewTunnel ...
func NewTunnel(sshPort, sshKey string, v1Core typed_core.CoreV1Interface) *Tunnel {
	return &Tunnel{
		sshPort: sshPort,
		sshKey:  sshKey,
		v1Core:  v1Core,
	}
}

// Start ...
func (t *Tunnel) Start() error {
	sshTunnels := make(map[string]*sshTunnel)

	for {
		services, err := t.v1Core.Services("").List(metav1.ListOptions{})
		if err != nil {
			return err
		}

		for _, sshTunnel := range sshTunnels {
			sshTunnel.sigkill = true
		}

		for _, s := range services.Items {
			if s.Spec.Type == v1.ServiceTypeLoadBalancer {
				name := sshTunnelName(s.Name, s.Spec.ClusterIP, s.Spec.Ports)
				existingSSHTunnel, ok := sshTunnels[name]

				if ok {
					existingSSHTunnel.sigkill = false
					continue
				}

				newSSHTunnel := createSSHTunnel(t, name, s.Spec.ClusterIP, s.Spec.Ports)
				sshTunnels[newSSHTunnel.name] = newSSHTunnel
			}
		}

		for _, sshTunnel := range sshTunnels {
			if sshTunnel.sigkill {
				sshTunnel.stop()
				delete(sshTunnels, sshTunnel.name)
			}
		}

		// TODO: which time to use?
		time.Sleep(1 * time.Second)
	}
}

// sshTunnelName creaets a uniq name for the tunnel, using its name/clusterIP/ports.
// This allows a new process to be created if an existing service was changed,
// the new process will support the IP/Ports change ocurred.
func sshTunnelName(name, clusterIP string, ports []v1.ServicePort) string {
	n := []string{
		name,
		"-",
		clusterIP,
	}

	for _, port := range ports {
		n = append(n, fmt.Sprintf("-%d", port.Port))
	}

	return strings.Join(n, "")
}<commit_msg>Refactor sshTunnelName<commit_after>package kic

import (
	"fmt"
	"strings"
	"time"

	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	typed_core "k8s.io/client-go/kubernetes/typed/core/v1"
)

// Tunnel ...
type Tunnel struct {
	sshPort string
	sshKey  string
	v1Core  typed_core.CoreV1Interface
}

// NewTunnel ...
func NewTunnel(sshPort, sshKey string, v1Core typed_core.CoreV1Interface) *Tunnel {
	return &Tunnel{
		sshPort: sshPort,
		sshKey:  sshKey,
		v1Core:  v1Core,
	}
}

// Start ...
func (t *Tunnel) Start() error {
	sshTunnels := make(map[string]*sshTunnel)

	for {
		services, err := t.v1Core.Services("").List(metav1.ListOptions{})
		if err != nil {
			return err
		}

		for _, sshTunnel := range sshTunnels {
			sshTunnel.sigkill = true
		}

		for _, s := range services.Items {
			if s.Spec.Type == v1.ServiceTypeLoadBalancer {
				name := sshTunnelName(s)
				existingSSHTunnel, ok := sshTunnels[name]

				if ok {
					existingSSHTunnel.sigkill = false
					continue
				}

				newSSHTunnel := createSSHTunnel(t, name, s.Spec.ClusterIP, s.Spec.Ports)
				sshTunnels[newSSHTunnel.name] = newSSHTunnel
			}
		}

		for _, sshTunnel := range sshTunnels {
			if sshTunnel.sigkill {
				sshTunnel.stop()
				delete(sshTunnels, sshTunnel.name)
			}
		}

		// TODO: which time to use?
		time.Sleep(1 * time.Second)
	}
}

// sshTunnelName creaets a uniq name for the tunnel, using its name/clusterIP/ports.
// This allows a new process to be created if an existing service was changed,
// the new process will support the IP/Ports change ocurred.
func sshTunnelName(service v1.Service) string {
	n := []string{
		service.Name,
		"-",
		service.Spec.ClusterIP,
	}

	for _, port := range service.Spec.Ports {
		n = append(n, fmt.Sprintf("-%d", port.Port))
	}

	return strings.Join(n, "")
}
{'fraction_non_alphanumeric': 0.13005464480874318, 'fraction_numerical': 0.010382513661202186, 'mean_word_length': 3.252032520325203, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6519406', 'n_tokens_mistral': 1592, 'n_tokens_neox': 1393, 'n_words': 312}