Skip to content

Commit

Permalink
Merge branch 'main' into diff-debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashansa-K authored Nov 25, 2024
2 parents 541e982 + 4c15b26 commit 81a4046
Show file tree
Hide file tree
Showing 29 changed files with 2,601 additions and 18 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/kong/go-database-reconciler

go 1.22.0

toolchain go1.22.4
toolchain go1.23.0

replace github.com/yudai/gojsondiff v1.0.0 => github.com/Kong/gojsondiff v1.3.0

Expand All @@ -21,15 +21,15 @@ require (
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/hexops/gotextdiff v1.0.3
github.com/kong/deck v1.34.0
github.com/kong/go-kong v0.59.0
github.com/kong/go-kong v0.60.0
github.com/samber/lo v1.47.0
github.com/shirou/gopsutil/v3 v3.24.5
github.com/ssgelm/cookiejarparser v1.0.1
github.com/stretchr/testify v1.9.0
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/sync v0.8.0
golang.org/x/term v0.24.0
k8s.io/code-generator v0.31.0
k8s.io/code-generator v0.31.2
sigs.k8s.io/yaml v1.4.0
)

Expand Down Expand Up @@ -105,7 +105,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tidwall/gjson v1.17.3 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ github.com/kong/deck v1.34.0 h1:iKSa5Cq8t7bdCv5R3XRV8UH+7FvnZB/YxhRTuih4rgg=
github.com/kong/deck v1.34.0/go.mod h1:IUAixgNa1YSvEphgX9OIHmDyyi1JRaGTB0bYieuD9qo=
github.com/kong/go-apiops v0.1.29 h1:c+AB8MmGIr+K01Afm4GB2xaOmJnD/8KWMJQkr9qssnc=
github.com/kong/go-apiops v0.1.29/go.mod h1:ZNdiTZyVrAssB4wjEYWV7BfpcV9UME9LxnDDZhMPuNU=
github.com/kong/go-kong v0.59.0 h1:U6dE2sqb8E8j0kESW/RCW9TkXH8Y3W0EtNDXJVsDNuM=
github.com/kong/go-kong v0.59.0/go.mod h1:8Vt6HmtgLNgL/7bSwAlz3DIWqBtzG7qEt9+OnMiQOa0=
github.com/kong/go-kong v0.60.0 h1:CVrLXRLVE+Gl4IZ3tdvpO7xNDz3c9YLTmra/HvT4oM8=
github.com/kong/go-kong v0.60.0/go.mod h1:t1eMY8GRS6778uQNzxgzRgnA3YKBXSZOEvYbNocH/aA=
github.com/kong/go-slugify v1.0.0 h1:vCFAyf2sdoSlBtLcrmDWUFn0ohlpKiKvQfXZkO5vSKY=
github.com/kong/go-slugify v1.0.0/go.mod h1:dbR2h3J2QKXQ1k0aww6cN7o4cIcwlWflr6RKRdcoaiw=
github.com/kong/semver/v4 v4.0.1 h1:DIcNR8W3gfx0KabFBADPalxxsp+q/5COwIFkkhrFQ2Y=
Expand Down Expand Up @@ -316,8 +316,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94=
github.com/tidwall/gjson v1.17.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
Expand Down Expand Up @@ -472,8 +472,8 @@ gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/code-generator v0.31.0 h1:w607nrMi1KeDKB3/F/J4lIoOgAwc+gV9ZKew4XRfMp8=
k8s.io/code-generator v0.31.0/go.mod h1:84y4w3es8rOJOUUP1rLsIiGlO1JuEaPFXQPA9e/K6U0=
k8s.io/code-generator v0.31.2 h1:xLWxG0HEpMSHfcM//3u3Ro2Hmc6AyyLINQS//Z2GEOI=
k8s.io/code-generator v0.31.2/go.mod h1:eEQHXgBU/m7LDaToDoiz3t97dUUVyOblQdwOr8rivqc=
k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 h1:NGrVE502P0s0/1hudf8zjgwki1X/TByhmAoILTarmzo=
k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70/go.mod h1:VH3AT8AaQOqiGjMF9p0/IM1Dj+82ZwjfxUP1IxaHE+8=
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
Expand Down
17 changes: 13 additions & 4 deletions pkg/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,17 +612,15 @@ func (sc *Syncer) Solve(ctx context.Context, parallelism int, dry bool, isJSONOu
pluginCopy := &state.Plugin{Plugin: *plugin.DeepCopy()}
e.Obj = pluginCopy

exists, err := utils.WorkspaceExists(ctx, sc.kongClient)
if err == nil && exists {
if exists, _ := utils.WorkspaceExists(ctx, sc.kongClient); exists {
var schema map[string]interface{}
schema, err = sc.kongClient.Plugins.GetFullSchema(ctx, pluginCopy.Plugin.Name)
if err != nil {
return nil, err
}

// fill defaults and auto fields for the configuration that will be used for the diff
newPlugin := &pluginCopy.Plugin
if err := kong.FillPluginsDefaults(newPlugin, schema); err != nil {
if err := kong.FillPluginsDefaults(&pluginCopy.Plugin, schema); err != nil {
return nil, fmt.Errorf("failed processing auto fields: %w", err)
}

Expand All @@ -635,6 +633,17 @@ func (sc *Syncer) Solve(ctx context.Context, parallelism int, dry bool, isJSONOu
}); err != nil {
return nil, fmt.Errorf("failed processing auto fields: %w", err)
}

// `oldPlugin` contains both new and deprecated fields.
// If `plugin` (the new plugin) contains only deprecated fields,
// we need to remove the new fields from `oldPlugin` to ensure both configurations align correctly.
if oldPlugin, ok := e.OldObj.(*state.Plugin); ok {
oldPluginCopy := &state.Plugin{Plugin: *oldPlugin.DeepCopy()}
e.OldObj = oldPluginCopy
if err := kong.ClearUnmatchingDeprecations(&pluginCopy.Plugin, &oldPluginCopy.Plugin, schema); err != nil {
return nil, fmt.Errorf("failed processing auto fields: %w", err)
}
}
}
}

Expand Down
12 changes: 8 additions & 4 deletions pkg/konnect/login_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ func (s *AuthService) Login(ctx context.Context, email,
return authResponse, nil
}

// getGlobalEndpoint returns the global endpoint for a given base Konnect URL.
func getGlobalEndpoint(baseURL string) string {
parts := strings.Split(baseURL, "api.konghq")
return baseEndpointUS + parts[len(parts)-1]
}

// getGlobalAuthEndpoint returns the global auth endpoint
// given a base Konnect URL.
func getGlobalAuthEndpoint(baseURL string) string {
parts := strings.Split(baseURL, "api.konghq")
return baseEndpointUS + parts[len(parts)-1] + authEndpointV2
return getGlobalEndpoint(baseURL) + authEndpointV2
}

func createAuthRequest(baseURL, email, password string) (*http.Request, error) {
Expand Down Expand Up @@ -129,8 +134,7 @@ func (s *AuthService) LoginV2(ctx context.Context, email,
func (s *AuthService) OrgUserInfo(ctx context.Context) (*OrgUserInfo, error) {
// replace geo-specific endpoint with global one for retrieving org info
client := *s.client
client.baseURL = strings.Replace(s.client.baseURL, "eu.", "global.", 1)
client.baseURL = strings.Replace(client.baseURL, "au.", "global.", 1)
client.baseURL = getGlobalEndpoint(client.baseURL)

req, err := client.NewRequest(http.MethodGet, "/v2/organizations/me", nil, nil)
if err != nil {
Expand Down
55 changes: 55 additions & 0 deletions pkg/konnect/login_service_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
package konnect

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type mockRoundTripper struct{ mockHost string }

func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req.Host = req.URL.Host
req.URL.Scheme = "http"
req.URL.Host = m.mockHost

return (&http.Client{}).Do(req)
}

func TestGetGlobalAuthEndpoint(t *testing.T) {
tests := []struct {
baseURL string
Expand Down Expand Up @@ -40,3 +55,43 @@ func TestGetGlobalAuthEndpoint(t *testing.T) {
assert.Equal(t, tt.expected, getGlobalAuthEndpoint(tt.baseURL))
}
}

func TestAuthService_OrgUserInfo(t *testing.T) {
expectedResp := OrgUserInfo{Name: "test-org", OrgID: "1234"}

mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Contains(t, r.Host, "global.api.konghq.com")

if r.URL.Path == "/v2/organizations/me" && r.Method == http.MethodGet {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

resp, err := json.Marshal(expectedResp)
require.NoError(t, err)

_, err = w.Write(resp)
require.NoError(t, err)

return
}

http.NotFound(w, r)
}))
defer mockServer.Close()

authService := &AuthService{
client: &Client{
baseURL: "https://some-geo.api.konghq.com",
client: &http.Client{
Transport: &mockRoundTripper{
mockHost: mockServer.Listener.Addr().String(),
},
},
},
}

info, err := authService.OrgUserInfo(context.Background())
require.NoError(t, err)
assert.Equal(t, expectedResp.Name, info.Name)
assert.Equal(t, expectedResp.OrgID, info.OrgID)
}
4 changes: 4 additions & 0 deletions pkg/types/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ func (d *pluginDiffer) createUpdatePlugin(plugin *state.Plugin) (*crud.Event, er
return nil, fmt.Errorf("failed processing auto fields: %w", err)
}

if err := kong.ClearUnmatchingDeprecations(&pluginWithDefaults.Plugin, &currentPlugin.Plugin, schema); err != nil {
return nil, fmt.Errorf("failed clearing unmatching deprecations fields: %w", err)
}

if !currentPlugin.EqualWithOpts(pluginWithDefaults, false, true, false) {
return &crud.Event{
Op: crud.Update,
Expand Down
Loading

0 comments on commit 81a4046

Please sign in to comment.