Skip to content

Commit

Permalink
docker is working
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Moser committed Jul 30, 2020
1 parent 7394517 commit 7cb5a62
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 59 deletions.
41 changes: 41 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Build...
FROM golang:1.14.4-alpine3.11 AS build

RUN mkdir -p src/github.com/MoserMichael/s9k
WORKDIR ./src/github.com/MoserMichael/s9k

COPY go.mod go.sum exec.go s9k.sh s9k.py Makefile ./
ADD ./static-file ./static-file


COPY vendor vendor
RUN apk --no-cache add make curl && pwd && ls -al && make kubexec-no-mod && ls -al
# -----------------------------------------------------------------------------
# Build Image...

FROM alpine:3.10.0

COPY --from=build /go/src/github.com/MoserMichael/s9k/kubeexec /bin/kubeexec
COPY --from=build /go/src/github.com/MoserMichael/s9k/s9k.py /bin/s9k.py
COPY --from=build /go/src/github.com/MoserMichael/s9k/s9k.sh /bin/s9k.sh
COPY --from=build /go/src/github.com/MoserMichael/s9k/static-file /static-file


ENV KUBE_LATEST_VERSION="v1.18.1"

#RUN apk update \
# && apk add --update ca-certificates \
# && apk add --update -t deps binutils file gcc libc-dev libev python3-dev libffi-dev curl python3 py3-pip \
# && (pip3 install -v bottle bottle-websocket || /bin/true)

RUN apk update \
&& apk add --update ca-certificates \
&& apk add --update -t deps binutils file gcc make libc-dev libev python3-dev libffi-dev curl python3 py3-pip openssl \
&& pip3 install -v bottle bottle-websocket \
&& curl -L https://storage.googleapis.com/kubernetes-release/release/${KUBE_LATEST_VERSION}/bin/linux/amd64/kubectl -o /bin/kubectl \
&& chmod +x /bin/kubectl \
&& rm /var/cache/apk/*

# && apk del --purge deps py3-pip curl make gcc binutils \

ENTRYPOINT [ "/bin/s9k.sh", "-d", "-v" ]
67 changes: 59 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,67 @@
[k9s](https://github.com/derailed/k9s) is a character UI application for managing kubernetes clusters.

This script [s9k] is a simple web server that serves a webapp with similar functionality as k9s. It works by parsing the output of kubectl.
This application is written in python3 and requires the [bottle library](https://bottlepy.org/docs/dev/), It also requires the presence of kubectl.


### Installing the requirements

Need to have python3 on the system.

Install dependent packages:

```
sudo pip3 install bottle
sudo pip3 install bottle-websocket
sudo pip3 install bottle-websocket
```

### Running the script
run make in project directory to build a go based executable required to attach a terminal to a container in a pod.

### Running the script from docker image

The following command runs the server in a docker environment; the public docker image is quay.io/mmoser/s9k-mm

./run-in-docker.sh -r

You can now access the web application by following url http://127.0.0.1:8000 (a self-signed certificate is created on each run)

Stop the web server with the following command:

./run-in-docker.sh -s

Additional options to run the script:

```
./run-in-docker.sh -h
Start s9k in docker
./run-in-docker.sh -r [-p <port>] [-i <host>] [-d <dir>] [-v] [-c <image>]
by default it listens on localhost on port 8000
Stop s9k in docker
Run s9k web server in a docker; by default the docker image is fetched from a public repository. (quay.io/mmoser/s9k-mm)
The web server creates a self-signed certificate on each docker run
Start the web server fir s9k
-r - start the web server
-p <port> - listening port (default 8000)
-i <host> - listening host (default 127.0.0.1)
-d <dir> - directory where kube config is (default /home/mmoser/.kube)
Stop the web server for s9k
-s - stop the web server
Common options:
-c <image> - override the container image location (default quay.io/mmoser/s9k-mm)
-v - run verbosely
```

### Running the script locally

./s9k.py runs the server; by default you can then connect to http://localhost:8000

You can customize it with the following command line options
```
Expand All @@ -38,20 +85,24 @@ optional arguments:
```

The script ./s9k.sh creates a self-signed certificate and passes it to s9k.py; (requires presence of openssl).
./s9k.sh runs the server, it also creates a self signed certificate for the server, connect to https://localhost:8000 (you get a browser warning on self signed certificates)

usage of s9k.sh
You can customize it with the following command line options

```
./s9k.sh [-i <host>] [-p <port>] [-c <cmd>] [-v -h]

run s9k.py python script with tls, creates a self signed certificate if needed.

```
-i <host> - listening host (default localhost)
-p <port> - listening port (default 8000)
-c <cmd> - (optional) kubectl command. (default kubectl)
-v - verbose output
-d - internal option to run in docker (listen on eth0, and take kubeconfig from mounted path)
```

## Acknowledgements

Adapted the [websocket terminal](https://github.com/sorgloomer/websocket_terminal) project by [sorgloomer](https://github.com/sorgloomer) for this project.

Thanks to the python [bottle framework](https://bottlepy.org/docs/dev/) and [bottle-websocket](https://pypi.org/project/bottle-websocket/) package.
29 changes: 0 additions & 29 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,6 @@ type DataPayload struct {
}


/*
func parseFlags() (*TermConnectPayload) {
ret := &TermConnectPayload{}
tmp := flag.String("namespace", "", "namespace of the pod")
if tmp != nil {
ret.Namespace = *tmp
}
tmp = flag.String("pod_name", "", "name of the pod")
if tmp != nil {
ret.PodName = *tmp
}
tmp = flag.String("container_name", "", "name of the container")
if tmp != nil {
ret.ContainerName = *tmp
}
flag.Parse()
if ret.PodName == "" {
log.Fatal("Error: no pod_name specified")
os.Exit(1)
}
return ret
}
*/

func HomeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
Expand Down
86 changes: 86 additions & 0 deletions run-in-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/sh

PORT="8000"
HOST=127.0.0.1
KUBE_DIR="$HOME/.kube"
IMAGE_LOCATION="quay.io/mmoser/s9k-mm"

Help() {
cat <<EOF
Start s9k in docker
$0 -r [-p <port>] [-i <host>] [-d <dir>] [-v] [-c <image>]
Stop s9k in docker
Run s9k web server in a docker; by default the docker image is fetched from a public repository. ($IMAGE_LOCATION)
The web server creates a self-signed certificate on each docker run
Start the web server fir s9k
-r - start the web server
-p <port> - listening port (default ${PORT})
-i <host> - listening host (default ${HOST})
-d <dir> - directory where kube config is (default ${KUBE_DIR})
Stop the web server for s9k
-s - stop the web server
Common options:
-c <image> - override the container image location (default ${IMAGE_LOCATION})
-v - run verbosely
EOF

exit 1
}

while getopts "hrsvi:p:c:" opt; do
case ${opt} in
h)
Help
;;
r)
ACTION="start"
;;
s)
ACTION="stop"
;;
p)
PORT="$OPTARG"
;;
i)
HOST="$OPTARG"
;;
c)
IMAGE_LOCATION="$OPTARG"
;;
d)
KUBE_DIR="$OPTARG"
;;
v)
set -x
export PS4='+(${BASH_SOURCE}:${LINENO}) '
;;
*)
Help "Invalid option"
;;
esac
done

if [[ $ACTION == 'start' ]]; then
docker run --network=host -v $KUBE_DIR:/kube-mount -e DHOST=$HOST -e DPORT=$PORT -dt ${IMAGE_LOCATION}
echo "Listen on https://${HOST}:${PORT}"
else
if [[ $ACTION == 'stop' ]]; then
DOCKER_ID=$(docker ps | grep ${IMAGE_LOCATION}[[:space:]] | awk '{ print $1 }')
docker stop $DOCKER_ID
docker rm $DOCKER_ID
else
Help 'must use either to start the server -r or to stop it -s'
fi
fi

Loading

0 comments on commit 7cb5a62

Please sign in to comment.