Skip to content

Commit 57be050

Browse files
ivanmatmatioktalz
authored andcommitted
MINOR: add 'standalone-backend' annotation on ingress/service to create a separate backend for this ingress
1 parent 8103720 commit 57be050

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

pkg/controller/global.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (c *HAProxyController) handleDefaultService() (reload bool) {
168168
SvcName: name,
169169
IsDefaultBackend: true,
170170
}
171-
if svc, err = service.New(c.store, ingressPath, nil, false, c.store.ConfigMaps.Main.Annotations); err == nil {
171+
if svc, err = service.New(c.store, ingressPath, nil, false, "", "", c.store.ConfigMaps.Main.Annotations); err == nil {
172172
reload, err = svc.SetDefaultBackend(c.store, c.haproxy, []string{c.haproxy.FrontHTTP, c.haproxy.FrontHTTPS}, c.annotations)
173173
}
174174
if err != nil {
@@ -237,7 +237,7 @@ func (c *HAProxyController) handleDefaultLocalService() (reload bool) {
237237
IsDefaultBackend: true,
238238
}
239239

240-
if svc, err = service.New(c.store, ingressPath, nil, false); err == nil {
240+
if svc, err = service.New(c.store, ingressPath, nil, false, "", ""); err == nil {
241241
reload, err = svc.SetDefaultBackend(c.store, c.haproxy, []string{c.haproxy.FrontHTTP, c.haproxy.FrontHTTPS}, c.annotations)
242242
}
243243
if err != nil {

pkg/handler/tcp-services.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (handler TCPServices) updateTCPFrontend(k store.K8s, h haproxy.HAProxy, fro
198198
SvcPortInt: p.port,
199199
IsDefaultBackend: true,
200200
}
201-
if svc, err = service.New(k, path, nil, true); err == nil {
201+
if svc, err = service.New(k, path, nil, true, "", ""); err == nil {
202202
r, err = svc.SetDefaultBackend(k, h, []string{frontend.Name}, a)
203203
}
204204
return reload || r, err

pkg/ingress/ingress.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (i Ingress) supported(k8s store.K8s, a annotations.Annotations) (supported
9494
}
9595

9696
func (i *Ingress) handlePath(k store.K8s, h haproxy.HAProxy, host string, path *store.IngressPath, a annotations.Annotations) (reload bool, err error) {
97-
svc, err := service.New(k, path, h.Certificates, i.sslPassthrough, i.resource.Annotations, k.ConfigMaps.Main.Annotations)
97+
svc, err := service.New(k, path, h.Certificates, i.sslPassthrough, i.resource.Namespace, i.resource.Name, i.resource.Annotations, k.ConfigMaps.Main.Annotations)
9898
if err != nil {
9999
return
100100
}
@@ -203,7 +203,7 @@ func addRules(list rules.List, h haproxy.HAProxy, ingressRule bool) []rules.Rule
203203
func (i *Ingress) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (reload bool) {
204204
// Default Backend
205205
if i.resource.DefaultBackend != nil {
206-
svc, err := service.New(k, i.resource.DefaultBackend, h.Certificates, false, i.resource.Annotations, k.ConfigMaps.Main.Annotations)
206+
svc, err := service.New(k, i.resource.DefaultBackend, h.Certificates, false, i.resource.Namespace, i.resource.Name, i.resource.Annotations, k.ConfigMaps.Main.Annotations)
207207
if svc != nil {
208208
reload, err = svc.SetDefaultBackend(k, h, []string{h.FrontHTTP, h.FrontHTTPS}, a)
209209
}

pkg/service/service.go

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,44 @@ var logger = utils.GetLogger()
3535
const cookieKey = "ohph7OoGhong"
3636

3737
type Service struct {
38-
path *store.IngressPath
39-
resource *store.Service
40-
backend *models.Backend
41-
certs certs.Certificates
42-
annotations []map[string]string
43-
modeTCP bool
44-
newBackend bool
38+
path *store.IngressPath
39+
resource *store.Service
40+
backend *models.Backend
41+
certs certs.Certificates
42+
annotations []map[string]string
43+
modeTCP bool
44+
newBackend bool
45+
standalone bool
46+
ingressName string
47+
ingressNamespace string
4548
}
4649

4750
// New returns a Service instance to handle the k8s IngressPath resource given in params.
4851
// An error will be returned if there is no k8s Service resource corresponding to the service description in IngressPath.
49-
func New(k store.K8s, path *store.IngressPath, certs certs.Certificates, tcpService bool, annList ...map[string]string) (*Service, error) {
52+
func New(k store.K8s, path *store.IngressPath, certs certs.Certificates, tcpService bool, ingressNamespace, ingressName string, annList ...map[string]string) (*Service, error) {
5053
service, err := k.GetService(path.SvcNamespace, path.SvcName)
5154
if err != nil {
5255
return nil, err
5356
}
5457
a := make([]map[string]string, 1, 3)
5558
a[0] = service.Annotations
5659
a = append(a, annList...)
60+
var standalone bool
61+
for _, anns := range a {
62+
standalone = len(anns) != 0 && anns["standalone-backend"] == "true"
63+
if standalone {
64+
break
65+
}
66+
}
5767
return &Service{
58-
path: path,
59-
resource: service,
60-
certs: certs,
61-
annotations: a,
62-
modeTCP: tcpService,
68+
path: path,
69+
resource: service,
70+
certs: certs,
71+
annotations: a,
72+
modeTCP: tcpService,
73+
standalone: standalone,
74+
ingressName: ingressName,
75+
ingressNamespace: ingressNamespace,
6376
}, nil
6477
}
6578

@@ -104,11 +117,20 @@ func (s *Service) GetBackendName() (name string, err error) {
104117
}
105118
return
106119
}
120+
resourceNamespace := s.resource.Namespace
121+
resourceName := s.resource.Name
122+
prefix := ""
123+
if s.standalone && s.ingressName != "" {
124+
resourceName = s.ingressName
125+
resourceNamespace = s.ingressNamespace
126+
prefix = "ing_"
127+
}
128+
107129
s.path.SvcPortResolved = &svcPort
108130
if svcPort.Name != "" {
109-
name = fmt.Sprintf("%s_%s_%s", s.resource.Namespace, s.resource.Name, svcPort.Name)
131+
name = fmt.Sprintf("%s%s_%s_%s", prefix, resourceNamespace, resourceName, svcPort.Name)
110132
} else {
111-
name = fmt.Sprintf("%s_%s_%s", s.resource.Namespace, s.resource.Name, strconv.Itoa(int(svcPort.Port)))
133+
name = fmt.Sprintf("%s%s_%s_%s", prefix, resourceNamespace, resourceName, strconv.Itoa(int(svcPort.Port)))
112134
}
113135
return
114136
}

0 commit comments

Comments
 (0)