Skip to content

Commit 3d9893e

Browse files
committed
feat(main): add probe library
1 parent 4dc6544 commit 3d9893e

File tree

19 files changed

+2053
-0
lines changed

19 files changed

+2053
-0
lines changed

cmd/app/options/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ import (
2828
type Options struct {
2929
LeaderElect bool
3030
LeaderElection *leaderelection.LeaderElectionConfig
31+
SyncPeriod time.Duration
3132
}
3233

34+
// fs.DurationVar(&o.config.IPVS.SyncPeriod.Duration, "ipvs-sync-period", o.config.IPVS.SyncPeriod.Duration, "The maximum interval of how often ipvs rules are refreshed (e.g. '5s', '1m', '2h22m'). Must be greater than 0.")
3335
func NewOptions() *Options {
3436
s := &Options{
3537
LeaderElection: &leaderelection.LeaderElectionConfig{

controllers/probe.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
Copyright 2022 [email protected].
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controllers
18+
19+
import (
20+
"fmt"
21+
"github.com/cuisongliu/endpoints-balance/library/probe"
22+
execprobe "github.com/cuisongliu/endpoints-balance/library/probe/exec"
23+
httpprobe "github.com/cuisongliu/endpoints-balance/library/probe/http"
24+
tcpprobe "github.com/cuisongliu/endpoints-balance/library/probe/tcp"
25+
v1 "k8s.io/api/core/v1"
26+
"k8s.io/apimachinery/pkg/util/intstr"
27+
"k8s.io/klog"
28+
"net"
29+
"net/http"
30+
"net/url"
31+
"strconv"
32+
"strings"
33+
"time"
34+
)
35+
36+
// Prober helps to check the liveness/readiness/startup of a container.
37+
type prober struct {
38+
exec execprobe.Prober
39+
http httpprobe.Prober
40+
tcp tcpprobe.Prober
41+
}
42+
43+
// NewProber creates a Prober, it takes a command runner and
44+
// several container info managers.
45+
func newProber() *prober {
46+
47+
const followNonLocalRedirects = false
48+
return &prober{
49+
exec: execprobe.New(),
50+
http: httpprobe.New(followNonLocalRedirects),
51+
tcp: tcpprobe.New(),
52+
}
53+
}
54+
func (pb *prober) runProbe(p *v1.Probe) (probe.Result, string, error) {
55+
timeout := time.Duration(p.TimeoutSeconds) * time.Second
56+
if p.Exec != nil {
57+
klog.V(4).Infof("Exec-Probe Command: %v", p.Exec.Command)
58+
//command := ""
59+
return probe.Unknown, "", nil
60+
}
61+
if p.HTTPGet != nil {
62+
scheme := strings.ToLower(string(p.HTTPGet.Scheme))
63+
host := p.HTTPGet.Host
64+
port, err := extractPort(p.HTTPGet.Port)
65+
if err != nil {
66+
return probe.Unknown, "", err
67+
}
68+
path := p.HTTPGet.Path
69+
klog.V(4).Infof("HTTP-Probe Host: %v://%v, Port: %v, Path: %v", scheme, host, port, path)
70+
url := formatURL(scheme, host, port, path)
71+
headers := buildHeader(p.HTTPGet.HTTPHeaders)
72+
klog.V(4).Infof("HTTP-Probe Headers: %v", headers)
73+
return pb.http.Probe(url, headers, timeout)
74+
}
75+
if p.TCPSocket != nil {
76+
port, err := extractPort(p.TCPSocket.Port)
77+
if err != nil {
78+
return probe.Unknown, "", err
79+
}
80+
host := p.TCPSocket.Host
81+
klog.V(4).Infof("TCP-Probe Host: %v, Port: %v, Timeout: %v", host, port, timeout)
82+
return pb.tcp.Probe(host, port, timeout)
83+
}
84+
klog.Warning("Failed to find probe builder for annotation")
85+
return probe.Unknown, "", fmt.Errorf("missing probe handler")
86+
}
87+
88+
func extractPort(param intstr.IntOrString) (int, error) {
89+
port := -1
90+
switch param.Type {
91+
case intstr.Int:
92+
port = param.IntValue()
93+
default:
94+
return port, fmt.Errorf("intOrString had no kind: %+v", param)
95+
}
96+
if port > 0 && port < 65536 {
97+
return port, nil
98+
}
99+
return port, fmt.Errorf("invalid port number: %v", port)
100+
}
101+
102+
// formatURL formats a URL from args. For testability.
103+
func formatURL(scheme string, host string, port int, path string) *url.URL {
104+
u, err := url.Parse(path)
105+
// Something is busted with the path, but it's too late to reject it. Pass it along as is.
106+
if err != nil {
107+
u = &url.URL{
108+
Path: path,
109+
}
110+
}
111+
u.Scheme = scheme
112+
u.Host = net.JoinHostPort(host, strconv.Itoa(port))
113+
return u
114+
}
115+
116+
// buildHeaderMap takes a list of HTTPHeader <name, value> string
117+
// pairs and returns a populated string->[]string http.Header map.
118+
func buildHeader(headerList []v1.HTTPHeader) http.Header {
119+
headers := make(http.Header)
120+
for _, header := range headerList {
121+
headers[header.Name] = append(headers[header.Name], header.Value)
122+
}
123+
return headers
124+
}

library/exec/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Exec
2+
3+
This package provides an interface for `os/exec`. It makes it easier to mock
4+
and replace in tests, especially with the [FakeExec](testing/fake_exec.go)
5+
struct.

library/exec/doc.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package exec provides an injectable interface and implementations for running commands.
18+
package exec // import "k8s.io/utils/exec"

0 commit comments

Comments
 (0)