Skip to content

Commit

Permalink
feat: bump deps and migrate away from deprecated metadata APIs
Browse files Browse the repository at this point in the history
Migrates to the new non-deprecated APIs for accessing project ID and
service account email from within the GCE runtime.
  • Loading branch information
odsod committed Jul 15, 2024
1 parent 2ead723 commit ec951f8
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Setup Sage
uses: einride/sage/actions/setup@master
with:
go-version: 1.21
go-version: 1.22

- name: Make
run: make
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Setup Sage
uses: einride/sage/actions/setup@master
with:
go-version: 1.21
go-version: 1.22

- name: Make
run: make
Expand Down
2 changes: 1 addition & 1 deletion cloudotel/metricexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func StartMetricExporter(
if !exporterConfig.Enabled {
return func() {}, nil
}
projectID, ok := cloudruntime.ProjectID()
projectID, ok := cloudruntime.ResolveProjectID(ctx)
if !ok {
return nil, fmt.Errorf("start metric exporter: unknown project ID")
}
Expand Down
2 changes: 1 addition & 1 deletion cloudotel/traceexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func StartTraceExporter(
if !exporterConfig.Enabled {
return func() {}, nil
}
projectID, ok := cloudruntime.ProjectID()
projectID, ok := cloudruntime.ResolveProjectID(ctx)
if !ok {
return nil, fmt.Errorf("start trace exporter: unknown project ID")
}
Expand Down
14 changes: 10 additions & 4 deletions cloudruntime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ type Config struct {
ServiceVersion string `env:"SERVICE_VERSION"`
}

// Autodetect the runtime config.
func (c *Config) Autodetect() error {
if projectID, ok := ProjectID(); ok {
// Resolve the runtime config.
func (c *Config) Resolve(ctx context.Context) error {
if projectID, ok := ResolveProjectID(ctx); ok {
c.ProjectID = projectID
}
if serviceVersion, ok := ServiceVersion(); ok {
c.ServiceVersion = serviceVersion
}
if serviceAccount, ok := ServiceAccount(); ok {
if serviceAccount, ok := ResolveServiceAccount(ctx); ok {
c.ServiceAccount = serviceAccount
}
if service, ok := Service(); ok {
Expand Down Expand Up @@ -82,3 +82,9 @@ func (c *Config) Autodetect() error {
}
return nil
}

// Autodetect the runtime config.
// Deprecated: Use the context-based [Config.Resolve] method instead.
func (c *Config) Autodetect() error {
return c.Resolve(context.Background())
}
32 changes: 22 additions & 10 deletions cloudruntime/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,54 @@ import (
//nolint:gochecknoglobals
var (
metadataOnGCE = metadata.OnGCE
metadataProjectID = metadata.ProjectID
metadataEmail = metadata.Email
metadataProjectIDWithContext = metadata.ProjectIDWithContext
metadataEmailWithContext = metadata.EmailWithContext
googleFindDefaultCredentials = google.FindDefaultCredentials
)

// ProjectID returns the Google Cloud Project ID of the current runtime.
// Deprecated: Use the context-based [ResolveProjectID] function.
func ProjectID() (string, bool) {
return ResolveProjectID(context.Background())
}

// ResolveProjectID resolves the Google Cloud Project ID of the current runtime.
func ResolveProjectID(ctx context.Context) (string, bool) {
if !metadataOnGCE() {
if projectFromEnv, ok := os.LookupEnv("GOOGLE_CLOUD_PROJECT"); ok {
return projectFromEnv, true
}
return projectIDFromDefaultCredentials()
return projectIDFromDefaultCredentials(ctx)
}
projectID, err := metadataProjectID()
projectID, err := metadataProjectIDWithContext(ctx)
return projectID, err == nil
}

// ServiceAccount returns the default service account of the current runtime.
// Deprecated: Use the context-based [ResolveServiceAccount] function.
func ServiceAccount() (string, bool) {
return ResolveServiceAccount(context.Background())
}

// ResolveServiceAccount resolves the default service account of the current runtime.
func ResolveServiceAccount(ctx context.Context) (string, bool) {
if !metadataOnGCE() {
return serviceAccountFromDefaultCredentials()
return serviceAccountFromDefaultCredentials(ctx)
}
serviceAccount, err := metadataEmail("default")
serviceAccount, err := metadataEmailWithContext(ctx, "default")
return serviceAccount, err == nil
}

func projectIDFromDefaultCredentials() (string, bool) {
defaultCredentials, err := googleFindDefaultCredentials(context.Background())
func projectIDFromDefaultCredentials(ctx context.Context) (string, bool) {
defaultCredentials, err := googleFindDefaultCredentials(ctx)
if err != nil {
return "", false
}
return defaultCredentials.ProjectID, defaultCredentials.ProjectID != ""
}

func serviceAccountFromDefaultCredentials() (string, bool) {
defaultCredentials, err := googleFindDefaultCredentials(context.Background())
func serviceAccountFromDefaultCredentials(ctx context.Context) (string, bool) {
defaultCredentials, err := googleFindDefaultCredentials(ctx)
if err != nil || defaultCredentials.JSON == nil {
return "", false
}
Expand Down
12 changes: 6 additions & 6 deletions cloudruntime/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,23 @@ func withGoogleDefaultCredentials(t *testing.T, credentials *google.Credentials,
}

func withMetadataProjectID(t *testing.T, value string, err error) {
prev := metadataProjectID
metadataProjectID = func() (string, error) {
prev := metadataProjectIDWithContext
metadataProjectIDWithContext = func(context.Context) (string, error) {
return value, err
}
t.Cleanup(func() {
metadataProjectID = prev
metadataProjectIDWithContext = prev
})
}

func withMetadataEmail(t *testing.T, expectedServiceAccount, value string, err error) {
prev := metadataEmail
metadataEmail = func(serviceAccount string) (string, error) {
prev := metadataEmailWithContext
metadataEmailWithContext = func(_ context.Context, serviceAccount string) (string, error) {
assert.Equal(t, expectedServiceAccount, serviceAccount)
return value, err
}
t.Cleanup(func() {
metadataEmail = prev
metadataEmailWithContext = prev
})
}

Expand Down
61 changes: 31 additions & 30 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,73 @@ module go.einride.tech/cloudrunner
go 1.21

require (
cloud.google.com/go/compute/metadata v0.3.0
cloud.google.com/go/profiler v0.4.0
cloud.google.com/go/compute/metadata v0.5.0
cloud.google.com/go/profiler v0.4.1
cloud.google.com/go/pubsub v1.40.0
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.48.0
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v1.24.0
github.com/GoogleCloudPlatform/opentelemetry-operations-go/propagator v0.48.0
github.com/google/go-cmp v0.6.0
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/soheilhy/cmux v0.1.5
go.einride.tech/protobuf-sensitive v0.6.0
go.opentelemetry.io/contrib/detectors/gcp v1.27.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.52.0
go.opentelemetry.io/contrib/instrumentation/host v0.52.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.52.0
go.opentelemetry.io/contrib/instrumentation/runtime v0.52.0
go.opentelemetry.io/otel v1.27.0
go.opentelemetry.io/otel/bridge/opencensus v1.27.0
go.opentelemetry.io/otel/metric v1.27.0
go.opentelemetry.io/otel/sdk v1.27.0
go.opentelemetry.io/otel/sdk/metric v1.27.0
go.einride.tech/protobuf-sensitive v0.7.0
go.opentelemetry.io/contrib/detectors/gcp v1.28.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0
go.opentelemetry.io/contrib/instrumentation/host v0.53.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0
go.opentelemetry.io/contrib/instrumentation/runtime v0.53.0
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel/bridge/opencensus v1.28.0
go.opentelemetry.io/otel/metric v1.28.0
go.opentelemetry.io/otel/sdk v1.28.0
go.opentelemetry.io/otel/sdk/metric v1.28.0
go.uber.org/zap v1.27.0
go.uber.org/zap/exp v0.2.0
golang.org/x/net v0.26.0
golang.org/x/net v0.27.0
golang.org/x/oauth2 v0.21.0
golang.org/x/sync v0.7.0
google.golang.org/api v0.186.0
google.golang.org/genproto v0.0.0-20240617180043-68d350f18fd4
google.golang.org/grpc v1.64.1
google.golang.org/grpc/examples v0.0.0-20240521232940-48b6b11b388f
google.golang.org/api v0.188.0
google.golang.org/genproto v0.0.0-20240711142825-46eb208f015d
google.golang.org/grpc v1.65.0
google.golang.org/grpc/examples v0.0.0-20240712214741-d27ddb5eb594
google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v3 v3.0.1
gotest.tools/v3 v3.5.1
)

require (
cloud.google.com/go v0.115.0 // indirect
cloud.google.com/go/auth v0.6.0 // indirect
cloud.google.com/go/auth v0.7.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.2 // indirect
cloud.google.com/go/monitoring v1.19.0 // indirect
cloud.google.com/go/trace v1.10.7 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.23.0 // indirect
cloud.google.com/go/monitoring v1.20.1 // indirect
cloud.google.com/go/trace v1.10.9 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.24.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.48.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/pprof v0.0.0-20230602150820-91b7bce49751 // indirect
github.com/google/pprof v0.0.0-20240528025155-186aa0362fba // indirect
github.com/google/s2a-go v0.1.7 // 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.12.5 // indirect
github.com/lufia/plan9stats v0.0.0-20240513124658-fba389f38bae // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/shirou/gopsutil/v3 v3.24.4 // indirect
github.com/shirou/gopsutil/v4 v4.24.6 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240617180043-68d350f18fd4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240708141625-4ad9e859172b // indirect
)
Loading

0 comments on commit ec951f8

Please sign in to comment.