github.com/jlmeeker/kismatic@v1.10.1-0.20180612190640-57f9005a1f1a/pkg/inspector/rule/tcp_port.go (about)

     1  package rule
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"time"
     7  )
     8  
     9  // TCPPortAvailable is a rule that ensures that a given port is available
    10  // on the node. The port is considered available if:
    11  // - The port is free and ready to be bound by a new process, or
    12  // - The port is bound to the process defined in ProcName
    13  type TCPPortAvailable struct {
    14  	Meta
    15  	// The port number to verify
    16  	Port int
    17  	// The name of the process that owns this port after KET installation
    18  	ProcName string
    19  }
    20  
    21  // Name is the name of the rule
    22  func (p TCPPortAvailable) Name() string {
    23  	return fmt.Sprintf("Port Available: %d", p.Port)
    24  }
    25  
    26  // IsRemoteRule returns true if the rule is to be run from outside the node
    27  func (p TCPPortAvailable) IsRemoteRule() bool { return false }
    28  
    29  // Validate the rule
    30  func (p TCPPortAvailable) Validate() []error {
    31  	var errs []error
    32  	if p.Port < 1 || p.Port > 65535 {
    33  		errs = append(errs, fmt.Errorf("Invalid port number %d specified", p.Port))
    34  	}
    35  	if p.ProcName == "" {
    36  		errs = append(errs, fmt.Errorf("ProcName cannot be empty"))
    37  	}
    38  	return errs
    39  }
    40  
    41  // TCPPortAccessible is a rule that ensures the given port on a remote node
    42  // is accessible from the network
    43  type TCPPortAccessible struct {
    44  	Meta
    45  	Port    int
    46  	Timeout string
    47  }
    48  
    49  // Name returns the name of the rule
    50  func (p TCPPortAccessible) Name() string {
    51  	return fmt.Sprintf("Port Accessible: %d", p.Port)
    52  }
    53  
    54  // IsRemoteRule returns true if the rule is to be run from a remote node
    55  func (p TCPPortAccessible) IsRemoteRule() bool { return true }
    56  
    57  // Validate the rule
    58  func (p TCPPortAccessible) Validate() []error {
    59  	errs := []error{}
    60  	if p.Port < 1 || p.Port > 65535 {
    61  		errs = append(errs, fmt.Errorf("Invalid port number %d specified", p.Port))
    62  	}
    63  	if p.Timeout == "" {
    64  		errs = append(errs, errors.New("Timeout cannot be empty"))
    65  	}
    66  	if p.Timeout != "" {
    67  		if _, err := time.ParseDuration(p.Timeout); err != nil {
    68  			errs = append(errs, fmt.Errorf("Invalid duration provided %q", p.Timeout))
    69  		}
    70  	}
    71  	if len(errs) > 0 {
    72  		return errs
    73  	}
    74  	return nil
    75  }