Skip to content

Commit f413e18

Browse files
committed
MINOR: CR: assign global and defaults CR via global annotations
ConfigMap annotations 'cr-global' and 'cr-defaults' can now be used to assign a specific global and default CRs to the haproxy instance,
1 parent 1198fff commit f413e18

File tree

6 files changed

+144
-26
lines changed

6 files changed

+144
-26
lines changed

controller/annotations/models.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package annotations
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/haproxytech/client-native/v2/models"
7+
8+
"github.com/haproxytech/kubernetes-ingress/controller/annotations/common"
9+
"github.com/haproxytech/kubernetes-ingress/controller/store"
10+
)
11+
12+
// ModelDefaults takes an annotation holding the path of a defaults cr and returns corresponding Defaults model
13+
func ModelDefaults(name, defaultNS string, k store.K8s, annotations ...map[string]string) (defaults *models.Defaults, err error) {
14+
d, modelErr := model(name, defaultNS, 2, k, annotations...)
15+
if modelErr != nil {
16+
err = modelErr
17+
return
18+
}
19+
if d != nil {
20+
defaults = d.(*models.Defaults)
21+
}
22+
return
23+
}
24+
25+
// ModelGlobal takes an annotation holding the path of a global cr and returns corresponding Global model
26+
func ModelGlobal(name, defaultNS string, k store.K8s, annotations ...map[string]string) (global *models.Global, err error) {
27+
g, modelErr := model(name, defaultNS, 0, k, annotations...)
28+
if modelErr != nil {
29+
err = modelErr
30+
return
31+
}
32+
if g != nil {
33+
global = g.(*models.Global)
34+
}
35+
return
36+
}
37+
38+
// ModelLog takes an annotation holding the path of a global cr and returns corresponding LogTargerts model
39+
func ModelLog(name, defaultNS string, k store.K8s, annotations ...map[string]string) (log models.LogTargets, err error) {
40+
l, modelErr := model(name, defaultNS, 1, k, annotations...)
41+
if modelErr != nil {
42+
err = modelErr
43+
return
44+
}
45+
if l != nil {
46+
log = l.(models.LogTargets)
47+
}
48+
return
49+
}
50+
51+
func model(name, defaultNS string, crType int, k store.K8s, annotations ...map[string]string) (model interface{}, err error) {
52+
var crNS, crName string
53+
crNS, crName, err = common.GetK8sPath(name, annotations...)
54+
if err != nil {
55+
err = fmt.Errorf("annotation '%s': %w", name, err)
56+
return
57+
}
58+
if crName == "" {
59+
return
60+
}
61+
if crNS == "" {
62+
crNS = defaultNS
63+
}
64+
ns, nsOk := k.Namespaces[crNS]
65+
if !nsOk {
66+
return nil, fmt.Errorf("annotation %s: custom resource '%s/%s' doest not exist, namespace not found", name, crNS, crName)
67+
}
68+
switch crType {
69+
case 0:
70+
global, globalOk := ns.CRs.Global[crName]
71+
if !globalOk {
72+
return nil, fmt.Errorf("annotation %s: custom resource '%s/%s' doest not exist", name, crNS, crName)
73+
}
74+
return global, nil
75+
case 1:
76+
lg, lgOk := ns.CRs.LogTargets[crName]
77+
if !lgOk {
78+
return nil, fmt.Errorf("annotation %s: custom resource '%s/%s' doest not exist", name, crNS, crName)
79+
}
80+
return lg, nil
81+
case 2:
82+
defaults, defaultsOk := ns.CRs.Defaults[crName]
83+
if !defaultsOk {
84+
return nil, fmt.Errorf("annotation %s: custom resource '%s/%s' doest not exist", name, crNS, crName)
85+
}
86+
return defaults, nil
87+
}
88+
return nil, nil
89+
}

controller/crs.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,19 @@ func (c GlobalCR) GetInformer(eventChan chan SyncDataEvent, factory informers.Sh
6868
}
6969

