From e64e08d5cfa2eaafdfba039ceac2351bf3e19671 Mon Sep 17 00:00:00 2001 From: Peeknut <33392138+Peeknut@users.noreply.github.com> Date: Wed, 31 Mar 2021 02:58:29 +0800 Subject: [PATCH] Add support for the conversion between kind and OpenYurt cluster (#234) --- README.md | 4 +- README.zh.md | 4 +- config/yurtctl-servant/Dockerfile | 1 - hack/lib/release-images.sh | 85 +++++++++++++---------------- pkg/yurtctl/cmd/convert/convert.go | 8 ++- pkg/yurtctl/cmd/convert/edgenode.go | 2 +- pkg/yurtctl/constants/constants.go | 4 +- pkg/yurtctl/util/edgenode/util.go | 13 +++-- pkg/yurtctl/util/kubernetes/util.go | 2 +- 9 files changed, 57 insertions(+), 66 deletions(-) delete mode 100644 config/yurtctl-servant/Dockerfile mode change 100755 => 100644 hack/lib/release-images.sh diff --git a/README.md b/README.md index d74a4b1b102..1791b65d5fa 100644 --- a/README.md +++ b/README.md @@ -84,10 +84,10 @@ make WHAT=cmd/yurtctl ``` The `yurtctl` binary can be found at `_output/bin`. To convert an existing Kubernetes cluster to an OpenYurt cluster, -the following simple command line can be used(support kubernetes clusters that managed by minikube, kubeadm and ACK): +the following simple command line can be used(support kubernetes clusters that managed by minikube, kubeadm, ACK and kind): ```bash -_output/bin/yurtctl convert --provider [minikube|kubeadm|ack] +_output/bin/yurtctl convert --provider [minikube|kubeadm|ack|kind] ``` To uninstall OpenYurt and revert back to the original Kubernetes cluster settings, you can run the following command: diff --git a/README.zh.md b/README.zh.md index 43eeff19f63..58b5be5365a 100644 --- a/README.zh.md +++ b/README.zh.md @@ -58,10 +58,10 @@ cd openyurt make WHAT=cmd/yurtctl ``` -`yurtctl` 的二进制文件位于_output /bin 目录。如果需要将已存在的 Kubernetes 集群转换为 OpenYurt 集群,你可以使用如下简单的命令(目前支持minikube, kubeadm, ack三种工具安装的kubernetes集群): +`yurtctl` 的二进制文件位于_output /bin 目录。如果需要将已存在的 Kubernetes 集群转换为 OpenYurt 集群,你可以使用如下简单的命令(目前支持minikube, kubeadm, ack, kind 四种工具安装的kubernetes集群): ```bash -_output/bin/yurtctl convert --provider [minikube|kubeadm|ack] +_output/bin/yurtctl convert --provider [minikube|kubeadm|ack|kind] ``` 要卸载 OpenYurt 并恢复为原始的 Kubernetes 集群设置,请运行以下命令: diff --git a/config/yurtctl-servant/Dockerfile b/config/yurtctl-servant/Dockerfile deleted file mode 100644 index 1154c8492cd..00000000000 --- a/config/yurtctl-servant/Dockerfile +++ /dev/null @@ -1 +0,0 @@ -FROM alpine:3.8 \ No newline at end of file diff --git a/hack/lib/release-images.sh b/hack/lib/release-images.sh old mode 100755 new mode 100644 index dcdbded2f87..c34f584d06e --- a/hack/lib/release-images.sh +++ b/hack/lib/release-images.sh @@ -1,11 +1,11 @@ # Copyright 2020 The OpenYurt Authors. -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -26,7 +26,7 @@ YURT_BUILD_IMAGE="golang:1.13-alpine" readonly -a YURT_BIN_TARGETS=( yurthub yurt-controller-manager - yurtctl-servant + yurtctl yurt-tunnel-server yurt-tunnel-agent yurt-app-manager @@ -41,7 +41,7 @@ readonly -a SUPPORTED_ARCH=( readonly SUPPORTED_OS=linux readonly -a bin_targets=(${WHAT:-${YURT_BIN_TARGETS[@]}}) -readonly -a bin_targets_without_servant=("${bin_targets[@]/yurtctl-servant}") +readonly -a bin_targets_process_servant=("${bin_targets[@]/yurtctl-servant/yurtctl}") readonly -a target_arch=(${ARCH:-${SUPPORTED_ARCH[@]}}) readonly region=${REGION:-us} @@ -74,7 +74,7 @@ function build_multi_arch_binaries() { cd /opt/src; umask 0022; \ rm -rf ${YURT_LOCAL_BIN_DIR}/* ;" for arch in ${target_arch[@]}; do - sub_commands+="GOARCH=$arch bash ./hack/make-rules/build.sh $(echo ${bin_targets_without_servant[@]}); " + sub_commands+="GOARCH=$arch bash ./hack/make-rules/build.sh $(echo ${bin_targets_process_servant[@]}); " done sub_commands+="chown -R $(id -u):$(id -g) /opt/src/_output" @@ -83,7 +83,7 @@ function build_multi_arch_binaries() { function build_docker_image() { for arch in ${target_arch[@]}; do - for binary in "${bin_targets_without_servant[@]}"; do + for binary in "${bin_targets_process_servant[@]}"; do local binary_name=$(get_output_name $binary) local binary_path=${YURT_LOCAL_BIN_DIR}/${SUPPORTED_OS}/${arch}/${binary_name} if [ -f ${binary_path} ]; then @@ -91,13 +91,38 @@ function build_docker_image() { local docker_file_path=${docker_build_path}/Dockerfile.${binary_name}-${arch} mkdir -p ${docker_build_path} - local yurt_component_image="${REPO}/${binary_name}:${TAG}-${arch}" - local base_image="k8s.gcr.io/debian-iptables-${arch}:v11.0.2" - cat < "${docker_file_path}" + local yurt_component_image + local base_image + if [[ ${binary} =~ yurtctl ]] + then + yurt_component_image=$REPO/yurtctl-servant:$TAG-$arch + case $arch in + amd64) + base_image="amd64/alpine:3.9" + ;; + arm64) + base_image="arm64v8/alpine:3.9" + ;; + arm) + base_image="arm32v7/alpine:3.9" + ;; + *) + echo unknown arch $arch + exit 1 + esac + cat << EOF > $docker_file_path +FROM ${base_image} +ADD ${binary_name} /usr/local/bin/yurtctl +EOF + else + yurt_component_image="${REPO}/${binary_name}:${TAG}-${arch}" + base_image="k8s.gcr.io/debian-iptables-${arch}:v11.0.2" + cat < "${docker_file_path}" FROM ${base_image} COPY ${binary_name} /usr/local/bin/${binary_name} ENTRYPOINT ["/usr/local/bin/${binary_name}"] EOF + fi ln "${binary_path}" "${docker_build_path}/${binary_name}" docker build --no-cache -t "${yurt_component_image}" -f "${docker_file_path}" ${docker_build_path} @@ -108,39 +133,6 @@ EOF done } -function build_yurtctl_servant_image() { - servant_script_path=$YURTCTL_SERVANT_DIR/setup_edgenode - for arch in ${target_arch[@]}; do - local docker_build_path=$DOCKER_BUILD_BASE_IDR/$SUPPORTED_OS/$arch - local docker_file_path=$docker_build_path/Dockerfile.yurtctl-servant-$arch - mkdir -p $docker_build_path - - local yurtctl_servant_image=$REPO/yurtctl-servant:$TAG-$arch - local base_image - case $arch in - amd64) - base_image="amd64/alpine:3.9" - ;; - arm64) - base_image="arm64v8/alpine:3.9" - ;; - arm) - base_image="arm32v7/alpine:3.9" - ;; - *) - echo unknown arch $arch - exit 1 - esac - cat << EOF > $docker_file_path -FROM $base_image -EOF - ln $servant_script_path $docker_build_path/setup_edgenode - docker build --no-cache -t $yurtctl_servant_image -f $docker_file_path $docker_build_path - docker save $yurtctl_servant_image > $YURT_IMAGE_DIR/yurtctl-servant-$SUPPORTED_OS-$arch.tar - rm -rf $docker_build_path - done -} - build_images() { # Always clean first rm -Rf ${YURT_OUTPUT_DIR} @@ -148,10 +140,7 @@ build_images() { mkdir -p ${YURT_LOCAL_BIN_DIR} mkdir -p ${YURT_IMAGE_DIR} mkdir -p ${DOCKER_BUILD_BASE_IDR} - + build_multi_arch_binaries build_docker_image - if [[ ${bin_targets[@]} =~ yurtctl-servant ]]; then - build_yurtctl_servant_image - fi -} +} \ No newline at end of file diff --git a/pkg/yurtctl/cmd/convert/convert.go b/pkg/yurtctl/cmd/convert/convert.go index 2328db3f2a8..d2d8640dbfa 100644 --- a/pkg/yurtctl/cmd/convert/convert.go +++ b/pkg/yurtctl/cmd/convert/convert.go @@ -47,6 +47,8 @@ const ( // ProviderACK is used if the target kubernetes is run on ack ProviderACK Provider = "ack" ProviderKubeadm Provider = "kubeadm" + // ProviderKind is used if the target kubernetes is run on kind + ProviderKind Provider = "kind" ) // ConvertOptions has the information that required by convert operation @@ -204,9 +206,9 @@ func (co *ConvertOptions) Complete(flags *pflag.FlagSet) error { // Validate makes sure provided values for ConvertOptions are valid func (co *ConvertOptions) Validate() error { - if co.Provider != ProviderMinikube && - co.Provider != ProviderACK && co.Provider != ProviderKubeadm { - return fmt.Errorf("unknown provider: %s, valid providers are: minikube, ack", + if co.Provider != ProviderMinikube && co.Provider != ProviderACK && + co.Provider != ProviderKubeadm && co.Provider != ProviderKind { + return fmt.Errorf("unknown provider: %s, valid providers are: minikube, ack, kubeadm, kind", co.Provider) } diff --git a/pkg/yurtctl/cmd/convert/edgenode.go b/pkg/yurtctl/cmd/convert/edgenode.go index 2ca9c27fc36..0917913bc8a 100644 --- a/pkg/yurtctl/cmd/convert/edgenode.go +++ b/pkg/yurtctl/cmd/convert/edgenode.go @@ -44,7 +44,7 @@ import ( const ( kubeletConfigRegularExpression = "\\-\\-kubeconfig=.*kubelet.conf" - apiserverAddrRegularExpression = "server: (http(s)?:\\/\\/)?[\\w][\\w]{0,62}(\\.[\\w][-\\w]{0,62})+(:[\\d]{1,5})?" + apiserverAddrRegularExpression = "server: (http(s)?:\\/\\/)?[\\w][-\\w]{0,62}(\\.[\\w][-\\w]{0,62})*(:[\\d]{1,5})?" hubHealthzCheckFrequency = 10 * time.Second failedRetry = 5 filemode = 0666 diff --git a/pkg/yurtctl/constants/constants.go b/pkg/yurtctl/constants/constants.go index 850fdc65a09..857b938ac6d 100644 --- a/pkg/yurtctl/constants/constants.go +++ b/pkg/yurtctl/constants/constants.go @@ -186,7 +186,7 @@ spec: - /bin/sh - -c args: - - "nsenter -t 1 -m -u -n -i -- /bin/yurtctl convert edgenode --yurthub-image {{.yurthub_image}} --join-token {{.joinToken}}" + - "cp /usr/local/bin/yurtctl /tmp && nsenter -t 1 -m -u -n -i -- /var/tmp/yurtctl convert edgenode --yurthub-image {{.yurthub_image}} --join-token {{.joinToken}} && rm /tmp/yurtctl" securityContext: privileged: true volumeMounts: @@ -231,7 +231,7 @@ spec: - /bin/sh - -c args: - - "nsenter -t 1 -m -u -n -i -- /bin/yurtctl revert edgenode" + - "cp /usr/local/bin/yurtctl /tmp && nsenter -t 1 -m -u -n -i -- /var/tmp/yurtctl revert edgenode && rm /tmp/yurtctl" securityContext: privileged: true volumeMounts: diff --git a/pkg/yurtctl/util/edgenode/util.go b/pkg/yurtctl/util/edgenode/util.go index 09920a9a34e..bde9433dec2 100644 --- a/pkg/yurtctl/util/edgenode/util.go +++ b/pkg/yurtctl/util/edgenode/util.go @@ -29,6 +29,7 @@ import ( "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/util/homedir" + "k8s.io/klog" ) // FileExists determines whether the file exists @@ -114,11 +115,11 @@ func GetNodeName() (string, error) { //2. find --hostname-override in 10-kubeadm.conf nodeName, err := GetSingleContentFromFile(KubeletSvcPath, KubeletHostname) - if err != nil { - return "", err - } else if nodeName != "" { + if nodeName != "" { nodeName = strings.Split(nodeName, "=")[1] return nodeName, nil + } else { + klog.V(4).Info("get nodename err: ", err) } //3. find --hostname-override in EnvironmentFile @@ -129,11 +130,11 @@ func GetNodeName() (string, error) { for _, ef := range environmentFiles { ef = strings.Split(ef, "-")[1] nodeName, err = GetSingleContentFromFile(ef, KubeletHostname) - if err != nil { - return "", err - } else if nodeName != "" { + if nodeName != "" { nodeName = strings.Split(nodeName, "=")[1] return nodeName, nil + } else { + klog.V(4).Info("get nodename err: ", err) } } diff --git a/pkg/yurtctl/util/kubernetes/util.go b/pkg/yurtctl/util/kubernetes/util.go index bbb642c1365..e2c75d318b0 100644 --- a/pkg/yurtctl/util/kubernetes/util.go +++ b/pkg/yurtctl/util/kubernetes/util.go @@ -60,7 +60,7 @@ const ( var ( // PropagationPolicy defines the propagation policy used when deleting a resource - PropagationPolicy = metav1.DeletePropagationForeground + PropagationPolicy = metav1.DeletePropagationBackground // WaitServantJobTimeout specifies the timeout value of waiting for the ServantJob to be succeeded WaitServantJobTimeout = time.Minute * 2 // CheckServantJobPeriod defines the time interval between two successive ServantJob statu's inspection