Merge pull request #52437 from erikmcc/master

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Update the Google API clients used by the GCE provider to identify Kubernetes as the origin of GCP API calls.

**What this PR does / why we need it**:
This modifies the Google API clients used for the GCE provider to identify GCP API calls as originating from Kubernetes.

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #39391

**Special notes for your reviewer**:

**Release note**:

```release-note
`NONE`
```
pull/6/head
Kubernetes Submit Queue 2017-10-23 00:28:08 -07:00 committed by GitHub
commit 0ee76d0258
2 changed files with 15 additions and 0 deletions

View File

@ -53,6 +53,7 @@ go_library(
"//pkg/master/ports:go_default_library",
"//pkg/util/net/sets:go_default_library",
"//pkg/util/version:go_default_library",
"//pkg/version:go_default_library",
"//pkg/volume:go_default_library",
"//pkg/volume/util:go_default_library",
"//vendor/cloud.google.com/go/compute/metadata:go_default_library",

View File

@ -21,6 +21,7 @@ import (
"io"
"net/http"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
@ -42,6 +43,7 @@ import (
"k8s.io/client-go/util/flowcontrol"
"k8s.io/kubernetes/pkg/cloudprovider"
"k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/version"
"github.com/golang/glog"
"golang.org/x/oauth2"
@ -344,6 +346,13 @@ func generateCloudConfig(configFile *ConfigFile) (cloudConfig *CloudConfig, err
// If no tokenSource is specified, uses oauth2.DefaultTokenSource.
// If managedZones is nil / empty all zones in the region will be managed.
func CreateGCECloud(config *CloudConfig) (*GCECloud, error) {
// Remove any pre-release version and build metadata from the semver, leaving only the MAJOR.MINOR.PATCH portion.
// See http://semver.org/.
version := strings.TrimLeft(strings.Split(strings.Split(version.Get().GitVersion, "-")[0], "+")[0], "v")
// Create a user-agent header append string to supply to the Google API clients, to identify Kubernetes as the origin of the GCP API calls.
userAgent := fmt.Sprintf("Kubernetes/%s (%s %s)", version, runtime.GOOS, runtime.GOARCH)
// Use ProjectID for NetworkProjectID, if it wasn't explicitly set.
if config.NetworkProjectID == "" {
config.NetworkProjectID = config.ProjectID
@ -357,6 +366,7 @@ func CreateGCECloud(config *CloudConfig) (*GCECloud, error) {
if err != nil {
return nil, err
}
service.UserAgent = userAgent
client, err = newOauthClient(config.TokenSource)
if err != nil {
@ -366,6 +376,7 @@ func CreateGCECloud(config *CloudConfig) (*GCECloud, error) {
if err != nil {
return nil, err
}
serviceBeta.UserAgent = userAgent
client, err = newOauthClient(config.TokenSource)
if err != nil {
@ -375,6 +386,7 @@ func CreateGCECloud(config *CloudConfig) (*GCECloud, error) {
if err != nil {
return nil, err
}
serviceAlpha.UserAgent = userAgent
// 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.
@ -390,11 +402,13 @@ func CreateGCECloud(config *CloudConfig) (*GCECloud, error) {
if err != nil {
return nil, err
}
containerService.UserAgent = userAgent
cloudkmsService, err := cloudkms.New(client)
if err != nil {
return nil, err
}
cloudkmsService.UserAgent = userAgent
// ProjectID and.NetworkProjectID may be project number or name.
projID, netProjID := tryConvertToProjectNames(config.ProjectID, config.NetworkProjectID, service)