github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/providers/gce/machine.go (about)

     1  package gce
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"sort"
     7  
     8  	"google.golang.org/api/compute/v1"
     9  
    10  	"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
    11  	"github.com/caos/orbos/internal/operator/orbiter/kinds/loadbalancers"
    12  	"github.com/caos/orbos/internal/operator/orbiter/kinds/providers/core"
    13  	"github.com/caos/orbos/mntr"
    14  	"github.com/caos/orbos/pkg/tree"
    15  )
    16  
    17  var _ infra.Machine = (*instance)(nil)
    18  
    19  type machine interface {
    20  	Execute(stdin io.Reader, cmd string) ([]byte, error)
    21  	Shell() error
    22  	WriteFile(path string, data io.Reader, permissions uint16) error
    23  	ReadFile(path string, data io.Writer) error
    24  	Zone() string
    25  }
    26  
    27  type instance struct {
    28  	mntr.Monitor
    29  	ip      string
    30  	url     string
    31  	pool    string
    32  	zone    string
    33  	remove  func() error
    34  	context *context
    35  	start   bool
    36  	machine
    37  	rebootRequired       bool
    38  	requireReboot        func()
    39  	unrequireReboot      func()
    40  	replacementRequired  bool
    41  	requireReplacement   func()
    42  	unrequireReplacement func()
    43  	X_ID                 string `header:"id"`
    44  	X_internalIP         string `header:"internal ip"`
    45  	X_externalIP         string `header:"external ip"`
    46  	X_Pool               string `header:"pool"`
    47  }
    48  
    49  func newMachine(
    50  	context *context,
    51  	monitor mntr.Monitor,
    52  	id,
    53  	ip,
    54  	url,
    55  	pool string,
    56  	zone string,
    57  	remove func() error,
    58  	start bool,
    59  	machine machine,
    60  	rebootRequired bool,
    61  	requireReboot func(),
    62  	unrequireReboot func(),
    63  	replacementRequired bool,
    64  	requireReplacement func(),
    65  	unrequireReplacement func()) *instance {
    66  	return &instance{
    67  		Monitor:              monitor,
    68  		X_ID:                 id,
    69  		ip:                   ip,
    70  		url:                  url,
    71  		pool:                 pool,
    72  		zone:                 zone,
    73  		remove:               remove,
    74  		context:              context,
    75  		start:                start,
    76  		machine:              machine,
    77  		rebootRequired:       rebootRequired,
    78  		requireReboot:        requireReboot,
    79  		unrequireReboot:      unrequireReboot,
    80  		replacementRequired:  replacementRequired,
    81  		requireReplacement:   requireReplacement,
    82  		unrequireReplacement: unrequireReplacement,
    83  	}
    84  }
    85  
    86  func (c *instance) Zone() string {
    87  	return c.zone
    88  }
    89  
    90  func (c *instance) ID() string {
    91  	return c.X_ID
    92  }
    93  
    94  func (c *instance) IP() string {
    95  	return c.ip
    96  }
    97  
    98  func (c *instance) RebootRequired() (bool, func(), func()) {
    99  	return c.rebootRequired, c.requireReboot, c.unrequireReboot
   100  }
   101  
   102  func (c *instance) ReplacementRequired() (bool, func(), func()) {
   103  	return c.replacementRequired, c.requireReplacement, c.unrequireReplacement
   104  }
   105  
   106  func (c *instance) Destroy() (func() error, error) {
   107  	return c.remove, nil
   108  }
   109  
   110  type instances []*instance
   111  
   112  func (c instances) Len() int           { return len(c) }
   113  func (c instances) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
   114  func (c instances) Less(i, j int) bool { return c[i].ID() < c[j].ID() }
   115  
   116  func (i instances) strings(field func(i *instance) string) []string {
   117  	sort.Sort(i)
   118  	l := len(i)
   119  	ret := make([]string, l, l)
   120  	for idx, i := range i {
   121  		ret[idx] = field(i)
   122  	}
   123  	return ret
   124  }
   125  
   126  func (i instances) refs() []*compute.InstanceReference {
   127  	sort.Sort(i)
   128  	l := len(i)
   129  	ret := make([]*compute.InstanceReference, l, l)
   130  	for idx, i := range i {
   131  		ret[idx] = &compute.InstanceReference{Instance: i.url}
   132  	}
   133  	return ret
   134  }
   135  
   136  func ListMachines(monitor mntr.Monitor, desiredTree *tree.Tree, orbID, providerID string) (map[string]infra.Machine, error) {
   137  	desired, err := parseDesiredV0(desiredTree)
   138  	if err != nil {
   139  		return nil, fmt.Errorf("parsing desired state failed: %w", err)
   140  	}
   141  	desiredTree.Parsed = desired
   142  
   143  	_, _, _, _, _, err = loadbalancers.GetQueryAndDestroyFunc(monitor, nil, desired.Loadbalancing, &tree.Tree{}, nil)
   144  	if err != nil {
   145  		return nil, err
   146  	}
   147  
   148  	svc, err := service(monitor, &desired.Spec, orbID, providerID, true)
   149  	if err != nil {
   150  		return nil, err
   151  	}
   152  
   153  	return core.ListMachines(svc)
   154  }