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 5 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
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
4 changes: 3 additions & 1 deletion cmd/diff.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"context"

"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand All @@ -24,7 +26,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(context.Background(), diffCmdKongStateFile, true, diffCmdParallelism, 0, diffWorkspace)
mmorel-35 marked this conversation as resolved.
Show resolved Hide resolved
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(diffCmdKongStateFile) == 0 {
Expand Down
11 changes: 6 additions & 5 deletions cmd/dump.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"net/http"
"strings"

Expand All @@ -21,7 +22,7 @@ var (
dumpWithID bool
)

func listWorkspaces(client *kong.Client, baseURL string) ([]string, error) {
func listWorkspaces(ctx context.Context, client *kong.Client, baseURL string) ([]string, error) {
type Workspace struct {
Name string
}
Expand All @@ -35,7 +36,7 @@ func listWorkspaces(client *kong.Client, baseURL string) ([]string, error) {
if err != nil {
return nil, errors.Wrap(err, "building request for fetching workspaces")
}
_, err = client.Do(nil, req, &response)
_, err = client.Do(ctx, req, &response)
if err != nil {
return nil, errors.Wrap(err, "fetching workspaces from Kong")
}
Expand All @@ -58,7 +59,7 @@ The file can then be read using the Sync o Diff command to again
configure Kong.`,
Args: validateNoArgs,
RunE: func(cmd *cobra.Command, args []string) error {

var ctx = context.Background()
mmorel-35 marked this conversation as resolved.
Show resolved Hide resolved
wsClient, err := utils.GetKongClient(rootConfig)
if err != nil {
return err
Expand All @@ -74,7 +75,7 @@ configure Kong.`,
if dumpCmdKongStateFile != "kong" {
return errors.New("output-file cannot be specified with --all-workspace flag")
}
workspaces, err := listWorkspaces(wsClient, rootConfig.Address)
workspaces, err := listWorkspaces(ctx, wsClient, rootConfig.Address)
if err != nil {
return err
}
Expand Down Expand Up @@ -118,7 +119,7 @@ configure Kong.`,
if dumpWorkspace != "" {
wsConfig := rootConfig.ForWorkspace(dumpWorkspace)

exists, err := workspaceExists(wsConfig)
exists, err := workspaceExists(ctx, wsConfig)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/ping.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"fmt"

"github.com/pkg/errors"
Expand All @@ -21,7 +22,7 @@ can connect to Kong's Admin API or not.`,
RunE: func(cmd *cobra.Command, args []string) error {

wsConfig := rootConfig.ForWorkspace(pingWorkspace)
version, err := kongVersion(wsConfig)
version, err := kongVersion(context.Background(), wsConfig)
mmorel-35 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return errors.Wrap(err, "reading Kong version")
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/reset.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"github.com/kong/deck/dump"
"github.com/kong/deck/reset"
"github.com/kong/deck/utils"
Expand Down Expand Up @@ -57,17 +58,17 @@ By default, this command will ask for a confirmation prompt.`,
if resetAllWorkspaces && resetWorkspace != "" {
return errors.New("workspace cannot be specified with --all-workspace flag")
}

var ctx = context.Background()
// Kong Enterprise
var workspaces []string
if resetAllWorkspaces {
workspaces, err = listWorkspaces(rootClient, rootConfig.Address)
workspaces, err = listWorkspaces(ctx, rootClient, rootConfig.Address)
if err != nil {
return err
}
}
if resetWorkspace != "" {
exists, err := workspaceExists(rootConfig.ForWorkspace(resetWorkspace))
exists, err := workspaceExists(ctx, rootConfig.ForWorkspace(resetWorkspace))
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/sync.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand All @@ -21,7 +22,8 @@ var syncCmd = &cobra.Command{
to get Kong's state in sync with the input state.`,
Args: validateNoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return syncMain(syncCmdKongStateFile, false, syncCmdParallelism, syncCmdDBUpdateDelay, syncWorkspace)
return syncMain(context.Background(), syncCmdKongStateFile, false, syncCmdParallelism,
mmorel-35 marked this conversation as resolved.
Show resolved Hide resolved
syncCmdDBUpdateDelay, syncWorkspace)
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(syncCmdKongStateFile) == 0 {
Expand Down