github.com/IBM-Cloud/bluemix-go@v0.0.0-20241117121028-a3be206688b3/api/container/containerv2/workers.go (about)

     1  package containerv2
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/IBM-Cloud/bluemix-go/client"
     7  )
     8  
     9  // Worker ...
    10  type Worker struct {
    11  	Billing           string `json:"billing,omitempty"`
    12  	HostID            string `json:"dedicatedHostId,omitempty"`
    13  	HostPoolID        string `json:"dedicatedHostPoolId,omitempty"`
    14  	Flavor            string `json:"flavor"`
    15  	ID                string `json:"id"`
    16  	KubeVersion       KubeDetails
    17  	Location          string          `json:"location"`
    18  	PoolID            string          `json:"poolID"`
    19  	PoolName          string          `json:"poolName"`
    20  	LifeCycle         WorkerLifeCycle `json:"lifecycle"`
    21  	Health            HealthStatus    `json:"health"`
    22  	NetworkInterfaces []Network       `json:"networkInterfaces"`
    23  }
    24  
    25  type KubeDetails struct {
    26  	Actual    string `json:"actual"`
    27  	Desired   string `json:"desired"`
    28  	Eos       string `json:"eos"`
    29  	MasterEOS string `json:"masterEos"`
    30  	Target    string `json:"target"`
    31  }
    32  type HealthStatus struct {
    33  	Message string `json:"message"`
    34  	State   string `json:"state"`
    35  }
    36  type WorkerLifeCycle struct {
    37  	ReasonForDelete       string `json:"reasonForDelete"`
    38  	ActualState           string `json:"actualState"`
    39  	DesiredState          string `json:"desiredState"`
    40  	Message               string `json:"message"`
    41  	MessageDate           string `json:"messageDate"`
    42  	MessageDetails        string `json:"messageDetails"`
    43  	MessageDetailsDate    string `json:"messageDetailsDate"`
    44  	PendingOperation      string `json:"pendingOperation"`
    45  	ActualOperatingSystem string `json:"actualOperatingSystem"`
    46  }
    47  
    48  type Network struct {
    49  	Cidr      string `json:"cidr"`
    50  	IpAddress string `json:"ipAddress"`
    51  	Primary   bool   `json:"primary"`
    52  	SubnetID  string `json:"subnetID"`
    53  }
    54  
    55  type ReplaceWorker struct {
    56  	ClusterIDOrName string `json:"cluster"`
    57  	Update          bool   `json:"update"`
    58  	WorkerID        string `json:"workerID"`
    59  }
    60  
    61  type VoulemeAttachments struct {
    62  	VolumeAttachments []VoulemeAttachment `json:"volume_attachments"`
    63  }
    64  
    65  type VoulemeAttachment struct {
    66  	Id     string     `json:"id"`
    67  	Volume Volume     `json:"volume"`
    68  	Device DeviceInfo `json:"device"`
    69  	Name   string     `json:"name"`
    70  	Status string     `json:"status"`
    71  	Type   string     `json:"type"`
    72  }
    73  
    74  type Volume struct {
    75  	Name string `json:"name"`
    76  	Id   string `json:"id"`
    77  }
    78  
    79  type DeviceInfo struct {
    80  	Id string `json:"id"`
    81  }
    82  
    83  type VolumeRequest struct {
    84  	Cluster            string `json:"cluster"`
    85  	VolumeAttachmentID string `json:"volumeAttachmentID,omitempty"`
    86  	VolumeID           string `json:"volumeID,omitempty"`
    87  	Worker             string `json:"worker"`
    88  }
    89  
    90  // Workers ...
    91  type Workers interface {
    92  	ListByWorkerPool(clusterIDOrName, workerPoolIDOrName string, showDeleted bool, target ClusterTargetHeader) ([]Worker, error)
    93  	ListWorkers(clusterIDOrName string, showDeleted bool, target ClusterTargetHeader) ([]Worker, error)
    94  	ListAllWorkers(clusterIDOrName string, showDeleted bool, target ClusterTargetHeader) ([]Worker, error)
    95  	Get(clusterIDOrName, workerID string, target ClusterTargetHeader) (Worker, error)
    96  	ReplaceWokerNode(clusterIDOrName, workerID string, target ClusterTargetHeader) (string, error)
    97  	ListStorageAttachemnts(clusterIDOrName, workerID string, target ClusterTargetHeader) (VoulemeAttachments, error)
    98  	GetStorageAttachment(clusterIDOrName, workerID, volumeAttachmentID string, target ClusterTargetHeader) (VoulemeAttachment, error)
    99  	CreateStorageAttachment(payload VolumeRequest, target ClusterTargetHeader) (VoulemeAttachment, error)
   100  	DeleteStorageAttachment(payload VolumeRequest, target ClusterTargetHeader) (string, error)
   101  }
   102  
   103  type worker struct {
   104  	client *client.Client
   105  }
   106  
   107  func newWorkerAPI(c *client.Client) Workers {
   108  	return &worker{
   109  		client: c,
   110  	}
   111  }
   112  
   113  // ListByWorkerPool ...
   114  func (r *worker) ListByWorkerPool(clusterIDOrName, workerPoolIDOrName string, showDeleted bool, target ClusterTargetHeader) ([]Worker, error) {
   115  	rawURL := fmt.Sprintf("/v2/vpc/getWorkers?cluster=%s&showDeleted=%t", clusterIDOrName, showDeleted)
   116  	if len(workerPoolIDOrName) > 0 {
   117  		rawURL += "&pool=" + workerPoolIDOrName
   118  	}
   119  	workers := []Worker{}
   120  	_, err := r.client.Get(rawURL, &workers, target.ToMap())
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  	return workers, err
   125  }
   126  
   127  // ListWorkers lists VPC workers
   128  func (r *worker) ListWorkers(clusterIDOrName string, showDeleted bool, target ClusterTargetHeader) ([]Worker, error) {
   129  	rawURL := fmt.Sprintf("/v2/vpc/getWorkers?cluster=%s&showDeleted=%t", clusterIDOrName, showDeleted)
   130  	workers := []Worker{}
   131  	_, err := r.client.Get(rawURL, &workers, target.ToMap())
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  	return workers, err
   136  }
   137  
   138  // ListAllWorkers lists workers of every type
   139  func (r *worker) ListAllWorkers(clusterIDOrName string, showDeleted bool, target ClusterTargetHeader) ([]Worker, error) {
   140  	rawURL := fmt.Sprintf("/v2/getWorkers?cluster=%s&showDeleted=%t", clusterIDOrName, showDeleted)
   141  	workers := []Worker{}
   142  	_, err := r.client.Get(rawURL, &workers, target.ToMap())
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  	return workers, err
   147  }
   148  
   149  // Get ...
   150  func (r *worker) Get(clusterIDOrName, workerID string, target ClusterTargetHeader) (Worker, error) {
   151  	rawURL := fmt.Sprintf("/v2/vpc/getWorker?cluster=%s&worker=%s", clusterIDOrName, workerID)
   152  	worker := Worker{}
   153  	_, err := r.client.Get(rawURL, &worker, target.ToMap())
   154  	if err != nil {
   155  		return worker, err
   156  	}
   157  	return worker, err
   158  }
   159  
   160  func (r *worker) ReplaceWokerNode(clusterIDOrName, workerID string, target ClusterTargetHeader) (string, error) {
   161  	payload := ReplaceWorker{
   162  		ClusterIDOrName: clusterIDOrName,
   163  		WorkerID:        workerID,
   164  		Update:          true,
   165  	}
   166  	var response string
   167  	_, err := r.client.Post("/v2/vpc/replaceWorker", payload, &response, target.ToMap())
   168  	if err != nil {
   169  		return response, err
   170  	}
   171  	return response, err
   172  }
   173  
   174  // ListStorageAttachemnts returns list of attached storage blaocks to a worker node
   175  func (r *worker) ListStorageAttachemnts(clusterIDOrName, workerID string, target ClusterTargetHeader) (VoulemeAttachments, error) {
   176  	rawURL := fmt.Sprintf("/v2/storage/getAttachments?cluster=%s&worker=%s", clusterIDOrName, workerID)
   177  	workerAttachements := VoulemeAttachments{}
   178  	_, err := r.client.Get(rawURL, &workerAttachements, target.ToMap())
   179  	if err != nil {
   180  		return workerAttachements, err
   181  	}
   182  	return workerAttachements, err
   183  
   184  }
   185  
   186  func (r *worker) GetStorageAttachment(clusterIDOrName, workerID, volumeAttachmentID string, target ClusterTargetHeader) (VoulemeAttachment, error) {
   187  	rawURL := fmt.Sprintf("/v2/storage/getAttachment?cluster=%s&worker=%s&volumeAttachmentID=%s", clusterIDOrName, workerID, volumeAttachmentID)
   188  	workerAttachement := VoulemeAttachment{}
   189  	_, err := r.client.Get(rawURL, &workerAttachement, target.ToMap())
   190  	if err != nil {
   191  		return workerAttachement, err
   192  	}
   193  	return workerAttachement, err
   194  
   195  }
   196  
   197  func (r *worker) CreateStorageAttachment(payload VolumeRequest, target ClusterTargetHeader) (VoulemeAttachment, error) {
   198  	response := VoulemeAttachment{}
   199  	_, err := r.client.Post("/v2/storage/createAttachment", payload, &response, target.ToMap())
   200  	if err != nil {
   201  		return response, err
   202  	}
   203  	return response, err
   204  }
   205  
   206  func (r *worker) DeleteStorageAttachment(payload VolumeRequest, target ClusterTargetHeader) (string, error) {
   207  	var response string
   208  	_, err := r.client.Post("/v2/storage/deleteAttachment", payload, &response, target.ToMap())
   209  	if err != nil {
   210  		return response, err
   211  	}
   212  	return response, err
   213  }