-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use active gcloud credentials for executing cloudbuild when available #2731
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,23 @@ limitations under the License. | |
package gcp | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"os/exec" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" | ||
"github.com/docker/cli/cli/config/configfile" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
"golang.org/x/oauth2/google" | ||
) | ||
|
||
var ( | ||
creds *google.Credentials | ||
credsOnce sync.Once | ||
credsErr error | ||
) | ||
|
||
// AutoConfigureGCRCredentialHelper automatically adds the `gcloud` credential helper | ||
|
@@ -50,3 +62,34 @@ func AutoConfigureGCRCredentialHelper(cf *configfile.ConfigFile, registry string | |
} | ||
cf.CredentialHelpers[registry] = "gcloud" | ||
} | ||
|
||
func activeUserCredentials() (*google.Credentials, error) { | ||
credsOnce.Do(func() { | ||
cmd := exec.Command("gcloud", "auth", "print-access-token", "--format=json") | ||
body, err := util.RunCmdOut(cmd) | ||
if err != nil { | ||
credsErr = errors.Wrap(err, "retrieving gcloud access token") | ||
return | ||
} | ||
jsonCreds := make(map[string]interface{}) | ||
json.Unmarshal(body, &jsonCreds) | ||
jsonCreds["type"] = "authorized_user" | ||
body, _ = json.Marshal(jsonCreds) | ||
|
||
c, err := google.CredentialsFromJSON(context.Background(), body) | ||
if err != nil { | ||
logrus.Infof("unable to retrieve google creds: %v", err) | ||
logrus.Infof("falling back to application default credentials") | ||
return | ||
} | ||
_, err = c.TokenSource.Token() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line might panic - if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch, added a return here |
||
if err != nil { | ||
logrus.Infof("unable to retrieve token: %v", err) | ||
logrus.Infof("falling back to application default credentials") | ||
return | ||
} | ||
creds = c | ||
}) | ||
|
||
return creds, credsErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Copyright 2019 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package gcp | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version" | ||
|
||
cstorage "cloud.google.com/go/storage" | ||
"github.com/pkg/errors" | ||
cloudbuild "google.golang.org/api/cloudbuild/v1" | ||
"google.golang.org/api/option" | ||
) | ||
|
||
var ( | ||
cbclient *cloudbuild.Service | ||
cbOnce sync.Once | ||
cbErr error | ||
) | ||
|
||
// CloudBuildClient returns an authenticated client for interacting with | ||
// the Google Cloud Build API. This client is created once and cached | ||
// for repeated use in Skaffold. | ||
func CloudBuildClient() (*cloudbuild.Service, error) { | ||
cbOnce.Do(func() { | ||
var options []option.ClientOption | ||
var err error | ||
creds, cErr := activeUserCredentials() | ||
if cErr == nil && creds != nil { | ||
options = append(options, option.WithCredentials(creds)) | ||
} | ||
|
||
c, err := cloudbuild.NewService(context.Background(), options...) | ||
if err != nil { | ||
cbErr = err | ||
return | ||
} | ||
c.UserAgent = version.UserAgent() | ||
cbclient = c | ||
}) | ||
|
||
return cbclient, cbErr | ||
} | ||
|
||
// CloudStorageClient returns an authenticated client for interacting with | ||
// the Google Cloud Storage API. This client is not cached by Skaffold, | ||
// because it needs to be closed each time it is done being used by the caller. | ||
func CloudStorageClient() (*cstorage.Client, error) { | ||
var options []option.ClientOption | ||
var err error | ||
creds, cErr := activeUserCredentials() | ||
if cErr == nil && creds != nil { | ||
options = append(options, option.WithCredentials(creds)) | ||
} | ||
c, err := cstorage.NewClient(context.Background(), options...) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "getting cloud storage client") | ||
} | ||
return c, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did we need this update?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤷♂️