Skip to content

Commit

Permalink
feat: replace prints with state warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rainest committed Dec 15, 2023
1 parent 78f4f43 commit 78622f3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
14 changes: 12 additions & 2 deletions pkg/file/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"sync"

"github.com/blang/semver/v4"
"github.com/kong/go-database-reconciler/pkg/konnect"
Expand All @@ -12,7 +13,11 @@ import (
"github.com/kong/go-kong/kong"
)

const ratelimitingAdvancedPluginName = "rate-limiting-advanced"
const (
ratelimitingAdvancedPluginName = "rate-limiting-advanced"
basicAuthPasswordWarning = "Warning: import/export of basic-auth" +
"credentials using decK doesn't work due to hashing of passwords in Kong."
)

type stateBuilder struct {
targetContent *Content
Expand All @@ -37,6 +42,8 @@ type stateBuilder struct {

checkRoutePaths bool

warnBasicAuth sync.Once

isConsumerGroupScopedPluginSupported bool

err error
Expand Down Expand Up @@ -361,6 +368,9 @@ func (b *stateBuilder) consumers() {

var basicAuths []kong.BasicAuth
for _, cred := range c.BasicAuths {
b.warnBasicAuth.Do(func() {
b.rawState.Warnings = append(b.rawState.Warnings, basicAuthPasswordWarning)
})
cred.Consumer = utils.GetConsumerReference(c.Consumer)
basicAuths = append(basicAuths, *cred)
}
Expand Down Expand Up @@ -811,7 +821,7 @@ func (b *stateBuilder) routes() {
}
}
if len(unsupportedRoutes) > 0 {
utils.PrintRouteRegexWarning(unsupportedRoutes)
b.rawState.Warnings = append(b.rawState.Warnings, utils.FormatRouteRegexWarning(unsupportedRoutes))
}
}
}
Expand Down
13 changes: 0 additions & 13 deletions pkg/types/basicauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"sync"

"github.com/kong/go-database-reconciler/pkg/cprint"
"github.com/kong/go-database-reconciler/pkg/crud"
"github.com/kong/go-database-reconciler/pkg/state"
"github.com/kong/go-database-reconciler/pkg/utils"
Expand Down Expand Up @@ -99,16 +98,6 @@ type basicAuthDiffer struct {
currentState, targetState *state.KongState
}

func (d *basicAuthDiffer) warnBasicAuth() {
const (
basicAuthPasswordWarning = "Warning: import/export of basic-auth" +
"credentials using decK doesn't work due to hashing of passwords in Kong."
)
d.once.Do(func() {
cprint.UpdatePrintln(basicAuthPasswordWarning)
})
}

func (d *basicAuthDiffer) Deletes(handler func(crud.Event) error) error {
currentBasicAuths, err := d.currentState.BasicAuths.GetAll()
if err != nil {
Expand All @@ -131,7 +120,6 @@ func (d *basicAuthDiffer) Deletes(handler func(crud.Event) error) error {
}

func (d *basicAuthDiffer) deleteBasicAuth(basicAuth *state.BasicAuth) (*crud.Event, error) {
d.warnBasicAuth()
_, err := d.targetState.BasicAuths.Get(*basicAuth.ID)
if errors.Is(err, state.ErrNotFound) {
return &crud.Event{
Expand Down Expand Up @@ -169,7 +157,6 @@ func (d *basicAuthDiffer) CreateAndUpdates(handler func(crud.Event) error) error
}

func (d *basicAuthDiffer) createUpdateBasicAuth(basicAuth *state.BasicAuth) (*crud.Event, error) {
d.warnBasicAuth()
basicAuth = &state.BasicAuth{BasicAuth: *basicAuth.DeepCopy()}
currentBasicAuth, err := d.currentState.BasicAuths.Get(*basicAuth.ID)
if errors.Is(err, state.ErrNotFound) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type KongRawState struct {

RBACRoles []*kong.RBACRole
RBACEndpointPermissions []*kong.RBACEndpointPermission

// Warnings are not Kong data. This field stores any warnings related to Kong data found while building state.
Warnings []string
}

// KonnectRawState contains all of Konnect resources.
Expand Down
7 changes: 3 additions & 4 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"

"github.com/blang/semver/v4"
"github.com/kong/go-database-reconciler/pkg/cprint"
"github.com/kong/go-kong/kong"
)

Expand Down Expand Up @@ -236,14 +235,14 @@ func HasPathsWithRegex300AndAbove(route kong.Route) bool {
return false
}

// PrintRouteRegexWarning prints out a warning about 3.x routes' path usage.
func PrintRouteRegexWarning(unsupportedRoutes []string) {
// FormatRouteRegexWarning returns a warning about 3.x routes' path usage.
func FormatRouteRegexWarning(unsupportedRoutes []string) string {
unsupportedRoutesLen := len(unsupportedRoutes)
// do not consider more than 10 sample routes to print out.
if unsupportedRoutesLen > 10 {
unsupportedRoutes = unsupportedRoutes[:10]
}
cprint.UpdatePrintf(
return fmt.Sprintf(
"%d unsupported routes' paths format with Kong version 3.0\n"+
"or above were detected. Some of these routes are (not an exhaustive list):\n\n"+
"%s\n\n"+UpgradeMessage,
Expand Down

0 comments on commit 78622f3

Please sign in to comment.