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
 | 
					    cat <<EOF >>/etc/gce.conf
 | 
				
			||||||
token-url = ${TOKEN_URL}
 | 
					token-url = ${TOKEN_URL}
 | 
				
			||||||
token-body = ${TOKEN_BODY}
 | 
					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
 | 
					EOF
 | 
				
			||||||
  fi
 | 
					  fi
 | 
				
			||||||
  if [[ -n "${PROJECT_ID:-}" ]]; then
 | 
					  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,
 | 
						// ApiEndpoint is the GCE compute API endpoint to use. If this is blank,
 | 
				
			||||||
	// then the default endpoint is used.
 | 
						// then the default endpoint is used.
 | 
				
			||||||
	ApiEndpoint string `gcfg:"api-endpoint"`
 | 
						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
 | 
						// LocalZone specifies the GCE zone that gce cloud client instance is
 | 
				
			||||||
	// located in (i.e. where the controller will be running). If this 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.
 | 
						// blank, then the local zone will be discovered via the metadata server.
 | 
				
			||||||
@@ -194,22 +197,23 @@ type ConfigFile struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// CloudConfig includes all the necessary configuration for creating GCECloud
 | 
					// CloudConfig includes all the necessary configuration for creating GCECloud
 | 
				
			||||||
type CloudConfig struct {
 | 
					type CloudConfig struct {
 | 
				
			||||||
	ApiEndpoint        string
 | 
						ApiEndpoint          string
 | 
				
			||||||
	ProjectID          string
 | 
						ContainerApiEndpoint string
 | 
				
			||||||
	NetworkProjectID   string
 | 
						ProjectID            string
 | 
				
			||||||
	Region             string
 | 
						NetworkProjectID     string
 | 
				
			||||||
	Zone               string
 | 
						Region               string
 | 
				
			||||||
	ManagedZones       []string
 | 
						Zone                 string
 | 
				
			||||||
	NetworkName        string
 | 
						ManagedZones         []string
 | 
				
			||||||
	NetworkURL         string
 | 
						NetworkName          string
 | 
				
			||||||
	SubnetworkName     string
 | 
						NetworkURL           string
 | 
				
			||||||
	SubnetworkURL      string
 | 
						SubnetworkName       string
 | 
				
			||||||
	SecondaryRangeName string
 | 
						SubnetworkURL        string
 | 
				
			||||||
	NodeTags           []string
 | 
						SecondaryRangeName   string
 | 
				
			||||||
	NodeInstancePrefix string
 | 
						NodeTags             []string
 | 
				
			||||||
	TokenSource        oauth2.TokenSource
 | 
						NodeInstancePrefix   string
 | 
				
			||||||
	UseMetadataServer  bool
 | 
						TokenSource          oauth2.TokenSource
 | 
				
			||||||
	AlphaFeatureGate   *AlphaFeatureGate
 | 
						UseMetadataServer    bool
 | 
				
			||||||
 | 
						AlphaFeatureGate     *AlphaFeatureGate
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
@@ -238,6 +242,11 @@ func (g *GCECloud) Compute() cloud.Cloud {
 | 
				
			|||||||
	return g.c
 | 
						return g.c
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// ContainerService returns the container service.
 | 
				
			||||||
 | 
					func (g *GCECloud) ContainerService() *container.Service {
 | 
				
			||||||
 | 
						return g.containerService
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// newGCECloud creates a new instance of GCECloud.
 | 
					// newGCECloud creates a new instance of GCECloud.
 | 
				
			||||||
func newGCECloud(config io.Reader) (gceCloud *GCECloud, err error) {
 | 
					func newGCECloud(config io.Reader) (gceCloud *GCECloud, err error) {
 | 
				
			||||||
	var cloudConfig *CloudConfig
 | 
						var cloudConfig *CloudConfig
 | 
				
			||||||
@@ -278,6 +287,10 @@ func generateCloudConfig(configFile *ConfigFile) (cloudConfig *CloudConfig, err
 | 
				
			|||||||
			cloudConfig.ApiEndpoint = configFile.Global.ApiEndpoint
 | 
								cloudConfig.ApiEndpoint = configFile.Global.ApiEndpoint
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if configFile.Global.ContainerApiEndpoint != "" {
 | 
				
			||||||
 | 
								cloudConfig.ContainerApiEndpoint = configFile.Global.ContainerApiEndpoint
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if configFile.Global.TokenURL != "" {
 | 
							if configFile.Global.TokenURL != "" {
 | 
				
			||||||
			// if tokenURL is nil, set tokenSource to nil. This will force the OAuth client to fall
 | 
								// 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.
 | 
								// back to use DefaultTokenSource. This allows running gceCloud remotely.
 | 
				
			||||||
@@ -419,6 +432,9 @@ func CreateGCECloud(config *CloudConfig) (*GCECloud, error) {
 | 
				
			|||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	containerService.UserAgent = userAgent
 | 
						containerService.UserAgent = userAgent
 | 
				
			||||||
 | 
						if config.ContainerApiEndpoint != "" {
 | 
				
			||||||
 | 
							containerService.BasePath = config.ContainerApiEndpoint
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tpuService, err := newTPUService(client)
 | 
						tpuService, err := newTPUService(client)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,7 +16,10 @@ limitations under the License.
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
package gce
 | 
					package gce
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import "context"
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
 | 
						container "google.golang.org/api/container/v1"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func newClustersMetricContext(request, zone string) *metricContext {
 | 
					func newClustersMetricContext(request, zone string) *metricContext {
 | 
				
			||||||
	return newGenericMetricContext("clusters", request, unusedMetricLabel, zone, computeV1Version)
 | 
						return newGenericMetricContext("clusters", request, unusedMetricLabel, zone, computeV1Version)
 | 
				
			||||||
@@ -37,11 +40,37 @@ func (gce *GCECloud) ListClusters(ctx context.Context) ([]string, error) {
 | 
				
			|||||||
	return allClusters, nil
 | 
						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) {
 | 
					func (gce *GCECloud) Master(ctx context.Context, clusterName string) (string, error) {
 | 
				
			||||||
	return "k8s-" + clusterName + "-master.internal", nil
 | 
						return "k8s-" + clusterName + "-master.internal", nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (gce *GCECloud) listClustersInZone(zone string) ([]string, error) {
 | 
					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)
 | 
						mc := newClustersMetricContext("list_zone", zone)
 | 
				
			||||||
	// TODO: use PageToken to list all not just the first 500
 | 
						// TODO: use PageToken to list all not just the first 500
 | 
				
			||||||
	list, err := gce.containerService.Projects.Zones.Clusters.List(gce.projectID, zone).Do()
 | 
						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)
 | 
							return nil, mc.Observe(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	result := []string{}
 | 
						return list.Clusters, mc.Observe(nil)
 | 
				
			||||||
	for _, cluster := range list.Clusters {
 | 
					 | 
				
			||||||
		result = append(result, cluster.Name)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return result, mc.Observe(nil)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user