Skip to content

Commit

Permalink
Start migrating EnvVariables to Env, with visibility always set to R…
Browse files Browse the repository at this point in the history
…UNTIME

PiperOrigin-RevId: 615880840
Change-Id: Ibf5406d6d92a7881d9ce542b8d465b7f3819b6d8
  • Loading branch information
GCP Buildpacks Team authored and copybara-github committed Mar 14, 2024
1 parent 17209c1 commit 990be6f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 49 deletions.
2 changes: 2 additions & 0 deletions pkg/firebase/publisher/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ go_test(
deps = [
"//pkg/firebase/apphostingschema",
"//pkg/testdata",
"//third_party/golang/protobuf/v2/proto",
"@com_github_google_go-cmp//cmp:go_default_library",
"@com_github_google_go-cmp//cmp:go_default_library/cmpopts",
"@in_gopkg_yaml_v2//:go_default_library",
],
)
19 changes: 15 additions & 4 deletions pkg/firebase/publisher/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ type outputBundleSchema struct {
// buildSchema is the internal Publisher representation of the final build settings that will
// ultimately be converted into an updateBuildRequest.
type buildSchema struct {
RunConfig *apphostingschema.RunConfig `yaml:"runConfig,omitempty"`
Runtime *runtime `yaml:"runtime,omitempty"`
RunConfig *apphostingschema.RunConfig `yaml:"runConfig,omitempty"`
Runtime *runtime `yaml:"runtime,omitempty"`
Env []apphostingschema.EnvironmentVariable `yaml:"env,omitempty"`
}

// TODO (b/328444933): Migrate this to the new EnvironmentVariable in apphostingschema.go
Expand Down Expand Up @@ -105,7 +106,7 @@ func writeToFile(buildSchema buildSchema, outputFilePath string) error {
return nil
}

func toBuildSchema(appHostingSchema apphostingschema.AppHostingSchema, bundleSchema outputBundleSchema, appHostingEnvVars map[string]string) buildSchema {
func toBuildSchema(schema apphostingschema.AppHostingSchema, bundleSchema outputBundleSchema, appHostingEnvVars map[string]string) buildSchema {
dCPU := float32(defaultCPU)
buildSchema := buildSchema{
RunConfig: &apphostingschema.RunConfig{
Expand All @@ -117,7 +118,7 @@ func toBuildSchema(appHostingSchema apphostingschema.AppHostingSchema, bundleSch
},
}
// Copy fields from apphosting.yaml.
b := appHostingSchema.RunConfig
b := schema.RunConfig
if b.CPU != nil {
cpu := float32(*b.CPU)
buildSchema.RunConfig.CPU = &cpu
Expand All @@ -138,6 +139,16 @@ func toBuildSchema(appHostingSchema apphostingschema.AppHostingSchema, bundleSch
// Copy fields from apphosting.env.
if len(appHostingEnvVars) > 0 {
buildSchema.Runtime = &runtime{EnvVariables: appHostingEnvVars}
envVars := []apphostingschema.EnvironmentVariable{}
for k, v := range appHostingEnvVars {
ev := apphostingschema.EnvironmentVariable{
Variable: k,
Value: v,
Availability: []string{"RUNTIME"},
}
envVars = append(envVars, ev)
}
buildSchema.Env = envVars
}
return buildSchema
}
Expand Down
115 changes: 70 additions & 45 deletions pkg/firebase/publisher/publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (

"github.com/GoogleCloudPlatform/buildpacks/pkg/testdata"
"github.com/google/go-cmp/cmp"
"google3/third_party/golang/cmp/cmpopts/cmpopts"
"google3/third_party/golang/protobuf/v2/proto/proto"
"gopkg.in/yaml.v2"

apphostingschema "github.com/GoogleCloudPlatform/buildpacks/pkg/firebase/apphostingschema"
Expand All @@ -33,18 +35,6 @@ var (
bundleYAMLPath string = testdata.MustGetPath("testdata/bundle.yaml")
)

func int32Ptr(i int) *int32 {
v := new(int32)
*v = int32(i)
return v
}

func float32Ptr(i int32) *float32 {
v := new(float32)
*v = float32(i)
return v
}

func toString(buildSchema buildSchema) string {
data, _ := json.MarshalIndent(buildSchema, "", " ")
return string(data)
Expand All @@ -64,11 +54,28 @@ func TestPublish(t *testing.T) {
envFilePath: envPath,
wantBuildSchema: buildSchema{
RunConfig: &apphostingschema.RunConfig{
CPU: float32Ptr(3),
MemoryMiB: int32Ptr(1024),
Concurrency: int32Ptr(100),
MaxInstances: int32Ptr(4),
MinInstances: int32Ptr(0),
CPU: proto.Float32(3),
MemoryMiB: proto.Int32(1024),
Concurrency: proto.Int32(100),
MaxInstances: proto.Int32(4),
MinInstances: proto.Int32(0),
},
Env: []apphostingschema.EnvironmentVariable{
apphostingschema.EnvironmentVariable{
Variable: "API_URL",
Value: "api.service.com",
Availability: []string{"RUNTIME"},
},
apphostingschema.EnvironmentVariable{
Variable: "ENVIRONMENT",
Value: "staging",
Availability: []string{"RUNTIME"},
},
apphostingschema.EnvironmentVariable{
Variable: "MULTILINE_ENV_VAR",
Value: "line 1\nline 2",
Availability: []string{"RUNTIME"},
},
},
Runtime: &runtime{
EnvVariables: map[string]string{
Expand All @@ -84,11 +91,28 @@ func TestPublish(t *testing.T) {
envFilePath: envPath,
wantBuildSchema: buildSchema{
RunConfig: &apphostingschema.RunConfig{
CPU: float32Ptr(1),
MemoryMiB: int32Ptr(512),
Concurrency: int32Ptr(80),
MaxInstances: int32Ptr(100),
MinInstances: int32Ptr(0),
CPU: proto.Float32(1),
MemoryMiB: proto.Int32(512),
Concurrency: proto.Int32(80),
MaxInstances: proto.Int32(100),
MinInstances: proto.Int32(0),
},
Env: []apphostingschema.EnvironmentVariable{
apphostingschema.EnvironmentVariable{
Variable: "ENVIRONMENT",
Value: "staging",
Availability: []string{"RUNTIME"},
},
apphostingschema.EnvironmentVariable{
Variable: "MULTILINE_ENV_VAR",
Value: "line 1\nline 2",
Availability: []string{"RUNTIME"},
},
apphostingschema.EnvironmentVariable{
Variable: "API_URL",
Value: "api.service.com",
Availability: []string{"RUNTIME"},
},
},
Runtime: &runtime{
EnvVariables: map[string]string{
Expand All @@ -104,11 +128,11 @@ func TestPublish(t *testing.T) {
envFilePath: "nonexistent",
wantBuildSchema: buildSchema{
RunConfig: &apphostingschema.RunConfig{
CPU: float32Ptr(3),
MemoryMiB: int32Ptr(1024),
Concurrency: int32Ptr(100),
MaxInstances: int32Ptr(4),
MinInstances: int32Ptr(0),
CPU: proto.Float32(3),
MemoryMiB: proto.Int32(1024),
Concurrency: proto.Int32(100),
MaxInstances: proto.Int32(4),
MinInstances: proto.Int32(0),
},
}},
}
Expand All @@ -134,7 +158,8 @@ func TestPublish(t *testing.T) {
t.Errorf("error unmarshalling %q as YAML: %v", actualBuildSchemaData, err)
}

if diff := cmp.Diff(test.wantBuildSchema, actualBuildSchema); diff != "" {
sort := cmpopts.SortSlices(func(a, b apphostingschema.EnvironmentVariable) bool { return a.Variable < b.Variable })
if diff := cmp.Diff(test.wantBuildSchema, actualBuildSchema, sort); diff != "" {
t.Errorf("Unexpected YAML for test %v (+got, -want):\n%v", test.desc, diff)
}
}
Expand All @@ -151,50 +176,50 @@ func TestToBuildSchemaRunConfig(t *testing.T) {
appHostingSchema: apphostingschema.AppHostingSchema{},
expected: buildSchema{
RunConfig: &apphostingschema.RunConfig{
CPU: float32Ptr(defaultCPU),
CPU: proto.Float32(float32(defaultCPU)),
MemoryMiB: &defaultMemory,
Concurrency: &defaultConcurrency,
MaxInstances: &defaultMaxInstances,
MinInstances: int32Ptr(0),
MinInstances: proto.Int32(0),
},
},
},
{
name: "Full AppHostingSchema",
appHostingSchema: apphostingschema.AppHostingSchema{
RunConfig: apphostingschema.RunConfig{
CPU: float32Ptr(1000),
MemoryMiB: int32Ptr(2048),
Concurrency: int32Ptr(2),
MaxInstances: int32Ptr(5),
MinInstances: int32Ptr(0),
CPU: proto.Float32(1000),
MemoryMiB: proto.Int32(2048),
Concurrency: proto.Int32(2),
MaxInstances: proto.Int32(5),
MinInstances: proto.Int32(0),
},
},
expected: buildSchema{
RunConfig: &apphostingschema.RunConfig{
CPU: float32Ptr(1000),
MemoryMiB: int32Ptr(2048),
Concurrency: int32Ptr(2),
MaxInstances: int32Ptr(5),
MinInstances: int32Ptr(0),
CPU: proto.Float32(1000),
MemoryMiB: proto.Int32(2048),
Concurrency: proto.Int32(2),
MaxInstances: proto.Int32(5),
MinInstances: proto.Int32(0),
},
},
},
{
name: "Partial AppHostingSchema",
appHostingSchema: apphostingschema.AppHostingSchema{
RunConfig: apphostingschema.RunConfig{
CPU: float32Ptr(1000),
Concurrency: int32Ptr(2),
CPU: proto.Float32(1000),
Concurrency: proto.Int32(2),
},
},
expected: buildSchema{
RunConfig: &apphostingschema.RunConfig{
CPU: float32Ptr(1000),
CPU: proto.Float32(1000),
MemoryMiB: &defaultMemory,
Concurrency: int32Ptr(2),
Concurrency: proto.Int32(2),
MaxInstances: &defaultMaxInstances,
MinInstances: int32Ptr(0),
MinInstances: proto.Int32(0),
},
},
},
Expand Down

0 comments on commit 990be6f

Please sign in to comment.