Skip to content

Commit 81bc837

Browse files
graysideknative-prow-robot
authored andcommitted
helloworld: go clarity tweaks and go modules for shell (#1908)
* helloworld: go clarity tweaks and go modules for shell * helloworld: go and shell README and comment alignment
1 parent e2ee73d commit 81bc837

File tree

8 files changed

+95
-56
lines changed

8 files changed

+95
-56
lines changed

docs/serving/samples/hello-world/helloworld-go/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ FROM golang:1.13 as builder
66
# Create and change to the app directory.
77
WORKDIR /app
88

9-
# Retrieve application dependencies.
10-
# This allows the container build to reuse cached dependencies.
9+
# Retrieve application dependencies using go modules.
10+
# Allows container builds to reuse downloaded dependencies.
1111
COPY go.* ./
1212
RUN go mod download
1313

1414
# Copy local code to the container image.
1515
COPY . ./
1616

1717
# Build the binary.
18+
# -mod=readonly ensures immutable go.mod and go.sum in container builds.
1819
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
1920

2021
# Use the official Alpine image for a lean production container.

docs/serving/samples/hello-world/helloworld-go/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go
4242
)
4343

4444
func handler(w http.ResponseWriter, r *http.Request) {
45-
log.Print("Hello world received a request.")
45+
log.Print("helloworld: received a request")
4646
target := os.Getenv("TARGET")
4747
if target == "" {
4848
target = "World"
@@ -51,7 +51,7 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go
5151
}
5252

5353
func main() {
54-
log.Print("Hello world sample started.")
54+
log.Print("helloworld: starting server...")
5555

5656
http.HandleFunc("/", handler)
5757

@@ -60,6 +60,7 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go
6060
port = "8080"
6161
}
6262

63+
log.Printf("helloworld: listening on port %s", port)
6364
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
6465
}
6566
```
@@ -77,15 +78,16 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-go
7778
# Create and change to the app directory.
7879
WORKDIR /app
7980
80-
# Retrieve application dependencies.
81-
# This allows the container build to reuse cached dependencies.
81+
# Retrieve application dependencies using go modules.
82+
# Allows container builds to reuse downloaded dependencies.
8283
COPY go.* ./
8384
RUN go mod download
8485
8586
# Copy local code to the container image.
8687
COPY . ./
8788
8889
# Build the binary.
90+
# -mod=readonly ensures immutable go.mod and go.sum in container builds.
8991
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
9092
9193
# Use the official Alpine image for a lean production container.

docs/serving/samples/hello-world/helloworld-go/helloworld.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func handler(w http.ResponseWriter, r *http.Request) {
11-
log.Print("Hello world received a request.")
11+
log.Print("helloworld: received a request")
1212
target := os.Getenv("TARGET")
1313
if target == "" {
1414
target = "World"
@@ -17,7 +17,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
1717
}
1818

1919
func main() {
20-
log.Print("Hello world sample started.")
20+
log.Print("helloworld: starting server...")
2121

2222
http.HandleFunc("/", handler)
2323

@@ -26,5 +26,6 @@ func main() {
2626
port = "8080"
2727
}
2828

29+
log.Printf("helloworld: listening on port %s", port)
2930
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
3031
}
Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1-
# Use the offical Golang image to create a build artifact.
1+
# Use the official Golang image to create a build artifact.
22
# This is based on Debian and sets the GOPATH to /go.
33
# https://hub.docker.com/_/golang
4-
FROM golang:1.12 as builder
4+
FROM golang:1.13 as builder
5+
6+
# Create and change to the app directory.
7+
WORKDIR /app
8+
9+
# Retrieve application dependencies using go modules.
10+
# Allows container builds to reuse downloaded dependencies.
11+
COPY go.* ./
12+
RUN go mod download
513

614
# Copy local code to the container image.
7-
WORKDIR /go/src/github.com/knative/docs/helloworld-shell
8-
COPY invoke.go .
15+
COPY invoke.go ./
916

10-
# Build the command inside the container.
11-
# (You may fetch or manage dependencies here,
12-
# either manually or with a tool like "godep".)
13-
RUN CGO_ENABLED=0 GOOS=linux go build -v -o invoke
17+
# Build the binary.
18+
# -mod=readonly ensures immutable go.mod and go.sum in container builds.
19+
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
1420

15-
# The official Alpine base image
21+
# Use the official Alpine image for a lean production container.
1622
# https://hub.docker.com/_/alpine
17-
# Use a Docker multi-stage build to create a lean production image.
1823
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
19-
FROM alpine:3.10
24+
FROM alpine:3
25+
RUN apk add --no-cache ca-certificates
2026

21-
# Copy Go binary
22-
COPY --from=builder /go/src/github.com/knative/docs/helloworld-shell/invoke /invoke
23-
COPY script.sh .
27+
# Copy the binary to the production image from the builder stage.
28+
COPY --from=builder /app/server /server
29+
COPY script.sh ./
2430

2531
# Run the web service on container startup.
26-
CMD ["/invoke"]
32+
CMD ["/server"]

docs/serving/samples/hello-world/helloworld-shell/README.md

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,64 +42,75 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-shell
4242
package main
4343

4444
import (
45-
"fmt"
46-
"log"
47-
"net/http"
48-
"os"
49-
"os/exec"
45+
"fmt"
46+
"log"
47+
"net/http"
48+
"os"
49+
"os/exec"
5050
)
5151

5252
func handler(w http.ResponseWriter, r *http.Request) {
53-
cmd := exec.CommandContext(r.Context(), "/bin/sh", "script.sh")
54-
cmd.Stderr = os.Stderr
55-
out, err := cmd.Output()
56-
if err != nil {
57-
w.WriteHeader(500)
58-
}
59-
w.Write(out)
53+
log.Print("helloworld: received a request")
54+
55+
cmd := exec.CommandContext(r.Context(), "/bin/sh", "script.sh")
56+
cmd.Stderr = os.Stderr
57+
out, err := cmd.Output()
58+
if err != nil {
59+
w.WriteHeader(500)
60+
}
61+
w.Write(out)
6062
}
6163

6264
func main() {
63-
http.HandleFunc("/", handler)
65+
log.Print("helloworld: starting server...")
6466

65-
port := os.Getenv("PORT")
66-
if port == "" {
67-
port = "8080"
68-
}
67+
http.HandleFunc("/", handler)
6968

70-
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
69+
port := os.Getenv("PORT")
70+
if port == "" {
71+
port = "8080"
72+
}
73+
74+
log.Printf("helloworld: listening on %s", port)
75+
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
7176
}
7277
```
7378

