/
wrap.go
61 lines (47 loc) · 1.36 KB
/
wrap.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
55
56
57
58
59
60
61
package main
import (
"strings"
"github.com/denkhaus/rancher-meta-template/logging"
"github.com/juju/errors"
"github.com/rancher/go-rancher-metadata/metadata"
)
//////////////////////////////////////////////////////////////////////////////////
type ServiceWrap struct {
metadata.Service
}
//////////////////////////////////////////////////////////////////////////////////
type ContainerWrap struct {
metadata.Container
}
//////////////////////////////////////////////////////////////////////////////////
func (p ContainerWrap) PortInternal(idx int) (string, error) {
if len(p.Ports) == 0 {
return "", nil
}
if idx >= len(p.Ports) {
return "", errors.Errorf("PortInternal:: index %d out of range", idx)
}
part := p.Ports[idx]
if part == "" {
return part, nil
}
logging.Debug(part)
items := strings.Split(part, ":")
part = items[len(items)-1]
if strings.Contains(part, "/") {
part = strings.Split(part, "/")[0]
}
return part, nil
}
//////////////////////////////////////////////////////////////////////////////////
func (p ContainerWrap) LabelByKey(key string) (string, error) {
if val, ok := p.Labels[key]; ok {
return val, nil
}
logging.Debug("LabelByKey:: container: %s: key %q is not present", p.Name, key)
return "", nil
}
//////////////////////////////////////////////////////////////////////////////////
type HostWrap struct {
metadata.Host
}