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

[v9] tsh list resources accross proxies and clusters (#12934) #13313

Merged
merged 9 commits into from
Jun 16, 2022
76 changes: 69 additions & 7 deletions integration/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ func (i *TeleInstance) GenerateConfig(t *testing.T, trustedSecrets []*InstanceSe
types.RoleTrustedCluster,
types.RoleApp,
types.RoleDatabase,
types.RoleKube,
},
Token: "token",
},
Expand Down Expand Up @@ -954,6 +955,55 @@ func (i *TeleInstance) StartDatabase(conf *service.Config) (*service.TeleportPro
return process, client, nil
}

func (i *TeleInstance) StartKube(conf *service.Config, clusterName string) (*service.TeleportProcess, error) {
dataDir, err := os.MkdirTemp("", "cluster-"+i.Secrets.SiteName)
if err != nil {
return nil, trace.Wrap(err)
}
i.tempDirs = append(i.tempDirs, dataDir)

conf.DataDir = dataDir
conf.AuthServers = []utils.NetAddr{
{
AddrNetwork: "tcp",
Addr: net.JoinHostPort(Loopback, i.GetPortWeb()),
},
}
conf.Token = "token"
conf.UploadEventsC = i.UploadEventsC
conf.Auth.Enabled = false
conf.Proxy.Enabled = false
conf.Apps.Enabled = false
conf.SSH.Enabled = false
conf.Databases.Enabled = false

conf.Kube.KubeconfigPath = filepath.Join(dataDir, "kube_config")
if err := enableKube(conf, clusterName); err != nil {
return nil, trace.Wrap(err)
}
conf.Kube.ListenAddr = nil

process, err := service.NewTeleport(conf)
if err != nil {
return nil, trace.Wrap(err)
}
i.Nodes = append(i.Nodes, process)

expectedEvents := []string{
service.KubeIdentityEvent,
service.KubernetesReady,
service.TeleportReadyEvent,
}

receivedEvents, err := startAndWait(process, expectedEvents)
if err != nil {
return nil, trace.Wrap(err)
}
log.Debugf("Teleport Kube Server (in instance %v) started: %v/%v events received.",
i.Secrets.SiteName, len(expectedEvents), len(receivedEvents))
return process, nil
}

// StartNodeAndProxy starts a SSH node and a Proxy Server and connects it to
// the cluster.
func (i *TeleInstance) StartNodeAndProxy(name string, sshPort, proxyWebPort, proxySSHPort int) error {
Expand Down Expand Up @@ -1195,6 +1245,9 @@ func (i *TeleInstance) Start() error {
if i.Config.Databases.Enabled {
expectedEvents = append(expectedEvents, service.DatabasesReady)
}
if i.Config.Kube.Enabled {
expectedEvents = append(expectedEvents, service.KubernetesReady)
}

// Start the process and block until the expected events have arrived.
receivedEvents, err := startAndWait(i.Process, expectedEvents)
Expand Down Expand Up @@ -1755,21 +1808,30 @@ func fatalIf(err error) {
}

func enableKubernetesService(t *testing.T, config *service.Config) {
kubeConfigPath := filepath.Join(t.TempDir(), "kube_config")
config.Kube.KubeconfigPath = filepath.Join(t.TempDir(), "kube_config")
require.NoError(t, enableKube(config, "teleport-cluster"))
}

func enableKube(config *service.Config, clusterName string) error {
kubeConfigPath := config.Kube.KubeconfigPath
if kubeConfigPath == "" {
return trace.BadParameter("missing kubeconfig path")
}
key, err := genUserKey()
require.NoError(t, err)

if err != nil {
return trace.Wrap(err)
}
err = kubeconfig.Update(kubeConfigPath, kubeconfig.Values{
TeleportClusterName: "teleport-cluster",
TeleportClusterName: clusterName,
ClusterAddr: "https://" + net.JoinHostPort(Host, ports.Pop()),
Credentials: key,
})
require.NoError(t, err)

if err != nil {
return trace.Wrap(err)
}
config.Kube.Enabled = true
config.Kube.KubeconfigPath = kubeConfigPath
config.Kube.ListenAddr = utils.MustParseAddr(net.JoinHostPort(Host, ports.Pop()))
return nil
}

// getKubeClusters gets all kubernetes clusters accessible from a given auth server.
Expand Down
Loading