Skip to content

Commit

Permalink
patch KONG_ADMIN_LISTEN env instead of update deployment to avoid flaky
Browse files Browse the repository at this point in the history
  • Loading branch information
randmonkey committed Nov 28, 2023
1 parent c6ce507 commit c2251ac
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 58 deletions.
13 changes: 0 additions & 13 deletions test/e2e/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,19 +280,6 @@ func (d ManifestDeploy) Run(ctx context.Context, t *testing.T, env environments.
t.Log("waiting for controller to be ready")
deployments := getManifestDeployments(d.Path)

if !d.SkipTestPatches {
adminAPINoHTTP2Range := kong.MustNewRange("<" + adminAPIHTTP2MinimalKongVersion.String())
kongVersion, err := getKongVersionFromOverrideImageTag()
if err == nil && adminAPINoHTTP2Range(kongVersion) {
t.Logf(
"Kong version %s is below %s, should disable HTTP/2 on admin API",
kongVersion.String(), adminAPIHTTP2MinimalKongVersion.String(),
)
removeAdminListenHTTP2(ctx, t, env, deployments.ProxyNN)
}

}

waitForDeploymentRollout(ctx, t, env, deployments.ControllerNN.Namespace, deployments.ControllerNN.Name)
waitForDeploymentRollout(ctx, t, env, deployments.ProxyNN.Namespace, deployments.ProxyNN.Name)

Expand Down
39 changes: 39 additions & 0 deletions test/e2e/kustomizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,42 @@ func patchReadinessProbePath(baseManifestReader io.Reader, deployment k8stypes.N
}
return kubectl.GetKustomizedManifest(kustomization, baseManifestReader)
}

const kustomizePatchKongAdminAPIListenFormat = `apiVersion: apps/v1
kind: Deployment
metadata:
namespace: %s
name: %s
spec:
template:
spec:
containers:
- name: %s
env:
- name: KONG_ADMIN_LISTEN
value: %s
`

func patchKongAdminAPIListen(baseManifestReader io.Reader, deployment k8stypes.NamespacedName, adminAPIListenConfig string) (io.Reader, error) {
kustomization := types.Kustomization{
Patches: []types.Patch{
{
Patch: fmt.Sprintf(kustomizePatchKongAdminAPIListenFormat,
deployment.Namespace, deployment.Name, proxyContainerName, adminAPIListenConfig,
),
Target: &types.Selector{
ResId: resid.ResId{
Gvk: resid.Gvk{
Group: "apps",
Version: "v1",
Kind: "Deployment",
},
Name: deployment.Name,
Namespace: deployment.Namespace,
},
},
},
},
}
return kubectl.GetKustomizedManifest(kustomization, baseManifestReader)
}
64 changes: 19 additions & 45 deletions test/e2e/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,48 +119,6 @@ func exposeAdminAPI(ctx context.Context, t *testing.T, env environments.Environm
}, time.Minute, time.Second)
}

func removeAdminListenHTTP2(ctx context.Context, t *testing.T, env environments.Environment, proxyDeployment k8stypes.NamespacedName) {
t.Helper()
t.Log("updating the proxy container KONG_ADMIN_LISTEN to disable the admin API listen on HTTP2")
deployment, err := env.Cluster().Client().AppsV1().Deployments(proxyDeployment.Namespace).Get(ctx, proxyDeployment.Name, metav1.GetOptions{})
require.NoError(t, err)

var kongAdminListenValue string
for _, containerSpec := range deployment.Spec.Template.Spec.Containers {
if containerSpec.Name == proxyContainerName {
for _, envVar := range containerSpec.Env {
if envVar.Name == "KONG_ADMIN_LISTEN" {
value := envVar.Value
kongAdminListenValue = strings.ReplaceAll(value, " http2", "")
}
}
}
}
_, err = env.Cluster().Client().AppsV1().Deployments(proxyDeployment.Namespace).Patch(
ctx, deployment.Name,
k8stypes.StrategicMergePatchType,
[]byte(fmt.Sprintf(
`{
"spec": {
"template":{
"spec":{
"containers":[
{
"name":"%s","env":[{"name":"KONG_ADMIN_LISTEN","value":"%s"}]
}
]
}
}
}
}`,
proxyContainerName,
kongAdminListenValue,
)),
metav1.PatchOptions{},
)
require.NoError(t, err)
}

// getTestManifest gets a manifest io.Reader, applying optional patches to the base manifest provided.
// In case of any failure while patching, the base manifest is returned.
// If skipTestPatches is true, no patches are applied (useful when untouched manifest is needed, e.g. in upgrade tests).
Expand Down Expand Up @@ -209,19 +167,35 @@ func getTestManifest(t *testing.T, baseManifestPath string, skipTestPatches bool
}

if kongImageOverride != "" {
kongVersion, err := getKongVersionFromOverrideImageTag()
kongVersion, getVersionErr := getKongVersionFromOverrideImageTag()
patchReadinessProbeRange := kong.MustNewRange("<" + statusReadyProbeMinimalKongVersion.String())
// If we could not get version from kong image, assume they are latest.
// So we do not patch the readiness probe path to the legacy path `/status`.
if err == nil && patchReadinessProbeRange(kongVersion) {
if getVersionErr == nil && patchReadinessProbeRange(kongVersion) {
manifestsReader, err = patchReadinessProbePath(manifestsReader, deployments.ProxyNN, "/status")
if err != nil {
t.Logf("failed patching controller readiness (%v), using default manifest %v", err, baseManifestPath)
return manifestsReader
}
}
}

adminAPINoHTTP2Range := kong.MustNewRange("<" + adminAPIHTTP2MinimalKongVersion.String())
// If we could not get version from kong image, assume they are latest.
// So we do not patch the Kong admin API listen to remove the HTTP/2 listen.
if getVersionErr == nil && adminAPINoHTTP2Range(kongVersion) {
t.Logf("configure Kong admin API to non-HTTP2 listen because Kong version %s is below %s",
kongVersion.String(), adminAPIHTTP2MinimalKongVersion.String(),
)
// REVIEW: replace to `0.0.0.0:8001, 0.0.0.0:8444` or extract the value then remove the `http2` as the new value?
manifestsReader, err = patchKongAdminAPIListen(manifestsReader, deployments.ProxyNN,
"0.0.0.0:8001, 0.0.0.0:8444 ssl",
)
if err != nil {
t.Logf("failed patching Kong admin API listen (%v), using default manifest %v", err, baseManifestPath)
return manifestsReader
}
}
}
}

return manifestsReader
Expand Down

0 comments on commit c2251ac

Please sign in to comment.