Skip to content

WASM filter httpCall to localhost sidecar container in Kubernetes #17178

Closed

Description

@pdeligia

I am writing an Envoy filter using the WASM C++ SDK and I want to make an httpCall (as described here) to a server running inside a container in the same Kubernetes pod as the Envoy proxy.

To give some quick context: I basically have a pod that is running 3 containers: Envoy (automatically injected through Istio), container A running server S^A (which external clients can make HTTP requests to) and container B running server S^B, which only the co-located (in the same pod) Envoy filter should be able to invoke using an httpCall. The use case should not matter really, but basically S^B manipulates the incoming request, based on the HTTP request type and content, and then the Envoy filter will route it back to S^A -- S^A stays oblivious to all this.

I am able to successfully make an httpCall if I register the server S^B (which normally listens to port, say 80) as an Istio ServiceEntry with a corresponding K8S Service on (say) port 8000, and then give a cluster name (as first parameter to httpCall) say outbound|8000||foo.default.svc.cluster.local. But this is not ideal for my scenario, as I want to communicate explicitly to the co-located server (which envoy already knows about!), and I do not want to go through a ServiceEntry (which could load balance to a non co-located container, if I understand correctly).

Envoy normally communicates with server S^B and routes requests to it via inbound|80||, if I see the logs and Envoy configuration. However, if I try to give inbound|80|| as the first argument to httpCall, then the call fails with InternalFailure for some reason! (Btw, if I give a wrong cluster name, for example outbound|80|| then the httpCall fails with BadArgument, so InternalFailure is strange. In general, I find using the API hard to debug, as there is not enough information or logging on why it failed, when it fails).

Is a WASM filter not able to do HTTP calls directly to a co-located pod? It seems that it should be straightforward, but it doesn't seem to work when I try to do that explicitly. Am I missing something here? Is there some other WASM SDK API that I should use for this purpose?

Thank you for your help!

