Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerza committed Jun 9, 2021
1 parent 8593138 commit 9110c1c
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 25 deletions.
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ repos:
rev: v2.0.0
hooks:
- id: codespell
- repo: https://github.com/syntaqx/git-hooks
rev: v0.0.17
hooks:
- id: forbid-binary
- id: go-fmt
- id: go-test
- id: go-mod-tidy
- id: shfmt
6 changes: 4 additions & 2 deletions clab/config/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"
)

// Split a string on commans and trim each
// Split a string on commas and trim each line
func SplitTrim(s string) []string {
res := strings.Split(s, ",")
for i, v := range res {
Expand All @@ -13,13 +13,15 @@ func SplitTrim(s string) []string {
return res
}

// the new agreed node config
// The new agreed node config
type NodeSettings struct {
Vars map[string]string
Transport string
Templates []string
}

// Temporary function to extract NodeSettings from the Labels
// In the next phase node settings will be added to the clab file
func GetNodeConfigFromLabels(labels map[string]string) NodeSettings {
nc := NodeSettings{
Vars: labels,
Expand Down
4 changes: 0 additions & 4 deletions clab/config/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ func RenderAll(nodes map[string]*types.Node, links map[int]*types.Link) (map[str
func (c *NodeConfig) String() string {

s := fmt.Sprintf("%s: %v", c.TargetNode.ShortName, c.Info)
// s := fmt.Sprintf("%s %s using %s/%s", c.TargetNode.ShortName, c.source, c.TargetNode.Kind, c.templateName)
// if c.Data != nil {
// s += fmt.Sprintf(" (%d lines)", bytes.Count(c.Data, []byte("\n"))+1)
// }
return s
}

Expand Down
14 changes: 7 additions & 7 deletions clab/config/transport/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ type SSHSession struct {

type SSHTransportOption func(*SSHTransport) error

// The reply the execute command and the prompt.
// The SSH reply, executed command and the prompt
type SSHReply struct{ result, prompt, command string }

// SSHTransport setting needs to be set before calling Connect()
// SSHTransport implements the Transport interface
type SSHTransport struct {
// Channel used to read. Can use Expect to Write & read wit timeout
// Channel used to read. Can use Expect to Write & read with timeout
in chan SSHReply
// SSH Session
ses *SSHSession
// Contains the first read after connecting
LoginMessage *SSHReply
// SSH parameters used in connect
// defualt: 22
// default: 22
Port int

// Keep the target for logging
Expand Down Expand Up @@ -64,8 +64,8 @@ func WithUserNamePassword(username, password string) SSHTransportOption {
}
}

// Add a basic username & password to a config.
// Will initilize the config if required
// Add a basic username & password to a config
// Will initialize the config if required
func HostKeyCallback(callback ...ssh.HostKeyCallback) SSHTransportOption {
return func(tx *SSHTransport) error {
tx.SSHConfig.HostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error {
Expand Down Expand Up @@ -106,7 +106,7 @@ func NewSSHTransport(node *types.Node, options ...SSHTransportOption) (*SSHTrans
}
return c, nil
}
return nil, fmt.Errorf("no tranport implemented for kind: %s", node.Kind)
return nil, fmt.Errorf("no transport implemented for kind: %s", node.Kind)
}

// Creates the channel reading the SSH connection
Expand Down Expand Up @@ -374,7 +374,7 @@ func (ses *SSHSession) Close() {
// The LogString will include the entire SSHReply
// Each field will be prefixed by a character.
// # - command sent
// | - result recieved
// | - result received
// ? - prompt part of the result
func (r *SSHReply) LogString(node string, linefeed, debug bool) string {
ind := 12 + len(node)
Expand Down
11 changes: 6 additions & 5 deletions clab/config/transport/sshkind.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ import (
log "github.com/sirupsen/logrus"
)

// an interface to implement kind specific methods for transactions and prompt checking
// An interface to implement kind specific methods for transactions and prompt checking
type SSHKind interface {
// Start a config transaction
ConfigStart(s *SSHTransport, transaction bool) error
// Commit a config transaction
ConfigCommit(s *SSHTransport) (*SSHReply, error)
// Prompt parsing function.
// Prompt parsing function
//
// This function receives string, split by the delimiter and should ensure this is a valid prompt
// Valid prompt, strip te prompt from the result and add it to the prompt in SSHReply
// Valid prompt, strip the prompt from the result and add it to the prompt in SSHReply
//
// A defualt implementation is promptParseNoSpaces, which simply ensures there are
// A default implementation is promptParseNoSpaces, which simply ensures there are
// no spaces between the start of the line and the #
PromptParse(s *SSHTransport, in *string) *SSHReply
}
Expand Down Expand Up @@ -88,7 +89,7 @@ func (sk *SrlSSHKind) PromptParse(s *SSHTransport, in *string) *SSHReply {
return promptParseNoSpaces(in, s.PromptChar, 2)
}

// This is a helper funciton to parse the prompt, and can be used by SSHKind's ParsePrompt
// This is a helper function to parse the prompt, and can be used by SSHKind's ParsePrompt
// Used in SRL today
func promptParseNoSpaces(in *string, promptChar string, lines int) *SSHReply {
n := strings.LastIndex(*in, "\n")
Expand Down
1 change: 1 addition & 0 deletions clab/config/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Transport interface {
Close()
}

// Write config to a node
func Write(tx Transport, host string, data, info []string, options ...TransportOption) error {
// the Kind should configure the transport parameters before

Expand Down
8 changes: 4 additions & 4 deletions clab/config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (

type Dict map[string]interface{}

// Prepare variables for all nodes. This will also prepare all variables for the links
func PrepareVars(nodes map[string]*types.Node, links map[int]*types.Link) map[string]Dict {

res := make(map[string]Dict)
Expand Down Expand Up @@ -51,6 +52,7 @@ func PrepareVars(nodes map[string]*types.Node, links map[int]*types.Link) map[st
return res
}

// Prepare variables for a specific link
func prepareLinkVars(lIdx int, link *types.Link, varsA, varsB map[string]interface{}) error {
ncA := GetNodeConfigFromLabels(link.A.Node.Labels)
ncB := GetNodeConfigFromLabels(link.B.Node.Labels)
Expand Down Expand Up @@ -110,9 +112,7 @@ func linkIPfromSystemIP(link *types.Link) (netaddr.IPPrefix, netaddr.IPPrefix, e
return ipA, ipA, fmt.Errorf("invalid ip %s", link.A.EndpointName)
}
} else {
// caluculate link IP from the system IPs - tbd
//var sysA, sysB netaddr.IPPrefix

// Calculate link IP from the system IPs
sysA, err := netaddr.ParseIPPrefix(link.A.Node.Labels[systemIP])
if err != nil {
return ipA, ipA, fmt.Errorf("no 'ip' on link & the '%s' of %s: %s", systemIP, link.A.Node.ShortName, err)
Expand Down Expand Up @@ -141,7 +141,7 @@ func ipLastOctet(in netaddr.IP) int {
}
res, err := strconv.Atoi(s[i+1:])
if err != nil {
log.Errorf("last octect %s from IP %s not a string", s[i+1:], s)
log.Errorf("last octet %s from IP %s not a string", s[i+1:], s)
}
return res
}
Expand Down
1 change: 0 additions & 1 deletion clab/config/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func TestIPLastOctect(t *testing.T) {
"10.0.0.1/32": 1,
"::1/32": 1,
}

for k, v := range lst {
n := netaddr.MustParseIPPrefix(k)
lo := ipLastOctet(n.IP())
Expand Down
4 changes: 2 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var configCmd = &cobra.Command{
return err
}

// config map per node. each node gets a config.NodeConfig
// Config map per node. Each node gets a config.NodeConfig
allConfig, err := config.RenderAll(c.Nodes, c.Links)
if err != nil {
return err
Expand Down Expand Up @@ -79,7 +79,7 @@ var configCmd = &cobra.Command{
log.Errorf("%s: %s", kind, err)
}
} else if ct == "grpc" {
// newGRPCTransport
// NewGRPCTransport
} else {
log.Errorf("Unknown transport: %s", ct)
return
Expand Down

0 comments on commit 9110c1c

Please sign in to comment.