-
Notifications
You must be signed in to change notification settings - Fork 1
/
ingress.go
83 lines (72 loc) · 2.17 KB
/
ingress.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package kube
import (
"fmt"
"strings"
"strconv"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const (
IngressConfigConfigmap = "ingress-config"
Domain = "domain"
Email = "email"
TLS = "tls"
Issuer = "issuer"
Exposer = "exposer"
)
type IngressConfig struct {
Email string `structs:"email" yaml:"email" json:"email"`
Domain string `structs:"domain" yaml:"domain" json:"domain"`
Issuer string `structs:"issuer" yaml:"issuer" json:"issuer"`
Exposer string `structs:"exposer" yaml:"exposer" json:"exposer"`
TLS bool `structs:"tls" yaml:"tls" json:"tls"`
}
func GetIngress(client kubernetes.Interface, ns, name string) (string, error) {
ing, err := client.ExtensionsV1beta1().Ingresses(ns).Get(name, meta_v1.GetOptions{})
if err != nil {
return "", fmt.Errorf("failed to get ingress rule %s. error: %v", name, err)
}
if ing == nil {
return "", fmt.Errorf("failed to find ingress rule %s", name)
}
// default to the first rule
if len(ing.Spec.Rules) > 0 {
return ing.Spec.Rules[0].Host, nil
}
return "", fmt.Errorf("no hostname found for ingress rule %s", name)
}
func GetIngressConfig(c kubernetes.Interface, ns string) (IngressConfig, error) {
var ic IngressConfig
configMapInterface := c.CoreV1().ConfigMaps(ns)
cm, err := configMapInterface.Get(IngressConfigConfigmap, meta_v1.GetOptions{})
data := map[string]string{}
if err != nil {
cm2, err2 := configMapInterface.Get("exposecontroller", meta_v1.GetOptions{})
if err2 != nil {
return ic, err
}
config := cm2.Data["config.yml"]
lines := strings.Split(config, "\n")
for _, pair := range lines {
z := strings.Split(pair, ":")
data[z[0]] = strings.TrimSpace(z[1])
}
return ic, err
} else {
data = cm.Data
}
ic.Domain = data[Domain]
ic.Email = data[Email]
ic.Exposer = data[Exposer]
ic.Issuer = data[Issuer]
tls, exists := data[TLS]
if exists {
ic.TLS, err = strconv.ParseBool(tls)
if err != nil {
return ic, fmt.Errorf("failed to parse TLS string %s to bool from %s: %v", tls, IngressConfigConfigmap, err)
}
} else {
ic.TLS = false
}
return ic, nil
}