github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/types/node.go (about)

     1  package types
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  
     7  	"github.com/mitchellh/mapstructure"
     8  	engine "github.com/projecteru2/core/engine"
     9  	resourcetypes "github.com/projecteru2/core/resource/types"
    10  )
    11  
    12  // NodeMeta .
    13  type NodeMeta struct {
    14  	Name     string            `json:"name" mapstructure:"name"`
    15  	Endpoint string            `json:"endpoint" mapstructure:"endpoint"`
    16  	Podname  string            `json:"podname" mapstructure:"podname"`
    17  	Labels   map[string]string `json:"labels" mapstructure:"labels"`
    18  
    19  	Ca   string `json:"-" mapstructure:"-"`
    20  	Cert string `json:"-" mapstructure:"-"`
    21  	Key  string `json:"-" mapstructure:"-"`
    22  }
    23  
    24  // DeepCopy returns a deepcopy of nodemeta
    25  func (n NodeMeta) DeepCopy() (nn NodeMeta, err error) {
    26  	return nn, mapstructure.Decode(n, &nn)
    27  }
    28  
    29  // NodeResourceInfo for node resource info
    30  type NodeResourceInfo struct {
    31  	Name      string                  `json:"-"`
    32  	Capacity  resourcetypes.Resources `json:"capacity,omitempty"`
    33  	Usage     resourcetypes.Resources `json:"usage,omitempty"`
    34  	Diffs     []string                `json:"diffs,omitempty"`
    35  	Workloads []*Workload             `json:"-"`
    36  }
    37  
    38  // Node store node info
    39  type Node struct {
    40  	NodeMeta
    41  	// Bypass if bypass is true, it will not participate in future scheduling
    42  	Bypass bool `json:"bypass,omitempty"`
    43  	// Test mean can ignore node health check
    44  	Test bool `json:"test,omitempty"`
    45  
    46  	ResourceInfo NodeResourceInfo `json:"-"`
    47  	NodeInfo     string           `json:"-"`
    48  	Available    bool             `json:"-"`
    49  	Engine       engine.API       `json:"-"`
    50  }
    51  
    52  // Info show node info
    53  func (n *Node) Info(ctx context.Context) (err error) {
    54  	info, err := n.Engine.Info(ctx)
    55  	if err != nil {
    56  		n.Available = false
    57  		n.NodeInfo = err.Error()
    58  		return err
    59  	}
    60  	bs, err := json.Marshal(info)
    61  	if err != nil {
    62  		n.NodeInfo = err.Error()
    63  		return err
    64  	}
    65  	n.NodeInfo = string(bs)
    66  	return nil
    67  }
    68  
    69  // IsDown returns if the node is marked as down.
    70  func (n *Node) IsDown() bool {
    71  	// If `bypass` is true, then even if the node is still healthy, the node will be regarded as `down`.
    72  	// Currently `bypass` will only be set when the cli calls the `up` and `down` commands.
    73  	return n.Bypass || !n.Available
    74  }
    75  
    76  // NodeStatus wraps node status
    77  // only used for node status stream
    78  type NodeStatus struct {
    79  	Nodename string
    80  	Podname  string
    81  	Alive    bool
    82  	Error    error
    83  }
    84  
    85  // NodeFilter is used to filter nodes in a pod
    86  // names in includes will be used
    87  // names in excludes will not be used
    88  type NodeFilter struct {
    89  	Podname  string
    90  	Includes []string
    91  	Excludes []string
    92  	Labels   map[string]string
    93  	All      bool
    94  }