Skip to content

Conversation

@grayside
Copy link
Contributor

@grayside grayside commented Sep 5, 2019

First step toward #1686

Proposed Changes

  • Upgrades from go 1.12 to go 1.13
  • Upgrades to use go modules
  • Pins alpine image to 3.10
  • Tweaks working directory to /app
  • Rename binary in container from helloworld to server
  • Use go mod download to facilitate a layer cache of go deps.
  • Use -mod=readonly flag on builds for strictness of container builds
  • Some minor style changes around syntax and comments

@googlebot googlebot added the cla: yes Indicates the PR's author has signed the CLA. label Sep 5, 2019
@knative-prow-robot knative-prow-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Sep 5, 2019
@grayside
Copy link
Contributor Author

grayside commented Sep 5, 2019

/assign @ahmetb

@grayside
Copy link
Contributor Author

grayside commented Sep 5, 2019

/test pull-knative-docs-unit-tests

Copy link
Contributor

@ahmetb ahmetb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Some nits.

# This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang
FROM golang:1.12 as builder
FROM golang:1.13 as builder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason not to use golang:1?

We have to keep updating this every 6 months otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that sometimes the minor version has significant change to best practices (e.g., modules) I think the clarity is worthwhile for the reader and motivation to update.

@knative-prow-robot knative-prow-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Sep 6, 2019
WORKDIR /go/src/github.com/knative/docs/helloworld
COPY . .
WORKDIR /app
COPY . ./
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should do the go mod download trick, since many people will copy this? I know it's been decided on in the past, but consider adding it.

