Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a new static mode #76

Merged
merged 1 commit into from
Aug 13, 2019
Merged

Conversation

matipan
Copy link
Contributor

@matipan matipan commented Aug 10, 2019

This mode allows the users to use the watchdog as a static server for
serving static files. The added benefit over a plain file server is that
they get the RED metrics already implemented by the watchdog.

Signed-off-by: Matias Pan matias.pan26@gmail.com

Description

A new mode was added called static. When the users set this mode they must also set the static_path variable that will be the directory the user wants to serve. If none is set we default to /home/app/public.

I also changed the signature of the config.New function, it was returning an error but in the function we were never actually returning an error. I updated the tests with this new change.

Closes #74

Motivation and Context

  • I have raised an issue to propose this change (required)
    When creating the hugo template we noticed that instead of depending on an external static server we could implement this functionality in the watchdog.

How Has This Been Tested?

After making the changes I built the binary and tweaked the hugo template for using the watchdog instead of the static server. I then built my personal blog that uses this template and served it locally with:

$ docker run -p 8080:8080 -it matipan/blog-watchdog:debug
2019/08/10 23:44:50 Serving files at /home/server/public
2019/08/10 23:44:50 OperationalMode: static
2019/08/10 23:44:50 Timeouts: read: 10s, write: 10s hard: 10s.
2019/08/10 23:44:50 Listening on port: 8080
2019/08/10 23:44:50 Writing lock-file to: /tmp/.lock
2019/08/10 23:44:50 Metrics listening on port: 8081

You can see that the logs indicate we are making use of the new static mode. I then went to http://localhost:8080 and saw that the website was being properly rendered:
of-watchdog

I also executed ./build.sh to make sure all of the binaries are being properly built:

Output of build.sh:
$ ./build.sh 
[+] Building 27.2s (17/17) FINISHED                                                                                                                                                                                
 => [internal] load .dockerignore                                                                                                                                                                             0.0s
 => => transferring context: 2B                                                                                                                                                                               0.0s
 => [internal] load build definition from Dockerfile                                                                                                                                                          0.0s
 => => transferring dockerfile: 1.11kB                                                                                                                                                                        0.0s
 => [internal] load metadata for docker.io/library/golang:1.10                                                                                                                                                2.2s
 => [1/12] FROM docker.io/library/golang:1.10@sha256:6d5e79878a3e4f1b30b7aa4d24fb6ee6184e905a9b172fc72593935633be4c46                                                                                         0.0s
 => [internal] load build context                                                                                                                                                                             0.0s
 => => transferring context: 1.01MB                                                                                                                                                                           0.0s
 => CACHED [2/12] RUN mkdir -p /go/src/github.com/openfaas-incubator/of-watchdog                                                                                                                              0.0s
 => CACHED [3/12] WORKDIR /go/src/github.com/openfaas-incubator/of-watchdog                                                                                                                                   0.0s
 => CACHED [4/12] COPY vendor              vendor                                                                                                                                                             0.0s
 => [5/12] COPY config              config                                                                                                                                                                    0.0s
 => [6/12] COPY executor            executor                                                                                                                                                                  0.0s
 => [7/12] COPY metrics             metrics                                                                                                                                                                   0.0s
 => [8/12] COPY metrics             metrics                                                                                                                                                                   0.0s
 => [9/12] COPY main.go             .                                                                                                                                                                         0.0s
 => [10/12] RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))"                                                                                                                  0.4s
 => [11/12] RUN go test -v ./...                                                                                                                                                                              2.3s
 => [12/12] RUN CGO_ENABLED=0 GOOS=linux go build -a -ldflags "-s -w" -installsuffix cgo -o of-watchdog .     && CGO_ENABLED=0 GOOS=darwin go build -a -ldflags "-s -w" -installsuffix cgo -o of-watchdog-d  21.2s
 => exporting to image                                                                                                                                                                                        0.8s 
 => => exporting layers                                                                                                                                                                                       0.8s 
 => => writing image sha256:47d8f0eb00de161216117c6c4b252c7fa9aff831cf7e14717097f410bd9e2970                                                                                                                  0.0s 
 => => naming to docker.io/openfaas/of-watchdog:build                                                                                                                                                         0.0s 
