#110: Started fix for docker container versions and k8s support

This commit is contained in:
frikky
2020-09-02 16:11:57 +02:00
13 changed files with 145 additions and 155 deletions
+14 -19
View File
@@ -1,21 +1,16 @@
#from golang as builder
#
#RUN mkdir /app
#WORKDIR /app
#COPY worker.go /app/worker.go
#
#RUN go get github.com/docker/docker/api/types
#RUN go get github.com/docker/docker/api/types/container
#RUN go get -u github.com/docker/docker/client
#
#RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o worker .
#
from golang as builder
# THis is a workaround until I get docker/docker to build in a dockerfile
# PS: This is tricky to google.
# Might not work on some machines.
from scratch
#COPY --from=builder /app/ /
COPY worker.bin /worker.bin
WORKDIR /app
CMD ["./worker.bin"]
RUN go get -u github.com/docker/docker/api/types
RUN go get -u github.com/docker/docker/api/types/container
RUN go get -u github.com/docker/docker/client
COPY worker.go /app/worker.go
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o worker .
FROM alpine:3.12
RUN apk add --no-cache bash
COPY --from=builder /app/ /
CMD ["./worker"]
+4 -4
View File
@@ -1,11 +1,11 @@
NAME=worker
VERSION=0.1.0
VERSION=0.6.0
echo "Running docker build with $NAME:$VERSION"
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o worker.bin .
docker build . -t frikky/shuffle:$NAME -t docker.pkg.github.com/frikky/shuffle/$NAME:$VERSION -t frikky/$NAME:$VERSION
# Push both for now..
docker push frikky/$NAME:$VERSION
docker push frikky/shuffle:$NAME
#docker push docker.pkg.github.com/frikky/shuffle/$NAME:$VERSION
#docker push frikky/$NAME:$VERSION
#docker push frikky/shuffle:$NAME
docker push docker.pkg.github.com/frikky/shuffle/$NAME:$VERSION
+35 -23
View File
@@ -11,12 +11,12 @@ import (
"log"
"net/http"
"os"
"os/exec"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
network "github.com/docker/docker/api/types/network"
dockerclient "github.com/docker/docker/client"
)
@@ -25,6 +25,29 @@ var baseUrl = os.Getenv("BASE_URL")
var baseimagename = "frikky/shuffle"
var sleepTime = 2
var containerId string
// form container id of current running container
func getThisContainerId() string {
id := ""
cmd := fmt.Sprintf("cat /proc/self/cgroup | grep memory | tail -1 | cut -d/ -f3")
out, err := exec.Command("bash", "-c", cmd).Output()
if err == nil {
id = strings.TrimSpace(string(out))
}
return id
}
func init() {
containerId = getThisContainerId()
if len(containerId) == 0 {
log.Printf("[ERROR] No container ID found.")
} else {
log.Printf("Found container ID: %s", containerId)
}
}
type User struct {
Username string `datastore:"Username" json:"username"`
Password string `datastore:"password,noindex" password:"password,omitempty"`
@@ -380,11 +403,11 @@ func shutdown(executionId, workflowId string) {
authorization := os.Getenv("AUTHORIZATION")
if len(authorization) > 0 {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", authorization))
} else {
log.Printf("No authorization specified for abort")
}
req.Header.Add("Content-Type", "application/json")
//req.Header.Add("Authorization", authorization)
client := &http.Client{
Transport: &http.Transport{
Proxy: nil,
@@ -414,6 +437,7 @@ func shutdown(executionId, workflowId string) {
// Deploys the internal worker whenever something happens
func deployApp(cli *dockerclient.Client, image string, identifier string, env []string) error {
// form basic hostConfig
hostConfig := &container.HostConfig{
LogConfig: container.LogConfig{
Type: "json-file",
@@ -421,28 +445,23 @@ func deployApp(cli *dockerclient.Client, image string, identifier string, env []
},
}
// form container id and use it as network source if it's not empty
if containerId != "" {
hostConfig.NetworkMode = container.NetworkMode(fmt.Sprintf("container:%s", containerId))
} else {
log.Printf("[WARNING] Empty self container id, continue without NetworkMode")
}
config := &container.Config{
Image: image,
Env: env,
}
networkConfig := &network.NetworkingConfig{}
shuffleNetwork := os.Getenv("DOCKER_NETWORK")
if len(shuffleNetwork) > 0 {
networkConfig = &network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{
shuffleNetwork: {
NetworkID: shuffleNetwork,
},
},
}
}
cont, err := cli.ContainerCreate(
context.Background(),
config,
hostConfig,
networkConfig,
nil,
nil,
identifier,
)
@@ -1107,13 +1126,6 @@ func main() {
}
}
shuffleNetwork := os.Getenv("DOCKER_NETWORK")
if len(shuffleNetwork) > 0 {
log.Printf("Running with Docker network %s", shuffleNetwork)
} else {
log.Printf("No docker network specified for Worker.")
}
// WORKER_TESTING_WORKFLOW should be a workflow ID
authorization := ""
executionId := ""