Use full URLs for GCE networks

This commit is contained in:
CJ Cullen 2015-08-06 17:45:44 -07:00
parent bee48f4ce5
commit 5882c35e45

View File

@ -59,7 +59,7 @@ type GCECloud struct {
zone string zone string
instanceID string instanceID string
externalID string externalID string
networkName string networkURL string
// Used for accessing the metadata server // Used for accessing the metadata server
metadataAccess func(string) (string, error) metadataAccess func(string) (string, error)
@ -166,6 +166,7 @@ func newGCECloud(config io.Reader) (*GCECloud, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
networkURL := gceNetworkURL(projectID, networkName)
tokenSource := google.ComputeTokenSource("") tokenSource := google.ComputeTokenSource("")
if config != nil { if config != nil {
var cfg Config var cfg Config
@ -177,7 +178,11 @@ func newGCECloud(config io.Reader) (*GCECloud, error) {
projectID = cfg.Global.ProjectID projectID = cfg.Global.ProjectID
} }
if cfg.Global.NetworkName != "" { if cfg.Global.NetworkName != "" {
networkName = cfg.Global.NetworkName if strings.Contains(cfg.Global.NetworkName, "/") {
networkURL = cfg.Global.NetworkName
} else {
networkURL = gceNetworkURL(cfg.Global.ProjectID, cfg.Global.NetworkName)
}
} }
if cfg.Global.TokenURL != "" { if cfg.Global.TokenURL != "" {
tokenSource = newAltTokenSource(cfg.Global.TokenURL) tokenSource = newAltTokenSource(cfg.Global.TokenURL)
@ -199,7 +204,7 @@ func newGCECloud(config io.Reader) (*GCECloud, error) {
zone: zone, zone: zone,
instanceID: instanceID, instanceID: instanceID,
externalID: externalID, externalID: externalID,
networkName: networkName, networkURL: networkURL,
metadataAccess: getMetadata, metadataAccess: getMetadata,
}, nil }, nil
} }
@ -426,7 +431,7 @@ func (gce *GCECloud) CreateTCPLoadBalancer(name, region string, externalIP net.I
firewall := &compute.Firewall{ firewall := &compute.Firewall{
Name: makeFirewallName(name), Name: makeFirewallName(name),
Description: fmt.Sprintf("KubernetesAutoGenerated_OnlyAllowTrafficForDestinationIP_%s", fwd.IPAddress), Description: fmt.Sprintf("KubernetesAutoGenerated_OnlyAllowTrafficForDestinationIP_%s", fwd.IPAddress),
Network: gce.gceNetworkName(), Network: gce.networkURL,
SourceRanges: []string{"0.0.0.0/0"}, SourceRanges: []string{"0.0.0.0/0"},
TargetTags: []string{hostTag}, TargetTags: []string{hostTag},
Allowed: []*compute.FirewallAllowed{ Allowed: []*compute.FirewallAllowed{
@ -759,7 +764,7 @@ func (gce *GCECloud) ListRoutes(clusterName string) ([]*cloudprovider.Route, err
} }
var routes []*cloudprovider.Route var routes []*cloudprovider.Route
for _, r := range res.Items { for _, r := range res.Items {
if path.Base(r.Network) != gce.networkName { if r.Network != gce.networkURL {
continue continue
} }
// Not managed if route description != "k8s-node-route" // Not managed if route description != "k8s-node-route"
@ -777,8 +782,8 @@ func (gce *GCECloud) ListRoutes(clusterName string) ([]*cloudprovider.Route, err
return routes, nil return routes, nil
} }
func (gce *GCECloud) gceNetworkName() string { func gceNetworkURL(project, network string) string {
return fmt.Sprintf("global/networks/%s", gce.networkName) return fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", project, network)
} }
func (gce *GCECloud) CreateRoute(clusterName string, nameHint string, route *cloudprovider.Route) error { func (gce *GCECloud) CreateRoute(clusterName string, nameHint string, route *cloudprovider.Route) error {
@ -789,7 +794,7 @@ func (gce *GCECloud) CreateRoute(clusterName string, nameHint string, route *clo
Name: routeName, Name: routeName,
DestRange: route.DestinationCIDR, DestRange: route.DestinationCIDR,
NextHopInstance: fmt.Sprintf("zones/%s/instances/%s", gce.zone, instanceName), NextHopInstance: fmt.Sprintf("zones/%s/instances/%s", gce.zone, instanceName),
Network: gce.gceNetworkName(), Network: gce.networkURL,
Priority: 1000, Priority: 1000,
Description: k8sNodeRouteTag, Description: k8sNodeRouteTag,
}).Do() }).Do()