(I'm not attached to adding it - definitely makes things more complex, but it might be worth it.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added go mod download and modified go build to use -mod=readonly for strictness of the build.

@knative-prow-robot knative-prow-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 11, 2019
@knative-prow-robot knative-prow-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 11, 2019
@grayside
Copy link
Contributor Author

/test pull-knative-docs-integration-tests

@grayside
Copy link
Contributor Author

Looking at the error, I see:

            Step 3/10 : COPY go.* ./
            COPY failed: no source files were specified

However, there is plainly a go.mod file in this sample and it builds locally just fine. Is it possible the test rig itself is skipping a file or missing the addition of go.mod somehow?

@chaodaiG could you take a look?

@chaodaiG
Copy link
Contributor

let me TAL

@chizhg
Copy link
Contributor

chizhg commented Sep 11, 2019

Looking at the error, I see:

            Step 3/10 : COPY go.* ./
            COPY failed: no source files were specified

However, there is plainly a go.mod file in this sample and it builds locally just fine. Is it possible the test rig itself is skipping a file or missing the addition of go.mod somehow?

@chaodaiG could you take a look?

@grayside you'll need to add go.mod in

- "helloworld.go"

Also as I tried in #1787, you'll also need to remove -mod=readonly from the go build command.

@chaodaiG
Copy link
Contributor

Thank you @Fredy-Z for looking into this!

@grayside
Copy link
Contributor Author

Thanks @Fredy-Z,

I want to make sure I understand what's happening here.

  1. Why can't I use -mod=readonly? That seems like it should be the best practice in a container, especially where the deps are downloaded via go mod download? In this case, the test framework appears to be constraining the implementation of the app in unexpected ways.
  2. Why doesn't the test process copy all the files, perhaps excluding just .dockerignore? In this case, the test framework seems to be defining unexpected conventions.

@@ -0,0 +1,3 @@
module github.com/knative/docs/docs/serving/samples/hello-world/helloworld-go

go 1.13
Copy link
Contributor

@chizhg chizhg Sep 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an empty line after this line. Otherwise go build will not work with -mod=readonly.

@chizhg
Copy link
Contributor

chizhg commented Sep 16, 2019

Thanks @Fredy-Z,

I want to make sure I understand what's happening here.

  1. Why can't I use -mod=readonly? That seems like it should be the best practice in a container, especially where the deps are downloaded via go mod download? In this case, the test framework appears to be constraining the implementation of the app in unexpected ways.
  2. Why doesn't the test process copy all the files, perhaps excluding just .dockerignore? In this case, the test framework seems to be defining unexpected conventions.

@grayside sorry for the late reply.

For 1, I double checked your PR, this reason that you cannot have -mod=readonly is because the go.mod file is not ended with an empty line, see #1774 (comment).
For 2, I'm not sure what's the historic reason for only copying specific files, to me it's totally fine to just copy all files. @chaodaiG can you take a look?

@chaodaiG
Copy link
Contributor

Thanks @Fredy-Z,
I want to make sure I understand what's happening here.

  1. Why can't I use -mod=readonly? That seems like it should be the best practice in a container, especially where the deps are downloaded via go mod download? In this case, the test framework appears to be constraining the implementation of the app in unexpected ways.
  2. Why doesn't the test process copy all the files, perhaps excluding just .dockerignore? In this case, the test framework seems to be defining unexpected conventions.

@grayside sorry for the late reply.

For 1, I double checked your PR, this reason that you cannot have -mod=readonly is because the go.mod file is not ended with an empty line, see #1774 (comment).
For 2, I'm not sure what's the historic reason for only copying specific files, to me it's totally fine to just copy all files. @chaodaiG can you take a look?

A piece of background, I have observed that all files needed for tests are already inlined in README.md files, so there is a unit test verifying the consistency between committed code and README, and the unit test goes through all files needs to be copied and verify if they are identical with README.md. See test here:

for _, f := range lc.Copies {

@grayside
Copy link
Contributor Author

For -mod=readonly, the container builds fine for me locally as-is, not sure what I'd change there. Why is go build in the container different in CI here?

For file-copy: It makes sense to me that we test that files we include in the README are consistent with source code, but that should not determine what is copied. For example, there are lockfiles such as package-lock.json for Node.js which need to be included in the build.

@grayside
Copy link
Contributor Author

Updated to add the go.mod to the test config, and updated the README to add an instruction on how to generate go.mod. Note that I am not copying the go.mod file into the README.

@chaodaiG
Copy link
Contributor

For -mod=readonly, the container builds fine for me locally as-is, not sure what I'd change there. Why is go build in the container different in CI here?

For file-copy: It makes sense to me that we test that files we include in the README are consistent with source code, but that should not determine what is copied. For example, there are lockfiles such as package-lock.json for Node.js which need to be included in the build.

For file-copy, I'm fine with any change to make it smarter. One thing to keep in mind is, currently it's very tightly enforced to make sure all files needed for compilation are inspected in unit+e2e tests, if we keep separated list of what's copied and what's verified, there is a risk of these 2 lists diverged. So for any example I would recommend inline the content in README, unless it doesn't make sense at all. And in the latter case there is a way to get around it(not highly encouraged though) by running custom cp command, see example for Spring here:

preCommands:

@grayside
Copy link
Contributor Author

It looks like the test is now passing, which tells me that at least some of what we were discussing was a misunderstanding that the tests require inlining all files.

@chaodaiG
Copy link
Contributor

that would be a bug if that's the case. We can have this PR pass and I'll TAL

@chaodaiG
Copy link
Contributor

/approve

@grayside
Copy link
Contributor Author

@chaodaiG there are files that are required for the build but are not and should not be inlined in the READMEs, for example, package-lock.json for Node.js. If the build leaves these out, they may pass but the result will be non-deterministic.

@chaodaiG
Copy link
Contributor

I agree that there are certain exceptions like in package.json case, which as I mentioned earlier can get exempted by adding a manual cp step in preCommand.

@samodell
Copy link
Contributor

/approve
/lgtm

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Sep 17, 2019
@knative-prow-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: chaodaiG, grayside, samodell, tbpg

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow-robot knative-prow-robot merged commit 61d24a1 into knative:master Sep 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes Indicates the PR's author has signed the CLA. lgtm Indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants