Skip to content

Commit

Permalink
feat: logs friendly names with plugins (#662)
Browse files Browse the repository at this point in the history
* feat: add names and usernames to plugin, consumer and route
* refactor: use of go-kong's FriendlyName() instead of Identifier() in logs
* feat: ignore diff for names in embedded entities
* tests: add test for plugins on entities creation

Co-authored-by: Matthieu Morel <[email protected]>
  • Loading branch information
GGabriele and mmorel-35 committed May 12, 2022
1 parent e05c002 commit e7b8777
Show file tree
Hide file tree
Showing 23 changed files with 564 additions and 143 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
### Added

- Log descriptive names when configuring plugins on other entities.
[#662](https://github.com/Kong/deck/pull/662)
- Docker images now include `jq` to assist with pre-processing environment
substitutions.
[#660](https://github.com/Kong/deck/pull/660)
Expand Down
38 changes: 18 additions & 20 deletions file/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (b *stateBuilder) consumers() {
// plugins for the Consumer
var plugins []FPlugin
for _, p := range c.Plugins {
p.Consumer = &kong.Consumer{ID: kong.String(*c.ID)}
p.Consumer = utils.GetConsumerReference(c.Consumer)
plugins = append(plugins, *p)
}
if err := b.ingestPlugins(plugins); err != nil {
Expand All @@ -211,7 +211,7 @@ func (b *stateBuilder) consumers() {

var keyAuths []kong.KeyAuth
for _, cred := range c.KeyAuths {
cred.Consumer = &kong.Consumer{ID: kong.String(*c.ID)}
cred.Consumer = utils.GetConsumerReference(c.Consumer)
keyAuths = append(keyAuths, *cred)
}
if err := b.ingestKeyAuths(keyAuths); err != nil {
Expand All @@ -221,7 +221,7 @@ func (b *stateBuilder) consumers() {

var basicAuths []kong.BasicAuth
for _, cred := range c.BasicAuths {
cred.Consumer = &kong.Consumer{ID: kong.String(*c.ID)}
cred.Consumer = utils.GetConsumerReference(c.Consumer)
basicAuths = append(basicAuths, *cred)
}
if err := b.ingestBasicAuths(basicAuths); err != nil {
Expand All @@ -231,7 +231,7 @@ func (b *stateBuilder) consumers() {

var hmacAuths []kong.HMACAuth
for _, cred := range c.HMACAuths {
cred.Consumer = &kong.Consumer{ID: kong.String(*c.ID)}
cred.Consumer = utils.GetConsumerReference(c.Consumer)
hmacAuths = append(hmacAuths, *cred)
}
if err := b.ingestHMACAuths(hmacAuths); err != nil {
Expand All @@ -241,7 +241,7 @@ func (b *stateBuilder) consumers() {

var jwtAuths []kong.JWTAuth
for _, cred := range c.JWTAuths {
cred.Consumer = &kong.Consumer{ID: kong.String(*c.ID)}
cred.Consumer = utils.GetConsumerReference(c.Consumer)
jwtAuths = append(jwtAuths, *cred)
}
if err := b.ingestJWTAuths(jwtAuths); err != nil {
Expand All @@ -251,7 +251,7 @@ func (b *stateBuilder) consumers() {

var oauth2Creds []kong.Oauth2Credential
for _, cred := range c.Oauth2Creds {
cred.Consumer = &kong.Consumer{ID: kong.String(*c.ID)}
cred.Consumer = utils.GetConsumerReference(c.Consumer)
oauth2Creds = append(oauth2Creds, *cred)
}
if err := b.ingestOauth2Creds(oauth2Creds); err != nil {
Expand All @@ -261,7 +261,7 @@ func (b *stateBuilder) consumers() {

var aclGroups []kong.ACLGroup
for _, cred := range c.ACLGroups {
cred.Consumer = &kong.Consumer{ID: kong.String(*c.ID)}
cred.Consumer = utils.GetConsumerReference(c.Consumer)
aclGroups = append(aclGroups, *cred)
}
if err := b.ingestACLGroups(aclGroups); err != nil {
Expand All @@ -271,9 +271,7 @@ func (b *stateBuilder) consumers() {

var mtlsAuths []kong.MTLSAuth
for _, cred := range c.MTLSAuths {
cred.Consumer = &kong.Consumer{
ID: kong.String(*c.ID),
}
cred.Consumer = utils.GetConsumerReference(c.Consumer)
mtlsAuths = append(mtlsAuths, *cred)
}

Expand Down Expand Up @@ -574,7 +572,7 @@ func (b *stateBuilder) ingestService(s *FService) error {
// plugins for the service
var plugins []FPlugin
for _, p := range s.Plugins {
p.Service = &kong.Service{ID: kong.String(*s.ID)}
p.Service = utils.GetServiceReference(s.Service)
plugins = append(plugins, *p)
}
if err := b.ingestPlugins(plugins); err != nil {
Expand All @@ -584,7 +582,7 @@ func (b *stateBuilder) ingestService(s *FService) error {
// routes for the service
for _, r := range s.Routes {
r := r
r.Service = &kong.Service{ID: kong.String(*s.ID)}
r.Service = utils.GetServiceReference(s.Service)
if err := b.ingestRoute(*r); err != nil {
return err
}
Expand Down Expand Up @@ -706,40 +704,40 @@ func (b *stateBuilder) plugins() {
c, err := b.intermediate.Consumers.Get(*p.Consumer.ID)
if err == state.ErrNotFound {
b.err = fmt.Errorf("consumer %v for plugin %v: %w",
*p.Consumer.ID, *p.Name, err)
p.Consumer.FriendlyName(), *p.Name, err)

return
} else if err != nil {
b.err = err
return
}
p.Consumer = &kong.Consumer{ID: kong.String(*c.ID)}
p.Consumer = utils.GetConsumerReference(c.Consumer)
}
if p.Service != nil && !utils.Empty(p.Service.ID) {
s, err := b.intermediate.Services.Get(*p.Service.ID)
if err == state.ErrNotFound {
b.err = fmt.Errorf("service %v for plugin %v: %w",
*p.Service.ID, *p.Name, err)
p.Service.FriendlyName(), *p.Name, err)

return
} else if err != nil {
b.err = err
return
}
p.Service = &kong.Service{ID: kong.String(*s.ID)}
p.Service = utils.GetServiceReference(s.Service)
}
if p.Route != nil && !utils.Empty(p.Route.ID) {
s, err := b.intermediate.Routes.Get(*p.Route.ID)
r, err := b.intermediate.Routes.Get(*p.Route.ID)
if err == state.ErrNotFound {
b.err = fmt.Errorf("route %v for plugin %v: %w",
*p.Route.ID, *p.Name, err)
p.Route.FriendlyName(), *p.Name, err)

return
} else if err != nil {
b.err = err
return
}
p.Route = &kong.Route{ID: kong.String(*s.ID)}
p.Route = utils.GetRouteReference(r.Route)
}
plugins = append(plugins, p)
}
Expand Down Expand Up @@ -797,7 +795,7 @@ func (b *stateBuilder) ingestRoute(r FRoute) error {
// plugins for the route
var plugins []FPlugin
for _, p := range r.Plugins {
p.Route = &kong.Route{ID: kong.String(*r.ID)}
p.Route = utils.GetRouteReference(r.Route)
plugins = append(plugins, *p)
}
if err := b.ingestPlugins(plugins); err != nil {
Expand Down
Loading

0 comments on commit e7b8777

Please sign in to comment.