Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the user to customize the listener ports inside the container. #55

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions internal/impl/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const (

var (
// Start value for ports used by the public and private listeners.
externalPort = 20000
externalPort int32 = 20000

// Resource allocation units for "cpu" and "memory" resources.
//
Expand Down Expand Up @@ -89,6 +89,11 @@ type ListenerOptions struct {
// from the public internet. If false, the listener is configured only
// for cluster-internal access.
Public bool

// If specified, the port inside the container on which the listener
// is reachable. If zero or not specified, the first available port
// is used.
Port int32
}

// KubeConfig stores the configuration information for one execution of a
Expand Down Expand Up @@ -341,6 +346,22 @@ func (r *replicaSetInfo) buildContainer() (corev1.Container, error) {
if err != nil {
return corev1.Container{}, err
}

// Always expose the metrics port from the container, so it can be
// discoverable for scraping by Prometheus.
ports := []corev1.ContainerPort{
{Name: "prometheus", ContainerPort: defaultMetricsPort},
}
// Expose all of the listener ports.
for _, ls := range r.components {
for _, l := range ls.Listeners {
ports = append(ports, corev1.ContainerPort{
Name: l.Name,
ContainerPort: l.ExternalPort,
})
}
}

return corev1.Container{
Name: appContainerName,
Image: r.image,
Expand All @@ -365,10 +386,7 @@ func (r *replicaSetInfo) buildContainer() (corev1.Container, error) {
// attach autoscalers to all of our containers, so the extra-usage
// should be only for a short period of time.
},

// Expose the metrics port from the container, so it can be discoverable for
// scraping by Prometheus.
Ports: []corev1.ContainerPort{{ContainerPort: defaultMetricsPort}},
Ports: ports,

// Enabling TTY and Stdin allows the user to run a shell inside the container,
// for debugging.
Expand Down Expand Up @@ -693,13 +711,20 @@ func getComponents(dep *protos.Deployment, cfg *KubeConfig) (map[string]*Replica
if opts := cfg.Listeners[lis]; opts != nil && opts.Public {
public = true
}
var port int32
if opts := cfg.Listeners[lis]; opts != nil && opts.Port != 0 {
port = opts.Port
} else {
// Pick an unused port.
port = externalPort
externalPort++
}
components[c.Component].Listeners = append(components[c.Component].Listeners,
&ReplicaSetConfig_Listener{
Name: lis,
ExternalPort: int32(externalPort),
ExternalPort: port,
IsPublic: public,
})
externalPort++
}
}
return components, nil
Expand Down