Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
contour: Fix the hostPort regression
Browse files Browse the repository at this point in the history
This change was introduced in `8d44c8513f67512527218c8a538e79a61cb7cd34`
- "don't expose Envoy using hostPorts". But a later commit overrode it:
`6a6a3c5d849b981559c2cc2cf3797bfd6b6c9159` - "contour: Update to
v1.7.0". This commit fixes this regression.

Add test to verify if hostPort does not exist, check if the converted
config does not contain the hostPort.

This commit also adds test to check if a certain JSONPath exists, it is
used in the above unit test.

Signed-off-by: Suraj Deshmukh <suraj@kinvolk.io>
  • Loading branch information
surajssd committed Feb 26, 2021
1 parent 874a1eb commit b8823d6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
2 changes: 0 additions & 2 deletions assets/charts/components/contour/templates/03-envoy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,9 @@ spec:
fieldPath: metadata.name
ports:
- containerPort: 80
hostPort: 80
name: http
protocol: TCP
- containerPort: 443
hostPort: 443
name: https
protocol: TCP
readinessProbe:
Expand Down
4 changes: 2 additions & 2 deletions pkg/assets/generated_assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pkg/components/contour/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,18 @@ func TestConversion(t *testing.T) {
})
}
}

func TestEnvoyHostPort(t *testing.T) {
t.Parallel()

componentCfg := `component "contour" {}`
expectedManifestName := k8sutil.ObjectMetadata{Version: "apps/v1", Kind: "DaemonSet", Name: "envoy"}
jsonPath := "{.spec.template.spec.containers[1].ports[0].hostPort}"
errExpected := "hostPort is not found"

component := NewConfig()
m := testutil.RenderManifests(t, component, Name, componentCfg)
gotConfig := testutil.ConfigFromMap(t, m, expectedManifestName)

testutil.JSONPathExists(t, gotConfig, jsonPath, errExpected)
}
20 changes: 20 additions & 0 deletions pkg/components/internal/testutil/jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package testutil
import (
"fmt"
"reflect"
"strings"
"testing"

"github.com/kinvolk/lokomotive/pkg/k8sutil"
Expand Down Expand Up @@ -101,3 +102,22 @@ func MatchJSONPathInt64Value(t *testing.T, yamlConfig string, jsonPath string, e
t.Fatalf("Expected: %d, Got: %d", expected, got)
}
}

// JSONPathExists checks if the given YAML config has an object at the given JSON path, also provide
// what error to expect.
func JSONPathExists(t *testing.T, yamlConfig string, jsonPath string, errExp string) {
_, err := jsonPathValue(yamlConfig, jsonPath)
if err != nil && errExp == "" {
t.Fatalf("Error not expected and failed with: %v", err)
}

if err == nil && errExp != "" {
t.Fatalf("Expected error %q but got none", errExp)
}

if err != nil && !strings.Contains(err.Error(), errExp) {
t.Fatalf("Extracting JSON path value, expected error: %v to contain: %q", err, errExp)
}

t.Logf("Failed with error: %v", err)
}

0 comments on commit b8823d6

Please sign in to comment.