7479
1. Create a new file named `Dockerfile` and copy the code block below into it.
7580

7681
```docker
77-
# Use the offical Golang image to create a build artifact.
82+
# Use the official Golang image to create a build artifact.
7883
# This is based on Debian and sets the GOPATH to /go.
7984
# https://hub.docker.com/_/golang
80-
FROM golang:1.12 as builder
85+
FROM golang:1.13 as builder
86+
87+
# Create and change to the app directory.
88+
WORKDIR /app
89+
90+
# Retrieve application dependencies using go modules.
91+
# Allows container builds to reuse downloaded dependencies.
92+
COPY go.* ./
93+
RUN go mod download
8194
8295
# Copy local code to the container image.
83-
WORKDIR /go/src/github.com/knative/docs/helloworld-shell
84-
COPY invoke.go .
96+
COPY invoke.go ./
8597
86-
# Build the command inside the container.
87-
# (You may fetch or manage dependencies here,
88-
# either manually or with a tool like "godep".)
89-
RUN CGO_ENABLED=0 GOOS=linux go build -v -o invoke
98+
# Build the binary.
99+
# -mod=readonly ensures immutable go.mod and go.sum in container builds.
100+
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
90101
91-
# The official Alpine base image
102+
# Use the official Alpine image for a lean production container.
92103
# https://hub.docker.com/_/alpine
93-
# Use a Docker multi-stage build to create a lean production image.
94104
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
95-
FROM alpine:3.10
105+
FROM alpine:3
106+
RUN apk add --no-cache ca-certificates
96107
97-
# Copy Go binary
98-
COPY --from=builder /go/src/github.com/knative/docs/helloworld-shell/invoke /invoke
99-
COPY script.sh .
108+
# Copy the binary to the production image from the builder stage.
109+
COPY --from=builder /app/server /server
110+
COPY script.sh ./
100111
101112
# Run the web service on container startup.
102-
CMD ["/invoke"]
113+
CMD ["/server"]
103114
```
104115

105116
1. Create a new file, `service.yaml` and copy the following service definition
@@ -122,6 +133,13 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-shell
122133
value: "Shell Sample v1"
123134
```
124135
136+
1. Use the go tool to create a
137+
[`go.mod`](https://github.com/golang/go/wiki/Modules#gomod) manifest.
138+
139+
```shell
140+
go mod init github.com/knative/docs/docs/serving/samples/hello-world/helloworld-shell
141+
```
142+
125143
## Building and deploying the sample
126144

127145
Once you have recreated the sample code files (or used the files in the sample
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/knative/docs/docs/serving/samples/hello-world/helloworld-shell
2+
3+
go 1.13

docs/serving/samples/hello-world/helloworld-shell/invoke.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
)
1010

1111
func handler(w http.ResponseWriter, r *http.Request) {
12+
log.Print("helloworld: received a request")
13+
1214
cmd := exec.CommandContext(r.Context(), "/bin/sh", "script.sh")
1315
cmd.Stderr = os.Stderr
1416
out, err := cmd.Output()
@@ -19,12 +21,15 @@ func handler(w http.ResponseWriter, r *http.Request) {
1921
}
2022

2123
func main() {
24+
log.Print("helloworld: starting server...")
25+
2226
http.HandleFunc("/", handler)
2327

2428
port := os.Getenv("PORT")
2529
if port == "" {
2630
port = "8080"
2731
}
2832

33+
log.Printf("helloworld: listening on %s", port)
2934
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
3035
}

test/sampleapp/config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ languages:
5858
- "Dockerfile"
5959
- language: "shell"
6060
expectedOutput: "Hello Shell Sample v1!"
61+
preCommands:
62+
- exec: "cp"
63+
args: "../../docs/serving/samples/hello-world/helloworld-shell/go.mod helloworld-shell_tmp/go.mod"
6164
copies:
6265
- "script.sh"
6366
- "invoke.go"

0 commit comments

Comments
 (0)