-
Notifications
You must be signed in to change notification settings - Fork 99
feat: implement artifact registry integration #273
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
Changes from all commits
f37ad99
0bf2053
40adf7e
621e297
8109b49
15484bc
d69052f
ffe4057
c025b42
a18ceab
e5391d0
8a2d4d7
663d28b
2f7898e
f49d870
a8802d9
b6d75f2
1b3d348
70781a6
c5fd234
607381c
4090f79
76fd937
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // Copyright 2024 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 | ||
| // | ||
| // https://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 main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| artifactregistry "cloud.google.com/go/artifactregistry/apiv1" | ||
| artifactregistrypb "cloud.google.com/go/artifactregistry/apiv1/artifactregistrypb" | ||
| codes "google.golang.org/grpc/codes" | ||
| status "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| // Create a "Cloud Run Source Deploy" repository in Artifact Registry (if it doesn't already exist) | ||
| func createArtifactRegistry(project string, region string, repoName string) error { | ||
|
|
||
| repoPrefix := fmt.Sprintf("projects/%s/locations/%s", project, region) | ||
| repoFull := fmt.Sprintf("%s/repositories/%s", repoPrefix, repoName) | ||
|
|
||
| ctx := context.Background() | ||
|
|
||
| client, err := artifactregistry.NewClient(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create artifact registry client: %w", err) | ||
| } | ||
|
|
||
| // Check for existing repo | ||
| req := &artifactregistrypb.GetRepositoryRequest{ | ||
| Name: repoFull, | ||
| } | ||
| existingRepo, err := client.GetRepository(ctx, req) | ||
|
|
||
| if err != nil { | ||
| // The repo might not already exist, so allow that specific grpc error | ||
| notFoundError := status.Error(codes.NotFound, "Requested entity was not found.") | ||
| if !(errors.Is(err, notFoundError)) { | ||
| return fmt.Errorf("failed to retrieve existing artifact registry client: %w", err) | ||
| } | ||
| } | ||
|
|
||
| // If the existing repo doesn't exist, create it | ||
| if existingRepo == nil { | ||
| req := &artifactregistrypb.CreateRepositoryRequest{ | ||
| Parent: repoPrefix, | ||
| RepositoryId: repoName, | ||
| Repository: &artifactregistrypb.Repository{ | ||
| Name: repoFull, | ||
| Format: artifactregistrypb.Repository_DOCKER, | ||
| }, | ||
| } | ||
|
|
||
| _, err := client.CreateRepository(context.TODO(), req) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create artifact registry: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
|
|
||
| } | ||
|
glasnt marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,12 +115,12 @@ func envVars(project, name, region string) (map[string]struct{}, error) { | |
| // prevent deployment failures due to Cloud Run service naming constraints such | ||
| // as: | ||
| // | ||
| // * names with a leading non-letter (e.g. digit or '-') are prefixed | ||
| // * names over 63 characters are truncated | ||
| // * names ending with a '-' have the suffix trimmed | ||
| func tryFixServiceName(name string) string { | ||
| // - names with a leading non-letter (e.g. digit or '-') are prefixed | ||
| // - names over 63 characters are truncated | ||
| // - names ending with a '-' have the suffix trimmed | ||
| func tryFixServiceName(name string) (string, error) { | ||
| if name == "" { | ||
|
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. Being very nitpicky here, but this seems like this would be an erroneous situation, and a function name that starts with Maybe better if we have this? func tryFixServiceName(name string) (string, error) {
if name == "" {
return "", fmt.Errorf("service name can't be empty")
}
Collaborator
Author
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. There's a lot of older code here that I don't think would pass Golang standards today. The above diff is specifically the output of a linting command, which happens to be near one of the many other issues. I won't be addressing all of those in this PR 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. Sorry, conversation isn't resolved yet. I think you thought I was commenting on the lint recommendations. I was commenting on and suggesting that 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. Hi @glasnt, the rest of the PR LGTM and I don't want to be too nitpicky. I asked @telpirion to take a look at this and resolve the conversation accordingly. |
||
| return name | ||
| return "", fmt.Errorf("service name can't be empty") | ||
| } | ||
|
|
||
| name = strings.ToLower(name) | ||
|
|
@@ -145,5 +145,5 @@ func tryFixServiceName(name string) string { | |
| name = name[:len(name)-1] | ||
| } | ||
|
|
||
| return name | ||
| return name, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,56 @@ | ||
| module github.com/GoogleCloudPlatform/cloud-run-button | ||
|
|
||
| go 1.14 | ||
| go 1.21 | ||
|
|
||
| toolchain go1.21.0 | ||
|
|
||
| require ( | ||
| cloud.google.com/go/compute v0.1.0 | ||
| cloud.google.com/go/artifactregistry v1.15.0 | ||
| cloud.google.com/go/compute/metadata v0.5.0 | ||
| github.com/AlecAivazis/survey/v2 v2.0.7 | ||
| github.com/Netflix/go-expect v0.0.0-20200312175327-da48e75238e2 // indirect | ||
| github.com/briandowns/spinner v1.9.0 | ||
| github.com/fatih/color v1.9.0 | ||
| github.com/fatih/color v1.15.0 | ||
| google.golang.org/api v0.193.0 | ||
| ) | ||
|
|
||
| require ( | ||
| cloud.google.com/go v0.115.1 // indirect | ||
| cloud.google.com/go/auth v0.9.0 // indirect | ||
| cloud.google.com/go/auth/oauth2adapt v0.2.4 // indirect | ||
| cloud.google.com/go/iam v1.1.13 // indirect | ||
| cloud.google.com/go/longrunning v0.5.12 // indirect | ||
| github.com/Netflix/go-expect v0.0.0-20200312175327-da48e75238e2 // indirect | ||
| github.com/creack/pty v1.1.9 // indirect | ||
| github.com/felixge/httpsnoop v1.0.4 // indirect | ||
| github.com/go-logr/logr v1.4.2 // indirect | ||
| github.com/go-logr/stdr v1.2.2 // indirect | ||
| github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect | ||
| github.com/google/s2a-go v0.1.8 // indirect | ||
| github.com/google/uuid v1.6.0 // indirect | ||
| github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect | ||
| github.com/googleapis/gax-go/v2 v2.13.0 // indirect | ||
| github.com/hinshun/vt10x v0.0.0-20180809195222-d55458df857c // indirect | ||
| github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect | ||
| github.com/kr/pty v1.1.8 // indirect | ||
| github.com/kr/text v0.2.0 // indirect | ||
| github.com/mattn/go-colorable v0.1.6 // indirect | ||
| github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect | ||
| google.golang.org/api v0.65.0 | ||
| gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect | ||
| github.com/mattn/go-colorable v0.1.13 // indirect | ||
| github.com/mattn/go-isatty v0.0.19 // indirect | ||
| github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect | ||
| go.opencensus.io v0.24.0 // indirect | ||
| go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.52.0 // indirect | ||
| go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect | ||
| go.opentelemetry.io/otel v1.28.0 // indirect | ||
| go.opentelemetry.io/otel/metric v1.28.0 // indirect | ||
| go.opentelemetry.io/otel/trace v1.28.0 // indirect | ||
| golang.org/x/crypto v0.26.0 // indirect | ||
| golang.org/x/net v0.28.0 // indirect | ||
| golang.org/x/oauth2 v0.22.0 // indirect | ||
| golang.org/x/sync v0.8.0 // indirect | ||
| golang.org/x/sys v0.24.0 // indirect | ||
| golang.org/x/text v0.17.0 // indirect | ||
| golang.org/x/time v0.6.0 // indirect | ||
| google.golang.org/genproto v0.0.0-20240814211410-ddb44dafa142 // indirect | ||
| google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 // indirect | ||
| google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect | ||
| google.golang.org/grpc v1.65.0 // indirect | ||
| google.golang.org/protobuf v1.34.2 // indirect | ||
| ) |
Uh oh!
There was an error while loading. Please reload this page.