mirror of https://github.com/k3s-io/k3s
use overrided api endpoint in gce cloud provider
parent
98ce69825a
commit
3e8b4a27c4
|
@ -19,6 +19,9 @@
|
||||||
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
|
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
|
||||||
source "${KUBE_ROOT}/cluster/gce/config-common.sh"
|
source "${KUBE_ROOT}/cluster/gce/config-common.sh"
|
||||||
|
|
||||||
|
# Specifying KUBE_GCE_API_ENDPOINT will override the default GCE Compute API endpoint (https://www.googleapis.com/compute/v1/).
|
||||||
|
# This endpoint has to be pointing to v1 api. For example, https://www.googleapis.com/compute/staging_v1/
|
||||||
|
GCE_API_ENDPOINT=${KUBE_GCE_API_ENDPOINT:-}
|
||||||
GCLOUD=gcloud
|
GCLOUD=gcloud
|
||||||
ZONE=${KUBE_GCE_ZONE:-us-central1-b}
|
ZONE=${KUBE_GCE_ZONE:-us-central1-b}
|
||||||
REGION=${ZONE%-*}
|
REGION=${ZONE%-*}
|
||||||
|
@ -73,7 +76,6 @@ GCI_VERSION=${KUBE_GCI_VERSION:-cos-stable-59-9460-64-0}
|
||||||
MASTER_IMAGE=${KUBE_GCE_MASTER_IMAGE:-}
|
MASTER_IMAGE=${KUBE_GCE_MASTER_IMAGE:-}
|
||||||
MASTER_IMAGE_PROJECT=${KUBE_GCE_MASTER_PROJECT:-cos-cloud}
|
MASTER_IMAGE_PROJECT=${KUBE_GCE_MASTER_PROJECT:-cos-cloud}
|
||||||
NODE_IMAGE=${KUBE_GCE_NODE_IMAGE:-${GCI_VERSION}}
|
NODE_IMAGE=${KUBE_GCE_NODE_IMAGE:-${GCI_VERSION}}
|
||||||
GCE_API_ENDPOINT=${KUBE_GCE_API_ENDPOINT:-}
|
|
||||||
NODE_IMAGE_PROJECT=${KUBE_GCE_NODE_PROJECT:-cos-cloud}
|
NODE_IMAGE_PROJECT=${KUBE_GCE_NODE_PROJECT:-cos-cloud}
|
||||||
CONTAINER_RUNTIME=${KUBE_CONTAINER_RUNTIME:-docker}
|
CONTAINER_RUNTIME=${KUBE_CONTAINER_RUNTIME:-docker}
|
||||||
RKT_VERSION=${KUBE_RKT_VERSION:-1.23.0}
|
RKT_VERSION=${KUBE_RKT_VERSION:-1.23.0}
|
||||||
|
|
|
@ -19,6 +19,9 @@
|
||||||
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
|
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
|
||||||
source "${KUBE_ROOT}/cluster/gce/config-common.sh"
|
source "${KUBE_ROOT}/cluster/gce/config-common.sh"
|
||||||
|
|
||||||
|
# Specifying KUBE_GCE_API_ENDPOINT will override the default GCE Compute API endpoint (https://www.googleapis.com/compute/v1/).
|
||||||
|
# This endpoint has to be pointing to v1 api. For example, https://www.googleapis.com/compute/staging_v1/
|
||||||
|
GCE_API_ENDPOINT=${KUBE_GCE_API_ENDPOINT:-}
|
||||||
GCLOUD=gcloud
|
GCLOUD=gcloud
|
||||||
ZONE=${KUBE_GCE_ZONE:-us-central1-b}
|
ZONE=${KUBE_GCE_ZONE:-us-central1-b}
|
||||||
REGION=${ZONE%-*}
|
REGION=${ZONE%-*}
|
||||||
|
|
|
@ -76,6 +76,8 @@ const (
|
||||||
gceHcHealthyThreshold = int64(1)
|
gceHcHealthyThreshold = int64(1)
|
||||||
// Defaults to 5 * 2 = 10 seconds before the LB will steer traffic away
|
// Defaults to 5 * 2 = 10 seconds before the LB will steer traffic away
|
||||||
gceHcUnhealthyThreshold = int64(5)
|
gceHcUnhealthyThreshold = int64(5)
|
||||||
|
|
||||||
|
gceComputeAPIEndpoint = "https://www.googleapis.com/compute/v1/"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GCECloud is an implementation of Interface, LoadBalancer and Instances for Google Compute Engine.
|
// GCECloud is an implementation of Interface, LoadBalancer and Instances for Google Compute Engine.
|
||||||
|
@ -146,6 +148,7 @@ type Config struct {
|
||||||
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"`
|
||||||
|
// Specifying ApiEndpoint will override the default GCE compute API endpoint.
|
||||||
ApiEndpoint string `gcfg:"api-endpoint"`
|
ApiEndpoint string `gcfg:"api-endpoint"`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -269,24 +272,33 @@ func CreateGCECloud(apiEndpoint, projectID, networkProjectID, region, zone strin
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
service, err := compute.New(client)
|
service, err := compute.New(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err = newOauthClient(tokenSource)
|
client, err = newOauthClient(tokenSource)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
serviceBeta, err := computebeta.New(client)
|
serviceBeta, err := computebeta.New(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err = newOauthClient(tokenSource)
|
client, err = newOauthClient(tokenSource)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
serviceAlpha, err := computealpha.New(client)
|
serviceAlpha, err := computealpha.New(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Expect override api endpoint to always be v1 api and follows the same pattern as prod.
|
||||||
|
// Generate alpha and beta api endpoints based on override v1 api endpoint.
|
||||||
|
// For example,
|
||||||
|
// staging API endpoint: https://www.googleapis.com/compute/staging_v1/
|
||||||
if apiEndpoint != "" {
|
if apiEndpoint != "" {
|
||||||
service.BasePath = fmt.Sprintf("%sprojects/", apiEndpoint)
|
service.BasePath = fmt.Sprintf("%sprojects/", apiEndpoint)
|
||||||
serviceBeta.BasePath = fmt.Sprintf("%sprojects/", strings.Replace(apiEndpoint, "v1", "beta", 0))
|
serviceBeta.BasePath = fmt.Sprintf("%sprojects/", strings.Replace(apiEndpoint, "v1", "beta", 0))
|
||||||
|
@ -423,16 +435,16 @@ var _ cloudprovider.Interface = (*GCECloud)(nil)
|
||||||
|
|
||||||
func gceNetworkURL(apiEndpoint, project, network string) string {
|
func gceNetworkURL(apiEndpoint, project, network string) string {
|
||||||
if apiEndpoint == "" {
|
if apiEndpoint == "" {
|
||||||
apiEndpoint = "https://www.googleapis.com/compute/v1/"
|
apiEndpoint = gceComputeAPIEndpoint
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%vprojects/%s/global/networks/%s", apiEndpoint, project, network)
|
return apiEndpoint + strings.Join([]string{"projects", project, "global", "networks", network}, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
func gceSubnetworkURL(apiEndpoint, project, region, subnetwork string) string {
|
func gceSubnetworkURL(apiEndpoint, project, region, subnetwork string) string {
|
||||||
if apiEndpoint == "" {
|
if apiEndpoint == "" {
|
||||||
apiEndpoint = "https://www.googleapis.com/compute/v1/"
|
apiEndpoint = gceComputeAPIEndpoint
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%vprojects/%s/regions/%s/subnetworks/%s", apiEndpoint, project, region, subnetwork)
|
return apiEndpoint + strings.Join([]string{"projects", project, "regions", region, "subnetworks", subnetwork}, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Project IDs cannot have a digit for the first characeter. If the id contains a digit,
|
// Project IDs cannot have a digit for the first characeter. If the id contains a digit,
|
||||||
|
@ -441,7 +453,6 @@ func isProjectNumber(idOrNumber string) bool {
|
||||||
if len(idOrNumber) == 0 {
|
if len(idOrNumber) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return idOrNumber[0] >= '0' && idOrNumber[0] <= '9'
|
return idOrNumber[0] >= '0' && idOrNumber[0] <= '9'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -41,7 +40,7 @@ const (
|
||||||
DiskTypeStandard = "pd-standard"
|
DiskTypeStandard = "pd-standard"
|
||||||
|
|
||||||
diskTypeDefault = DiskTypeStandard
|
diskTypeDefault = DiskTypeStandard
|
||||||
diskTypeUriTemplate = "https://www.googleapis.com/compute/v1/projects/%s/zones/%s/diskTypes/%s"
|
diskTypeUriTemplate = "%s/zones/%s/diskTypes/%s"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Disks is interface for manipulation with GCE PDs.
|
// Disks is interface for manipulation with GCE PDs.
|
||||||
|
@ -234,7 +233,12 @@ func (gce *GCECloud) CreateDisk(
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("invalid GCE disk type %q", diskType)
|
return fmt.Errorf("invalid GCE disk type %q", diskType)
|
||||||
}
|
}
|
||||||
diskTypeUri := fmt.Sprintf(diskTypeUriTemplate, gce.projectID, zone, diskType)
|
|
||||||
|
apiEndpoint := gceComputeAPIEndpoint + "projects/"
|
||||||
|
if gce.service != nil {
|
||||||
|
apiEndpoint = gce.service.BasePath
|
||||||
|
}
|
||||||
|
diskTypeUri := apiEndpoint + fmt.Sprintf(diskTypeUriTemplate, gce.projectID, zone, diskType)
|
||||||
|
|
||||||
diskToCreate := &compute.Disk{
|
diskToCreate := &compute.Disk{
|
||||||
Name: name,
|
Name: name,
|
||||||
|
@ -424,9 +428,8 @@ func (gce *GCECloud) convertDiskToAttachedDisk(disk *GCEDisk, readWrite string)
|
||||||
DeviceName: disk.Name,
|
DeviceName: disk.Name,
|
||||||
Kind: disk.Kind,
|
Kind: disk.Kind,
|
||||||
Mode: readWrite,
|
Mode: readWrite,
|
||||||
Source: "https://" + path.Join(
|
Source: gce.service.BasePath + strings.Join([]string{
|
||||||
"www.googleapis.com/compute/v1/projects/",
|
gce.projectID, "zones", disk.Zone, "disks", disk.Name}, "/"),
|
||||||
gce.projectID, "zones", disk.Zone, "disks", disk.Name),
|
|
||||||
Type: "PERSISTENT",
|
Type: "PERSISTENT",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,7 @@ func TestCreateDisk_Basic(t *testing.T) {
|
||||||
tags := make(map[string]string)
|
tags := make(map[string]string)
|
||||||
tags["test-tag"] = "test-value"
|
tags["test-tag"] = "test-value"
|
||||||
|
|
||||||
diskTypeUri := fmt.Sprintf(diskTypeUriTemplate, projectId, zone, diskType)
|
diskTypeUri := gceComputeAPIEndpoint + "projects/" + fmt.Sprintf(diskTypeUriTemplate, projectId, zone, diskType)
|
||||||
expectedDescription := "{\"test-tag\":\"test-value\"}"
|
expectedDescription := "{\"test-tag\":\"test-value\"}"
|
||||||
|
|
||||||
/* Act */
|
/* Act */
|
||||||
|
|
|
@ -25,7 +25,7 @@ import (
|
||||||
|
|
||||||
"cloud.google.com/go/compute/metadata"
|
"cloud.google.com/go/compute/metadata"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
computealpha "google.golang.org/api/compute/v0.beta"
|
computebeta "google.golang.org/api/compute/v0.beta"
|
||||||
compute "google.golang.org/api/compute/v1"
|
compute "google.golang.org/api/compute/v1"
|
||||||
|
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
|
@ -69,7 +69,7 @@ func getZone(n *v1.Node) string {
|
||||||
// ToInstanceReferences returns instance references by links
|
// ToInstanceReferences returns instance references by links
|
||||||
func (gce *GCECloud) ToInstanceReferences(zone string, instanceNames []string) (refs []*compute.InstanceReference) {
|
func (gce *GCECloud) ToInstanceReferences(zone string, instanceNames []string) (refs []*compute.InstanceReference) {
|
||||||
for _, ins := range instanceNames {
|
for _, ins := range instanceNames {
|
||||||
instanceLink := makeHostURL(gce.projectID, zone, ins)
|
instanceLink := makeHostURL(gce.service.BasePath, gce.projectID, zone, ins)
|
||||||
refs = append(refs, &compute.InstanceReference{Instance: instanceLink})
|
refs = append(refs, &compute.InstanceReference{Instance: instanceLink})
|
||||||
}
|
}
|
||||||
return refs
|
return refs
|
||||||
|
@ -303,7 +303,7 @@ func (gce *GCECloud) AliasRanges(nodeName types.NodeName) (cidrs []string, err e
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var res *computealpha.Instance
|
var res *computebeta.Instance
|
||||||
res, err = gce.serviceBeta.Instances.Get(
|
res, err = gce.serviceBeta.Instances.Get(
|
||||||
gce.projectID, instance.Zone, instance.Name).Do()
|
gce.projectID, instance.Zone, instance.Name).Do()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -483,7 +483,7 @@ func (gce *GCECloud) createTargetPool(name, serviceName, ipAddress, region, clus
|
||||||
|
|
||||||
var instances []string
|
var instances []string
|
||||||
for _, host := range hosts {
|
for _, host := range hosts {
|
||||||
instances = append(instances, makeHostURL(gce.projectID, host.Zone, host.Name))
|
instances = append(instances, makeHostURL(gce.service.BasePath, gce.projectID, host.Zone, host.Name))
|
||||||
}
|
}
|
||||||
glog.Infof("Creating targetpool %v with %d healthchecks", name, len(hcLinks))
|
glog.Infof("Creating targetpool %v with %d healthchecks", name, len(hcLinks))
|
||||||
pool := &compute.TargetPool{
|
pool := &compute.TargetPool{
|
||||||
|
@ -542,7 +542,7 @@ func (gce *GCECloud) updateTargetPool(loadBalancerName string, existing sets.Str
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gce *GCECloud) targetPoolURL(name, region string) string {
|
func (gce *GCECloud) targetPoolURL(name, region string) string {
|
||||||
return fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/regions/%s/targetPools/%s", gce.projectID, region, name)
|
return gce.service.BasePath + strings.Join([]string{"projects", gce.projectID, "regions", region, "targetPools", name}, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeHttpHealthCheck(name, path string, port int32) *compute.HttpHealthCheck {
|
func makeHttpHealthCheck(name, path string, port int32) *compute.HttpHealthCheck {
|
||||||
|
@ -671,10 +671,9 @@ func nodeNames(nodes []*v1.Node) []string {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeHostURL(projectID, zone, host string) string {
|
func makeHostURL(apiEndpoint, projectID, zone, host string) string {
|
||||||
host = canonicalizeInstanceName(host)
|
host = canonicalizeInstanceName(host)
|
||||||
return fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/instances/%s",
|
return apiEndpoint + strings.Join([]string{"projects", projectID, "zones", zone, "instances", host}, "/")
|
||||||
projectID, zone, host)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func hostURLToComparablePath(hostURL string) string {
|
func hostURLToComparablePath(hostURL string) string {
|
||||||
|
|
|
@ -624,7 +624,7 @@ func getPortsAndProtocol(svcPorts []v1.ServicePort) (ports []string, protocol v1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gce *GCECloud) getBackendServiceLink(name string) string {
|
func (gce *GCECloud) getBackendServiceLink(name string) string {
|
||||||
return fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/regions/%s/backendServices/%s", gce.projectID, gce.region, name)
|
return gce.service.BasePath + strings.Join([]string{"projects", gce.projectID, "regions", gce.region, "backendServices", name}, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getNameFromLink(link string) string {
|
func getNameFromLink(link string) string {
|
||||||
|
|
|
@ -23,6 +23,7 @@ import (
|
||||||
compute "google.golang.org/api/compute/v1"
|
compute "google.golang.org/api/compute/v1"
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/cloudprovider"
|
"k8s.io/kubernetes/pkg/cloudprovider"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newZonesMetricContext(request, region string) *metricContext {
|
func newZonesMetricContext(request, region string) *metricContext {
|
||||||
|
@ -52,5 +53,5 @@ func (gce *GCECloud) ListZonesInRegion(region string) ([]*compute.Zone, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gce *GCECloud) getRegionLink(region string) string {
|
func (gce *GCECloud) getRegionLink(region string) string {
|
||||||
return fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%v/regions/%v", gce.networkProjectID, region)
|
return gce.service.BasePath + strings.Join([]string{"projects", gce.networkProjectID, "regions", region}, "/")
|
||||||
}
|
}
|
||||||
|
|
|
@ -352,6 +352,7 @@ filegroup(
|
||||||
"//vendor/golang.org/x/tools/container/intsets:all-srcs",
|
"//vendor/golang.org/x/tools/container/intsets:all-srcs",
|
||||||
"//vendor/google.golang.org/api/cloudkms/v1:all-srcs",
|
"//vendor/google.golang.org/api/cloudkms/v1:all-srcs",
|
||||||
"//vendor/google.golang.org/api/cloudmonitoring/v2beta2:all-srcs",
|
"//vendor/google.golang.org/api/cloudmonitoring/v2beta2:all-srcs",
|
||||||
|
"//vendor/google.golang.org/api/compute/v0.alpha:all-srcs",
|
||||||
"//vendor/google.golang.org/api/compute/v0.beta:all-srcs",
|
"//vendor/google.golang.org/api/compute/v0.beta:all-srcs",
|
||||||
"//vendor/google.golang.org/api/compute/v1:all-srcs",
|
"//vendor/google.golang.org/api/compute/v1:all-srcs",
|
||||||
"//vendor/google.golang.org/api/container/v1:all-srcs",
|
"//vendor/google.golang.org/api/container/v1:all-srcs",
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
package(default_visibility = ["//visibility:public"])
|
||||||
|
|
||||||
|
licenses(["notice"])
|
||||||
|
|
||||||
|
load(
|
||||||
|
"@io_bazel_rules_go//go:def.bzl",
|
||||||
|
"go_library",
|
||||||
|
)
|
||||||
|
|
||||||
|
go_library(
|
||||||
|
name = "go_default_library",
|
||||||
|
srcs = ["compute-gen.go"],
|
||||||
|
tags = ["automanaged"],
|
||||||
|
deps = [
|
||||||
|
"//vendor/golang.org/x/net/context:go_default_library",
|
||||||
|
"//vendor/golang.org/x/net/context/ctxhttp:go_default_library",
|
||||||
|
"//vendor/google.golang.org/api/gensupport:go_default_library",
|
||||||
|
"//vendor/google.golang.org/api/googleapi:go_default_library",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
filegroup(
|
||||||
|
name = "package-srcs",
|
||||||
|
srcs = glob(["**"]),
|
||||||
|
tags = ["automanaged"],
|
||||||
|
visibility = ["//visibility:private"],
|
||||||
|
)
|
||||||
|
|
||||||
|
filegroup(
|
||||||
|
name = "all-srcs",
|
||||||
|
srcs = [":package-srcs"],
|
||||||
|
tags = ["automanaged"],
|
||||||
|
)
|
Loading…
Reference in New Issue