This repository has been archived by the owner on Aug 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 266
/
ingress.go
194 lines (158 loc) · 4.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package ingress
import (
"fmt"
"strings"
"github.com/jetstack/kube-lego/pkg/kubelego_const"
"github.com/Sirupsen/logrus"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
k8sMeta "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sExtensionsTyped "k8s.io/client-go/kubernetes/typed/extensions/v1beta1"
k8sApi "k8s.io/client-go/pkg/api/v1"
k8sExtensions "k8s.io/client-go/pkg/apis/extensions/v1beta1"
)
func IsSupportedIngressClass(supportedClass []string, in string) (out string, err error) {
out = strings.ToLower(in)
for _, ingClass := range supportedClass {
if ingClass == out {
return out, nil
}
}
return "", fmt.Errorf("unsupported ingress class '%s'. Did you you forget to specify LEGO_DEFAULT_INGRESS_CLASS ?", in)
}
func IgnoreIngress(ing *k8sExtensions.Ingress) error {
key := kubelego.AnnotationEnabled
val, ok := ing.Annotations[key]
if !ok {
return fmt.Errorf("has no annotation '%s'", key)
}
if strings.ToLower(val) != "true" {
return fmt.Errorf("annotation '%s' is not true", key)
}
return nil
}
func New(client kubelego.KubeLego, namespace string, name string) *Ingress {
ingress := &Ingress{
exists: true,
kubelego: client,
}
var err error
ingress.IngressApi, err = client.KubeClient().ExtensionsV1beta1().Ingresses(namespace).Get(name, k8sMeta.GetOptions{})
if err != nil {
if k8sErrors.IsNotFound(err) {
ingress.IngressApi = &k8sExtensions.Ingress{
ObjectMeta: k8sMeta.ObjectMeta{
Namespace: namespace,
Name: name,
},
}
ingress.exists = false
} else {
client.Log().Warn("Error while getting secret: ", err)
}
}
return ingress
}
func All(client kubelego.KubeLego) (ingresses []kubelego.Ingress, err error) {
ingSlice, err := client.KubeClient().Extensions().Ingresses(k8sApi.NamespaceAll).List(k8sMeta.ListOptions{})
if err != nil {
return
}
for i, _ := range ingSlice.Items {
ingresses = append(
ingresses,
&Ingress{
IngressApi: &ingSlice.Items[i],
exists: true,
kubelego: client,
},
)
}
return ingresses, nil
}
var _ kubelego.Ingress = &Ingress{}
type Ingress struct {
IngressApi *k8sExtensions.Ingress
exists bool
kubelego kubelego.KubeLego
}
func (i *Ingress) Log() *logrus.Entry {
log := i.kubelego.Log().WithField("context", "ingress")
if i.IngressApi != nil && i.IngressApi.Name != "" {
log = log.WithField("name", i.IngressApi.Name)
}
if i.IngressApi != nil && i.IngressApi.Namespace != "" {
log = log.WithField("namespace", i.IngressApi.Namespace)
}
return log
}
func (o *Ingress) client() k8sExtensionsTyped.IngressInterface {
return o.kubelego.KubeClient().Extensions().Ingresses(o.IngressApi.Namespace)
}
func (o *Ingress) Save() (err error) {
var obj *k8sExtensions.Ingress
// check if it contians rules
if len(o.IngressApi.Spec.Rules) > 0 {
if o.exists {
obj, err = o.client().Update(o.IngressApi)
} else {
obj, err = o.client().Create(o.IngressApi)
o.exists = true
}
} else {
if o.exists {
err = o.client().Delete(o.IngressApi.Namespace, &k8sMeta.DeleteOptions{})
obj = nil
}
}
if err != nil {
return
}
o.IngressApi = obj
return
}
func (i *Ingress) Delete() error {
if i.IngressApi == nil || !i.exists {
return nil
}
err := i.client().Delete(i.IngressApi.Namespace, &k8sMeta.DeleteOptions{})
if err != nil {
return err
}
i.IngressApi = nil
return nil
}
func (i *Ingress) Object() *k8sExtensions.Ingress {
return i.IngressApi
}
func (i *Ingress) IngressClass() string {
val, ok := i.IngressApi.Annotations[kubelego.AnnotationIngressClass]
if !ok {
return i.kubelego.LegoDefaultIngressClass()
}
return strings.ToLower(val)
}
func (i *Ingress) Ignore() bool {
err := IgnoreIngress(i.IngressApi)
if err != nil {
i.Log().Info("ignoring as ", err)
return true
}
_, err = IsSupportedIngressClass(i.kubelego.LegoSupportedIngressClass(), i.IngressClass())
if err != nil {
i.Log().Info("ignoring as ", err)
return true
}
return false
}
func (i *Ingress) KubeLego() kubelego.KubeLego {
return i.kubelego
}
func (i *Ingress) Tls() (out []kubelego.Tls) {
for count, _ := range i.IngressApi.Spec.TLS {
out = append(out, &Tls{
IngressTLS: &i.IngressApi.Spec.TLS[count],
ingress: i,
})
}
return
}