Skip to content

Commit

Permalink
Merge branch 'main' into feat/default-lookup-services-consumerGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashansa-K authored Sep 3, 2024
2 parents 0dbb4f8 + 0b1b75f commit 7b2a02e
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 61 deletions.
22 changes: 12 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/kong/go-database-reconciler

go 1.21.1
go 1.22.0

toolchain go1.22.4

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

Expand All @@ -19,15 +21,15 @@ require (
github.com/hashicorp/go-retryablehttp v0.7.5
github.com/hexops/gotextdiff v1.0.3
github.com/kong/deck v1.34.0
github.com/kong/go-kong v0.55.0
github.com/kong/go-kong v0.58.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.23.0
k8s.io/code-generator v0.29.4
k8s.io/code-generator v0.31.0
sigs.k8s.io/yaml v1.4.0
)

Expand All @@ -48,12 +50,12 @@ require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/getkin/kin-openapi v0.108.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
Expand Down Expand Up @@ -103,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.1 // indirect
github.com/tidwall/gjson v1.17.3 // 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 All @@ -121,12 +123,12 @@ require (
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/gengo v0.0.0-20230829151522-9cce18d56c01 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)
68 changes: 26 additions & 42 deletions go.sum

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions pkg/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,7 @@ func (sc *Syncer) Solve(ctx context.Context, parallelism int, dry bool, isJSONOu
// that will be used for the diff. This is needed to avoid highlighting
// default values that were populated by Kong as differences.
if plugin, ok := e.Obj.(*state.Plugin); ok {
pluginCopy := &state.Plugin{
Plugin: *plugin.DeepCopy(),
Meta: plugin.Meta,
}
pluginCopy := &state.Plugin{Plugin: *plugin.DeepCopy()}
e.Obj = pluginCopy

exists, err := utils.WorkspaceExists(ctx, sc.kongClient)
Expand All @@ -623,8 +620,13 @@ func (sc *Syncer) Solve(ctx context.Context, parallelism int, dry bool, isJSONOu
return nil, err
}

if err := kong.FillPluginsDefaults(&pluginCopy.Plugin, schema); err != nil {
return nil, fmt.Errorf("failed filling plugin defaults: %w", err)
var oldPlugin *kong.Plugin
if kongStatePlugin, ok := e.OldObj.(*state.Plugin); ok {
oldPlugin = &kongStatePlugin.Plugin
}
newPlugin := &pluginCopy.Plugin
if err := kong.FillPluginsDefaultsAutoFields(newPlugin, schema, oldPlugin); err != nil {
return nil, fmt.Errorf("failed processing auto fields: %w", err)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/state/rbac_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ func TestRBACRolesCollection_Get(t *testing.T) {
}
rbacRole1 := RBACRole{
RBACRole: kong.RBACRole{
ID: kong.String("foo-id"),
ID: kong.String("foo-id"),
Name: kong.String("foo-name"),
},
}
rbacRole2 := RBACRole{
Expand Down Expand Up @@ -157,7 +158,7 @@ func TestRBACRolesCollection_Get(t *testing.T) {
k.Add(rbacRole1)
k.Add(rbacRole2)
for _, tt := range tests {
tc := &tt //nolint:gosec
tc := &tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := k.Get(tc.args.nameOrID)
Expand Down
1 change: 1 addition & 0 deletions pkg/types/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func NewEntity(t EntityType, opts EntityOpts) (Entity, error) {
kind: entityTypeToKind(Plugin),
currentState: opts.CurrentState,
targetState: opts.TargetState,
kongClient: opts.KongClient,
},
}, nil
case Consumer:
Expand Down
13 changes: 12 additions & 1 deletion pkg/types/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type pluginDiffer struct {
kind crud.Kind

currentState, targetState *state.KongState
kongClient *kong.Client
}

func (d *pluginDiffer) Deletes(handler func(crud.Event) error) error {
Expand Down Expand Up @@ -174,8 +175,18 @@ func (d *pluginDiffer) createUpdatePlugin(plugin *state.Plugin) (*crud.Event, er
}
currentPlugin = &state.Plugin{Plugin: *currentPlugin.DeepCopy()}
// found, check if update needed
// before checking the diff, fill in the defaults
schema, err := d.kongClient.Plugins.GetFullSchema(context.TODO(), plugin.Name)
if err != nil {
return nil, fmt.Errorf("failed getting schema: %w", err)
}
pluginWithDefaults := &state.Plugin{Plugin: *plugin.DeepCopy()}
err = kong.FillPluginsDefaultsAutoFields(&pluginWithDefaults.Plugin, schema, &currentPlugin.Plugin)
if err != nil {
return nil, fmt.Errorf("failed processing auto fields: %w", err)
}

if !currentPlugin.EqualWithOpts(plugin, false, true, false) {
if !currentPlugin.EqualWithOpts(pluginWithDefaults, false, true, false) {
return &crud.Event{
Op: crud.Update,
Kind: d.kind,
Expand Down
59 changes: 59 additions & 0 deletions tests/integration/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net/http/httptest"
"net/url"
"os"
"regexp"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -5627,3 +5628,61 @@ func Test_Sync_PluginDoNotFillDefaults(t *testing.T) {
})
})
}

func Test_Sync_PluginAutoFields(t *testing.T) {
client, err := getTestClient()

require.NoError(t, err)
ctx := context.Background()
t.Run("plugin_with_auto_fields", func(t *testing.T) {
mustResetKongState(ctx, t, client, deckDump.Config{})

currentState, err := fetchCurrentState(ctx, client, deckDump.Config{})
require.NoError(t, err)
targetState := stateFromFile(ctx, t,
"testdata/sync/034-fill-auto-oauth2/kong.yaml",
client,
deckDump.Config{},
)

kongURL, err := url.Parse(client.BaseRootURL())
require.NoError(t, err)
p := NewRecordRequestProxy(kongURL)
s := httptest.NewServer(p)
c, err := utils.GetKongClient(utils.KongClientConfig{
Address: s.URL,
})
require.NoError(t, err)

syncer, err := deckDiff.NewSyncer(deckDiff.SyncerOpts{
CurrentState: currentState,
TargetState: targetState,

KongClient: c,
})
stats, errs, changes := syncer.Solve(ctx, 1, false, true)
require.Empty(t, errs, "Should have no errors in syncing")
require.NoError(t, err)

require.Equal(t, int32(1), stats.CreateOps.Count(), "Should create 1 entity")
require.Len(t, changes.Creating, 1, "Should have 1 creating record in changes")

t.Run("should not override auto values with nils", func(t *testing.T) {
newState, err := fetchCurrentState(ctx, client, deckDump.Config{})
require.NoError(t, err)
plugins, err := newState.Plugins.GetAll()
require.NoError(t, err)
require.Len(t, plugins, 1)
plugin := plugins[0]
require.Equal(t, "oauth2", *plugin.Name)
provisionKey, ok := plugin.Config["provision_key"]
require.True(t, ok)

provisionKeyStr, ok := provisionKey.(string)
require.True(t, ok, "provision_key is not a string")
pattern := `^[a-zA-Z0-9]+$`
re := regexp.MustCompile(pattern)
require.True(t, re.MatchString(provisionKeyStr), "provision_key does not match the pattern")
})
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_format_version: "3.0"
plugins:
- name: oauth2
config:
enable_password_grant: true

0 comments on commit 7b2a02e

Please sign in to comment.