Skip to content

Commit

Permalink
Fix - Platform controllers - Configuration Controller - fail to watch… (
Browse files Browse the repository at this point in the history
#104)

* Fix - Platform controllers - Configuration Controller - fail to watch configMap changes

* fix test
  • Loading branch information
alexandra-c authored Dec 3, 2024
1 parent e86362c commit 7ae01a4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,77 @@ func TestConfigurationDomainController_processNextWorkItem(t *testing.T) {
}
})

t.Run("update aggregate when config map changes", func(t *testing.T) {
// Arrange
platform, namespace, domain := "dev", "team1", "domain1"
configMaps := []runtime.Object{
newConfigMap("configMap1", domain, namespace, platform, map[string]string{"k1": "v1"}),
newConfigMap("configMap2", domain, namespace, platform, map[string]string{"k2": "v2"}),
}
configurationDomains := []runtime.Object{
newConfigurationDomain(domain, namespace, platform, true, false),
}
platforms := []runtime.Object{
newPlatform(platform, platform),
}
spcs := []runtime.Object{}
kubeSecrets := []runtime.Object{}
c, msgChan := runController(platforms, configurationDomains, configMaps, kubeSecrets, spcs)
if c.workqueue.Len() != 1 {
t.Error("queue should have only 1 item, but it has", c.workqueue.Len())
return
}

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

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

// update configMap1
configMap1, err := c.kubeClientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), "configMap1", metav1.GetOptions{})
if err != nil {
t.Error(err)
return
}
configMap1.Data = map[string]string{"k1": "new_value"}
// save the updated configMap1
_, err = c.kubeClientset.CoreV1().ConfigMaps(namespace).Update(context.TODO(), configMap1, metav1.UpdateOptions{})
if err != nil {
t.Error(err)
return
}

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

// wait for the controller to process the update
time.Sleep(1 * time.Second)

// Assert
outputConfigmap := getOutputConfigmapName(domain)
output, err := c.kubeClientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), outputConfigmap, metav1.GetOptions{})
if err != nil {
t.Error(err)
return
}

expectedOutput := map[string]string{"k1": "new_value", "k2": "v2"}
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 published to topic ", syncedSuccessfullyTopic, ", got", msg.Topic)
}
})

t.Run("aggregate two kube secrets", func(t *testing.T) {
// Arrange
platform, namespace, domain := "dev", "team1", "domain1"
Expand Down Expand Up @@ -575,7 +646,11 @@ func TestConfigurationDomainController_processNextWorkItem(t *testing.T) {
t.Error("processing failed")
}

time.Sleep(100 * time.Millisecond)
if result := c.processNextWorkItem(); !result {
t.Error("processing failed")
}

time.Sleep(1 * time.Second)

// Assert
if c.workqueue.Len() != 0 {
Expand Down
10 changes: 5 additions & 5 deletions internal/controllers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ func GetPlatformNsAndDomain(obj metav1.Object) (platform, namespace, domain stri
return platform, obj.GetNamespace(), domain, false
}

domain, namespace, found := strings.Cut(domain, ".")
if !found {
return platform, obj.GetNamespace(), domain, false
domain, namespace, _ = strings.Cut(domain, ".")
if namespace == "" {
return platform, obj.GetNamespace(), domain, true
} else {
return platform, namespace, domain, true
}

return platform, namespace, domain, true
}

0 comments on commit 7ae01a4

Please sign in to comment.