[+] Building 0.2s (6/6) FINISHED                                                                                                                                                                                   
 => [internal] load build definition from Dockerfile.packager                                                                                                                                                 0.0s
 => => transferring dockerfile: 223B                                                                                                                                                                          0.0s
 => [internal] load .dockerignore                                                                                                                                                                             0.0s
 => => transferring context: 2B                                                                                                                                                                               0.0s
 => [internal] load metadata for docker.io/openfaas/of-watchdog:build                                                                                                                                         0.0s
 => [build 1/1] FROM docker.io/openfaas/of-watchdog:build                                                                                                                                                     0.0s
 => => resolve docker.io/openfaas/of-watchdog:build                                                                                                                                                           0.0s
 => [stage-1 1/1] COPY --from=build /go/src/github.com/openfaas-incubator/of-watchdog/of-watchdog ./fwatchdog                                                                                                 0.0s
 => exporting to image                                                                                                                                                                                        0.1s
 => => exporting layers                                                                                                                                                                                       0.1s
 => => writing image sha256:d371050872a22e51571683749fb3c213091ab6bb2b02def46680509d20af3da2                                                                                                                  0.0s
 => => naming to docker.io/openfaas/of-watchdog:latest-dev-darwin                                                                                                                                             0.0s
[+] Building 0.1s (6/6) FINISHED                                                                                                                                                                                   
 => [internal] load .dockerignore                                                                                                                                                                             0.0s
 => => transferring context: 2B                                                                                                                                                                               0.0s
 => [internal] load build definition from Dockerfile.packager                                                                                                                                                 0.0s
 => => transferring dockerfile: 47B                                                                                                                                                                           0.0s
 => [internal] load metadata for docker.io/openfaas/of-watchdog:build                                                                                                                                         0.0s
 => CACHED [build 1/1] FROM docker.io/openfaas/of-watchdog:build                                                                                                                                              0.0s
 => [stage-1 1/1] COPY --from=build /go/src/github.com/openfaas-incubator/of-watchdog/of-watchdog ./fwatchdog                                                                                                 0.0s
 => exporting to image                                                                                                                                                                                        0.0s
 => => exporting layers                                                                                                                                                                                       0.0s
 => => writing image sha256:d5620b3ac0dfea9df2f38d1b51b0a0d6210548d18cb576bbe021ccd63fefa945                                                                                                                  0.0s
 => => naming to docker.io/openfaas/of-watchdog:latest-dev-armhf                                                                                                                                              0.0s
[+] Building 0.2s (6/6) FINISHED                                                                                                                                                                                   
 => [internal] load build definition from Dockerfile.packager                                                                                                                                                 0.0s
 => => transferring dockerfile: 47B                                                                                                                                                                           0.0s
 => [internal] load .dockerignore                                                                                                                                                                             0.0s
 => => transferring context: 2B                                                                                                                                                                               0.0s
 => [internal] load metadata for docker.io/openfaas/of-watchdog:build                                                                                                                                         0.0s
 => CACHED [build 1/1] FROM docker.io/openfaas/of-watchdog:build                                                                                                                                              0.0s
 => [stage-1 1/1] COPY --from=build /go/src/github.com/openfaas-incubator/of-watchdog/of-watchdog ./fwatchdog                                                                                                 0.0s
 => exporting to image                                                                                                                                                                                        0.1s
 => => exporting layers                                                                                                                                                                                       0.1s
 => => writing image sha256:b6f1615845a8b4c669efe8d0c71c978120d8a374770b1453d0ac351fde42f01d                                                                                                                  0.0s
 => => naming to docker.io/openfaas/of-watchdog:latest-dev-arm64                                                                                                                                              0.0s
