Skip to content

Commit

Permalink
add node-servant
Browse files Browse the repository at this point in the history
  • Loading branch information
adamzhoul committed Oct 29, 2021
1 parent 55154c8 commit 16010ab
Show file tree
Hide file tree
Showing 14 changed files with 1,179 additions and 0 deletions.
66 changes: 66 additions & 0 deletions cmd/yurt-node-servant/convert/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2021 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.
See the License for the specific language governing permissions and
limitations under the License.
*/

package convert

import (
"time"

"github.com/spf13/cobra"
"k8s.io/klog"

nodeconverter "github.com/openyurtio/openyurt/pkg/node-servant/convert"
)

const (
// defaultYurthubHealthCheckTimeout defines the default timeout for yurthub health check phase
defaultYurthubHealthCheckTimeout = 2 * time.Minute
)

// NewConvertCmd generates a new convert command
func NewConvertCmd() *cobra.Command {
o := nodeconverter.NewConvertOptions()
cmd := &cobra.Command{
Use: "convert --working-mode",
Short: "",
Run: func(cmd *cobra.Command, args []string) {
if err := o.Complete(cmd.Flags()); err != nil {
klog.Fatalf("fail to complete the convert option: %s", err)
}

converter := nodeconverter.NewConverterWithOptions(o)
if err := converter.Do(); err != nil {
klog.Fatalf("fail to convert the kubernetes node to a yurt node: %s", err)
}
klog.Info("convert success")
},
}
setFlags(cmd)

return cmd
}

// setFlags sets flags.
func setFlags(cmd *cobra.Command) {
cmd.Flags().String("yurthub-image", "openyurt/yurthub:latest",
"The yurthub image.")
cmd.Flags().Duration("yurthub-healthcheck-timeout", defaultYurthubHealthCheckTimeout,
"The timeout for yurthub health check.")
cmd.Flags().String("kubeadm-conf-path", "",
"The path to kubelet service conf that is used by kubelet component to join the cluster on the work node.")
cmd.Flags().String("join-token", "", "The token used by yurthub for joining the cluster.")
cmd.Flags().String("working-mode", "edge", "The node type cloud/edge, effect yurthub workingMode.")
}
50 changes: 50 additions & 0 deletions cmd/yurt-node-servant/node-servant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2021 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.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"fmt"
"math/rand"
"os"
"time"

"github.com/openyurtio/openyurt/cmd/yurt-node-servant/convert"
"github.com/openyurtio/openyurt/cmd/yurt-node-servant/revert"
"github.com/openyurtio/openyurt/pkg/projectinfo"
"github.com/spf13/cobra"
)

// node-servant
// running on specific node, do convert/revert job
// yurtctl convert/revert join/reset, yurtcluster operator shall start a k8s job to run this.
func main() {
rand.Seed(time.Now().UnixNano())

version := fmt.Sprintf("%#v", projectinfo.Get())
rootCmd := &cobra.Command{
Use: "node-servant",
Short: "node-servant do convert/revert specific node",
Version: version,
}
rootCmd.PersistentFlags().String("kubeconfig", "", "The path to the kubeconfig file")
rootCmd.AddCommand(convert.NewConvertCmd())
rootCmd.AddCommand(revert.NewRevertCmd())

if err := rootCmd.Execute(); err != nil { // run command
os.Exit(1)
}
}
52 changes: 52 additions & 0 deletions cmd/yurt-node-servant/revert/revert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2021 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.
See the License for the specific language governing permissions and
limitations under the License.
*/

package revert

import (
"github.com/openyurtio/openyurt/pkg/node-servant/revert"
"github.com/spf13/cobra"
"k8s.io/klog"
)

// NewRevertCmd generates a new revert command
func NewRevertCmd() *cobra.Command {
o := revert.NewRevertOptions()
cmd := &cobra.Command{
Use: "revert",
Short: "",
Run: func(cmd *cobra.Command, args []string) {
if err := o.Complete(cmd.Flags()); err != nil {
klog.Fatalf("fail to complete the revert option: %s", err)
}

r := revert.NewReverterWithOptions(o)
if err := r.Do(); err != nil {
klog.Fatalf("fail to revert the yurt node to a kubernetes node: %s", err)
}
klog.Info("revert success")
},
}
setFlags(cmd)

return cmd
}

// setFlags sets flags.
func setFlags(cmd *cobra.Command) {
cmd.Flags().String("kubeadm-conf-path", "",
"The path to kubelet service conf that is used by kubelet component to join the cluster on the edge node.")
}
1 change: 1 addition & 0 deletions hack/lib/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set -x

readonly YURT_ALL_TARGETS=(
yurtctl
yurt-node-servant
yurthub
yurt-controller-manager
yurt-tunnel-server
Expand Down
29 changes: 29 additions & 0 deletions hack/lib/node-servant-entry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env sh

# Copyright 2021 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.
# See the License for the specific language governing permissions and
# limitations under the License.

set -xe

if [ -d "/openyurt" ]; then
rm -rf /openyurt/usr/local/servant
fi

mkdir -p /openyurt/usr/local/servant
cp /usr/local/bin/node-servant /openyurt/usr/local/servant/

chmod -R +x /openyurt/usr/local/servant/
chroot /openyurt /usr/local/servant/node-servant "$@"
rm -rf /openyurt/usr/local/servant

25 changes: 25 additions & 0 deletions hack/lib/release-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ readonly -a YURT_BIN_TARGETS=(
yurthub
yurt-controller-manager
yurtctl
yurt-node-servant
yurt-tunnel-server
yurt-tunnel-agent
)
Expand Down Expand Up @@ -126,6 +127,30 @@ function build_docker_image() {
cat << EOF > $docker_file_path
FROM ${base_image}
ADD ${binary_name} /usr/local/bin/yurtctl
EOF
elif [[ ${binary} =~ yurt-node-servant ]];
then
yurt_component_name="node-servant"
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
ln ./hack/lib/node-servant-entry.sh "${docker_build_path}/entry.sh"
cat << EOF > $docker_file_path
FROM ${base_image}
ADD entry.sh /usr/local/bin/entry.sh
RUN chmod +x /usr/local/bin/entry.sh
ADD ${binary_name} /usr/local/bin/node-servant
EOF
else
yurt_component_name=${binary_name}
Expand Down
Loading

0 comments on commit 16010ab

Please sign in to comment.