-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
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) | ||
} | ||
}, | ||
|
||
} | ||
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 edge node.") | ||
cmd.Flags().String("join-token", "", "The token used by yurthub for joining the cluster.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
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.AddCommand(convert.NewConvertCmd()) | ||
rootCmd.AddCommand(revert.NewRevertCmd()) | ||
|
||
if err := rootCmd.Execute(); err != nil { // run command | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
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 ( | ||
nodeReverter "github.com/openyurtio/openyurt/pkg/node-servant/revert" | ||
|
||
"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 := nodeReverter.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) | ||
} | ||
}, | ||
|
||
} | ||
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.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* | ||
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 components | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
"k8s.io/klog" | ||
|
||
enutil "github.com/openyurtio/openyurt/pkg/yurtctl/util/edgenode" | ||
) | ||
|
||
const ( | ||
kubeletConfigRegularExpression = "\\-\\-kubeconfig=.*kubelet.conf" | ||
apiserverAddrRegularExpression = "server: (http(s)?:\\/\\/)?[\\w][-\\w]{0,62}(\\.[\\w][-\\w]{0,62})*(:[\\d]{1,5})?" | ||
|
||
dirMode = 0755 | ||
) | ||
|
||
type kubeletOperator struct { | ||
openyurtDir string | ||
} | ||
|
||
func NewKubeletOperator(openyurtDir string) *kubeletOperator { | ||
return &kubeletOperator{ | ||
openyurtDir: openyurtDir, | ||
} | ||
} | ||
|
||
// RedirectTrafficToYurtHub | ||
func (op *kubeletOperator) RedirectTrafficToYurtHub() error { | ||
// 1. create a working dir to store revised kubelet.conf | ||
configPath, err := op.writeYurthubKubeletConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 2. append /var/lib/kubelet/kubeadm-flags.env | ||
if err := op.appendConfig(configPath); err != nil { | ||
return err | ||
} | ||
|
||
// 3. restart | ||
return restartKubeletService() | ||
} | ||
|
||
// UndoRedirectTrafficToYurtHub | ||
func (op *kubeletOperator) UndoRedirectTrafficToYurtHub() error { | ||
if err := op.undoAppendConfig(); err != nil { | ||
return err | ||
} | ||
|
||
if err := restartKubeletService(); err != nil { | ||
return err | ||
} | ||
|
||
if err := op.undoWriteYurthubKubeletConfig(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (op *kubeletOperator) writeYurthubKubeletConfig() (string, error) { | ||
err := os.MkdirAll(op.openyurtDir, dirMode) | ||
if err != nil { | ||
return "", err | ||
} | ||
fullPath := op.getYurthubKubeletConf() | ||
err = ioutil.WriteFile(fullPath, []byte(enutil.OpenyurtKubeletConf), fileMode) | ||
if err != nil { | ||
return "", err | ||
} | ||
klog.Infof("revised kubeconfig %s is generated", fullPath) | ||
return fullPath, nil | ||
} | ||
|
||
func (op *kubeletOperator) undoWriteYurthubKubeletConfig() error { | ||
yurtKubeletConf := op.getYurthubKubeletConf() | ||
return os.Remove(yurtKubeletConf) | ||
} | ||
|
||
func (op *kubeletOperator) appendConfig(configPath string) error { | ||
// set env KUBELET_KUBEADM_ARGS, args set later will override before | ||
// ExecStart: kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS | ||
file, err := os.OpenFile("/var/lib/kubelet/kubeadm-flags.env", os.O_APPEND|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
_ = file.Close() | ||
}() | ||
|
||
kubeConfigSetup := fmt.Sprintf("KUBELET_KUBEADM_ARGS=$KUBELET_KUBEADM_ARGS\""+ | ||
"--kubeconfig=%s -bootstrap-kubeconfig=\"", configPath) | ||
|
||
// if wrote, return | ||
contentbyte, err := ioutil.ReadFile("/var/lib/kubelet/kubeadm-flags.env") | ||
if err != nil { | ||
return err | ||
} | ||
if strings.Contains(string(contentbyte), kubeConfigSetup) { | ||
klog.Info("kubeConfigSetup has wrote before") | ||
return nil | ||
} | ||
|
||
if _, err := file.WriteString(kubeConfigSetup); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (op *kubeletOperator) undoAppendConfig() error { | ||
configPath := op.getYurthubKubeletConf() | ||
kubeConfigSetup := fmt.Sprintf("KUBELET_KUBEADM_ARGS=$KUBELET_KUBEADM_ARGS\""+ | ||
"--kubeconfig=%s -bootstrap-kubeconfig=\"", configPath) | ||
contentbyte, err := ioutil.ReadFile("/var/lib/kubelet/kubeadm-flags.env") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
content := strings.Replace(string(contentbyte), kubeConfigSetup, "", -1) | ||
err = ioutil.WriteFile("/var/lib/kubelet/kubeadm-flags.env", []byte(content), 0644) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (op *kubeletOperator) getYurthubKubeletConf() string { | ||
return filepath.Join(op.openyurtDir, enutil.KubeletConfName) | ||
} | ||
|
||
func restartKubeletService() error { | ||
klog.Info(enutil.DaemonReload) | ||
cmd := exec.Command("bash", "-c", enutil.DaemonReload) | ||
if err := enutil.Exec(cmd); err != nil { | ||
return err | ||
} | ||
klog.Info(enutil.RestartKubeletSvc) | ||
cmd = exec.Command("bash", "-c", enutil.RestartKubeletSvc) | ||
if err := enutil.Exec(cmd); err != nil { | ||
return err | ||
} | ||
klog.Infof("kubelet has been restarted") | ||
return nil | ||
} | ||
|
||
func GetApiServerAddress(kubeadmConfPath string) (string, error) { | ||
kubeletConfPath, err := enutil.GetSingleContentFromFile(kubeadmConfPath, kubeletConfigRegularExpression) | ||
if err != nil { | ||
return "", err | ||
} | ||
kubeletConfPath = strings.Split(kubeletConfPath, "=")[1] | ||
apiserverAddr, err := enutil.GetSingleContentFromFile(kubeletConfPath, apiserverAddrRegularExpression) | ||
if err != nil { | ||
return "", err | ||
} | ||
apiserverAddr = strings.Split(apiserverAddr, " ")[1] | ||
return apiserverAddr, nil | ||
} |
Oops, something went wrong.