github.com/hashicorp/nomad/api@v0.0.0-20240306165712-3193ac204f65/node_pools.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package api
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"net/url"
    10  )
    11  
    12  const (
    13  	// NodePoolAll is the node pool that always includes all nodes.
    14  	NodePoolAll = "all"
    15  
    16  	// NodePoolDefault is the default node pool.
    17  	NodePoolDefault = "default"
    18  )
    19  
    20  // NodePools is used to access node pools endpoints.
    21  type NodePools struct {
    22  	client *Client
    23  }
    24  
    25  // NodePools returns a handle on the node pools endpoints.
    26  func (c *Client) NodePools() *NodePools {
    27  	return &NodePools{client: c}
    28  }
    29  
    30  // List is used to list all node pools.
    31  func (n *NodePools) List(q *QueryOptions) ([]*NodePool, *QueryMeta, error) {
    32  	var resp []*NodePool
    33  	qm, err := n.client.query("/v1/node/pools", &resp, q)
    34  	if err != nil {
    35  		return nil, nil, err
    36  	}
    37  	return resp, qm, nil
    38  }
    39  
    40  // PrefixList is used to list node pools that match a given prefix.
    41  func (n *NodePools) PrefixList(prefix string, q *QueryOptions) ([]*NodePool, *QueryMeta, error) {
    42  	if q == nil {
    43  		q = &QueryOptions{}
    44  	}
    45  	q.Prefix = prefix
    46  	return n.List(q)
    47  }
    48  
    49  // Info is used to fetch details of a specific node pool.
    50  func (n *NodePools) Info(name string, q *QueryOptions) (*NodePool, *QueryMeta, error) {
    51  	if name == "" {
    52  		return nil, nil, errors.New("missing node pool name")
    53  	}
    54  
    55  	var resp NodePool
    56  	qm, err := n.client.query("/v1/node/pool/"+url.PathEscape(name), &resp, q)
    57  	if err != nil {
    58  		return nil, nil, err
    59  	}
    60  	return &resp, qm, nil
    61  }
    62  
    63  // Register is used to create or update a node pool.
    64  func (n *NodePools) Register(pool *NodePool, w *WriteOptions) (*WriteMeta, error) {
    65  	if pool == nil {
    66  		return nil, errors.New("missing node pool")
    67  	}
    68  	if pool.Name == "" {
    69  		return nil, errors.New("missing node pool name")
    70  	}
    71  
    72  	wm, err := n.client.put("/v1/node/pools", pool, nil, w)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	return wm, nil
    77  }
    78  
    79  // Delete is used to delete a node pool.
    80  func (n *NodePools) Delete(name string, w *WriteOptions) (*WriteMeta, error) {
    81  	if name == "" {
    82  		return nil, errors.New("missing node pool name")
    83  	}
    84  
    85  	wm, err := n.client.delete("/v1/node/pool/"+url.PathEscape(name), nil, nil, w)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	return wm, nil
    90  }
    91  
    92  // ListJobs is used to list all the jobs in a node pool.
    93  func (n *NodePools) ListJobs(poolName string, q *QueryOptions) ([]*JobListStub, *QueryMeta, error) {
    94  	var resp []*JobListStub
    95  	qm, err := n.client.query(
    96  		fmt.Sprintf("/v1/node/pool/%s/jobs", url.PathEscape(poolName)),
    97  		&resp, q)
    98  	if err != nil {
    99  		return nil, nil, err
   100  	}
   101  	return resp, qm, nil
   102  }
   103  
   104  // ListNodes is used to list all the nodes in a node pool.
   105  func (n *NodePools) ListNodes(poolName string, q *QueryOptions) ([]*NodeListStub, *QueryMeta, error) {
   106  	var resp []*NodeListStub
   107  	qm, err := n.client.query(
   108  		fmt.Sprintf("/v1/node/pool/%s/nodes", url.PathEscape(poolName)),
   109  		&resp, q)
   110  	if err != nil {
   111  		return nil, nil, err
   112  	}
   113  	return resp, qm, nil
   114  }
   115  
   116  // NodePool is used to serialize a node pool.
   117  type NodePool struct {
   118  	Name                   string                          `hcl:"name,label"`
   119  	Description            string                          `hcl:"description,optional"`
   120  	Meta                   map[string]string               `hcl:"meta,block"`
   121  	SchedulerConfiguration *NodePoolSchedulerConfiguration `hcl:"scheduler_config,block"`
   122  	CreateIndex            uint64
   123  	ModifyIndex            uint64
   124  }
   125  
   126  // NodePoolSchedulerConfiguration is used to serialize the scheduler
   127  // configuration of a node pool.
   128  type NodePoolSchedulerConfiguration struct {
   129  	SchedulerAlgorithm            SchedulerAlgorithm `hcl:"scheduler_algorithm,optional"`
   130  	MemoryOversubscriptionEnabled *bool              `hcl:"memory_oversubscription_enabled,optional"`
   131  }