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

feat: add konnect dev mode #820

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 3 additions & 5 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
var kongVersion string
var parsedKongVersion semver.Version
if mode == modeKonnect {
kongVersion, err = fetchKonnectKongVersion(ctx, kongClient)
if err != nil {
return fmt.Errorf("reading Konnect Kong version: %w", err)
}
kongVersion = fetchKonnectKongVersion()
} else {
kongVersion, err = fetchKongVersion(ctx, wsConfig)
if err != nil {
Expand Down Expand Up @@ -399,7 +396,8 @@ func inKonnectMode(targetContent *file.Content) bool {
return true
} else if rootConfig.Address != defaultKongURL {
return false
} else if konnectConfig.Email != "" ||
} else if konnectConfig.Dev ||
konnectConfig.Email != "" ||
konnectConfig.Password != "" ||
konnectConfig.Token != "" {
return true
Expand Down
66 changes: 34 additions & 32 deletions cmd/common_konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"strings"
Expand Down Expand Up @@ -76,23 +75,28 @@ func GetKongClientForKonnectMode(
if err != nil {
return nil, fmt.Errorf("parsing %s address: %v", address, err)
}
_, err = authenticate(ctx, konnectClient, parsedAddress.Host, *konnectConfig)
if err == nil {
break
}
// Personal Access Token authentication is not supported with the
// legacy Konnect, so we don't need to fallback in case of 401s.
if konnect.IsUnauthorizedErr(err) && konnectConfig.Token != "" {
return nil, fmt.Errorf("authenticating with Konnect: %w", err)
}
if konnect.IsUnauthorizedErr(err) {
continue
if !konnectConfig.Dev {
_, err = authenticate(ctx, konnectClient, parsedAddress.Host, *konnectConfig)
if err == nil {
break
}
// Personal Access Token authentication is not supported with the
// legacy Konnect, so we don't need to fallback in case of 401s.
if konnect.IsUnauthorizedErr(err) && konnectConfig.Token != "" {
return nil, fmt.Errorf("authenticating with Konnect: %w", err)
}
if konnect.IsUnauthorizedErr(err) {
continue
}
}
}
if err != nil {
return nil, fmt.Errorf("authenticating with Konnect: %w", err)
}
if strings.Contains(parsedAddress.Host, konnectWithRuntimeGroupsDomain) {
if konnectConfig.Dev {
// Local Development mode enabled, use konnect-addr as the CP addr.
konnectAddress = konnectConfig.Address
} else if strings.Contains(parsedAddress.Host, konnectWithRuntimeGroupsDomain) {
// get kong runtime group ID
kongRGID, err := fetchKongRuntimeGroupID(ctx, konnectClient)
if err != nil {
Expand All @@ -115,13 +119,22 @@ func GetKongClientForKonnectMode(
}

// initialize kong client
return utils.GetKongClient(utils.KongClientConfig{
kongClient, err := utils.GetKongClient(utils.KongClientConfig{
Address: konnectAddress,
HTTPClient: httpClient,
Debug: konnectConfig.Debug,
Headers: konnectConfig.Headers,
Retryable: true,
})
if err != nil {
return kongClient, err
}

if konnectConfig.Dev {
kongClient.QueryParams.Add("cluster.id", konnectRuntimeGroup)
}

return kongClient, nil
}

func resetKonnectV2(ctx context.Context) error {
Expand Down Expand Up @@ -158,10 +171,6 @@ func dumpKonnectV2(ctx context.Context) error {
if dumpConfig.KonnectRuntimeGroup == "" {
dumpConfig.KonnectRuntimeGroup = defaultRuntimeGroupName
}
kongVersion, err := fetchKonnectKongVersion(ctx, client)
if err != nil {
return fmt.Errorf("reading Konnect Kong version: %w", err)
}
rawState, err := dump.Get(ctx, client, dumpConfig)
if err != nil {
return fmt.Errorf("reading configuration from Kong: %w", err)
Expand All @@ -176,7 +185,7 @@ func dumpKonnectV2(ctx context.Context) error {
FileFormat: file.Format(strings.ToUpper(konnectDumpCmdStateFormat)),
WithID: dumpWithID,
RuntimeGroupName: konnectRuntimeGroup,
KongVersion: kongVersion,
KongVersion: fetchKonnectKongVersion(),
})
}

Expand Down Expand Up @@ -372,17 +381,10 @@ func getKonnectState(ctx context.Context,
return ks, nil
}

func fetchKonnectKongVersion(ctx context.Context, client *kong.Client) (string, error) {
req, err := http.NewRequest("GET",
utils.CleanAddress(client.BaseRootURL())+"/v1", nil)
if err != nil {
return "", err
}

var resp map[string]interface{}
_, err = client.Do(ctx, req, &resp)
if err != nil {
return "", err
}
return resp["version"].(string), nil
func fetchKonnectKongVersion() string {
// Returning an hardcoded version for now. decK only needs the version
// to determine the format_version expected in the state file. Since
// Konnect is always on the latest version, we can safely return the
// latest version here and avoid making an extra and unnecessary request.
return "3.5.0.0"
}
35 changes: 22 additions & 13 deletions cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,28 @@ func pingKonnect(ctx context.Context) error {
return err
}
u, _ := url.Parse(konnectConfig.Address)
// authenticate with konnect
res, err := authenticate(ctx, konnectClient, u.Host, konnectConfig)
if err != nil {
return fmt.Errorf("authenticating with Konnect: %w", err)
}
fullName := res.FullName
if res.FullName == "" {
fullName = fmt.Sprintf("%s %s", res.FirstName, res.LastName)
}
fmt.Printf("Successfully Konnected as %s (%s)!\n",
fullName, res.Organization)
if konnectConfig.Debug {
fmt.Printf("Organization ID: %s\n", res.OrganizationID)
if konnectConfig.Dev {
version := fetchKonnectKongVersion()
if err != nil {
return fmt.Errorf("reading Konnect Kong version: %w", err)
}
fmt.Println("Successfully Konnected to Kong!")
fmt.Println("Kong version: ", version)
} else {
// authenticate with konnect
res, err := authenticate(ctx, konnectClient, u.Host, konnectConfig)
if err != nil {
return fmt.Errorf("authenticating with Konnect: %w", err)
}
fullName := res.FullName
if res.FullName == "" {
fullName = fmt.Sprintf("%s %s", res.FirstName, res.LastName)
}
fmt.Printf("Successfully Konnected as %s (%s)!\n",
fullName, res.Organization)
if konnectConfig.Debug {
fmt.Printf("Organization ID: %s\n", res.OrganizationID)
}
}
_ = sendAnalytics("ping", "", modeKonnect)
return nil
Expand Down
11 changes: 11 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ It can be used to export, import, or sync entities to Kong.`,
viper.BindPFlag("konnect-runtime-group-name",
rootCmd.PersistentFlags().Lookup("konnect-runtime-group-name"))

// TMP
// when using --konnect-dev with dump, diff or sync,
// pass --konnect-runtime-group-name <cluster-id>
rootCmd.PersistentFlags().Bool("konnect-dev", false,
"Enable konnect local development mode.\n"+
"Use this mode to communicate directly with a konnect CP.")
rootCmd.Flags().MarkHidden("konnect-dev") // TODO make this one work
viper.BindPFlag("konnect-dev",
rootCmd.PersistentFlags().Lookup("konnect-dev"))

rootCmd.AddCommand(newSyncCmd())
rootCmd.AddCommand(newVersionCmd())
rootCmd.AddCommand(newValidateCmd())
Expand Down Expand Up @@ -370,6 +380,7 @@ func initKonnectConfig() error {
konnectConfig.Debug = (viper.GetInt("verbose") >= 1)
konnectConfig.Address = viper.GetString("konnect-addr")
konnectConfig.Headers = extendHeaders(viper.GetStringSlice("headers"))
konnectConfig.Dev = viper.GetBool("konnect-dev")
konnectRuntimeGroup = viper.GetString("konnect-runtime-group-name")
return nil
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/kong/deck
go 1.18

replace github.com/yudai/gojsondiff v1.0.0 => github.com/Kong/gojsondiff v1.3.0
replace github.com/kong/go-kong v0.34.1-0.20221222170410-6c81ce561662 => ./go-kong

require (
github.com/Kong/gojsondiff v1.3.2
Expand Down
2 changes: 1 addition & 1 deletion konnect/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (c *Client) SetControlPlaneID(cpID string) {
c.common.controlPlaneID = cpID
}

// SetControlPlaneID sets the konnect runtime-group ID in the client.
// SetRuntimeGroupID sets the konnect runtime-group ID in the client.
func (c *Client) SetRuntimeGroupID(rgID string) {
c.common.runtimeGroupID = rgID
}
Expand Down
3 changes: 2 additions & 1 deletion utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"github.com/hashicorp/go-retryablehttp"
"github.com/kong/deck/konnect"
"github.com/kong/go-kong/kong"
"github.com/kong/go-kong/kong/custom"

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong/kong-gateway:2.1.4.6)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong/kong-gateway:2.3.3.4)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong/kong-gateway:2.7.0.0)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong/kong-gateway:2.6.0.2)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong/kong-gateway:2.4.1.3)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong/kong-gateway:2.5.1.2)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong/kong-gateway:2.8.0.0)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong/kong-gateway:1.5.0.11)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong:1.5.1)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong:1.4.3)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong:2.3.3)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong:2.1.4)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong:2.4.1)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong:3.0.0)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong:2.2.2)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong/kong:master-alpine)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong:2.0.5)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong:2.8.0)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong:2.5.1)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong:2.7.0)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist

Check failure on line 21 in utils/types.go

View workflow job for this annotation

GitHub Actions / integration (kong:2.6.0)

github.com/kong/go-kong@v0.34.1-0.20221222170410-6c81ce561662: replacement directory ./go-kong does not exist
"github.com/ssgelm/cookiejarparser"
)

Expand Down Expand Up @@ -116,6 +116,7 @@
Password string
Token string
Debug bool
Dev bool

Address string

Expand Down Expand Up @@ -172,7 +173,7 @@
// 429 Too Many Requests is recoverable. Sometimes the server puts
// a Retry-After response header to indicate when the server is
// available to start processing request from client.
if resp.StatusCode == http.StatusTooManyRequests {
if resp != nil && resp.StatusCode == http.StatusTooManyRequests {
return true, nil
}
return false, nil
Expand Down
Loading