mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	Merge pull request #67736 from cheftako/GetClusters
Automatic merge from submit-queue (batch tested with PRs 67736, 68123, 68138). If you want to cherry-pick this change to another branch, please follow the instructions here: https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md. Added support to get clusters in gce cloud provider. **What this PR does / why we need it**: Implemented the call to get all cluster objects in a zone for a project. Also added code to allow the container api to be set in the gce.conf file. **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes # **Special notes for your reviewer**: **Release note**: ```release-note NONE ```
This commit is contained in:
		@@ -570,6 +570,12 @@ EOF
 | 
			
		||||
    cat <<EOF >>/etc/gce.conf
 | 
			
		||||
token-url = ${TOKEN_URL}
 | 
			
		||||
token-body = ${TOKEN_BODY}
 | 
			
		||||
EOF
 | 
			
		||||
  fi
 | 
			
		||||
  if [[ -n "${CONTAINER_API_ENDPOINT:-}" ]]; then
 | 
			
		||||
    use_cloud_config="true"
 | 
			
		||||
    cat <<EOF >>/etc/gce.conf
 | 
			
		||||
container-api-endpoint = ${CONTAINER_API_ENDPOINT}
 | 
			
		||||
EOF
 | 
			
		||||
  fi
 | 
			
		||||
  if [[ -n "${PROJECT_ID:-}" ]]; then
 | 
			
		||||
 
 | 
			
		||||
@@ -178,6 +178,9 @@ type ConfigGlobal struct {
 | 
			
		||||
	// ApiEndpoint is the GCE compute API endpoint to use. If this is blank,
 | 
			
		||||
	// then the default endpoint is used.
 | 
			
		||||
	ApiEndpoint string `gcfg:"api-endpoint"`
 | 
			
		||||
	// ContainerApiEndpoint is the GCE container API endpoint to use. If this is blank,
 | 
			
		||||
	// then the default endpoint is used.
 | 
			
		||||
	ContainerApiEndpoint string `gcfg:"container-api-endpoint"`
 | 
			
		||||
	// LocalZone specifies the GCE zone that gce cloud client instance is
 | 
			
		||||
	// located in (i.e. where the controller will be running). If this is
 | 
			
		||||
	// blank, then the local zone will be discovered via the metadata server.
 | 
			
		||||
@@ -195,6 +198,7 @@ type ConfigFile struct {
 | 
			
		||||
// CloudConfig includes all the necessary configuration for creating GCECloud
 | 
			
		||||
type CloudConfig struct {
 | 
			
		||||
	ApiEndpoint          string
 | 
			
		||||
	ContainerApiEndpoint string
 | 
			
		||||
	ProjectID            string
 | 
			
		||||
	NetworkProjectID     string
 | 
			
		||||
	Region               string
 | 
			
		||||
@@ -238,6 +242,11 @@ func (g *GCECloud) Compute() cloud.Cloud {
 | 
			
		||||
	return g.c
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ContainerService returns the container service.
 | 
			
		||||
func (g *GCECloud) ContainerService() *container.Service {
 | 
			
		||||
	return g.containerService
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// newGCECloud creates a new instance of GCECloud.
 | 
			
		||||
func newGCECloud(config io.Reader) (gceCloud *GCECloud, err error) {
 | 
			
		||||
	var cloudConfig *CloudConfig
 | 
			
		||||
@@ -278,6 +287,10 @@ func generateCloudConfig(configFile *ConfigFile) (cloudConfig *CloudConfig, err
 | 
			
		||||
			cloudConfig.ApiEndpoint = configFile.Global.ApiEndpoint
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if configFile.Global.ContainerApiEndpoint != "" {
 | 
			
		||||
			cloudConfig.ContainerApiEndpoint = configFile.Global.ContainerApiEndpoint
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if configFile.Global.TokenURL != "" {
 | 
			
		||||
			// if tokenURL is nil, set tokenSource to nil. This will force the OAuth client to fall
 | 
			
		||||
			// back to use DefaultTokenSource. This allows running gceCloud remotely.
 | 
			
		||||
@@ -419,6 +432,9 @@ func CreateGCECloud(config *CloudConfig) (*GCECloud, error) {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	containerService.UserAgent = userAgent
 | 
			
		||||
	if config.ContainerApiEndpoint != "" {
 | 
			
		||||
		containerService.BasePath = config.ContainerApiEndpoint
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	tpuService, err := newTPUService(client)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -16,7 +16,10 @@ limitations under the License.
 | 
			
		||||
 | 
			
		||||
package gce
 | 
			
		||||
 | 
			
		||||
import "context"
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	container "google.golang.org/api/container/v1"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func newClustersMetricContext(request, zone string) *metricContext {
 | 
			
		||||
	return newGenericMetricContext("clusters", request, unusedMetricLabel, zone, computeV1Version)
 | 
			
		||||
@@ -37,11 +40,37 @@ func (gce *GCECloud) ListClusters(ctx context.Context) ([]string, error) {
 | 
			
		||||
	return allClusters, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (gce *GCECloud) GetManagedClusters(ctx context.Context) ([]*container.Cluster, error) {
 | 
			
		||||
	managedClusters := []*container.Cluster{}
 | 
			
		||||
	for _, zone := range gce.managedZones {
 | 
			
		||||
		clusters, err := gce.getClustersInZone(zone)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		managedClusters = append(managedClusters, clusters...)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return managedClusters, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (gce *GCECloud) Master(ctx context.Context, clusterName string) (string, error) {
 | 
			
		||||
	return "k8s-" + clusterName + "-master.internal", nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (gce *GCECloud) listClustersInZone(zone string) ([]string, error) {
 | 
			
		||||
	clusters, err := gce.getClustersInZone(zone)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	result := []string{}
 | 
			
		||||
	for _, cluster := range clusters {
 | 
			
		||||
		result = append(result, cluster.Name)
 | 
			
		||||
	}
 | 
			
		||||
	return result, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (gce *GCECloud) getClustersInZone(zone string) ([]*container.Cluster, error) {
 | 
			
		||||
	mc := newClustersMetricContext("list_zone", zone)
 | 
			
		||||
	// TODO: use PageToken to list all not just the first 500
 | 
			
		||||
	list, err := gce.containerService.Projects.Zones.Clusters.List(gce.projectID, zone).Do()
 | 
			
		||||
@@ -49,9 +78,5 @@ func (gce *GCECloud) listClustersInZone(zone string) ([]string, error) {
 | 
			
		||||
		return nil, mc.Observe(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	result := []string{}
 | 
			
		||||
	for _, cluster := range list.Clusters {
 | 
			
		||||
		result = append(result, cluster.Name)
 | 
			
		||||
	}
 | 
			
		||||
	return result, mc.Observe(nil)
 | 
			
		||||
	return list.Clusters, mc.Observe(nil)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user