Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify version detection #329

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .ci/setup_kong.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

set -e
# download Kong deb

sudo apt-get update
sudo apt-get install openssl libpcre3 procps perl wget zlibc

function setup_kong(){
SWITCH="1.3.100"

URL="https://kong.bintray.com/kong-deb/kong-${KONG_VERSION}.xenial.all.deb"

if [[ "$KONG_VERSION" > "$SWITCH" ]];
then
URL="https://kong.bintray.com/kong-deb/kong-${KONG_VERSION}.xenial.amd64.deb"
fi

/usr/bin/curl -sL $URL -o kong.deb
}

function setup_kong_enterprise(){
KONG_VERSION="${KONG_VERSION#enterprise-}"
URL="https://kong.bintray.com/kong-enterprise-edition-deb/dists/kong-enterprise-edition-${KONG_VERSION}.xenial.all.deb"
RESPONSE_CODE=$(/usr/bin/curl -sL \
-w "%{http_code}" \
-u $KONG_ENTERPRISE_REPO_USERNAME:$KONG_ENTERPRISE_REPO_PASSSWORD \
$URL -o kong.deb)
if [[ $RESPONSE_CODE != "200" ]]; then
echo "error retrieving kong enterprise package from ${URL}. response code ${RESPONSE_CODE}"
exit 1
fi
}

if [[ $KONG_VERSION == *"enterprise"* ]]; then
setup_kong_enterprise
else
setup_kong
fi

sudo dpkg -i kong.deb
echo $KONG_LICENSE_DATA | sudo tee /etc/kong/license.json
export KONG_LICENSE_PATH=/tmp/license.json
export KONG_PASSWORD=kong
export KONG_ENFORCE_RBAC=on
export KONG_PORTAL=on

sudo kong migrations bootstrap
sudo kong version
sudo kong start
52 changes: 52 additions & 0 deletions .github/workflows/integration-test-enterprise.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: "Integration Test : Enterprise"

on: [push, pull_request]

jobs:
test:
continue-on-error: true
strategy:
matrix:
kong_version:
- "enterprise-1.3.0.2"
- "enterprise-1.5.0.9"
- "enterprise-2.1.4.4"
- "enterprise-2.2.1.0"
- "enterprise-2.3.2.0"
env:
KONG_VERSION: ${{ matrix.kong_version }}
KONG_ENTERPRISE_REPO_USERNAME: ${{ secrets.KONG_ENTERPRISE_REPO_USERNAME }}
KONG_ENTERPRISE_REPO_PASSSWORD: ${{ secrets.KONG_ENTERPRISE_REPO_PASSSWORD }}
KONG_LICENSE_DATA: ${{ secrets.KONG_LICENSE_DATA }}
KONG_ANONYMOUS_REPORTS: "off"
KONG_ADMIN_TOKEN: kong
runs-on: ubuntu-latest
services:
postgres:
image: postgres:11.6-alpine
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Setup go
uses: actions/setup-go@v2
with:
go-version: "^1.16"
- name: Checkout repository
uses: actions/checkout@v2
- name: Setup Postgres
run: |
psql -c 'create database kong;' -U postgres -h 127.0.0.1 -p 5432
psql -c 'create user kong;' -U postgres -h 127.0.0.1 -p 5432
psql -c 'GRANT ALL PRIVILEGES ON DATABASE "kong" to kong;' -U postgres -h 127.0.0.1 -p 5432
- name: Setup Kong
run: make setup-kong
- name: Run tests
run: make integration-test-coverage
- name: Upload Code Coverage
uses: codecov/codecov-action@v1
with:
name: codecov-${{ matrix.kong_version }}
flags: ${{ matrix.kong_version }},integration,enterprise
fail_ci_if_error: true
52 changes: 52 additions & 0 deletions .github/workflows/integration-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: "Integration Test : Community"

on: [push, pull_request]

