/
podstring.go
54 lines (45 loc) · 1.42 KB
/
podstring.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package probe
import (
"fmt"
"github.com/pkg/errors"
"strings"
)
// PodString represents a namespace 'x' + pod 'a' as "x/a".
type PodString string
// NewPodString instantiates a PodString from the given namespace and name.
func NewPodString(namespace string, podName string) PodString {
return PodString(fmt.Sprintf("%s/%s", namespace, podName))
}
// String converts back to a string
func (pod PodString) String() string {
return string(pod)
}
func (pod PodString) split() (string, string) {
pieces := strings.Split(string(pod), "/")
if len(pieces) != 2 {
panic(errors.Errorf("expected ns/pod, found %+v", pieces))
}
return pieces[0], pieces[1]
}
// Namespace extracts the namespace
func (pod PodString) Namespace() string {
ns, _ := pod.split()
return ns
}
// PodName extracts the pod name
func (pod PodString) PodName() string {
_, podName := pod.split()
return podName
}
// Peer is used for matching pods by either or both of the pod's namespace and name.
type Peer struct {
Namespace string
Pod string
}
// Matches checks whether the Peer matches the PodString:
// - an empty namespace means the namespace will always match
// - otherwise, the namespace must match the PodString's namespace
// - same goes for Pod: empty matches everything, otherwise must match exactly
func (p *Peer) Matches(pod PodString) bool {
return (p.Namespace == "" || p.Namespace == pod.Namespace()) && (p.Pod == "" || p.Pod == pod.PodName())
}