github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/docker/custom-scripts/devcontainer-init.sh (about) 1 #!/usr/bin/env bash 2 3 # Copyright 2021 The Dapr Authors 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # Unless required by applicable law or agreed to in writing, software 9 # distributed under the License is distributed on an "AS IS" BASIS, 10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 # See the License for the specific language governing permissions and 12 # limitations under the License. 13 # 14 # 15 # Initializes the devcontainer tasks each time the container starts. 16 # Users can edit this copy under /usr/local/share in the container to 17 # customize this as needed for their custom localhost bindings. 18 19 set -e 20 echo "Running devcontainer-init.sh ..." 21 22 # Clone kubectl and minikube config from host if requested when running local devcontainer. 23 if [[ "${SYNC_LOCALHOST_KUBECONFIG,,}" == "true" && "${CODESPACES,,}" != "true" ]]; then 24 mkdir -p ${HOME}/.kube 25 if [ -d "${HOME}/.kube-localhost" ]; then 26 cp -r ${HOME}/.kube-localhost/* ${HOME}/.kube 27 fi 28 29 # [EXPERIMENTAL] As a convenience feature, when using localhost minikube cluster in the devcontainer, 30 # attempt to clone the credentials from the default localhost .minikube profile and fixup 31 # the container's copy of .kube/config with the correct endpoint and path to cloned credentials. 32 # It does not support modifying the minikube configuration from the container (minikube needs to already 33 # be started on the local host) and assumes the only kubernetes context pointing to a localhost 34 # server (i.e. 127.0.0.1 address) belongs to the minikube default profile and should be updated. 35 36 if [ -d "${HOME}/.minikube-localhost" ]; then 37 mkdir -p ${HOME}/.minikube 38 if [ -r ${HOME}/.minikube-localhost/ca.crt ]; then 39 cp -r ${HOME}/.minikube-localhost/ca.crt ${HOME}/.minikube 40 sed -i -r "s|(\s*certificate-authority:\s).*|\\1${HOME}\/.minikube\/ca.crt|g" ${HOME}/.kube/config 41 fi 42 if [ -r ${HOME}/.minikube-localhost/profiles/minikube/client.crt ]; then 43 cp -r ${HOME}/.minikube-localhost/profiles/minikube/client.crt ${HOME}/.minikube 44 sed -i -r "s|(\s*client-certificate:\s).*|\\1${HOME}\/.minikube\/client.crt|g" ${HOME}/.kube/config 45 fi 46 if [ -r ${HOME}/.minikube-localhost/profiles/minikube/client.key ]; then 47 cp -r ${HOME}/.minikube-localhost/profiles/minikube/client.key ${HOME}/.minikube 48 sed -i -r "s|(\s*client-key:\s).*|\\1${HOME}\/.minikube\/client.key|g" ${HOME}/.kube/config 49 fi 50 if [ -r ${HOME}/.minikube-localhost/profiles/minikube/config.json ]; then 51 ENDPOINT=$(grep -E '\"IP\":|\"Port\":' ${HOME}/.minikube-localhost/profiles/minikube/config.json \ 52 | sed -r '{N;s/\s*\"IP\": \"(.+)\",\s*\"Port\": ([0-9]*),/\1:\2/;}') 53 sed -i -r 's/(server: https:\/\/)127.0.0.1:[0-9]*(.*)/\1'"${ENDPOINT}"'\2/' ${HOME}/.kube/config 54 fi 55 fi 56 fi 57 58 # Invoke /usr/local/share/docker-bind-mount.sh or docker-init.sh as appropriate 59 set +e 60 if [[ "${BIND_LOCALHOST_DOCKER,,}" == "true" ]]; then 61 echo "Invoking docker-bind-mount.sh ..." 62 exec /usr/local/share/docker-bind-mount.sh "$@" 63 else 64 echo "Invoking docker-init.sh ..." 65 exec /usr/local/share/docker-init.sh "$@" 66 fi