7070
func (c GlobalCR) ProcessEvent(s *store.K8s, job SyncDataEvent) bool {
71+
ns := s.GetNamespace(job.Namespace)
7172
if job.Data == nil {
72-
s.CR.Global = nil
73-
s.CR.LogTargets = nil
73+
delete(ns.CRs.Global, job.Name)
74+
delete(ns.CRs.LogTargets, job.Name)
7475
return true
7576
}
7677
data, ok := job.Data.(*corev1alpha1.Global)
7778
if !ok {
7879
logger.Warning(CoreGroupVersion + ": type mismatch with Global kind")
7980
return false
8081
}
81-
s.CR.Global = data.Spec.Config
82-
s.CR.LogTargets = data.Spec.LogTargets
82+
ns.CRs.Global[job.Name] = data.Spec.Config
83+
ns.CRs.LogTargets[job.Name] = data.Spec.LogTargets
8384
return true
8485
}
8586

@@ -115,15 +116,16 @@ func (c DefaultsCR) GetInformer(eventChan chan SyncDataEvent, factory informers.
115116
}
116117

117118
func (c DefaultsCR) ProcessEvent(s *store.K8s, job SyncDataEvent) bool {
119+
ns := s.GetNamespace(job.Namespace)
118120
if job.Data == nil {
119-
s.CR.Defaults = nil
121+
delete(ns.CRs.Defaults, job.Name)
120122
return true
121123
}
122124
data, ok := job.Data.(*corev1alpha1.Defaults)
123125
if !ok {
124126
logger.Warning(CoreGroupVersion + ": type mismatch with Defaults kind")
125127
return false
126128
}
127-
s.CR.Defaults = data.Spec.Config
129+
ns.CRs.Defaults[job.Name] = data.Spec.Config
128130
return true
129131
}

