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

ssh - completion with machine objects #347

Merged
merged 16 commits into from
Jan 10, 2024
Prev Previous commit
Next Next commit
fallback to node objects in case of errors
  • Loading branch information
petersutter committed Jan 9, 2024
commit 6cf5a94485dc3013737eae1defd9565cf72585d9
82 changes: 45 additions & 37 deletions pkg/cmd/ssh/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,13 +821,15 @@ func cleanup(ctx context.Context, o *SSHOptions, gardenClient client.Client, bas
}
}

func getNodeNamesFromShoot(f util.Factory, prefix string) ([]string, error) {
func getNodeNamesFromMachinesOrNodes(f util.Factory) ([]string, error) {
petersutter marked this conversation as resolved.
Show resolved Hide resolved
ctx := f.Context()
logger := klog.FromContext(ctx)

manager, err := f.Manager()
if err != nil {
return nil, err
}

// validate the current target
currentTarget, err := manager.CurrentTarget()
if err != nil {
return nil, err
Expand All @@ -837,62 +839,68 @@ func getNodeNamesFromShoot(f util.Factory, prefix string) ([]string, error) {
return nil, errors.New("no Shoot cluster targeted")
}

// create client for the gardener cluster
client, err := manager.GardenClient(currentTarget.GardenName())
nodeNames, err := getNodeNamesFromMachines(ctx, manager, currentTarget)
if err != nil {
return nil, fmt.Errorf("failed to create garden cluster client: %w", err)
if !apierrors.IsForbidden(err) {
logger.Info("failed to fetch node names from machine objects", "err", err)
}

return getNodeNamesFromNodes(ctx, manager, currentTarget)
}

// collect names, filter by prefix
var nodeNames []string
return nodeNames, nil
}

func getNodeNamesFromMachines(ctx context.Context, manager target.Manager, currentTarget target.Target) ([]string, error) {
gardenClient, err := manager.GardenClient(currentTarget.GardenName())
if err != nil {
return nil, fmt.Errorf("failed to create garden cluster client: %w", err)
}

shoot, err := client.FindShoot(f.Context(), currentTarget.AsListOption())
shoot, err := gardenClient.FindShoot(ctx, currentTarget.AsListOption())
if err != nil {
return nil, err
}

newTarget := target.NewTarget(currentTarget.GardenName(), "", *shoot.Spec.SeedName, "")
// create client for the seed cluster
seedClient, err := manager.SeedClient(f.Context(), newTarget)
seedTarget := target.NewTarget(currentTarget.GardenName(), "", *shoot.Spec.SeedName, "")

seedClient, err := manager.SeedClient(ctx, seedTarget)
if err != nil {
if !apierrors.IsForbidden(err) {
return nil, err
}
return nil, err
}

shootClient, err := manager.ShootClient(f.Context(), currentTarget)
if err != nil {
return nil, err
}
machines, err := getMachines(ctx, seedClient, shoot.Status.TechnicalID)
if err != nil {
return nil, err
}

// fetch all nodes
nodes, err := getNodes(f.Context(), shootClient)
if err != nil {
return nil, err
}
var nodeNames []string

for _, node := range nodes {
if strings.HasPrefix(node.Name, prefix) {
nodeNames = append(nodeNames, node.Name)
}
for _, machine := range machines {
if _, ok := machine.Labels[machinev1alpha1.NodeLabelKey]; !ok {
continue
}

return nodeNames, nil
nodeNames = append(nodeNames, machine.Labels[machinev1alpha1.NodeLabelKey])
}

// fetch all machines
machines, err := getMachines(f.Context(), seedClient, shoot.Status.TechnicalID)
return nodeNames, nil
}

func getNodeNamesFromNodes(ctx context.Context, manager target.Manager, currentTarget target.Target) ([]string, error) {
shootClient, err := manager.ShootClient(ctx, currentTarget)
if err != nil {
return nil, err
}

for _, machine := range machines {
if _, ok := machine.Labels["node"]; !ok {
continue
}
nodes, err := getNodes(ctx, shootClient)
if err != nil {
return nil, err
}

if strings.HasPrefix(machine.Labels["node"], prefix) {
nodeNames = append(nodeNames, machine.Labels["node"])
}
var nodeNames []string
for _, node := range nodes {
nodeNames = append(nodeNames, node.Name)
}

return nodeNames, nil
Expand Down
13 changes: 11 additions & 2 deletions pkg/cmd/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ SPDX-License-Identifier: Apache-2.0
package ssh

import (
"strings"

"github.com/spf13/cobra"
"k8s.io/klog/v2"

Expand Down Expand Up @@ -51,13 +53,20 @@ gardenctl ssh --keep-bastion --bastion-name cli-xxxxxxxx --public-key-file /path
return nil, cobra.ShellCompDirectiveNoFileComp
}

nodeNames, err := getNodeNamesFromShoot(f, toComplete)
nodeNames, err := getNodeNamesFromMachinesOrNodes(f)
if err != nil {
logger.Error(err, "could not get node names from shoot")
return nil, cobra.ShellCompDirectiveNoFileComp
}

return nodeNames, cobra.ShellCompDirectiveNoFileComp
var completions []string
for _, nodeName := range nodeNames {
if strings.HasPrefix(nodeName, toComplete) {
completions = append(completions, nodeName)
}
}

return completions, cobra.ShellCompDirectiveNoFileComp
},
RunE: base.WrapRunE(o, f),
}
Expand Down
Loading