Activity

  1. added
    triage
    on Jun 28, 2021
  2. pdeligia commented on Jun 28, 2021

    @pdeligia
    Author

    cc @PiotrSikora for visibility (I have seen you reply in other issues related to WASM extensions, apologies if some other person is more appropriate to cc)

  3. added
    question
    and removed
    triage
    on Jun 29, 2021
  4. lizan commented on Jun 29, 2021

    @lizan
    Member
  5. mathetake commented on Jun 29, 2021

    @mathetake
    Member

    then the call fails with InternalFailure for some reason! (Btw, if I give a wrong cluster name, for example outbound|80|| then the httpCall fails with BadArgument, so InternalFailure is strange.

    yeah agreed, that seems weird. Could you enable the trace log by passing -l trace to Envoy and give us the trace log here?

    I imagine you have reached remote_close condition when making httpCall:

  6. PiotrSikora commented on Jun 29, 2021

    @PiotrSikora
    Contributor

    Could you paste trace logs from the time httpCall is invoked to when InternalError is returned?
    See: https://knative.tips/troubleshooting/debugging-envoy/ for how to change log levels in Istio (change debug to trace).

    I suspect that you might be missing some required HTTP headers in the outgoing request.

  7. pdeligia commented on Jun 29, 2021

    @pdeligia
    Author

    Thanks @PiotrSikora @mathetake for the prompt help with this issue! I will get the Envoy trace logs from Istio and come back.

    Could you paste trace logs from the time httpCall is invoked to when InternalError is returned?
    See: https://knative.tips/troubleshooting/debugging-envoy/ for how to change log levels in Istio (change debug to trace).

    I suspect that you might be missing some required HTTP headers in the outgoing request.

    I do not think it is the HTTP headers, as I am able to make a successful httpCall request to 'foo' (i.e. server S^B from above) if I give the Envoy cluster name outbound|8000||foo.default.svc.cluster.local (which I grabbed from the Envoy config). The problem happens when instead I am trying to use the cluster name inbound|80||, which then fails with InternalFailure. (If I mess up the headers, then I get a BadArgument, but I believe that I resolved that already, else I would not be able to make the httpCall to outbound|8000||foo.default.svc.cluster.local.)

    Basically, the Envoy in this pod is configured to have two co-located app containers (I have only kept a few details from the dump), for which it acts as a proxy in the pod:

    {
     "configs": [
      {
       "@type": "type.googleapis.com/envoy.admin.v3.BootstrapConfigDump",
       "bootstrap": {
        "node": {
         "cluster": "bar.default",
         "metadata": {
          "ISTIO_VERSION": "1.10.2",
          "ISTIO_PROXY_SHA": "istio-proxy:5952f5fb5f85f495ac6d0925539dcd445cd618a0",
          "WORKLOAD_NAME": "bar",
          "PROXY_CONFIG": {
           "serviceCluster": "bar.default",
          },
          "LABELS": {
           "istio.io/rev": "default",
           "app": "bar",
          },
          "SERVICE_ACCOUNT": "default",
          "POD_PORTS": "[{\"containerPort\":88,\"protocol\":\"TCP\"},{\"containerPort\":80,\"protocol\":\"TCP\"}]",
          "MESH_ID": "cluster.local",
          "APP_CONTAINERS": "bar,foo",
          "NAMESPACE": "default"
         },
    

    My scenario (abstractly) is that basically an external client can only route requests to bar (on port 88), but I want Envoy to intercept some request types (this is what my custom WASM filter does) and route them to the local foo via httpCall (on port 80), which is basically a sidecar container, does a bunch of stuff, replies to the custom filter, which then continues with the request to bar.

    I need to guarantee that Envoy though routes this request only to the local foo sidecar, and not on a foo located in some other pod in the system. Please correct me if I am wrong, but I was under the impression that doing the httpCall using the cluster name outbound|8000||foo.default.svc.cluster.local would go through Kubernetes service load balancing and end up doing the call to any foo in the cluster. That is why I was thinking that instead I should be using inbound|80||. Envoy after all should know that this co-located container is in port 80 of the same pod! But then using this cluster name throws the InternalFailure.

    Is what I am trying to do not possible? Or should I be using some different cluster name to talk to the co-located sidecar container?

    Hope this description helps clarify my scenario. I can try to grab the Envoy trace through Istio, but please let me know if there is anything else I can add above that might help.

    Thanks again!

  8. pdeligia commented on Jun 29, 2021

    @pdeligia
    Author

    @PiotrSikora @mathetake, I got the traces:

    2021-06-29T18:08:17.932154Z	trace	envoy wasm	[vm->host] env.proxy_http_call(5248552, 14, 5249576, 104, 2061, 0, 0, 0, 5000, 5248160)
    2021-06-29T18:08:17.932182Z	debug	envoy router	[C0][S870967954827920301] cluster 'inbound|80||' match for URL '/status'
    2021-06-29T18:08:17.932192Z	warning	envoy upstream	original_dst_load_balancer: No downstream connection or no original_dst.
    2021-06-29T18:08:17.932195Z	debug	envoy upstream	no healthy host for HTTP connection pool
    2021-06-29T18:08:17.932244Z	debug	envoy http	async http request response headers (end_stream=false):
    ':status', '503'
    'content-length', '19'
    'content-type', 'text/plain'
    
    2021-06-29T18:08:17.932249Z	trace	envoy http	async http request response data (length=19 end_stream=true)
    2021-06-29T18:08:17.932263Z	trace	envoy wasm	[vm<-host] env.proxy_http_call return: 10
    2021-06-29T18:08:17.932267Z	trace	envoy wasm	[vm->host] env.proxy_log(3, 5249576, 83)
    2021-06-29T18:08:17.932268Z	warning	envoy wasm	wasm log foo_sidecar_plugin_root_id foo_sidecar_plugin_vm_id: SidecarPluginContext::onRequestHeaders() failed http call, wrSec is InternalFailure
    

    Strange, as it is gives a warning: original_dst_load_balancer: No downstream connection or no original_dst. However, the co-located container (that this Envoy container is a proxy to) can be seen in the Envoy configuration:

    {
         "version_info": "2021-06-28T23:56:43Z/229",
         "cluster": {
          "@type": "type.googleapis.com/envoy.config.cluster.v3.Cluster",
          "name": "inbound|80||",
          "type": "ORIGINAL_DST",
          "connect_timeout": "10s",
          "lb_policy": "CLUSTER_PROVIDED",
          "circuit_breakers": {
           "thresholds": [
            {
             "max_connections": 4294967295,
             "max_pending_requests": 4294967295,
             "max_requests": 4294967295,
             "max_retries": 4294967295,
             "track_remaining": true
            }
           ]
          },
          "cleanup_interval": "60s",
          "upstream_bind_config": {
           "source_address": {
            "address": "127.0.0.6",
            "port_value": 0
           }
          },
          "metadata": {
           "filter_metadata": {
            "istio": {
             "services": [
              {
               "name": "bar",
               "host": "bar.default.svc.cluster.local",
               "namespace": "default"
              }
             ]
            }
           }
          }
         },
         "last_updated": "2021-06-28T23:56:47.724Z"
        }
    

    As I mentioned above, if I use outbound|8000||bar.default.svc.cluster.local and the rest of the parameters/callback are exactly the same, then the httpCall succeeds with this trace log:

    2021-06-29T18:01:33.243540Z	trace	envoy wasm	[vm->host] env.proxy_http_call(5248992, 61, 5249624, 104, 2061, 0, 0, 0, 5000, 5248160)
    2021-06-29T18:01:33.243569Z	debug	envoy router	[C0][S13130202362542368434] cluster 'outbound|8000||bar.default.svc.cluster.local' match for URL '/status'
    2021-06-29T18:01:33.243608Z	debug	envoy router	[C0][S13130202362542368434] router decoding headers:
    ':path', '/status'
    ':method', 'GET'
    ':authority', ''
    ':scheme', 'http'
    'content-type', 'application/json'
    'x-envoy-internal', 'true'
    'x-forwarded-for', '10.244.1.62'
    'x-envoy-expected-rq-timeout-ms', '5000'
    
    2021-06-29T18:01:33.243614Z	debug	envoy pool	queueing stream due to no available connections
    2021-06-29T18:01:33.243616Z	debug	envoy pool	trying to create new connection
    2021-06-29T18:01:33.243636Z	trace	envoy pool	ConnPoolImplBase 0x56427c3c4780, ready_clients_.size(): 0, busy_clients_.size(): 0, connecting_clients_.size(): 0, connecting_stream_capacity_: 0, num_active_streams_: 0, pending_streams_.size(): 1 per upstream preconnect ratio: 1
    2021-06-29T18:01:33.243639Z	debug	envoy pool	creating a new connection
    2021-06-29T18:01:33.243685Z	debug	envoy client	[C89] connecting
    2021-06-29T18:01:33.243689Z	debug	envoy connection	[C89] connecting to 10.244.1.62:8383
    2021-06-29T18:01:33.243754Z	debug	envoy connection	[C89] connection in progress
    2021-06-29T18:01:33.243791Z	trace	envoy pool	not creating a new connection, shouldCreateNewConnection returned false.
    2021-06-29T18:01:33.243791Z	debug	envoy filter	original_dst: New connection accepted
    2021-06-29T18:01:33.243798Z	trace	envoy wasm	[vm<-host] env.proxy_http_call return: 0
    2021-06-29T18:01:33.243828Z	debug	envoy filter	tls inspector: new connection accepted
    2021-06-29T18:01:33.243834Z	trace	envoy wasm	[vm->host] env.proxy_log(3, 2177, 60)
    2021-06-29T18:01:33.243835Z	trace	envoy filter	tls inspector: recv: 0
    2021-06-29T18:01:33.243837Z	warning	envoy wasm	wasm log foo_sidecar_plugin_root_id foo_sidecar_plugin_vm_id: SidecarPluginContext::onRequestHeaders() succeeded http call
    

    So my HTTP request (headers) should be fine, the issue must be with the cluster name (which the No downstream connection or no original_dst warning hints, if I understand correctly, but its weird as the inbound|80|| has type ORIGINAL_DST as you can see in the above config).

    Is there a different cluster name I am supposed to use so that Envoy directly calls the co-located container in localhost and port 80?

  9. PiotrSikora commented on Jun 29, 2021

    @PiotrSikora
    Contributor

    I do not think it is the HTTP headers, as I am able to make a successful httpCall request to 'foo' (i.e. server S^B from above) if I give the Envoy cluster name outbound|8000||foo.default.svc.cluster.local (which I grabbed from the Envoy config).

    I meant that perhaps you're missing :authority/Host header, and Envoy doesn't know where to route the request when using passthru cluster.

    Strange, as it is gives a warning: original_dst_load_balancer: No downstream connection or no original_dst. However, the co-located container (that this Envoy container is a proxy to) can be seen in the Envoy configuration:

    ORIGINAL_DST cluster uses downstream connection's destination address as the endpoint address when making an outgoing connection. However, since httpCall is a standalone request that doesn't have the downstream connection to copy the destination address from, using such cluster fails.

    I believe ServiceEntry is the currently recommended way to make callouts in Istio. cc @mandarjog

  10. pdeligia commented on Jun 30, 2021

    @pdeligia
    Author

    ORIGINAL_DST cluster uses downstream connection's destination address as the endpoint address when making an outgoing connection. However, since httpCall is a standalone request that doesn't have the downstream connection to copy the destination address from, using such cluster fails.

    I believe ServiceEntry is the currently recommended way to make callouts in Istio. cc @mandarjog

    Thanks @PiotrSikora for your answer, really appreciate your help.

    The thing is that the destination address in our case is just localhost and some well-known port (which is statically defined on the deployment YAML), as the destination container is guaranteed to be on the same pod (Envoy is its proxy). So I am not trying to make a call to any ORIGINAL_DST cluster, just a localhost address and port (which Envoy should know already as it is the proxy for this destination and already intercepts all ingress traffic and can route HTTP requests to this container, I just want to also be able to do similar calls from inside WASM without having to go through Istio/K8S)!

    Going back to what you suggested, since the container that I want my WASM filter to talk to is guaranteed to be on localhost, I want to avoid using an Istio ServiceEntry, which cannot work for our scenario. We need to make sure that the filter only talks to the localhost foo container and not some other replica that might be on a different VM/pod. This is not guaranteed (please correct me if wrong) if I use the Istio ServiceEntry cluster name, right?

    Question: Does Envoy not support making an explicit httpCall from inside a WASM filter to an app/container on localhost on the same pod, without having to go through Istio's ServiceEntry?

    If not, is there another way to make a call to localhost from inside the WASM filter? How does someone workaround this limitation today?

    Thank you!

  11. changed the title [-]WASM filter httpCall to containerized server in same pod in Kubernetes[/-] [+]WASM filter httpCall to localhost sidecar container in Kubernetes[/+] on Jun 30, 2021
  12. PiotrSikora commented on Jul 3, 2021

    @PiotrSikora
    Contributor

    Going back to what you suggested, since the container that I want my WASM filter to talk to is guaranteed to be on localhost, I want to avoid using an Istio ServiceEntry, which cannot work for our scenario. We need to make sure that the filter only talks to the localhost foo container and not some other replica that might be on a different VM/pod. This is not guaranteed (please correct me if wrong) if I use the Istio ServiceEntry cluster name, right?

    I think you might be confusing Service and ServiceEntry.

    If you add a ServiceEntry like this:

    apiVersion: networking.istio.io/v1beta1
    kind: ServiceEntry
    metadata:
      name: localhost.for.http.call
    spec:
      hosts:
      - localhost.for.http.call
      ports:
      - number: 8080
        name: http
        protocol: HTTP
      location: MESH_EXTERNAL
      resolution: STATIC
      endpoints:
      - address: 127.0.0.1
    

    then you should be able to make calls to 127.0.0.1:8080 using outbound|8080||localhost.for.http.call cluster.

    Question: Does Envoy not support making an explicit httpCall from inside a WASM filter to an app/container on localhost on the same pod, without having to go through Istio's ServiceEntry?

    If not, is there another way to make a call to localhost from inside the WASM filter? How does someone workaround this limitation today?

    Wasm plugins can make HTTP calls to any cluster that Envoy knows about, but you need to figure how to get your control plane (Istio) to add a localhost cluster to Envoy.

    I think the ServiceEntry above should work, but if it doesn't work for you, then you could provide a custom bootstrap with statically configured localhost cluster. See: https://github.com/istio/istio/tree/master/samples/custom-bootstrap.

  13. pdeligia commented on Jul 3, 2021

    @pdeligia
    Author

    Thanks @PiotrSikora, this is incredibly helpful, I had no idea you could configure an Istio ServiceEntry like this for localhost calls through Envoy! This seems like exactly what I need, but let's indeed see if it works, else will investigate the custom bootstrap as you suggest. Will try this ASAP and report back.

  14. pdeligia commented on Jul 6, 2021

    @pdeligia
    Author

    Just to update you @PiotrSikora, the ServiceEntry that explicitly defines the localhost worked! Closing this issue now, as it is resolved. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/wasmquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions