Skip to content

Commit

Permalink
cmd/scaleway: add kubernetes deployment to run in repair mode
Browse files Browse the repository at this point in the history
Adds a daemon mode to the command.

Updates golang/go#21238.

Change-Id: Iab76a2fbe4263e21f925571b2f3bfc6d22f1fe5e
Reviewed-on: https://go-review.googlesource.com/52270
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
  • Loading branch information
jessfraz authored and bradfitz committed Nov 15, 2017
1 parent 6114c7b commit 411e5f3
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cmd/scaleway/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scaleway
ca-certificates.crt
9 changes: 9 additions & 0 deletions cmd/scaleway/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright 2017 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
FROM scratch
LABEL maintainer "golang-dev@googlegroups.com"

COPY ca-certificates.crt /etc/ssl/certs/
COPY scaleway /
ENTRYPOINT ["/scaleway"]
21 changes: 21 additions & 0 deletions cmd/scaleway/Dockerfile.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2017 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

FROM golang:1.8
LABEL maintainer "golang-dev@googlegroups.com"

# BEGIN deps (run `make update-deps` to update)

# Repo go4.org at 034d17a (2017-05-25)
ENV REV=034d17a462f7b2dcd1a4a73553ec5357ff6e6c6e
RUN go get -d go4.org/types &&\
(cd /go/src/go4.org && (git cat-file -t $REV 2>/dev/null || git fetch -q origin $REV) && git reset --hard $REV)

# Optimization to speed up iterative development, not necessary for correctness:
RUN go install go4.org/types
# END deps.

COPY . /go/src/golang.org/x/build/

RUN go install -ldflags "-linkmode=external -extldflags '-static -pthread' -X 'main.Version=$version'" golang.org/x/build/cmd/scaleway
39 changes: 39 additions & 0 deletions cmd/scaleway/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2017 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

VERSION=latest
DOCKER_IMAGE_build0=build0/scaleway:latest
DOCKER_CTR_build0=scaleway-build0

build0: *.go Dockerfile.0
docker build --force-rm -f Dockerfile.0 --tag=$(DOCKER_IMAGE_build0) ../..

scaleway: build0
docker create --name $(DOCKER_CTR_build0) $(DOCKER_IMAGE_build0)
docker cp $(DOCKER_CTR_build0):/go/bin/$@ $@
docker rm $(DOCKER_CTR_build0)

ca-certificates.crt:
docker create --name $(DOCKER_CTR_build0) $(DOCKER_IMAGE_build0)
docker cp $(DOCKER_CTR_build0):/etc/ssl/certs/$@ $@
docker rm $(DOCKER_CTR_build0)

update-deps:
go install golang.org/x/build/cmd/gitlock
gitlock --update=Dockerfile.0 golang.org/x/build/cmd/scaleway

docker-prod: Dockerfile scaleway ca-certificates.crt
docker build --force-rm --tag=gcr.io/symbolic-datum-552/scaleway:$(VERSION) .
docker-dev: Dockerfile scaleway ca-certificates.crt
docker build --force-rm --tag=gcr.io/go-dashboard-dev/scaleway:latest .

push-prod: docker-prod
gcloud docker -- push gcr.io/symbolic-datum-552/scaleway:$(VERSION)
push-dev: docker-dev
gcloud docker -- push gcr.io/go-dashboard-dev/scaleway:latest

.PHONY: clean
clean:
$(RM) scaleway
$(RM) ca-certificates.crt
5 changes: 5 additions & 0 deletions cmd/scaleway/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The scaleway command creates ARM servers on Scaleway.com.

