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

     1  package gce
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"strings"
     7  
     8  	"github.com/caos/orbos/pkg/kubernetes"
     9  	macherrs "k8s.io/apimachinery/pkg/api/errors"
    10  
    11  	"github.com/caos/orbos/internal/executables"
    12  
    13  	"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
    14  	"github.com/caos/orbos/internal/operator/orbiter/kinds/providers/core"
    15  	"github.com/caos/orbos/pkg/tree"
    16  )
    17  
    18  var _ infra.ProviderCurrent = (*Current)(nil)
    19  
    20  type Current struct {
    21  	Common  *tree.Common `yaml:",inline"`
    22  	Current struct {
    23  		pools      map[string]infra.Pool `yaml:"-"`
    24  		Ingresses  map[string]*infra.Address
    25  		cleanupped <-chan error `yaml:"-"`
    26  	}
    27  }
    28  
    29  func (c *Current) Pools() map[string]infra.Pool {
    30  	return c.Current.pools
    31  }
    32  func (c *Current) Ingresses() map[string]*infra.Address {
    33  	return c.Current.Ingresses
    34  }
    35  func (c *Current) Cleanupped() <-chan error {
    36  	return c.Current.cleanupped
    37  }
    38  func (c *Current) PrivateInterface() string { return "eth0" }
    39  
    40  func (c *Current) Kubernetes() infra.Kubernetes {
    41  	return infra.Kubernetes{
    42  		CleanupAndApply: func(k8sClient kubernetes.ClientInt) (io.Reader, error) {
    43  			var create bool
    44  			deployment, err := k8sClient.GetDeployment("gce-pd-csi-driver", "csi-gce-pd-controller")
    45  			if macherrs.IsNotFound(err) {
    46  				create = true
    47  				err = nil
    48  			}
    49  			if err != nil {
    50  				return nil, err
    51  			}
    52  
    53  			apply := func() io.Reader {
    54  				return bytes.NewReader(executables.PreBuilt("kubernetes_gce.yaml"))
    55  			}
    56  
    57  			if create {
    58  				return apply(), nil
    59  			}
    60  
    61  			containers := deployment.Spec.Template.Spec.Containers
    62  			if len(containers) != 5 {
    63  				return apply(), nil
    64  			}
    65  			for idx := range containers {
    66  				container := containers[idx]
    67  				switch container.Name {
    68  				case "csi-provisioner":
    69  					if imageVersion(container.Image) != "v2.0.4" {
    70  						return apply(), nil
    71  					}
    72  				case "csi-attacher":
    73  					if imageVersion(container.Image) != "v3.0.1" {
    74  						return apply(), nil
    75  					}
    76  				case "csi-resizer":
    77  					if imageVersion(container.Image) != "v1.0.1" {
    78  						return apply(), nil
    79  					}
    80  				case "csi-snapshotter":
    81  					if imageVersion(container.Image) != "v3.0.1" {
    82  						return apply(), nil
    83  					}
    84  				case "gce-pd-driver":
    85  					if imageVersion(container.Image) != "v1.2.1-gke.0" {
    86  						return apply(), nil
    87  					}
    88  				}
    89  			}
    90  			return nil, nil
    91  
    92  		},
    93  		/*		CloudController: infra.CloudControllerManager{
    94  							Supported: true,
    95  							CloudConfig: func(machine infra.Machine) io.Reader {
    96  								instance := machine.(*instance)
    97  								ctx := instance.context
    98  								return strings.NewReader(fmt.Sprintf(`[Global]
    99  				project-id = "%s"
   100  				network-name = "%s"
   101  				node-instance-prefix = "orbos-"
   102  				multizone = false
   103  				local-zone = "%s"
   104  				container-api-endpoint = "Don't use container API'"
   105  				`,
   106  									ctx.projectID,
   107  									ctx.networkName,
   108  									ctx.desired.Zone,
   109  								))
   110  							},
   111  							ProviderName: "external",
   112  						},*/
   113  	}
   114  }
   115  
   116  func imageVersion(image string) string {
   117  	return strings.Split(image, ":")[1]
   118  }
   119  
   120  func initPools(current *Current, desired *Spec, svc *machinesService, normalized []*normalizedLoadbalancer, machines core.MachinesService) error {
   121  
   122  	current.Current.pools = make(map[string]infra.Pool)
   123  	for pool := range desired.Pools {
   124  		current.Current.pools[pool] = newInfraPool(pool, svc, normalized, machines)
   125  	}
   126  
   127  	pools, err := machines.ListPools()
   128  	if err != nil {
   129  		return nil
   130  	}
   131  	for _, pool := range pools {
   132  		// Also return pools that are not configured
   133  		if _, ok := current.Current.pools[pool]; !ok {
   134  			current.Current.pools[pool] = newInfraPool(pool, svc, normalized, machines)
   135  		}
   136  	}
   137  	return nil
   138  }