-
Notifications
You must be signed in to change notification settings - Fork 500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add fault inject for kube proxy #384
Merged
xiaojingchen
merged 5 commits into
pingcap:master
from
onlymellb:add-fault-inject-for-kube-proxy
Apr 19, 2019
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1e315a3
fix typo
4b7d020
support for start and stop kube-proxy for each k8s node
333eab4
add KubeProxyLabel template variable
7453355
add StartKubeProxy and StopKubeProxy method into Client interface
a45fbcd
merge branch master into add-fault-inject-for-kube-proxy
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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,156 @@ | ||
// Copyright 2019 PingCAP, Inc. | ||
// | ||
// 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package manager | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/golang/glog" | ||
|
||
"github.com/pingcap/tidb-operator/tests/pkg/util" | ||
) | ||
|
||
const ( | ||
kubeProxyPath = "/etc/kubernetes/kube-proxy" | ||
kubeProxyManifest = "kube-proxy-ds.yml" | ||
kubeProxyLabel = "app.kubernetes.io/kube-proxy" | ||
) | ||
|
||
// UpdateKubeProxyDaemonset add node affinity for previous kube-proxy daemonset | ||
func (m *Manager) UpdateKubeProxyDaemonset(hostname string) error { | ||
kubectlPath, err := getKubectlPath() | ||
if err != nil { | ||
glog.Infof("skip update kube-proxy daemonset, err: %v", err) | ||
return nil | ||
} | ||
|
||
// Get all k8s master nodes | ||
k8sMasterNodes, err := util.ListK8sNodes(kubectlPath, fmt.Sprintf("%s=", util.LabelNodeRoleMaster)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Select the first k8s master node to execute the update operation | ||
if hostname != k8sMasterNodes[0] { | ||
glog.Infof("not the first k8s master node, skip update kube-proxy daemonset, node name: %s", hostname) | ||
return nil | ||
} | ||
|
||
// Ensure kubeProxyPath exists | ||
err = util.EnsureDirectoryExist(kubeProxyPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Render kub-proxy daemonset's template | ||
kubeProxyManifestFullPath := filepath.Join(kubeProxyPath, kubeProxyManifest) | ||
f, err := os.Create(kubeProxyManifestFullPath) | ||
if err != nil { | ||
return fmt.Errorf("create file %s failed, err: %v", kubeProxyManifestFullPath, err) | ||
} | ||
defer f.Close() | ||
proxyDaemonSetBytes, err := util.ParseTemplate(KubeProxyDaemonSet, struct{ Image, KubeProxyLabel string }{ | ||
Image: m.kubeProxyImages, | ||
KubeProxyLabel: kubeProxyLabel, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("parsing kube-proxy daemonset template failed, err: %v", err) | ||
} | ||
_, err = f.Write(proxyDaemonSetBytes) | ||
if err != nil { | ||
return fmt.Errorf("write kube-proxy daemonset manifest into file %s failed, err: %v", kubeProxyManifestFullPath, err) | ||
} | ||
|
||
// Apply kube-proxy daemonset manifest | ||
commandStr := fmt.Sprintf("%s apply -f %s", kubectlPath, kubeProxyManifestFullPath) | ||
cmd := exec.Command("/bin/sh", "-c", commandStr) | ||
output, err := cmd.CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("exec: [%s] failed, output: %s, error: %v", commandStr, string(output), err) | ||
} | ||
|
||
// Add app.kubernetes.io/kube-proxy= label for all k8s nodes | ||
allK8sNodes, err := util.ListK8sNodes(kubectlPath, "") | ||
if err != nil { | ||
return err | ||
} | ||
for _, node := range allK8sNodes { | ||
err := addKubeProxyLabel(kubectlPath, node) | ||
if err != nil { | ||
glog.Error(err) | ||
return err | ||
} | ||
} | ||
|
||
glog.Infof("update kube-proxy daemonset success") | ||
return nil | ||
} | ||
|
||
// StartKubeProxy start kube-proxy of the specified node | ||
func (m *Manager) StartKubeProxy(nodeName string) error { | ||
kubectlPath, err := getKubectlPath() | ||
if err != nil { | ||
glog.Error(err) | ||
return err | ||
} | ||
err = addKubeProxyLabel(kubectlPath, nodeName) | ||
if err != nil { | ||
glog.Error(err) | ||
return err | ||
} | ||
|
||
glog.Infof("node %s kube-proxy is started", nodeName) | ||
return nil | ||
} | ||
|
||
// StopKubeProxy stop kube-proxy of the specified node | ||
func (m *Manager) StopKubeProxy(nodeName string) error { | ||
kubectlPath, err := getKubectlPath() | ||
if err != nil { | ||
glog.Error(err) | ||
return err | ||
} | ||
commandStr := fmt.Sprintf("%s label no %s %s-", kubectlPath, nodeName, kubeProxyLabel) | ||
cmd := exec.Command("/bin/sh", "-c", commandStr) | ||
output, err := cmd.CombinedOutput() | ||
if err != nil { | ||
glog.Errorf("remove kube-proxy label failed, command: [%s], output: %s, error: %v", commandStr, string(output), err) | ||
return err | ||
} | ||
|
||
glog.Infof("node %s kube-proxy is stoped", nodeName) | ||
return nil | ||
} | ||
|
||
func getKubectlPath() (string, error) { | ||
paths := filepath.SplitList(os.Getenv("PATH")) | ||
kubectlPath, err := util.FindInPath("kubectl", paths) | ||
if err != nil { | ||
return "", fmt.Errorf("can't found kubectl binary, not deployed on the k8s master node, err: %v", err) | ||
} | ||
return kubectlPath, nil | ||
} | ||
|
||
func addKubeProxyLabel(kubectlPath, nodeName string) error { | ||
commandStr := fmt.Sprintf("%s label no %s %s= --overwrite", kubectlPath, nodeName, kubeProxyLabel) | ||
cmd := exec.Command("/bin/sh", "-c", commandStr) | ||
output, err := cmd.CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("add kube-proxy label failed, command: [%s], output: %s, error: %v", commandStr, string(output), err) | ||
} | ||
return nil | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not use kubeClient to update proxy's daemonSet?