Skip to content

Commit

Permalink
Ensure placeholder port is removed from k8s service when open-port is…
Browse files Browse the repository at this point in the history
… used
  • Loading branch information
wallyworld committed Nov 27, 2023
1 parent 060e175 commit 0fe2b13
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
21 changes: 16 additions & 5 deletions caas/kubernetes/provider/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
}},
},
})
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions caas/kubernetes/provider/application/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}},
},
}
}
Expand Down

0 comments on commit 0fe2b13

Please sign in to comment.