-
Notifications
You must be signed in to change notification settings - Fork 431
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
258 changed files
with
19,470 additions
and
6 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
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,23 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/alauda/kube-ovn/pkg/pinger" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"k8s.io/klog" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
config, err := pinger.ParseFlags() | ||
if err != nil { | ||
klog.Fatalf("parse config failed %v", err) | ||
} | ||
if config.Mode == "server" { | ||
http.Handle("/metrics", promhttp.Handler()) | ||
go func() { | ||
klog.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", config.Port), nil)) | ||
}() | ||
} | ||
pinger.StartPinger(config) | ||
} |
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,33 @@ | ||
FROM centos:7 | ||
|
||
ENV PYTHONDONTWRITEBYTECODE yes | ||
|
||
RUN yum install -y \ | ||
PyYAML bind-utils \ | ||
openssl \ | ||
numactl-libs \ | ||
firewalld-filesystem \ | ||
libpcap \ | ||
hostname \ | ||
iproute strace socat nc \ | ||
unbound unbound-devel && \ | ||
yum clean all | ||
|
||
ENV OVS_VERSION=2.11.4 | ||
ENV OVS_SUBVERSION=1 | ||
|
||
RUN rpm -ivh https://github.com/alauda/ovs/releases/download/v${OVS_VERSION}-${OVS_SUBVERSION}/openvswitch-${OVS_VERSION}-${OVS_SUBVERSION}.el7.x86_64.rpm && \ | ||
rpm -ivh https://github.com/alauda/ovs/releases/download/v${OVS_VERSION}-${OVS_SUBVERSION}/openvswitch-devel-${OVS_VERSION}-${OVS_SUBVERSION}.el7.x86_64.rpm && \ | ||
rpm -ivh https://github.com/alauda/ovs/releases/download/v${OVS_VERSION}-${OVS_SUBVERSION}/ovn-${OVS_VERSION}-${OVS_SUBVERSION}.el7.x86_64.rpm && \ | ||
rpm -ivh https://github.com/alauda/ovs/releases/download/v${OVS_VERSION}-${OVS_SUBVERSION}/ovn-common-${OVS_VERSION}-${OVS_SUBVERSION}.el7.x86_64.rpm && \ | ||
rpm -ivh https://github.com/alauda/ovs/releases/download/v${OVS_VERSION}-${OVS_SUBVERSION}/ovn-vtep-${OVS_VERSION}-${OVS_SUBVERSION}.el7.x86_64.rpm && \ | ||
rpm -ivh https://github.com/alauda/ovs/releases/download/v${OVS_VERSION}-${OVS_SUBVERSION}/ovn-central-${OVS_VERSION}-${OVS_SUBVERSION}.el7.x86_64.rpm && \ | ||
rpm -ivh https://github.com/alauda/ovs/releases/download/v${OVS_VERSION}-${OVS_SUBVERSION}/ovn-host-${OVS_VERSION}-${OVS_SUBVERSION}.el7.x86_64.rpm | ||
|
||
RUN mkdir -p /var/run/openvswitch | ||
WORKDIR /kube-ovn | ||
|
||
CMD ["/kube-ovn/kube-ovn-pinger"] | ||
|
||
COPY kube-ovn-pinger /kube-ovn/kube-ovn-pinger | ||
RUN chmod +x /kube-ovn/kube-ovn-pinger |
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,90 @@ | ||
package pinger | ||
|
||
import ( | ||
"flag" | ||
"github.com/spf13/pflag" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"k8s.io/klog" | ||
) | ||
|
||
type Configuration struct { | ||
KubeConfigFile string | ||
KubeClient kubernetes.Interface | ||
Port int | ||
DaemonSetNamespace string | ||
DaemonSetName string | ||
Interval int | ||
Mode string | ||
DNS string | ||
} | ||
|
||
func ParseFlags() (*Configuration, error) { | ||
var ( | ||
argPort = pflag.Int("port", 8080, "metrics port") | ||
argKubeConfigFile = pflag.String("kubeconfig", "", "Path to kubeconfig file with authorization and master location information. If not set use the inCluster token.") | ||
argDaemonSetNameSpace = pflag.String("ds-namespace", "kube-ovn", "kube-ovn-pinger daemonset namespace") | ||
argDaemonSetName = pflag.String("ds-name", "kube-ovn-pinger", "kube-ovn-pinger daemonset name") | ||
argInterval = pflag.Int("interval", 5, "interval seconds between consecutive pings") | ||
argMode = pflag.String("mode", "server", "server or job Mode") | ||
argDns = pflag.String("dns", "kubernetes.default.svc.cluster.local", "check dns from pod") | ||
) | ||
|
||
klogFlags := flag.NewFlagSet("klog", flag.ExitOnError) | ||
klog.InitFlags(klogFlags) | ||
|
||
// Sync the glog and klog flags. | ||
flag.CommandLine.VisitAll(func(f1 *flag.Flag) { | ||
f2 := klogFlags.Lookup(f1.Name) | ||
if f2 != nil { | ||
value := f1.Value.String() | ||
f2.Value.Set(value) | ||
} | ||
}) | ||
|
||
pflag.CommandLine.AddGoFlagSet(klogFlags) | ||
pflag.CommandLine.AddGoFlagSet(flag.CommandLine) | ||
pflag.Parse() | ||
|
||
config := &Configuration{ | ||
KubeConfigFile: *argKubeConfigFile, | ||
KubeClient: nil, | ||
Port: *argPort, | ||
DaemonSetNamespace: *argDaemonSetNameSpace, | ||
DaemonSetName: *argDaemonSetName, | ||
Interval: *argInterval, | ||
Mode: *argMode, | ||
DNS: *argDns, | ||
} | ||
if err := config.initKubeClient(); err != nil { | ||
return nil, err | ||
} | ||
return config, nil | ||
} | ||
|
||
func (config *Configuration) initKubeClient() error { | ||
var cfg *rest.Config | ||
var err error | ||
if config.KubeConfigFile == "" { | ||
cfg, err = rest.InClusterConfig() | ||
if err != nil { | ||
klog.Errorf("use in cluster config failed %v", err) | ||
return err | ||
} | ||
} else { | ||
cfg, err = clientcmd.BuildConfigFromFlags("", config.KubeConfigFile) | ||
if err != nil { | ||
klog.Errorf("use --kubeconfig %s failed %v", config.KubeConfigFile, err) | ||
return err | ||
} | ||
} | ||
|
||
kubeClient, err := kubernetes.NewForConfig(cfg) | ||
if err != nil { | ||
klog.Errorf("init kubernetes client failed %v", err) | ||
return err | ||
} | ||
config.KubeClient = kubeClient | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package pinger | ||
|
||
import ( | ||
goping "github.com/sparrc/go-ping" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/klog" | ||
"net" | ||
"os/exec" | ||
"sync" | ||
"time" | ||
) | ||
|
||
func StartPinger(config *Configuration) { | ||
for { | ||
checkOvs() | ||
checkOvnController() | ||
ping(config) | ||
if config.Mode != "server" { | ||
break | ||
} | ||
time.Sleep(time.Duration(config.Interval) * time.Second) | ||
} | ||
} | ||
|
||
func ping(config *Configuration) { | ||
pingNodes(config.KubeClient) | ||
pingPods(config.KubeClient, config.DaemonSetNamespace, config.DaemonSetName) | ||
nslookup(config.DNS) | ||
} | ||
|
||
func pingNodes(client kubernetes.Interface) { | ||
klog.Infof("start to check node connectivity") | ||
nodes, err := client.CoreV1().Nodes().List(metav1.ListOptions{}) | ||
if err != nil { | ||
klog.Errorf("failed to list nodes, %v", err) | ||
return | ||
} | ||
wg := sync.WaitGroup{} | ||
for _, no := range nodes.Items { | ||
for _, addr := range no.Status.Addresses { | ||
if addr.Type == v1.NodeInternalIP { | ||
wg.Add(1) | ||
go func(nodeIP, nodeName string) { | ||
defer wg.Done() | ||
pinger, err := goping.NewPinger(nodeIP) | ||
if err != nil { | ||
klog.Errorf("failed to init pinger, %v", err) | ||
return | ||
} | ||
pinger.SetPrivileged(true) | ||
pinger.Count = 5 | ||
pinger.Run() | ||
stats := pinger.Statistics() | ||
klog.Infof("ping node: %s %s, count: %d, loss rate %.2f%%, average rtt %.2fms", | ||
nodeName, nodeIP, pinger.Count, stats.PacketLoss*100, float64(stats.AvgRtt)/float64(time.Millisecond)) | ||
}(addr.Address, no.Name) | ||
} | ||
} | ||
} | ||
wg.Wait() | ||
} | ||
|
||
func pingPods(client kubernetes.Interface, dsNamespace, dsName string) { | ||
klog.Infof("start to check pod connectivity") | ||
ds, err := client.AppsV1().DaemonSets(dsNamespace).Get(dsName, metav1.GetOptions{}) | ||
if err != nil { | ||
klog.Errorf("failed to get peer ds: %v", err) | ||
return | ||
} | ||
pods, err := client.CoreV1().Pods(dsNamespace).List(metav1.ListOptions{LabelSelector: labels.Set(ds.Spec.Selector.MatchLabels).String()}) | ||
if err != nil { | ||
klog.Errorf("failed to list peer pods: %v", err) | ||
return | ||
} | ||
|
||
wg := sync.WaitGroup{} | ||
for _, pod := range pods.Items { | ||
if pod.Status.PodIP != "" { | ||
wg.Add(1) | ||
go func(podIp, podName, podNamespace string) { | ||
defer wg.Done() | ||
pinger, err := goping.NewPinger(podIp) | ||
if err != nil { | ||
klog.Errorf("failed to init pinger, %v", err) | ||
return | ||
} | ||
pinger.SetPrivileged(true) | ||
pinger.Count = 5 | ||
pinger.Run() | ||
stats := pinger.Statistics() | ||
klog.Infof("ping pod: %s/%s %s, count: %d, loss rate %.2f, average rtt %.2fms", | ||
podNamespace, podName, podIp, pinger.Count, stats.PacketLoss*100, float64(stats.AvgRtt)/float64(time.Millisecond)) | ||
}(pod.Status.PodIP, pod.Name, pod.Namespace) | ||
} | ||
} | ||
wg.Wait() | ||
} | ||
|
||
func nslookup(dns string) { | ||
klog.Infof("start to check dns connectivity") | ||
t1 := time.Now() | ||
addrs, err := net.LookupHost(dns) | ||
elpased := time.Since(t1) | ||
if err != nil { | ||
klog.Errorf("failed to resolve dns %s, %v", dns, err) | ||
return | ||
} | ||
klog.Infof("resolve dns %s to %v in %.2fms", dns, addrs, float64(elpased)/float64(time.Millisecond)) | ||
} | ||
|
||
func checkOvs() { | ||
output, err := exec.Command("/usr/share/openvswitch/scripts/ovs-ctl", "status").CombinedOutput() | ||
if err != nil { | ||
klog.Errorf("check ovs status failed %v, %s", err, string(output)) | ||
} | ||
klog.Infof("ovs-vswitchd and ovsdb are up") | ||
} | ||
|
||
func checkOvnController() { | ||
output, err := exec.Command("/usr/share/openvswitch/scripts/ovn-ctl", "status_controller").CombinedOutput() | ||
if err != nil { | ||
klog.Errorf("check ovn_controller status failed %v, %s", err, string(output)) | ||
} | ||
klog.Infof("ovn_controller is up") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.