[+] Building 0.2s (6/6) FINISHED                                                                                                                                                                                   
 => [internal] load build definition from Dockerfile.packager                                                                                                                                                 0.0s
 => => transferring dockerfile: 47B                                                                                                                                                                           0.0s
 => [internal] load .dockerignore                                                                                                                                                                             0.0s
 => => transferring context: 2B                                                                                                                                                                               0.0s
 => [internal] load metadata for docker.io/openfaas/of-watchdog:build                                                                                                                                         0.0s
 => CACHED [build 1/1] FROM docker.io/openfaas/of-watchdog:build                                                                                                                                              0.0s
 => [stage-1 1/1] COPY --from=build /go/src/github.com/openfaas-incubator/of-watchdog/of-watchdog ./fwatchdog                                                                                                 0.0s
 => exporting to image                                                                                                                                                                                        0.1s
 => => exporting layers                                                                                                                                                                                       0.1s
 => => writing image sha256:a55312cdda8c3f2218df4880b15a0fc0c820fba4ac0327b1638476b6b006c404                                                                                                                  0.0s
 => => naming to docker.io/openfaas/of-watchdog:latest-dev-windows                                                                                                                                            0.0s
[+] Building 0.2s (6/6) FINISHED                                                                                                                                                                                   
 => [internal] load build definition from Dockerfile.packager                                                                                                                                                 0.0s
 => => transferring dockerfile: 47B                                                                                                                                                                           0.0s
 => [internal] load .dockerignore                                                                                                                                                                             0.0s
 => => transferring context: 2B                                                                                                                                                                               0.0s
 => [internal] load metadata for docker.io/openfaas/of-watchdog:build                                                                                                                                         0.0s
 => CACHED [build 1/1] FROM docker.io/openfaas/of-watchdog:build                                                                                                                                              0.0s
 => [stage-1 1/1] COPY --from=build /go/src/github.com/openfaas-incubator/of-watchdog/of-watchdog ./fwatchdog                                                                                                 0.0s
 => exporting to image                                                                                                                                                                                        0.1s
 => => exporting layers                                                                                                                                                                                       0.0s
 => => writing image sha256:a4f46d41ea02ce6ab1f35d78295f4016ee95913e8082f08662a5edd4d34ceec0                                                                                                                  0.0s
 => => naming to docker.io/openfaas/of-watchdog:latest-dev-x86_64                                                                                                                                             0.0s
5bab7edf4e8eedaff0521f932eb5dad8dda4119dd970c39972f0121b316ff6d4
buildoutput

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I've read the CONTRIBUTION guide
  • I have signed-off my commits with git commit -s
  • I have added tests to cover my changes.
  • All new and existing tests passed.

This mode allows the users to use the watchdog as a static server for
serving static files. The added benefit over a plain file server is that
they get the RED metrics already implemented by the watchdog.

Signed-off-by: Matias Pan <matias.pan26@gmail.com>
@@ -48,7 +49,7 @@ func (w WatchdogConfig) Process() (string, []string) {
}

// New create config based upon environmental variables.
func New(env []string) (WatchdogConfig, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We were not using the error return value so I thought about removing it and cleanup the code a bit.

@alexellis
Copy link
Member

Nice work 💪

Please can you show this website operating through the gateway, where there is a prefixed path?

Alex

@alexellis
Copy link
Member

Please can you also?

  • show the logs of the watchdog whilst it is serving traffic
  • and confirm that the local /_/metrics endpoint is registering the traffic, despite using a static file server

@@ -338,6 +336,15 @@ func makeHTTPRequestHandler(watchdogConfig config.WatchdogConfig) func(http.Resp
}
}

func makeStaticRequestHandler(watchdogConfig config.WatchdogConfig) http.HandlerFunc {
if watchdogConfig.StaticPath == "" {
log.Fatal(`For mode=static you must specify the "static_path" to serve`)
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to have a default? I don't know.

Copy link
Member

@alexellis alexellis left a comment

Choose a reason for hiding this comment

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

Approved

@alexellis
Copy link
Member

@matipan I would like you to acknowledge the comments and provide the feedback I've asked for. Given that you are the only consume at the moment, I'll merge and release the change, but please can you prioritise the feedback?

@alexellis alexellis merged commit 28449bb into openfaas:master Aug 13, 2019
@alexellis
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a new static mode for serving static content
2 participants