From 0fe2b13d5a50bfa9ea1806b366b9d4d1b683f393 Mon Sep 17 00:00:00 2001 From: Ian Booth Date: Mon, 27 Nov 2023 16:43:19 +1000 Subject: [PATCH] Ensure placeholder port is removed from k8s service when open-port is used --- .../provider/application/application.go | 21 ++++++++++++++----- .../provider/application/application_test.go | 4 ++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/caas/kubernetes/provider/application/application.go b/caas/kubernetes/provider/application/application.go index 04a309f5030..1aab85e8f9c 100644 --- a/caas/kubernetes/provider/application/application.go +++ b/caas/kubernetes/provider/application/application.go @@ -658,6 +658,11 @@ func (a *app) configureHeadlessService(name string, annotation annotations.Annot return svc.Apply(context.Background(), a.client) } +const ( + placeholderPortName = "placeholder" + placeholderPort = 65535 +) + // configureDefaultService configures the default service for the application. // It's only configured once when the application was deployed in the first time. func (a *app) configureDefaultService(annotation annotations.Annotation) (err error) { @@ -670,8 +675,8 @@ func (a *app) configureDefaultService(annotation annotations.Annotation) (err er Selector: a.selectorLabels(), Type: corev1.ServiceTypeClusterIP, Ports: []corev1.ServicePort{{ - Name: "placeholder", - Port: 65535, + Name: placeholderPortName, + Port: placeholderPort, }}, }, }) @@ -749,10 +754,16 @@ func (a *app) UpdatePorts(ports []caas.ServicePort, updateContainerPorts bool) e var expectedPorts []corev1.ServicePort for _, p := range svc.Service.Spec.Ports { - if !strings.HasPrefix(p.Name, portNamePrefix) { - // The ports are not mamanged by Juju should be kept. - expectedPorts = append(expectedPorts, p) + if p.Name == placeholderPortName && len(ports) > 0 { + // Ignore placeholder port if there are ports supplied by the charm. + continue + } + if strings.HasPrefix(p.Name, portNamePrefix) { + // Port managed by Juju - will be replaced, + continue } + // The ports that are not managed by Juju should be kept. + expectedPorts = append(expectedPorts, p) } for _, port := range ports { sp, err := convertServicePort(port) diff --git a/caas/kubernetes/provider/application/application_test.go b/caas/kubernetes/provider/application/application_test.go index f6b3f2e6908..d17e2db2054 100644 --- a/caas/kubernetes/provider/application/application_test.go +++ b/caas/kubernetes/provider/application/application_test.go @@ -1475,6 +1475,10 @@ func getDefaultSvc() *corev1.Service { Selector: map[string]string{"app.kubernetes.io/name": "gitlab"}, Type: corev1.ServiceTypeClusterIP, + Ports: []corev1.ServicePort{{ + Name: "placeholder", + Port: 65535, + }}, }, } }