This repository was archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
add support for Cloud Build config (resolve #8) #27
Open
SaketramDurbha
wants to merge
18
commits into
GoogleCloudPlatform:master
Choose a base branch
from
SaketramDurbha:cloud-build-config
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d54f706
implement cloud build config support (part one)
1dfb723
add region substitution for cloud build config
a3fcdf6
refactor cloud build config implementation
6b800ed
add comments, update README.md for Cloud Build config
4c5ed77
various cloud build config updates
ded600c
minor README.md update
de1a9c8
remove tempfiles.go in favor of cleanup function returns
c4a998a
refactor temp file cleanup logic
c3fafc3
minor code refactor in internal/lifecycle.go
9c10184
README.md format update
5bbe9ea
explicitly set viper default for cloud build substitutions
63f0e32
update error message format and temp file format
4f275e4
clarify defaults section README.md
7e45dc9
update to replaceServiceName algorithm
686765c
go mod tidy
77d2e3d
fix rebase
a13dafa
partially revert refactor
1c12fa0
update comment for cmd.init
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,122 @@ | ||
| // Copyright 2020 Google LLC | ||
| // | ||
| // 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 lifecycle | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "github.com/GoogleCloudPlatform/serverless-sample-tester/internal/util" | ||
| "gopkg.in/yaml.v2" | ||
| "io/ioutil" | ||
| "log" | ||
| "os" | ||
| "os/exec" | ||
| "strings" | ||
| ) | ||
|
|
||
| // runRegionSubstitution is the substitution used to specify which Cloud Run region Cloud Build configs will deploy | ||
| // samples to. | ||
| const runRegionSubstitution = "_SST_RUN_REGION" | ||
|
|
||
| // getCloudBuildConfigLifecycle returns a Lifecycle for the executing the provided Cloud Build config file. It creates | ||
| // and uses a temporary copy of the file where it replaces the Cloud Run service names and Container Registry tags with | ||
| // the provided inputs. It provides also passes in the provided substitutions as well a runRegionSubstitution with the | ||
| // provided region. Also returns a function that removes the temp file created while making Lifecycle. This function | ||
| // should be called after Lifecycle is done executing. | ||
| func getCloudBuildConfigLifecycle(filename, serviceName, gcrURL, runRegion string, substitutions map[string]string) (Lifecycle, func(), error) { | ||
| config := make(map[string]interface{}) | ||
|
|
||
| buildConfigBytes, err := ioutil.ReadFile(filename) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("ioutil.ReadFile: %w", err) | ||
| } | ||
|
|
||
| err = yaml.Unmarshal(buildConfigBytes, &config) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("yaml.Unmarshal: %s: %w", filename, err) | ||
| } | ||
|
|
||
| // Replace Cloud Run service names and Container Registry URLs | ||
| for stepIndex := range config["steps"].([]interface{}) { | ||
| var args []string | ||
| for argIndex := range config["steps"].([]interface{})[stepIndex].(map[interface{}]interface{})["args"].([]interface{}) { | ||
| arg := config["steps"].([]interface{})[stepIndex].(map[interface{}]interface{})["args"].([]interface{})[argIndex].(string) | ||
| arg = gcrURLRegexp.ReplaceAllString(arg, gcrURL) | ||
|
|
||
| args = append(args, arg) | ||
| } | ||
|
|
||
| prog := config["steps"].([]interface{})[stepIndex].(map[interface{}]interface{})["name"].(string) | ||
| err := replaceServiceName(prog, args, serviceName) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("replacing Cloud Run service name in %s step args: %w", filename, err) | ||
| } | ||
|
|
||
| config["steps"].([]interface{})[stepIndex].(map[interface{}]interface{})["args"] = args | ||
| } | ||
|
|
||
| configMarshalBytes, err := yaml.Marshal(&config) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("yaml.Marshal: modified %s: %w", filename, err) | ||
| } | ||
|
|
||
| tempBuildConfigFile, err := ioutil.TempFile("", "cloudbuild.*.yaml") | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("ioutil.TempFile: %w\n", err) | ||
| } | ||
| cleanup := func() { | ||
| err := os.Remove(tempBuildConfigFile.Name()) | ||
| if err != nil { | ||
| log.Printf("Error removing Temp File for Cloud Build config: %v\n", err) | ||
| } | ||
| } | ||
|
|
||
| if _, err := tempBuildConfigFile.Write(configMarshalBytes); err != nil { | ||
| cleanup() | ||
| return nil, nil, fmt.Errorf("os.File.Write: TempFile %s: %w", tempBuildConfigFile.Name(), err) | ||
| } | ||
| if err := tempBuildConfigFile.Close(); err != nil { | ||
| cleanup() | ||
| return nil, nil, fmt.Errorf("os.File.Close: TempFile %s: %w", tempBuildConfigFile.Name(), err) | ||
| } | ||
|
|
||
| return buildCloudBuildConfigLifecycle(tempBuildConfigFile.Name(), runRegion, substitutions), cleanup, nil | ||
| } | ||
|
|
||
| // buildCloudBuildConfigLifecycle returns a Lifecycle with a single command that calls gcloud builds subit and passes | ||
| // in the provided Cloud Build config file. It also adds a `--substitutions` flag according to the substitutions | ||
| // provided and adds a substitution for the Cloud Run region with the name runRegionSubstitution and value provided. | ||
| func buildCloudBuildConfigLifecycle(buildConfigFilename, runRegion string, substitutions map[string]string) Lifecycle { | ||
| a := append(util.GcloudCommonFlags, "builds", "submit", | ||
| fmt.Sprintf("--config=%s", buildConfigFilename)) | ||
|
|
||
| subsitutions := substitutionsString(substitutions, runRegion) | ||
| a = append(a, fmt.Sprintf("--substitutions=%s", subsitutions)) | ||
|
|
||
| return Lifecycle{exec.Command("gcloud", a...)} | ||
| } | ||
|
|
||
| // substitutionsString takes a string to string map and converts it into an argument for the `gcloud builds submit` | ||
| // `--config` file. It treats the keys in the map as the substitutions and the values as the substitution values. It | ||
| // also adds a substitution for the Cloud Run region with the name runRegionSubstitution and value provided. | ||
| func substitutionsString(m map[string]string, runRegion string) string { | ||
| var subs []string | ||
| subs = append(subs, fmt.Sprintf("%s=%s", runRegionSubstitution, runRegion)) | ||
|
|
||
| for k, v := range m { | ||
| subs = append(subs, fmt.Sprintf("%s=%s", k, v)) | ||
| } | ||
|
|
||
| return strings.Join(subs, ",") | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.