Skip to content

Commit

Permalink
Domain namespace (#86)
Browse files Browse the repository at this point in the history
* concat domain ns

* Fix test

* refactor domain enqueuing

---------

Co-authored-by: Lucian Ghinet <[email protected]>
  • Loading branch information
fraliv13 and lghinet committed Jan 12, 2024
1 parent 73f1fe2 commit f3f11ed
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ func (c *ConfigurationDomainController) syncHandler(key string) error {

// Get the ConfigurationDomain resource
configDomain, err := c.configDomainsLister.ConfigurationDomains(namespace).Get(domain)

if err != nil {
if k8serrors.IsNotFound(err) {
return nil
Expand Down Expand Up @@ -414,6 +415,7 @@ func (c *ConfigurationDomainController) handleConfigMap(platform, namespace, dom

func (c *ConfigurationDomainController) enqueueConfigurationDomain(namespace, domain string) {
key := encodeDomainKey(namespace, domain)
klog.InfoS("enqueue ConfigurationDomain", "namespace", namespace, "domain", domain)
c.workqueue.Add(key)
}

Expand Down Expand Up @@ -461,13 +463,13 @@ func addConfigMapHandlers(informer coreInformers.ConfigMapInformer, handler func
AddFunc: func(obj interface{}) {
comp := obj.(*corev1.ConfigMap)

if platform, domain, ok := getConfigMapPlatformAndDomain(comp); ok {
if platform, domainNs, domain, ok := getConfigMapPlatformNsAndDomain(comp); ok {
if isOutputConfigMap(comp) {
return
}

klog.V(4).InfoS("Config map added", "name", comp.Name, "namespace", comp.Namespace, "platform", platform, "domain", domain)
handler(platform, comp.Namespace, domain)
klog.V(4).InfoS("Config map added", "name", comp.Name, "namespace", domainNs, "platform", platform, "domain", domain)
handler(platform, domainNs, domain)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
Expand All @@ -478,22 +480,22 @@ func addConfigMapHandlers(informer coreInformers.ConfigMapInformer, handler func
// return
// }

oldPlatform, oldDomain, oldOk := getConfigMapPlatformAndDomain(oldComp)
newPlatform, newDomain, newOk := getConfigMapPlatformAndDomain(newComp)
oldPlatform, oldDomainNs, oldDomain, oldOk := getConfigMapPlatformNsAndDomain(oldComp)
newPlatform, newDomainNs, newDomain, newOk := getConfigMapPlatformNsAndDomain(newComp)
targetChanged := oldPlatform != newPlatform || oldDomain != newDomain

if !oldOk && !newOk {
return
}

if oldOk && targetChanged {
klog.V(4).InfoS("Config map updated", "name", newComp.Name, "namespace", newComp.Namespace, "platform", oldPlatform, "domain", oldDomain)
handler(oldPlatform, oldComp.Namespace, oldDomain)
klog.V(4).InfoS("Config map updated", "name", newComp.Name, "namespace", oldDomainNs, "platform", oldPlatform, "domain", oldDomain)
handler(oldPlatform, oldDomainNs, oldDomain)
}
dataChanged := !reflect.DeepEqual(oldComp.Data, newComp.Data) || !reflect.DeepEqual(oldComp.Labels, newComp.Labels)
if newOk && dataChanged {
klog.V(4).InfoS("Config map updated", "name", newComp.Name, "namespace", newComp.Namespace, "platform", newPlatform, "domain", newDomain)
handler(newPlatform, newComp.Namespace, newDomain)
klog.V(4).InfoS("Config map updated", "name", newComp.Name, "namespace", newDomainNs, "platform", newPlatform, "domain", newDomain)
handler(newPlatform, newDomainNs, newDomain)
}
},
DeleteFunc: func(obj interface{}) {
Expand All @@ -503,10 +505,10 @@ func addConfigMapHandlers(informer coreInformers.ConfigMapInformer, handler func
// return
// }

if platform, domain, ok := getConfigMapPlatformAndDomain(comp); ok {
if platform, domainNs, domain, ok := getConfigMapPlatformNsAndDomain(comp); ok {

klog.V(4).InfoS("Config map deleted", "name", comp.Name, "namespace", comp.Namespace, "platform", platform, "domain", domain)
handler(platform, comp.Namespace, domain)
klog.V(4).InfoS("Config map deleted", "name", comp.Name, "namespace", domainNs, "platform", platform, "domain", domain)
handler(platform, domainNs, domain)
}
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,72 @@ func TestConfigurationDomainController_processNextWorkItem(t *testing.T) {
t.Error("expected output config ", expectedOutput, ", got", output.Data)
}

msg := <-msgChan
if msg.Topic != syncedSuccessfullyTopic {
t.Error("expected message published to topic ", syncedSuccessfullyTopic, ", got", msg.Topic)
}
})

t.Run("aggregate domain specific config maps from a different namespace", func(t *testing.T) {
// Arrange
platform, namespace1, namespace2, domain := "pl1", "n1", "n2", "domain"
configMaps := []runtime.Object{
newConfigMap("configMap1", domain, namespace1, platform, map[string]string{"k1": "v1"}),
newConfigMap("configMap2", domain+"."+namespace2, namespace1, platform, map[string]string{"k2": "v2"}),
newConfigMap("configMap3", controllers.GlobalDomainLabelValue, namespace1, platform, map[string]string{"k3": "v3"}),
newConfigMap("configMap4", controllers.GlobalDomainLabelValue, namespace2, platform, map[string]string{"k4": "v4"}),
}
configurationDomains := []runtime.Object{
newConfigurationDomain(domain, namespace1, platform, true, false),
newConfigurationDomain(domain, namespace2, platform, true, false),
}
platforms := []runtime.Object{
newPlatform(platform, platform),
}
spcs := []runtime.Object{}

c, msgChan := runController(platforms, configurationDomains, configMaps, spcs)
if c.workqueue.Len() != 2 {
items := c.workqueue.Len()
t.Error("queue should have only 2 items, but it has", items)
return
}

// Act
if result := c.processNextWorkItem(); !result {
t.Error("processing failed")
}
if result := c.processNextWorkItem(); !result {
t.Error("processing failed")
}

// Assert
if c.workqueue.Len() != 0 {
item, _ := c.workqueue.Get()
t.Error("queue should be empty, but contains ", item)
}

outputConfigmap := getOutputConfigmapName(domain)
output, err := c.kubeClientset.CoreV1().ConfigMaps(namespace1).Get(context.TODO(), outputConfigmap, metav1.GetOptions{})
if err != nil {
t.Error(err)
return
}
expectedOutput := map[string]string{"k1": "v1", "k3": "v3"}
if !reflect.DeepEqual(output.Data, expectedOutput) {
t.Error("expected output config ", expectedOutput, ", got", output.Data)
}

output, err = c.kubeClientset.CoreV1().ConfigMaps(namespace2).Get(context.TODO(), outputConfigmap, metav1.GetOptions{})
if err != nil {
t.Error(err)
return
}
expectedOutput = map[string]string{"k2": "v2", "k4": "v4"}
if !reflect.DeepEqual(output.Data, expectedOutput) {
t.Error("expected output config ", expectedOutput, ", got", output.Data)
}

msg := <-msgChan
if msg.Topic != syncedSuccessfullyTopic {
t.Error("expected message pblished to topic ", syncedSuccessfullyTopic, ", got", msg.Topic)
Expand Down Expand Up @@ -217,9 +283,6 @@ func TestConfigurationDomainController_processNextWorkItem(t *testing.T) {
t.Error("queue should have 1 item, but contains ", c.workqueue.Len())
}

if result := c.processNextWorkItem(); !result {
t.Error("processing failed")
}
if result := c.processNextWorkItem(); !result {
t.Error("processing failed")
}
Expand Down Expand Up @@ -275,10 +338,6 @@ func TestConfigurationDomainController_processNextWorkItem(t *testing.T) {

time.Sleep(100 * time.Millisecond)

if result := c.processNextWorkItem(); !result {
t.Error("processing failed")
}

// Assert
if c.workqueue.Len() != 0 {
item, _ := c.workqueue.Get()
Expand Down
37 changes: 30 additions & 7 deletions internal/controllers/configuration-domain/configuration_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"reflect"
"strings"

corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -119,6 +120,17 @@ func (h *configurationHandler) getConfigMapsFor(platform *platformv1.Platform, n
return nil, ErrNonRetryAble
}

domainNsAndPlatformLabelSelector, err :=
labels.ValidatedSelectorFromSet(map[string]string{
controllers.DomainLabelName: domain + "." + namespace,
controllers.PlatformLabelName: platform.Name,
})

if err != nil {
utilruntime.HandleError(err)
return nil, ErrNonRetryAble
}

globalDomainAndPlatformLabelSelector, err :=
labels.ValidatedSelectorFromSet(map[string]string{
controllers.DomainLabelName: controllers.GlobalDomainLabelValue,
Expand All @@ -140,13 +152,18 @@ func (h *configurationHandler) getConfigMapsFor(platform *platformv1.Platform, n
return nil, err
}

configMaps, err := h.configMapsLister.ConfigMaps(namespace).List(domainAndPlatformLabelSelector)
currentNsConfigMaps, err := h.configMapsLister.ConfigMaps(namespace).List(domainAndPlatformLabelSelector)
if err != nil {
return nil, err
}

nsConfigMaps, err := h.configMapsLister.List(domainNsAndPlatformLabelSelector)
if err != nil {
return nil, err
}

configMaps = append(append(platformConfigMaps, globalDomainConfigMaps...), configMaps...)
return configMaps, nil
currentNsConfigMaps = append(append(append(platformConfigMaps, globalDomainConfigMaps...), currentNsConfigMaps...), nsConfigMaps...)
return currentNsConfigMaps, nil
}

func (h *configurationHandler) aggregateConfigMaps(configurationDomain *v1alpha1.ConfigurationDomain, configMaps []*corev1.ConfigMap, outputName string) *corev1.ConfigMap {
Expand Down Expand Up @@ -191,16 +208,22 @@ func isOutputConfigMap(configMap *corev1.ConfigMap) bool {
owner.APIVersion == "configuration.totalsoft.ro/v1alpha1")
}

func getConfigMapPlatformAndDomain(configMap *corev1.ConfigMap) (platform string, domain string, ok bool) {
func getConfigMapPlatformNsAndDomain(configMap *corev1.ConfigMap) (platform, namespace, domain string, ok bool) {

domain, domainLabelExists := configMap.Labels[controllers.DomainLabelName]
if !domainLabelExists || len(domain) == 0 {
return "", domain, false
return "", configMap.Namespace, domain, false
}

platform, platformLabelExists := configMap.Labels[controllers.PlatformLabelName]
if !platformLabelExists || len(platform) == 0 {
return platform, domain, false
return platform, configMap.Namespace, domain, false
}

domain, namespace, found := strings.Cut(domain, ".")
if !found {
return platform, configMap.Namespace, domain, false
}

return platform, domain, true
return platform, namespace, domain, true
}

0 comments on commit f3f11ed

Please sign in to comment.