jobs:
test:
strategy:
matrix:
kong_version:
- "1.0.3"
- "1.1.2"
- "1.2.0"
- "1.3.0"
- "1.4.0"
- "2.0.4"
- "2.1.0"
- "2.2.0"
- "2.3.0"
env:
KONG_VERSION: ${{ matrix.kong_version }}
KONG_LICENSE_DATA: ${{ secrets.KONG_LICENSE_DATA }}
KONG_ANONYMOUS_REPORTS: "off"
runs-on: ubuntu-latest
services:
postgres:
image: postgres:11.6-alpine
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Setup go
uses: actions/setup-go@v2
with:
go-version: "^1.16"
- name: Checkout repository
uses: actions/checkout@v2
- name: Setup Postgres
run: |
psql -c 'create database kong;' -U postgres -h 127.0.0.1 -p 5432
psql -c 'create user kong;' -U postgres -h 127.0.0.1 -p 5432
psql -c 'GRANT ALL PRIVILEGES ON DATABASE "kong" to kong;' -U postgres -h 127.0.0.1 -p 5432
- name: Setup Kong
run: make setup-kong
- name: Run tests
run: make integration-test-coverage
- name: Upload Code Coverage
uses: codecov/codecov-action@v1
with:
name: codecov-${{ matrix.kong_version }}
flags: ${{ matrix.kong_version }},integration,community
fail_ci_if_error: true
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- name: Setup go
uses: actions/setup-go@v2
with:
go-version: '^1.16'
go-version: "^1.16"
- name: Checkout repository
uses: actions/checkout@v2
- name: Setup golangci-lint
Expand All @@ -19,7 +19,7 @@ jobs:
- name: Verify Codegen
run: make verify-codegen
- name: Run tests with Coverage
run: make coverage
run: make test-coverage
- name: Upload Code Coverage
run: "bash <(curl -s https://codecov.io/bash)"
- name: Build
Expand Down
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,20 @@ update-codegen:
go generate ./...
./scripts/update-deepcopy-gen.sh

.PHONY: coverage
coverage:
.PHONY: test-coverage
test-coverage:
go test -race -v -count=1 -coverprofile=coverage.out.tmp ./...
# ignoring generated code for coverage
grep -E -v 'generated.deepcopy.go' coverage.out.tmp > coverage.out
rm -f coverage.out.tmp
rm -f coverage.out.tmp

.PHONY: integration-test-coverage
integration-test-coverage:
go test -tags=integration -race -v -count=1 -coverprofile=coverage.out.tmp ./...
# ignoring generated code for coverage
grep -E -v 'generated.deepcopy.go' coverage.out.tmp > coverage.out
rm -f coverage.out.tmp

.PHONY: setup-kong
setup-kong:
bash .ci/setup_kong.sh
38 changes: 16 additions & 22 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"context"
"fmt"
"net/http"
"os"

"github.com/blang/semver/v4"
Expand Down Expand Up @@ -38,7 +37,7 @@ func SetStopCh(stopCh chan struct{}) {
}

// workspaceExists checks if workspace exists in Kong.
func workspaceExists(config utils.KongClientConfig) (bool, error) {
func workspaceExists(ctx context.Context, config utils.KongClientConfig) (bool, error) {
if config.Workspace == "" {
// default workspace always exists
return true, nil
Expand All @@ -54,7 +53,7 @@ func workspaceExists(config utils.KongClientConfig) (bool, error) {
return false, err
}

_, _, err = wsClient.Routes.List(context.TODO(), nil)
_, _, err = wsClient.Routes.List(ctx, nil)
switch {
case kong.IsNotFoundErr(err):
return false, nil
Expand All @@ -65,7 +64,7 @@ func workspaceExists(config utils.KongClientConfig) (bool, error) {
}
}

func syncMain(filenames []string, dry bool, parallelism, delay int, workspace string) error {
func syncMain(ctx context.Context, filenames []string, dry bool, parallelism, delay int, workspace string) error {

// read target file
targetContent, err := file.GetContentFromFiles(filenames)
Expand All @@ -89,12 +88,12 @@ func syncMain(filenames []string, dry bool, parallelism, delay int, workspace st
}

// load Kong version after workspace
kongVersion, err := kongVersion(wsConfig)
kongVersion, err := kongVersion(ctx, wsConfig)
if err != nil {
return errors.Wrap(err, "reading Kong version")
}

workspaceExists, err := workspaceExists(wsConfig)
workspaceExists, err := workspaceExists(ctx, wsConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -131,7 +130,7 @@ func syncMain(filenames []string, dry bool, parallelism, delay int, workspace st
}

if !dry {
_, err = rootClient.Workspaces.Create(nil, &kong.Workspace{Name: &wsConfig.Workspace})
_, err = rootClient.Workspaces.Create(ctx, &kong.Workspace{Name: &wsConfig.Workspace})
if err != nil {
return err
}
Expand Down Expand Up @@ -173,37 +172,32 @@ func syncMain(filenames []string, dry bool, parallelism, delay int, workspace st
return nil
}

func kongVersion(config utils.KongClientConfig) (semver.Version, error) {
func kongVersion(ctx context.Context,
config utils.KongClientConfig) (semver.Version, error) {

var version string

workspace := config.Workspace

// remove workspace to be able to call top-level / endpoint
config.Workspace = ""
client, err := utils.GetKongClient(config)
if err != nil {
return semver.Version{}, err
}
root, err := client.Root(nil)
if err != nil {
if workspace == "" {
return semver.Version{}, err
}
// try with workspace path
req, err := http.NewRequest("GET",
utils.CleanAddress(config.Address)+"/"+workspace+"/kong",
nil)

if len(config.Workspace) > 0 {
req, err := client.NewRequest("GET", "/kong", nil, nil)
if err != nil {
return semver.Version{}, err
}
var resp map[string]interface{}
_, err = client.Do(nil, req, &resp)
_, err = client.Do(ctx, req, &resp)
if err != nil {
return semver.Version{}, err
}
version = resp["version"].(string)
} else {
root, err := client.Root(ctx)
if err != nil {
return semver.Version{}, err
}
version = root["version"].(string)
}

Expand Down
71 changes: 71 additions & 0 deletions cmd/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// +build integration

package cmd

import (
"context"
"github.com/blang/semver/v4"
"github.com/kong/deck/utils"
"github.com/kong/go-kong/kong"
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
)

var (
defaultCtx = context.Background()
)

func Test_kongVersion_Community(T *testing.T) {
kongVersionEnv, _ := os.LookupEnv("KONG_VERSION")
if strings.Contains(kongVersionEnv, "enterprise") {
T.Skip()
}
var expectedVersion = semver.MustParse(kongVersionEnv)
var config = NewTestClientConfig("")
version, err := kongVersion(defaultCtx, config)
assert := assert.New(T)
assert.Nil(err)
assert.NotNil(version)
assert.Equal(version.Major, expectedVersion.Major, "The two version should have the same major")
assert.Equal(version.Minor, expectedVersion.Minor, "The two version should have the same minor")
}

func Test_kongVersion_Enterprise(T *testing.T) {
kongVersionEnv, _ := os.LookupEnv("KONG_VERSION")
if !strings.Contains(kongVersionEnv, "enterprise") {
T.Skip()
}
kongVersionEnv = strings.Replace(kongVersionEnv, "enterprise-", "", 1)
var expectedVersion = semver.MustParse(kongVersionEnv)
var config = NewTestClientConfig("")
version, err := kongVersion(defaultCtx, config)
assert := assert.New(T)
assert.Nil(err)
assert.NotNil(version)
assert.Equal(version.Major, expectedVersion.Major, "The two version should have the same major")
assert.Equal(version.Minor, expectedVersion.Minor, "The two version should have the same minor")

client, err := utils.GetKongClient(config)
ws := &kong.Workspace{
Name: kong.String("test"),
}
client.Workspaces.Create(defaultCtx, ws)
config = NewTestClientConfig(*ws.Name)
workspaceversion, err := kongVersion(defaultCtx, config)
assert.Nil(err)
assert.NotNil(workspaceversion)
assert.Equal(workspaceversion.Major, expectedVersion.Major, "The two version should have the same major")
assert.Equal(workspaceversion.Minor, expectedVersion.Minor, "The two version should have the same minor")

}

func NewTestClientConfig(workspace string) utils.KongClientConfig {
kongAdminToken, _ := os.LookupEnv("KONG_ADMIN_TOKEN")
return utils.KongClientConfig{
Address: "http://localhost:8001",
Workspace: workspace,
Headers: []string{"kong-admin-token:" + kongAdminToken},
}
}
2 changes: 1 addition & 1 deletion cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ that will be created or updated or deleted.
`,
Args: validateNoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return syncMain(diffCmdKongStateFile, true, diffCmdParallelism, 0, diffWorkspace)
return syncMain(cmd.Context(), diffCmdKongStateFile, true, diffCmdParallelism, 0, diffWorkspace)
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(diffCmdKongStateFile) == 0 {
Expand Down
Loading