Skip to content

Commit

Permalink
Refactor the all-in-one image
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Mar 26, 2024
1 parent 864f788 commit 40712ac
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 68 deletions.
4 changes: 2 additions & 2 deletions images/cluster/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ COPY --chmod=0755 bin/$TARGETPLATFORM/kwok /usr/local/bin/
COPY --chmod=0755 bin/$TARGETPLATFORM/kwokctl /usr/local/bin/

ENV KWOK_CONTROLLER_BINARY /usr/local/bin/kwok
ENV KWOK_KUBE_APISERVER_PORT 8080
ENV KWOK_KUBE_APISERVER_INSECURE_PORT 8080

ARG kube_version
ENV KWOK_KUBE_VERSION $kube_version
Expand All @@ -40,7 +40,7 @@ COPY --chmod=0755 bin/$TARGETPLATFORM/kwok /usr/local/bin/
COPY --chmod=0755 bin/$TARGETPLATFORM/kwokctl /usr/local/bin/

ENV KWOK_CONTROLLER_BINARY /usr/local/bin/kwok
ENV KWOK_KUBE_APISERVER_PORT 8080
ENV KWOK_KUBE_APISERVER_INSECURE_PORT 8080

ARG kube_version
ENV KWOK_KUBE_VERSION $kube_version
Expand Down
67 changes: 29 additions & 38 deletions images/cluster/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,36 @@
# See the License for the specific language governing permissions and
# limitations under the License.

show_info() {
sleep 1

# Print some useful information
echo "###############################################################################"
echo "> kubectl -s :${KWOK_KUBE_APISERVER_PORT} version"
kwokctl kubectl version || true

echo "###############################################################################"
echo "# The following kubeconfig can be used to connect to the Kubernetes API server"
cat <<EOF
apiVersion: v1
clusters:
- cluster:
server: http://127.0.0.1:${KWOK_KUBE_APISERVER_PORT}
name: kwok
contexts:
- context:
cluster: kwok
name: kwok
current-context: kwok
kind: Config
preferences: {}
users: null
EOF

echo "###############################################################################"
echo "> kubectl -s :${KWOK_KUBE_APISERVER_PORT} get ns"
kwokctl kubectl get ns || true

echo "###############################################################################"
echo "# The above example works if your host's port is the same as the container's,"
echo "# otherwise, change it to your host's port"
export KWOK_KUBE_APISERVER_INSECURE_PORT="${KWOK_KUBE_APISERVER_INSECURE_PORT:-${KWOK_KUBE_APISERVER_PORT}}"
export KWOK_KUBE_APISERVER_PORT=0
export ALIVE=true

echo "See https://kwok.sigs.k8s.io/docs/user/all-in-one-image/ for more information"

echo "Starting cluster"
kwokctl create cluster || {
echo "Failed to create cluster"
exit 1
}

catch_exit() {
ALIVE=false
echo "Stopping cluster"
kwokctl stop cluster || true
exit 0
}

keep_alive() {
while [ "${ALIVE}" = true ]; do
if ! output="$(kwokctl start cluster 2>&1)"; then
echo "Failed to start cluster: ${output}"
fi
sleep 10
done
}

# Create a cluster
KWOK_KUBE_APISERVER_PORT=0 kwokctl create cluster "$@" || exit 1
trap catch_exit SIGINT SIGTERM

show_info &
keep_alive

# Start a proxy to the Kubernetes API server
kwokctl kubectl proxy --port="${KWOK_KUBE_APISERVER_PORT}" --accept-hosts='^*$' --address="0.0.0.0"
catch_exit
41 changes: 13 additions & 28 deletions site/content/en/docs/user/all-in-one-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,19 @@ This document walks through the steps to create a cluster with the all-in-one im
docker run --rm -it -p 8080:8080 registry.k8s.io/kwok/cluster:v0.4.0-k8s.v1.28.0
```

``` log
Cluster is creating cluster=kwok
Cluster is created elapsed=0.3s cluster=kwok
Cluster is starting cluster=kwok
Cluster is started elapsed=0.7s cluster=kwok
You can now use your cluster with:
kubectl cluster-info --context kwok-kwok
Thanks for using kwok!
Starting to serve on [::]:8080
###############################################################################
> kubectl -s :8080 version
Client Version: v1.28.0
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.28.0
###############################################################################
# The following kubeconfig can be used to connect to the Kubernetes API server
### Quick Verification

You can use the `kubectl` with the `-s` option to connect to the cluster.

``` bash
kubectl -s :8080 version
```

### Setting up kubeconfig

You can set up the `kubeconfig` file to connect to the cluster.

``` bash
apiVersion: v1
clusters:
- cluster:
Expand All @@ -47,16 +42,6 @@ current-context: kwok
kind: Config
preferences: {}
users: null
###############################################################################
> kubectl -s :8080 get ns
NAME STATUS AGE
default Active 1s
kube-node-lease Active 1s
kube-public Active 1s
kube-system Active 1s
###############################################################################
# The above example works if your host's port is the same as the container's,
# otherwise, change it to your host's port
```

## Use in a pod
Expand Down

0 comments on commit 40712ac

Please sign in to comment.