Skip to content

Commit 378a0e5

Browse files
authored
Remove ExposePodsByName service and mapping with Pod (#2060)
Bug fix updates the site bindings logic so that when the network status is updated and the final connector for a per-target address is removed, corresponding per-target listeners and services will also be removed. Fixes issue #2054 Signed-off-by: Christian Kruse <christian@c-kruse.com>
1 parent c7f4e4d commit 378a0e5

2 files changed

Lines changed: 66 additions & 21 deletions

File tree

internal/kube/controller/controller_test.go

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"flag"
77
"fmt"
88
"reflect"
9+
"strings"
910
"testing"
1011

1112
"github.com/google/uuid"
@@ -619,6 +620,26 @@ func TestUpdate(t *testing.T) {
619620
serviceCheck("adifferentsvc", "test").check,
620621
negativeServiceCheck("mysvc", "test"),
621622
},
623+
}, {
624+
name: "exposePodsByName handles pod delete",
625+
k8sObjects: []runtime.Object{
626+
f.skupperNetworkStatus("test", f.networkStatusInfo("mysite", "test", map[string]string{"mysvc": "mysvc", "mysvc.mypod-1": "mysvc@mypod-1", "mysvc.mypod-2": "mysvc@mypod-2"}, map[string]string{"mysvc": "mysvc", "mysvc.mypod-1": "10.1.1.10", "mysvc.mypod-2": "10.1.1.11"}).info()),
627+
f.pod("mypod-1", "test", map[string]string{"app": "foo"}, nil, f.podStatus("10.1.1.10", corev1.PodRunning, f.podCondition(corev1.PodReady, corev1.ConditionTrue))),
628+
f.pod("mypod-2", "test", map[string]string{"app": "foo"}, nil, f.podStatus("10.1.1.11", corev1.PodRunning, f.podCondition(corev1.PodReady, corev1.ConditionTrue))),
629+
},
630+
skupperObjects: []runtime.Object{
631+
f.site("mysite", "test", "", false, false),
632+
f.connectorWithExposePodsByName("myconnector", "test", "mysvc", "app=foo", 8080),
633+
f.listenerWithExposePodsByName("mylistener", "test", "mysvc", "mysvc", 8080),
634+
},
635+
functions: []WaitFunction{
636+
isConnectorStatusConditionTrue("myconnector", "test", skupperv2alpha1.CONDITION_TYPE_CONFIGURED),
637+
isListenerStatusConditionTrue("mylistener", "test", skupperv2alpha1.CONDITION_TYPE_CONFIGURED),
638+
serviceCheck("mysvc", "test").check,
639+
serviceCheck("mypod-1", "test").check,
640+
deleteTargetPod("mypod-1", "test"),
641+
serviceCheck("mypod-1", "test").checkAbsent,
642+
},
622643
},
623644
}
624645
for _, tt := range testTable {
@@ -641,6 +662,7 @@ func TestUpdate(t *testing.T) {
641662
for i := 0; i < len(tt.k8sObjects)+len(tt.skupperObjects); i++ {
642663
controller.controller.TestProcess()
643664
}
665+
644666
for _, f := range tt.functions {
645667
for !f(t, clients) {
646668
controller.controller.TestProcess()
@@ -1645,26 +1667,6 @@ func serviceCheck(name string, namespace string) *ServiceCheck {
16451667
}
16461668
}
16471669

1648-
func (s *ServiceCheck) selector(selector map[string]string) *ServiceCheck {
1649-
s.selector_ = selector
1650-
return s
1651-
}
1652-
1653-
func (s *ServiceCheck) ports(ports ...corev1.ServicePort) *ServiceCheck {
1654-
s.ports_ = ports
1655-
return s
1656-
}
1657-
1658-
func (s *ServiceCheck) labels(labels map[string]string) *ServiceCheck {
1659-
s.labels_ = labels
1660-
return s
1661-
}
1662-
1663-
func (s *ServiceCheck) annotations(annotations map[string]string) *ServiceCheck {
1664-
s.annotations_ = annotations
1665-
return s
1666-
}
1667-
16681670
func (s *ServiceCheck) check(t *testing.T, clients internalclient.Clients) bool {
16691671
actual, err := clients.GetKubeClient().CoreV1().Services(s.namespace).Get(context.Background(), s.name, metav1.GetOptions{})
16701672
assert.Assert(t, err)
@@ -1692,6 +1694,18 @@ func (s *ServiceCheck) check(t *testing.T, clients internalclient.Clients) bool
16921694
return true
16931695
}
16941696

1697+
func (s *ServiceCheck) checkAbsent(t *testing.T, clients internalclient.Clients) bool {
1698+
_, err := clients.GetKubeClient().CoreV1().Services(s.namespace).Get(context.Background(), s.name, metav1.GetOptions{})
1699+
if err == nil {
1700+
return false
1701+
}
1702+
if errors.IsNotFound(err) {
1703+
return true
1704+
}
1705+
assert.Assert(t, err)
1706+
return false
1707+
}
1708+
16951709
func updateListener(name string, namespace string, host string, port int) WaitFunction {
16961710
return func(t *testing.T, clients internalclient.Clients) bool {
16971711
ctxt := context.Background()
@@ -1713,3 +1727,34 @@ func negativeServiceCheck(name string, namespace string) WaitFunction {
17131727
return true
17141728
}
17151729
}
1730+
1731+
func deleteTargetPod(name string, namespace string) WaitFunction {
1732+
return func(t *testing.T, clients internalclient.Clients) bool {
1733+
ctxt := context.Background()
1734+
err := clients.GetKubeClient().CoreV1().Pods(namespace).Delete(ctxt, name, metav1.DeleteOptions{})
1735+
assert.Assert(t, err)
1736+
cm, err := clients.GetKubeClient().CoreV1().ConfigMaps(namespace).Get(ctxt, "skupper-network-status", metav1.GetOptions{})
1737+
assert.Assert(t, err)
1738+
cm = cm.DeepCopy()
1739+
var status network.NetworkStatusInfo
1740+
assert.Assert(t, json.Unmarshal([]byte(cm.Data["NetworkStatus"]), &status))
1741+
suffix := "." + name
1742+
for sIdx := range status.SiteStatus {
1743+
for rIdx, rs := range status.SiteStatus[sIdx].RouterStatus {
1744+
connectors := rs.Connectors[:0]
1745+
for _, c := range rs.Connectors {
1746+
if !strings.HasSuffix(c.Address, suffix) {
1747+
connectors = append(connectors, c)
1748+
}
1749+
}
1750+
status.SiteStatus[sIdx].RouterStatus[rIdx].Connectors = connectors
1751+
}
1752+
}
1753+
sb, err := json.Marshal(status)
1754+
assert.Assert(t, err)
1755+
cm.Data["NetworkStatus"] = string(sb)
1756+
_, err = clients.GetKubeClient().CoreV1().ConfigMaps(namespace).Update(ctxt, cm, metav1.UpdateOptions{})
1757+
assert.Assert(t, err)
1758+
return true
1759+
}
1760+
}

internal/kube/site/per_target_listener.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func extractTargets(prefix string, network []skupperv2alpha1.SiteRecord) []strin
143143
var results []string
144144
for _, site := range network {
145145
for _, service := range site.Services {
146-
if strings.HasPrefix(service.RoutingKey, prefix) {
146+
if strings.HasPrefix(service.RoutingKey, prefix) && len(service.Connectors) > 0 {
147147
results = append(results, strings.TrimPrefix(service.RoutingKey, prefix))
148148
}
149149
}

0 commit comments

Comments
 (0)