Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
fix: check if envoy filters are updated before starting gateway (#168)
Browse files Browse the repository at this point in the history
there are race conditions otherwise where the gateway comes up before
the filter and routing doesn't work

there are also situations where there might be a bug due to which the
gateway filter isn't deployed (it has bugs); but the gateway comes up
anyway
  • Loading branch information
h4ck3rk3y authored Aug 21, 2024
1 parent 4f587d0 commit 7e79072
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions kardinal-cli/deployment/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package deployment

import (
"context"
"encoding/json"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -53,6 +54,12 @@ func StartGateway(host, flowId string) error {
return fmt.Errorf("failed to find pod for service: %v", err)
}

// Check for the Envoy filter before proceeding
err = checkGatewayEnvoyFilter(client.clientSet, host)
if err != nil {
return err
}

// Start port forwarding
stopChan := make(chan struct{}, 1)
readyChan := make(chan struct{})
Expand Down Expand Up @@ -190,6 +197,47 @@ func findPodForService(client *kubernetes.Clientset) (string, error) {
return podName, nil
}

func checkGatewayEnvoyFilter(client *kubernetes.Clientset, host string) error {
for retry := 0; retry < maxRetries; retry++ {
envoyFilterRaw, err := client.RESTClient().
Get().
AbsPath("/apis/networking.istio.io/v1alpha3/namespaces/istio-system/envoyfilters/kardinal-gateway-tracing").
Do(context.Background()).
Raw()
if err != nil {
log.Printf("Error getting Envoy filter (attempt %d/%d): %v", retry+1, maxRetries, err)
time.Sleep(retryInterval)
continue
}

var envoyFilter map[string]interface{}
err = json.Unmarshal(envoyFilterRaw, &envoyFilter)
if err != nil {
log.Printf("Error unmarshaling Envoy filter (attempt %d/%d): %v", retry+1, maxRetries, err)
time.Sleep(retryInterval)
continue
}

luaCode, ok := envoyFilter["spec"].(map[string]interface{})["configPatches"].([]interface{})[0].(map[string]interface{})["patch"].(map[string]interface{})["value"].(map[string]interface{})["typed_config"].(map[string]interface{})["inlineCode"].(string)
if !ok {
log.Printf("Error getting Lua code from Envoy filter (attempt %d/%d)", retry+1, maxRetries)
time.Sleep(retryInterval)
continue
}

if !strings.Contains(luaCode, host) {
log.Printf("Envoy filter 'kardinal-gateway-tracing' does not contain the expected host string: %s (attempt %d/%d)", host, retry+1, maxRetries)
time.Sleep(retryInterval)
continue
}

log.Printf("Envoy filter 'kardinal-gateway-tracing' found and contains the expected host string: %s", host)
return nil
}

return fmt.Errorf("failed to find Envoy filter 'kardinal-gateway-tracing' containing the expected host string after %d attempts", maxRetries)
}

func portForwardPod(config *rest.Config, podName string, stopChan <-chan struct{}, readyChan chan struct{}) error {
roundTripper, upgrader, err := spdy.RoundTripperFor(config)
if err != nil {
Expand Down

0 comments on commit 7e79072

Please sign in to comment.