Skip to content

Use Context in ssh client #1816

@guettli

Description

@guettli

/kind feature

Currently, the ssh client used by caph does not use the Context.

This means, when the ctx times out, the ssh client does not notice that.

Current code:

type Factory interface {
	NewClient(Input) Client
}

type Input struct {
	IP         string
	PrivateKey string
	Port       int
}

We could add Context to Input.

Using the ctx is not straight forward, because the ssh package was created before context was added to Go.

But it is not difficult either:

  1. create conn with net.Dialer.DialContext(ctx, ...)
  2. pass that conn into ssh.NewClientConn(...)
  3. in a goroutine: select { case <-ctx.Done(): conn.Close() }

Additional benefit: We could get the logger from the context.


Alternative:

Currently:

type Client interface {
	GetHostName() Output
	GetHardwareDetailsRAM() Output
	GetHardwareDetailsNics() Output
	...

We add the ctx to each function of the interface:

type Client interface {
	GetHostName(ctx context.Context) Output
...
	

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions