github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/provider/gcp_provider.go (about)

     1  package provider
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/internal/networking"
     8  
     9  	"github.com/kyma-project/kyma-environment-broker/internal/ptr"
    10  
    11  	"github.com/kyma-project/kyma-environment-broker/internal/broker"
    12  
    13  	"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
    14  	"github.com/kyma-project/kyma-environment-broker/internal"
    15  )
    16  
    17  const (
    18  	DefaultGCPRegion         = "europe-west3"
    19  	DefaultGCPMachineType    = "n2-standard-4"
    20  	DefaultGCPMultiZoneCount = 3
    21  )
    22  
    23  var europeGcp = "europe-west3"
    24  var usGcp = "us-central1"
    25  var asiaGcp = "asia-south1"
    26  
    27  var toGCPSpecific = map[string]*string{
    28  	string(broker.Europe): &europeGcp,
    29  	string(broker.Us):     &usGcp,
    30  	string(broker.Asia):   &asiaGcp,
    31  }
    32  
    33  type (
    34  	GcpInput struct {
    35  		MultiZone                    bool
    36  		ControlPlaneFailureTolerance string
    37  	}
    38  	GcpTrialInput struct {
    39  		PlatformRegionMapping map[string]string
    40  	}
    41  )
    42  
    43  func (p *GcpInput) Defaults() *gqlschema.ClusterConfigInput {
    44  	zonesCount := 1
    45  	if p.MultiZone {
    46  		zonesCount = DefaultGCPMultiZoneCount
    47  	}
    48  	var controlPlaneFailureTolerance *string = nil
    49  	if p.ControlPlaneFailureTolerance != "" {
    50  		controlPlaneFailureTolerance = &p.ControlPlaneFailureTolerance
    51  	}
    52  	return &gqlschema.ClusterConfigInput{
    53  		GardenerConfig: &gqlschema.GardenerConfigInput{
    54  			DiskType:       ptr.String("pd-standard"),
    55  			VolumeSizeGb:   ptr.Integer(50),
    56  			MachineType:    DefaultGCPMachineType,
    57  			Region:         DefaultGCPRegion,
    58  			Provider:       "gcp",
    59  			WorkerCidr:     networking.DefaultNodesCIDR,
    60  			AutoScalerMin:  3,
    61  			AutoScalerMax:  20,
    62  			MaxSurge:       zonesCount,
    63  			MaxUnavailable: 0,
    64  			ProviderSpecificConfig: &gqlschema.ProviderSpecificInput{
    65  				GcpConfig: &gqlschema.GCPProviderConfigInput{
    66  					Zones: ZonesForGCPRegion(DefaultGCPRegion, zonesCount),
    67  				},
    68  			},
    69  			ControlPlaneFailureTolerance: controlPlaneFailureTolerance,
    70  		},
    71  	}
    72  }
    73  
    74  func (p *GcpInput) ApplyParameters(input *gqlschema.ClusterConfigInput, pp internal.ProvisioningParameters) {
    75  	if pp.Parameters.Networking != nil {
    76  		input.GardenerConfig.WorkerCidr = pp.Parameters.Networking.NodesCidr
    77  	}
    78  	switch {
    79  	// explicit zones list is provided
    80  	case len(pp.Parameters.Zones) > 0:
    81  		updateSlice(&input.GardenerConfig.ProviderSpecificConfig.GcpConfig.Zones, pp.Parameters.Zones)
    82  	// region is provided, with or without zonesCount
    83  	case pp.Parameters.Region != nil && *pp.Parameters.Region != "":
    84  		zonesCount := 1
    85  		if p.MultiZone {
    86  			zonesCount = DefaultGCPMultiZoneCount
    87  		}
    88  		updateSlice(&input.GardenerConfig.ProviderSpecificConfig.GcpConfig.Zones, ZonesForGCPRegion(*pp.Parameters.Region, zonesCount))
    89  	}
    90  }
    91  
    92  func (p *GcpInput) Profile() gqlschema.KymaProfile {
    93  	return gqlschema.KymaProfileProduction
    94  }
    95  
    96  func (p *GcpInput) Provider() internal.CloudProvider {
    97  	return internal.GCP
    98  }
    99  
   100  func (p *GcpTrialInput) Defaults() *gqlschema.ClusterConfigInput {
   101  	return &gqlschema.ClusterConfigInput{
   102  		GardenerConfig: &gqlschema.GardenerConfigInput{
   103  			DiskType:       ptr.String("pd-standard"),
   104  			VolumeSizeGb:   ptr.Integer(30),
   105  			MachineType:    "n2-standard-4",
   106  			Region:         DefaultGCPRegion,
   107  			Provider:       "gcp",
   108  			WorkerCidr:     "10.250.0.0/19",
   109  			AutoScalerMin:  1,
   110  			AutoScalerMax:  1,
   111  			MaxSurge:       1,
   112  			MaxUnavailable: 0,
   113  			ProviderSpecificConfig: &gqlschema.ProviderSpecificInput{
   114  				GcpConfig: &gqlschema.GCPProviderConfigInput{
   115  					Zones: ZonesForGCPRegion(DefaultGCPRegion, 1),
   116  				},
   117  			},
   118  		},
   119  	}
   120  }
   121  
   122  func (p *GcpTrialInput) ApplyParameters(input *gqlschema.ClusterConfigInput, pp internal.ProvisioningParameters) {
   123  	params := pp.Parameters
   124  	var region string
   125  
   126  	// if there is a platform region - use it
   127  	if pp.PlatformRegion != "" {
   128  		abstractRegion, found := p.PlatformRegionMapping[pp.PlatformRegion]
   129  		if found {
   130  			region = *toGCPSpecific[abstractRegion]
   131  		}
   132  	}
   133  
   134  	// if the user provides a region - use this one
   135  	if params.Region != nil && *params.Region != "" {
   136  		region = *toGCPSpecific[*params.Region]
   137  	}
   138  
   139  	// region is not empty - it means override the default one
   140  	if region != "" {
   141  		updateString(&input.GardenerConfig.Region, &region)
   142  		updateSlice(&input.GardenerConfig.ProviderSpecificConfig.GcpConfig.Zones, ZonesForGCPRegion(region, 1))
   143  	}
   144  }
   145  
   146  func (p *GcpTrialInput) Profile() gqlschema.KymaProfile {
   147  	return gqlschema.KymaProfileEvaluation
   148  }
   149  
   150  func (p *GcpTrialInput) Provider() internal.CloudProvider {
   151  	return internal.GCP
   152  }
   153  
   154  func ZonesForGCPRegion(region string, zonesCount int) []string {
   155  	zoneCodes := []string{"a", "b", "c"}
   156  	var zones []string
   157  	rand.Shuffle(len(zoneCodes), func(i, j int) { zoneCodes[i], zoneCodes[j] = zoneCodes[j], zoneCodes[i] })
   158  
   159  	for i := 0; i < zonesCount && i < len(zoneCodes); i++ {
   160  		zones = append(zones, fmt.Sprintf("%s-%s", region, zoneCodes[i]))
   161  	}
   162  
   163  	return zones
   164  }