Search code examples
kubernetesgoogle-kubernetes-enginekubernetes-helmazure-akskubernetes-pod

Extending a service-per-pod to multiple ports in metacontroller (kubernetes)


I have created a service-per-pod for statefulset, and it's working for a single port (80/tcp). However, I need to use it for multiple ports (e.g., 80/tcp, 30000/udp, etc.).

How can I extend this to multiple ports in service-per-pod Metacontroller?

sync-service-per-pod.jsonnet - image


Solution

  • The service-per-pod sample actually shows you how to do this in manifest/sync-service-per-pod.jsonnet.

    The service-per-pod-ports annotation allows you to specify a comma-delimited set of ports which will be exposed by the generated Service:

    service-per-pod-ports: "80:80,8080:8080"
    

    Then in sync-service-per-pod.jsonnet:

    Get the ports

    local ports = statefulset.metadata.annotations["service-per-pod-ports"]
    

    And then loop through them creating multiple ports in the Service spec:

       ports: [
          {
            local parts = std.split(portnums, ":"),
            name: "port-" + std.parseInt(parts[0]),
            port: std.parseInt(parts[0]),
            targetPort: std.parseInt(parts[1]),
          }
          for portnums in std.split(ports, ",")
        ]
    

    Note the for portnums in std.split(ports, ",") iterates over the ports set above.

    Looks like you are trying to do something a bit more dynamic, so you could so something like:

    for index in std.range(1,N) 
    

    where N is is the maximum value of index.