controller/global.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,16 @@ func (c *HAProxyController) globalCfg() (reload, restart bool) {
4949
logger.Error(errL)
5050
return
5151
}
52-
newGlobal = &models.Global{}
53-
if c.Store.CR.Global != nil {
54-
newGlobal = c.Store.CR.Global
55-
newLg = c.Store.CR.LogTargets
56-
} else {
52+
newGlobal, err = annotations.ModelGlobal("cr-global", c.PodNamespace, c.Store, c.Store.ConfigMaps.Main.Annotations)
53+
if err != nil {
54+
logger.Errorf("Global config: %s", err)
55+
}
56+
newLg, err = annotations.ModelLog("cr-global", c.PodNamespace, c.Store, c.Store.ConfigMaps.Main.Annotations)
57+
if err != nil {
58+
logger.Errorf("Global logging: %s", err)
59+
}
60+
if newGlobal == nil {
61+
newGlobal = &models.Global{}
5762
for _, a := range annotations.Global(newGlobal, &newLg) {
5863
err = a.Process(c.Store, c.Store.ConfigMaps.Main.Annotations)
5964
if err != nil {
@@ -96,10 +101,12 @@ func (c *HAProxyController) defaultsCfg() (reload bool) {
96101
logger.Error(err)
97102
return
98103
}
99-
newDefaults = &models.Defaults{}
100-
if c.Store.CR.Defaults != nil {
101-
newDefaults = c.Store.CR.Defaults
102-
} else {
104+
newDefaults, err = annotations.ModelDefaults("cr-defaults", c.PodNamespace, c.Store, c.Store.ConfigMaps.Main.Annotations)
105+
if err != nil {
106+
logger.Errorf("Defaults config: %s", err)
107+
}
108+
if newDefaults == nil {
109+
newDefaults = &models.Defaults{}
103110
for _, a := range annotations.Defaults(newDefaults) {
104111
logger.Error(a.Process(c.Store, c.Store.ConfigMaps.Main.Annotations))
105112
}

controller/kubernetes.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"k8s.io/client-go/tools/cache"
3030
"k8s.io/client-go/tools/clientcmd"
3131

32+
"github.com/haproxytech/client-native/v2/models"
33+
3234
ingstatus "github.com/haproxytech/kubernetes-ingress/controller/status"
3335
"github.com/haproxytech/kubernetes-ingress/controller/store"
3436
"github.com/haproxytech/kubernetes-ingress/controller/utils"
@@ -123,7 +125,12 @@ func (k *K8s) EventsNamespaces(channel chan SyncDataEvent, stop chan struct{}, i
123125
Services: make(map[string]*store.Service),
124126
Ingresses: make(map[string]*store.Ingress),
125127
Secret: make(map[string]*store.Secret),
126-
Status: status,
128+
CRs: &store.CustomResources{
129+
Global: make(map[string]*models.Global),
130+
Defaults: make(map[string]*models.Defaults),
131+
LogTargets: make(map[string]models.LogTargets),
132+
},
133+
Status: status,
127134
}
128135
k.Logger.Tracef("%s %s: %s", NAMESPACE, item.Status, item.Name)
129136
channel <- SyncDataEvent{SyncType: NAMESPACE, Namespace: item.Name, Data: item}
@@ -141,7 +148,12 @@ func (k *K8s) EventsNamespaces(channel chan SyncDataEvent, stop chan struct{}, i
141148
Services: make(map[string]*store.Service),
142149
Ingresses: make(map[string]*store.Ingress),
143150
Secret: make(map[string]*store.Secret),
144-
Status: status,
151+
CRs: &store.CustomResources{
152+
Global: make(map[string]*models.Global),
153+
Defaults: make(map[string]*models.Defaults),
154+
LogTargets: make(map[string]models.LogTargets),
155+
},
156+
Status: status,
145157
}
146158
k.Logger.Tracef("%s %s: %s", NAMESPACE, item.Status, item.Name)
147159
channel <- SyncDataEvent{SyncType: NAMESPACE, Namespace: item.Name, Data: item}

controller/store/store.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ type K8s struct {
2828
IngressClasses map[string]*IngressClass
2929
NamespacesAccess NamespacesWatch
3030
ConfigMaps ConfigMaps
31-
CR CustomResources
32-
}
33-
34-
type CustomResources struct {
35-
Global *models.Global
36-
Defaults *models.Defaults
37-
LogTargets models.LogTargets
3831
}
3932

4033
type NamespacesWatch struct {
@@ -72,7 +65,6 @@ func NewK8sStore(args utils.OSArgs) K8s {
7265
Name: args.ConfigMapPatternFiles.Name,
7366
},
7467
},
75-
CR: CustomResources{},
7668
}
7769
}
7870

@@ -177,7 +169,12 @@ func (k K8s) GetNamespace(name string) *Namespace {
177169
Services: make(map[string]*Service),
178170
Ingresses: make(map[string]*Ingress),
179171
Secret: make(map[string]*Secret),
180-
Status: ADDED,
172+
CRs: &CustomResources{
173+
Global: make(map[string]*models.Global),
174+
Defaults: make(map[string]*models.Defaults),
175+
LogTargets: make(map[string]models.LogTargets),
176+
},
177+
Status: ADDED,
181178
}
182179
k.Namespaces[name] = newNamespace
183180
return newNamespace

controller/store/types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
package store
1616

17+
import (
18+
"github.com/haproxytech/client-native/v2/models"
19+
)
20+
1721
// ServicePort describes port of a service
1822
type ServicePort struct {
1923
Name string
@@ -72,9 +76,16 @@ type Namespace struct {
7276
Endpoints map[string]*Endpoints
7377
Services map[string]*Service
7478
Secret map[string]*Secret
79+
CRs *CustomResources
7580
Status Status
7681
}
7782

83+
type CustomResources struct {
84+
Global map[string]*models.Global
85+
Defaults map[string]*models.Defaults
86+
LogTargets map[string]models.LogTargets
87+
}
88+
7889
type IngressClass struct {
7990
APIVersion string
8091
Name string

0 commit comments

Comments
 (0)