Skip to content

Commit 40e7e5a

Browse files
authored
Custom port number support (GoogleCloudPlatform#192)
Fixes GoogleCloudPlatform#185. Signed-off-by: Ahmet Alp Balkan <[email protected]>
1 parent d420e1e commit 40e7e5a

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ If you include an `app.json` at the root of your repository, it allows you
4747
customize the experience such as defining an alternative service name, or
4848
prompting for additional environment variables.
4949
50-
For example:
50+
For example, a fully populated `app.json` file looks like this:
5151
5252
```json
5353
{
@@ -68,7 +68,8 @@ For example:
6868
"options": {
6969
"allow-unauthenticated": false,
7070
"memory": "512Mi",
71-
"cpu": "1"
71+
"cpu": "1",
72+
"port": "80"
7273
},
7374
"build": {
7475
"skip": false,
@@ -117,6 +118,8 @@ Reference:
117118
- `allow-unauthenticated`: _(optional, default: `true`)_ allow unauthenticated requests
118119
- `memory`: _(optional)_ memory for each instance
119120
- `cpu`: _(optional)_ cpu for each instance
121+
- `port`: _(optional)_ if your application doesn't respect the PORT environment
122+
variable provided by Cloud Run, specify the port number it listens on.
120123
- `build`: _(optional)_ Build configuration
121124
- `skip`: _(optional, default: `false`)_ skips the built-in build methods (`docker build`, `Maven Jib`, and
122125
`buildpacks`), but still allows for `prebuild` and `postbuild` hooks to be run in order to build the container image

cmd/cloudshell_open/appfile.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,19 @@ type options struct {
3939
AllowUnauthenticated *bool `json:"allow-unauthenticated"`
4040
Memory string `json:"memory"`
4141
CPU string `json:"cpu"`
42+
Port int `json:"port"`
4243
}
4344

4445
type hook struct {
4546
Commands []string `json:"commands"`
4647
}
4748

4849
type buildpacks struct {
49-
Builder string `json:"builder"`
50+
Builder string `json:"builder"`
5051
}
5152

5253
type build struct {
53-
Skip *bool `json:"skip"`
54+
Skip *bool `json:"skip"`
5455
Buildpacks buildpacks `json:"buildpacks"`
5556
}
5657

cmd/cloudshell_open/deploy.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ func newService(name, project, image string, envs map[string]string, options opt
106106
Spec: &runapi.RevisionSpec{
107107
Containers: []*runapi.Container{
108108
{
109-
Image: image,
110-
Env: envVars,
109+
Image: image,
110+
Env: envVars,
111111
Resources: optionsToResourceRequirements(options),
112112
},
113113
},
@@ -117,6 +117,10 @@ func newService(name, project, image string, envs map[string]string, options opt
117117
},
118118
},
119119
}
120+
if options.Port > 0 {
121+
svc.Spec.Template.Spec.Containers[0].Ports = append(svc.Spec.Template.Spec.Containers[0].Ports,
122+
&runapi.ContainerPort{ContainerPort: int64(options.Port)})
123+
}
120124
applyMeta(svc.Metadata, image)
121125
applyMeta(svc.Spec.Template.Metadata, image)
122126

cmd/cloudshell_open/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,10 @@ func run(opts runOpts) error {
369369
cmdColor.Printf("\t --region=%s", parameter(region))
370370
cmdColor.Println("\\")
371371
cmdColor.Printf("\t --image=%s", parameter(image))
372-
372+
if appFile.Options.Port > 0 {
373+
cmdColor.Println("\\")
374+
cmdColor.Printf("\t --port=%s", parameter(fmt.Sprintf("%d", appFile.Options.Port)))
375+
}
373376
if len(envs) > 0 {
374377
cmdColor.Println("\\")
375378
cmdColor.Printf("\t --update-env-vars=%s", parameter(strings.Join(envs, ",")))

0 commit comments

Comments
 (0)