mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-18 16:21:13 +00:00
Merge pull request #46366 from nicksardo/gce-subnetwork-url
Automatic merge from submit-queue (batch tested with PRs 45573, 46354, 46376, 46162, 46366)
GCE - Retrieve subnetwork name/url from gce.conf
**What this PR does / why we need it**:
Features like ILB require specifying the subnetwork if the network is type manual.
**Notes:**
The network URL can be [constructed](68e7e18698/pkg/cloudprovider/providers/gce/gce.go (L211-L217)
) by fetching instance metadata; however, the subnetwork is not provided through this feature. Users must specify the subnetwork name/url through the gce.conf.
Although multiple subnets can exist in the same region for a network, the cloud provider will only use one subnet url for creating LBs.
**Release note**:
```release-note
NONE
```
This commit is contained in:
commit
9c1480bb61
@ -184,6 +184,11 @@ token-body = ${TOKEN_BODY}
|
|||||||
project-id = ${PROJECT_ID}
|
project-id = ${PROJECT_ID}
|
||||||
network-name = ${NODE_NETWORK}
|
network-name = ${NODE_NETWORK}
|
||||||
EOF
|
EOF
|
||||||
|
if [[ -n "${NODE_SUBNETWORK:-}" ]]; then
|
||||||
|
cat <<EOF >>/etc/gce.conf
|
||||||
|
subnetwork-name = ${NODE_SUBNETWORK}
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
if [[ -n "${NODE_INSTANCE_PREFIX:-}" ]]; then
|
if [[ -n "${NODE_INSTANCE_PREFIX:-}" ]]; then
|
||||||
use_cloud_config="true"
|
use_cloud_config="true"
|
||||||
|
@ -326,6 +326,11 @@ token-body = ${TOKEN_BODY}
|
|||||||
project-id = ${PROJECT_ID}
|
project-id = ${PROJECT_ID}
|
||||||
network-name = ${NODE_NETWORK}
|
network-name = ${NODE_NETWORK}
|
||||||
EOF
|
EOF
|
||||||
|
if [[ -n "${NODE_SUBNETWORK:-}" ]]; then
|
||||||
|
cat <<EOF >>/etc/gce.conf
|
||||||
|
subnetwork-name = ${NODE_SUBNETWORK}
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
if [[ -n "${NODE_INSTANCE_PREFIX:-}" ]]; then
|
if [[ -n "${NODE_INSTANCE_PREFIX:-}" ]]; then
|
||||||
use_cloud_config="true"
|
use_cloud_config="true"
|
||||||
|
@ -86,6 +86,7 @@ type GCECloud struct {
|
|||||||
localZone string // The zone in which we are running
|
localZone string // The zone in which we are running
|
||||||
managedZones []string // List of zones we are spanning (for multi-AZ clusters, primarily when running on master)
|
managedZones []string // List of zones we are spanning (for multi-AZ clusters, primarily when running on master)
|
||||||
networkURL string
|
networkURL string
|
||||||
|
subnetworkURL string
|
||||||
nodeTags []string // List of tags to use on firewall rules for load balancers
|
nodeTags []string // List of tags to use on firewall rules for load balancers
|
||||||
nodeInstancePrefix string // If non-"", an advisory prefix for all nodes in the cluster
|
nodeInstancePrefix string // If non-"", an advisory prefix for all nodes in the cluster
|
||||||
useMetadataServer bool
|
useMetadataServer bool
|
||||||
@ -98,6 +99,7 @@ type Config struct {
|
|||||||
TokenBody string `gcfg:"token-body"`
|
TokenBody string `gcfg:"token-body"`
|
||||||
ProjectID string `gcfg:"project-id"`
|
ProjectID string `gcfg:"project-id"`
|
||||||
NetworkName string `gcfg:"network-name"`
|
NetworkName string `gcfg:"network-name"`
|
||||||
|
SubnetworkName string `gcfg:"subnetwork-name"`
|
||||||
NodeTags []string `gcfg:"node-tags"`
|
NodeTags []string `gcfg:"node-tags"`
|
||||||
NodeInstancePrefix string `gcfg:"node-instance-prefix"`
|
NodeInstancePrefix string `gcfg:"node-instance-prefix"`
|
||||||
Multizone bool `gcfg:"multizone"`
|
Multizone bool `gcfg:"multizone"`
|
||||||
@ -134,6 +136,7 @@ func newGCECloud(config io.Reader) (*GCECloud, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
networkURL := gceNetworkURL(projectID, networkName)
|
networkURL := gceNetworkURL(projectID, networkName)
|
||||||
|
subnetworkURL := ""
|
||||||
|
|
||||||
// By default, Kubernetes clusters only run against one zone
|
// By default, Kubernetes clusters only run against one zone
|
||||||
managedZones := []string{zone}
|
managedZones := []string{zone}
|
||||||
@ -158,6 +161,13 @@ func newGCECloud(config io.Reader) (*GCECloud, error) {
|
|||||||
networkURL = gceNetworkURL(cfg.Global.ProjectID, cfg.Global.NetworkName)
|
networkURL = gceNetworkURL(cfg.Global.ProjectID, cfg.Global.NetworkName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if cfg.Global.SubnetworkName != "" {
|
||||||
|
if strings.Contains(cfg.Global.SubnetworkName, "/") {
|
||||||
|
subnetworkURL = cfg.Global.SubnetworkName
|
||||||
|
} else {
|
||||||
|
subnetworkURL = gceSubnetworkURL(cfg.Global.ProjectID, region, cfg.Global.SubnetworkName)
|
||||||
|
}
|
||||||
|
}
|
||||||
if cfg.Global.TokenURL != "" {
|
if cfg.Global.TokenURL != "" {
|
||||||
tokenSource = NewAltTokenSource(cfg.Global.TokenURL, cfg.Global.TokenBody)
|
tokenSource = NewAltTokenSource(cfg.Global.TokenURL, cfg.Global.TokenBody)
|
||||||
}
|
}
|
||||||
@ -168,15 +178,15 @@ func newGCECloud(config io.Reader) (*GCECloud, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return CreateGCECloud(projectID, region, zone, managedZones, networkURL, nodeTags,
|
return CreateGCECloud(projectID, region, zone, managedZones, networkURL, subnetworkURL,
|
||||||
nodeInstancePrefix, tokenSource, true /* useMetadataServer */)
|
nodeTags, nodeInstancePrefix, tokenSource, true /* useMetadataServer */)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a GCECloud object using the specified parameters.
|
// Creates a GCECloud object using the specified parameters.
|
||||||
// If no networkUrl is specified, loads networkName via rest call.
|
// If no networkUrl is specified, loads networkName via rest call.
|
||||||
// If no tokenSource is specified, uses oauth2.DefaultTokenSource.
|
// If no tokenSource is specified, uses oauth2.DefaultTokenSource.
|
||||||
// If managedZones is nil / empty all zones in the region will be managed.
|
// If managedZones is nil / empty all zones in the region will be managed.
|
||||||
func CreateGCECloud(projectID, region, zone string, managedZones []string, networkURL string, nodeTags []string,
|
func CreateGCECloud(projectID, region, zone string, managedZones []string, networkURL, subnetworkURL string, nodeTags []string,
|
||||||
nodeInstancePrefix string, tokenSource oauth2.TokenSource, useMetadataServer bool) (*GCECloud, error) {
|
nodeInstancePrefix string, tokenSource oauth2.TokenSource, useMetadataServer bool) (*GCECloud, error) {
|
||||||
|
|
||||||
client, err := newOauthClient(tokenSource)
|
client, err := newOauthClient(tokenSource)
|
||||||
@ -229,6 +239,7 @@ func CreateGCECloud(projectID, region, zone string, managedZones []string, netwo
|
|||||||
localZone: zone,
|
localZone: zone,
|
||||||
managedZones: managedZones,
|
managedZones: managedZones,
|
||||||
networkURL: networkURL,
|
networkURL: networkURL,
|
||||||
|
subnetworkURL: subnetworkURL,
|
||||||
nodeTags: nodeTags,
|
nodeTags: nodeTags,
|
||||||
nodeInstancePrefix: nodeInstancePrefix,
|
nodeInstancePrefix: nodeInstancePrefix,
|
||||||
useMetadataServer: useMetadataServer,
|
useMetadataServer: useMetadataServer,
|
||||||
@ -293,6 +304,10 @@ func gceNetworkURL(project, network string) string {
|
|||||||
return fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", project, network)
|
return fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", project, network)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func gceSubnetworkURL(project, region, subnetwork string) string {
|
||||||
|
return fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/regions/%s/subnetworks/%s", project, region, subnetwork)
|
||||||
|
}
|
||||||
|
|
||||||
func getNetworkNameViaMetadata() (string, error) {
|
func getNetworkNameViaMetadata() (string, error) {
|
||||||
result, err := metadata.Get("instance/network-interfaces/0/network")
|
result, err := metadata.Get("instance/network-interfaces/0/network")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -77,7 +77,9 @@ func setupProviderConfig() error {
|
|||||||
if !framework.TestContext.CloudConfig.MultiZone {
|
if !framework.TestContext.CloudConfig.MultiZone {
|
||||||
managedZones = []string{zone}
|
managedZones = []string{zone}
|
||||||
}
|
}
|
||||||
cloudConfig.Provider, err = gcecloud.CreateGCECloud(framework.TestContext.CloudConfig.ProjectID, region, zone, managedZones, "" /* networkUrl */, nil /* nodeTags */, "" /* nodeInstancePerfix */, nil /* tokenSource */, false /* useMetadataServer */)
|
cloudConfig.Provider, err = gcecloud.CreateGCECloud(framework.TestContext.CloudConfig.ProjectID,
|
||||||
|
region, zone, managedZones, "" /* networkUrl */, "" /* subnetworkUrl */, nil, /* nodeTags */
|
||||||
|
"" /* nodeInstancePerfix */, nil /* tokenSource */, false /* useMetadataServer */)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error building GCE/GKE provider: %v", err)
|
return fmt.Errorf("Error building GCE/GKE provider: %v", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user