Skip to content

Commit

Permalink
Merge pull request juju#16617 from wallyworld/remove-placeholder-port
Browse files Browse the repository at this point in the history
juju#16617

When a charm uses open-port to ask Juju to add a service port to the application service, the original placeholder port was left behind. This PR fixes that situation.

## QA steps

Before this patch the ports would have included `65535`.

```sh
juju deploy zinc-k8s
kubectl get service/zinc-k8s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
zinc-k8s ClusterIP 10.152.183.173 <none> 4080/TCP 15m
```

## Links

https://bugs.launchpad.net/juju/+bug/2003782

**Jira card:** JUJU-[5065]
  • Loading branch information
jujubot authored Nov 27, 2023
2 parents 060e175 + 0fe2b13 commit d57438a
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 d57438a

Please sign in to comment.