The Makefile, Dockerfiles, and kubernetes deployment file
are for running the command in daemon mode for server repair.
This will check on Scaleway and restart down or wedged instances.
31 changes: 31 additions & 0 deletions cmd/scaleway/deployment-prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apiVersion: extensions/v1beta1
kind: Deployment # TODO(jessfraz): make this a cronjob once out of k8s alpha and remove daemon mode
metadata:
name: scaleway-deployment
spec:
replicas: 1 # more than one will create a race with creating new servers
template:
metadata:
labels:
app: scaleway
annotations:
container.seccomp.security.alpha.kubernetes.io/scaleway: docker/default
container.apparmor.security.beta.kubernetes.io/scaleway: runtime/default
spec:
volumes:
- name: secrets-volume
secret:
secretName: scaleway-secrets # http://go/golang-k8s-scaleway-secret
items:
- key: scalewayToken
path: go-scaleway.token
- key: masterKey
path: gobuilder-master.key
containers:
- name: scaleway
image: gcr.io/symbolic-datum-552/scaleway:latest
imagePullPolicy: Always
command: ["/scaleway", "-daemon"]
volumeMounts:
- name: secrets-volume
mountPath: /keys
31 changes: 22 additions & 9 deletions cmd/scaleway/scaleway.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ import (
)

var (
token = flag.String("token", "", "API token. If empty, the file is read from $HOME/keys/go-scaleway.token. Googlers on the Go team can get the value from http://go/golang-scaleway-token")
org = flag.String("org", "1f34701d-668b-441b-bf08-0b13544e99de", "Organization ID (default is bradfitz@golang.org's account)")
image = flag.String("image", "e488d5e3-d278-47a7-8f7d-1154e1f61dc9", "Disk image ID; default is the snapshot we made last")
num = flag.Int("n", 0, "Number of servers to create; if zero, defaults to a value as a function of --staging")
tags = flag.String("tags", "", "Comma-separated list of tags. The build key tags should be of the form 'buildkey_linux-arm_HEXHEXHEXHEXHEX'. If empty, it's automatic.")
staging = flag.Bool("staging", false, "If true, deploy staging instances (with staging names and tags) instead of prod.")
listAll = flag.Bool("list-all", false, "If true, list all (prod, staging, other) current Scaleway servers and stop without making changes.")
list = flag.Bool("list", false, "If true, list all prod (or staging, if -staging) servers, including missing ones.")
token = flag.String("token", "", "API token. If empty, the file is read from $HOME/keys/go-scaleway.token. Googlers on the Go team can get the value from http://go/golang-scaleway-token")
org = flag.String("org", "1f34701d-668b-441b-bf08-0b13544e99de", "Organization ID (default is bradfitz@golang.org's account)")
image = flag.String("image", "e488d5e3-d278-47a7-8f7d-1154e1f61dc9", "Disk image ID; default is the snapshot we made last")
num = flag.Int("n", 0, "Number of servers to create; if zero, defaults to a value as a function of --staging")
tags = flag.String("tags", "", "Comma-separated list of tags. The build key tags should be of the form 'buildkey_linux-arm_HEXHEXHEXHEXHEX'. If empty, it's automatic.")
staging = flag.Bool("staging", false, "If true, deploy staging instances (with staging names and tags) instead of prod.")
listAll = flag.Bool("list-all", false, "If true, list all (prod, staging, other) current Scaleway servers and stop without making changes.")
list = flag.Bool("list", false, "If true, list all prod (or staging, if -staging) servers, including missing ones.")
fixInterval = flag.Duration("fix-interval", 10*time.Minute, "Interval to wait before running again (only applies to daemon mode)")
daemonMode = flag.Bool("daemon", false, "Run in daemon mode in a loop")
)

const (
Expand Down Expand Up @@ -71,6 +73,17 @@ func main() {
*token = strings.TrimSpace(string(slurp))
}

// Loop over checkServers() in daemon mode.
for {
checkServers()
if !*daemonMode {
return
}
time.Sleep(*fixInterval)
}
}

func checkServers() {
cl := &Client{Token: *token}
serverList, err := cl.Servers()
if err != nil {
Expand Down Expand Up @@ -154,7 +167,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
log.Printf("Doing req %q for token %q", body, *token)
log.Printf("Doing req %q", body)
req, err := http.NewRequest("POST", scalewayAPIBase+"/servers", bytes.NewReader(body))
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit 411e5f3

Please sign in to comment.