Skip to content

Commit

Permalink
per feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
tedteng committed Jan 15, 2024
1 parent 0a253f9 commit 326401e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 59 deletions.
29 changes: 29 additions & 0 deletions pkg/cmd/ssh/connect_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type ConnectInformation struct {

// User is the name of the Shoot cluster node ssh login username
User string

// Machines is a list of machines name
Machines []string
}

var _ fmt.Stringer = &ConnectInformation{}
Expand Down Expand Up @@ -87,6 +90,7 @@ func NewConnectInformation(
sshPrivateKeyFile PrivateKeyFile,
nodePrivateKeyFiles []PrivateKeyFile,
nodes []corev1.Node,
machines []string,
user string,
) (*ConnectInformation, error) {
var nodeList []Node
Expand Down Expand Up @@ -144,6 +148,7 @@ func NewConnectInformation(
NodeHostname: nodeHostname,
NodePrivateKeyFiles: nodePrivateKeyFiles,
Nodes: nodeList,
Machines: machines,
User: user,
}, nil
}
Expand Down Expand Up @@ -195,6 +200,30 @@ func (p *ConnectInformation) String() string {
return ""
}

if len(p.Machines) != 0 && len(p.Machines) != len(p.Nodes) {
type empty struct{}

nodeSets := make(map[string]empty, len(p.Nodes))

var missingNodes []string

for _, node := range p.Nodes {
nodeSets[node.Name] = empty{}
}

for _, machine := range p.Machines {
if _, ok := nodeSets[machine]; !ok {
missingNodes = append(missingNodes, machine)
}
}

for _, node := range missingNodes {
table.Rows = append(table.Rows, metav1.TableRow{
Cells: []interface{}{node, "unknown", "unknown"},
})
}
}

fmt.Fprintln(&buf, "")
}

Expand Down
67 changes: 8 additions & 59 deletions pkg/cmd/ssh/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,6 @@ type SSHOptions struct {
// ConfirmAccessRestriction, when set to true, implies the user understands the access restrictions for the targeted shoot.
// In this case, the access restriction banner is displayed without further confirmation.
ConfirmAccessRestriction bool

// Operator is determines whether the command should be have the operator role assigned.
Operator bool

client client.Client
}

// NewSSHOptions returns initialized SSHOptions.
Expand All @@ -252,7 +247,6 @@ func NewSSHOptions(ioStreams util.IOStreams) *SSHOptions {
NoKeepalive: false,
BastionPort: strconv.Itoa(SSHPort),
User: DefaultUsername,
Operator: false,
}
}

Expand Down Expand Up @@ -543,27 +537,6 @@ func (o *SSHOptions) Run(f util.Factory) error {
return err
}

if shoot.Status.TechnicalID == "" {
return fmt.Errorf("shoot technical ID is empty: %w", err)
}

// check operator role
if !o.Operator {
seedTarget := target.NewTarget(currentTarget.GardenName(), "", *shoot.Spec.SeedName, "")

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

return err
}

o.Operator = true
o.client = seedClient
}

// check access restrictions
ok, err := o.checkAccessRestrictions(manager.Configuration(), currentTarget.GardenName(), f.TargetFlags(), shoot)
if err != nil {
Expand Down Expand Up @@ -674,47 +647,22 @@ func (o *SSHOptions) Run(f util.Factory) error {

if !o.Interactive {
var nodes []corev1.Node

machines := make([]string, 0)

if nodeHostname == "" {
nodes, err = getNodes(ctx, shootClient)
if err != nil {
return fmt.Errorf("failed to list shoot cluster nodes: %w", err)
}
}

if o.Operator {
machines, err := getMachines(ctx, o.client, shoot.Status.TechnicalID)
machines, err = getNodeNamesFromMachines(ctx, manager, currentTarget)
if err != nil {
return fmt.Errorf("failed to list shoot cluster machines: %w", err)
}

if len(machines) != len(nodes) {
type empty struct{}

nodeSets := make(map[string]empty, len(nodes))

for _, node := range nodes {
nodeSets[node.Name] = empty{}
if !apierrors.IsForbidden(err) {
return nil
}

for _, machine := range machines {
if _, ok := machine.Labels[machinev1alpha1.NodeLabelKey]; !ok {
continue
}

if _, ok := nodeSets[machine.Labels[machinev1alpha1.NodeLabelKey]]; !ok {
nodes = append(nodes, corev1.Node{
ObjectMeta: metav1.ObjectMeta{Name: machine.Labels[machinev1alpha1.NodeLabelKey]},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionUnknown,
},
},
},
})
}
}
return fmt.Errorf("failed to get shoot cluster node names from machines: %w", err)
}
}

Expand All @@ -728,6 +676,7 @@ func (o *SSHOptions) Run(f util.Factory) error {
o.SSHPrivateKeyFile,
nodePrivateKeyFiles,
nodes,
machines,
o.User,
)
if err != nil {
Expand Down

0 comments on commit 326401e

Please sign in to comment.