Skip to content

Commit

Permalink
Merge pull request #6284 from olagacek/master
Browse files Browse the repository at this point in the history
Allow overriding domain suffix in GCE cloud provider.
  • Loading branch information
k8s-ci-robot authored Nov 21, 2023
2 parents de44d78 + 0ca611a commit 0979221
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type autoscalingGceClientV1 struct {
gceService *gce.Service

projectId string
domainUrl string

// These can be overridden, e.g. for testing.
operationWaitTimeout time.Duration
Expand All @@ -141,7 +142,7 @@ func NewAutoscalingGceClientV1(client *http.Client, projectId string, userAgent

// NewCustomAutoscalingGceClientV1 creates a new client using custom server url and timeouts
// for communicating with GCE v1 API.
func NewCustomAutoscalingGceClientV1(client *http.Client, projectId, serverUrl, userAgent string,
func NewCustomAutoscalingGceClientV1(client *http.Client, projectId, serverUrl, userAgent, domainUrl string,
waitTimeout, pollInterval time.Duration, deletionPollInterval time.Duration) (*autoscalingGceClientV1, error) {
gceService, err := gce.New(client)
if err != nil {
Expand All @@ -153,6 +154,7 @@ func NewCustomAutoscalingGceClientV1(client *http.Client, projectId, serverUrl,
return &autoscalingGceClientV1{
projectId: projectId,
gceService: gceService,
domainUrl: domainUrl,
operationWaitTimeout: waitTimeout,
operationPollInterval: pollInterval,
operationDeletionPollInterval: deletionPollInterval,
Expand Down Expand Up @@ -295,7 +297,7 @@ func (client *autoscalingGceClientV1) DeleteInstances(migRef GceRef, instances [
SkipInstancesOnValidationError: true,
}
for _, i := range instances {
req.Instances = append(req.Instances, GenerateInstanceUrl(i))
req.Instances = append(req.Instances, GenerateInstanceUrl(client.domainUrl, i))
}
op, err := client.gceService.InstanceGroupManagers.DeleteInstances(migRef.Project, migRef.Zone, migRef.Name, &req).Do()
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func TestErrors(t *testing.T) {

func TestFetchMigInstancesInstanceUrlHandling(t *testing.T) {
const goodInstanceUrlTempl = "https://content.googleapis.com/compute/v1/projects/myprojid/zones/myzone/instances/myinst_%d"
const badInstanceUrl = "https://badurl.com/compute/v1/projects/myprojid/zones/myzone/instances/myinst"
const badInstanceUrl = "https://badurl.com/compute/v1/projects3/myprojid/zones/myzone/instances/myinst"
server := test_util.NewHttpServerMock()
defer server.Close()
g := newTestAutoscalingGceClient(t, "project1", server.URL, "")
Expand Down
5 changes: 3 additions & 2 deletions cluster-autoscaler/cloudprovider/gce/gce_cloud_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ type gceMig struct {
gceManager GceManager
minSize int
maxSize int
domainUrl string
}

// GceRef returns Mig's GceRef
Expand Down Expand Up @@ -307,7 +308,7 @@ func (mig *gceMig) DeleteNodes(nodes []*apiv1.Node) error {

// Id returns mig url.
func (mig *gceMig) Id() string {
return GenerateMigUrl(mig.gceRef)
return GenerateMigUrl(mig.domainUrl, mig.gceRef)
}

// Debug returns a debug string for the Mig.
Expand Down Expand Up @@ -369,7 +370,7 @@ func BuildGCE(opts config.AutoscalingOptions, do cloudprovider.NodeGroupDiscover
defer config.Close()
}

manager, err := CreateGceManager(config, do, opts.Regional, opts.GCEOptions.ConcurrentRefreshes, opts.UserAgent, opts.GCEOptions.MigInstancesMinRefreshWaitTime)
manager, err := CreateGceManager(config, do, opts.Regional, opts.GCEOptions.ConcurrentRefreshes, opts.UserAgent, opts.GCEOptions.DomainUrl, opts.GCEOptions.MigInstancesMinRefreshWaitTime)
if err != nil {
klog.Fatalf("Failed to create GCE Manager: %v", err)
}
Expand Down
5 changes: 4 additions & 1 deletion cluster-autoscaler/cloudprovider/gce/gce_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ type gceManagerImpl struct {

location string
projectId string
domainUrl string
templates *GceTemplateBuilder
interrupt chan struct{}
regional bool
Expand All @@ -123,7 +124,7 @@ type gceManagerImpl struct {
}

// CreateGceManager constructs GceManager object.
func CreateGceManager(configReader io.Reader, discoveryOpts cloudprovider.NodeGroupDiscoveryOptions, regional bool, concurrentGceRefreshes int, userAgent string, migInstancesMinRefreshWaitTime time.Duration) (GceManager, error) {
func CreateGceManager(configReader io.Reader, discoveryOpts cloudprovider.NodeGroupDiscoveryOptions, regional bool, concurrentGceRefreshes int, userAgent, domainUrl string, migInstancesMinRefreshWaitTime time.Duration) (GceManager, error) {
// Create Google Compute Engine token.
var err error
tokenSource := google.ComputeTokenSource("")
Expand Down Expand Up @@ -192,6 +193,7 @@ func CreateGceManager(configReader io.Reader, discoveryOpts cloudprovider.NodeGr
explicitlyConfigured: make(map[GceRef]bool),
concurrentGceRefreshes: concurrentGceRefreshes,
reserved: &GceReserved{},
domainUrl: domainUrl,
}

if err := manager.fetchExplicitMigs(discoveryOpts.NodeGroupSpecs); err != nil {
Expand Down Expand Up @@ -416,6 +418,7 @@ func (m *gceManagerImpl) buildMigFromSpec(s *dynamic.NodeGroupSpec) (Mig, error)
gceManager: m,
minSize: s.MinSize,
maxSize: s.MaxSize,
domainUrl: m.domainUrl,
}
return mig, nil
}
Expand Down
37 changes: 17 additions & 20 deletions cluster-autoscaler/cloudprovider/gce/gce_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ package gce

import (
"fmt"
"regexp"
"strings"
)

const (
gceUrlSchema = "https"
gceDomainSuffix = "googleapis.com/compute/v1/projects/"
// Cluster Autoscaler previously used "content" instead of "www" here, for reasons unknown.
gcePrefix = gceUrlSchema + "://www." + gceDomainSuffix
instanceUrlTemplate = gcePrefix + "%s/zones/%s/instances/%s"
migUrlTemplate = gcePrefix + "%s/zones/%s/instanceGroups/%s"
projectsSubstring = "/projects/"
defaultDomainUrl = "https://www.googleapis.com/compute/v1"
)

// ParseMigUrl expects url in format:
Expand Down Expand Up @@ -64,30 +61,30 @@ func ParseInstanceUrlRef(url string) (GceRef, error) {
}

// GenerateInstanceUrl generates url for instance.
func GenerateInstanceUrl(ref GceRef) string {
func GenerateInstanceUrl(domainUrl string, ref GceRef) string {
if domainUrl == "" {
domainUrl = defaultDomainUrl
}
instanceUrlTemplate := domainUrl + projectsSubstring + "%s/zones/%s/instances/%s"
return fmt.Sprintf(instanceUrlTemplate, ref.Project, ref.Zone, ref.Name)
}

// GenerateMigUrl generates url for instance.
func GenerateMigUrl(ref GceRef) string {
func GenerateMigUrl(domainUrl string, ref GceRef) string {
if domainUrl == "" {
domainUrl = defaultDomainUrl
}
migUrlTemplate := domainUrl + projectsSubstring + "%s/zones/%s/instanceGroups/%s"
return fmt.Sprintf(migUrlTemplate, ref.Project, ref.Zone, ref.Name)
}

func parseGceUrl(url, expectedResource string) (project string, zone string, name string, err error) {
errMsg := fmt.Errorf("wrong url: expected format https://www.googleapis.com/compute/v1/projects/<project-id>/zones/<zone>/%s/<name>, got %s", expectedResource, url)
if !strings.Contains(url, gceDomainSuffix) {
return "", "", "", errMsg
}
if !strings.HasPrefix(url, gceUrlSchema) {
reg := regexp.MustCompile(fmt.Sprintf("https://.*/projects/.*/zones/.*/%s/.*", expectedResource))
errMsg := fmt.Errorf("wrong url: expected format <url>/projects/<project-id>/zones/<zone>/%s/<name>, got %s", expectedResource, url)
if !reg.MatchString(url) {
return "", "", "", errMsg
}
splitted := strings.Split(strings.Split(url, gceDomainSuffix)[1], "/")
if len(splitted) != 5 || splitted[1] != "zones" {
return "", "", "", errMsg
}
if splitted[3] != expectedResource {
return "", "", "", fmt.Errorf("wrong resource in url: expected %s, got %s", expectedResource, splitted[3])
}
splitted := strings.Split(strings.Split(url, projectsSubstring)[1], "/")
project = splitted[0]
zone = splitted[2]
name = splitted[4]
Expand Down
Loading

0 comments on commit 0979221

Please sign in to comment.