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 description parameter to ApisixConsumer #2223

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docs/en/latest/concepts/apisix_consumer.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ kind: ApisixConsumer
metadata:
name: jack
spec:
description: "Jack consumer"
authParameter:
keyAuth:
value:
Expand Down
1 change: 1 addition & 0 deletions docs/en/latest/references/apisix_consumer_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ See the [definition](../../../../samples/deploy/crd/v1/ApisixConsumer.yaml) on G

| Field | Type | Description |
|------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------|
| description | string | Description of the consumer |
| authParameter | object | Configuration of one of the available authentication plugins. |
| authParameter.basicAuth.value | object | Plugin configuration for [`basic-auth` plugin](https://apisix.apache.org/docs/apisix/plugins/basic-auth/) |
| authParameter.basicAuth.secretRef.name | string | You can store plugin configuration in Kubernetes secret and reference here with the secret name |
Expand Down
2 changes: 2 additions & 0 deletions pkg/kube/apisix/apis/config/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ type ApisixConsumerSpec struct {
// +optional
IngressClassName string `json:"ingressClassName,omitempty" yaml:"ingressClassName,omitempty"`

Description string `json:"description,omitempty" yaml:"description,omitempty"`

AuthParameter ApisixConsumerAuthParameter `json:"authParameter" yaml:"authParameter"`
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/providers/apisix/translation/apisix_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,11 @@ func (t *translator) TranslateApisixConsumerV2(ac *configv2.ApisixConsumer) (*ap
}
consumer.Username = apisixv1.ComposeConsumerName(ac.Namespace, ac.Name)
consumer.Plugins = plugins
if len(ac.Spec.Description) != 0 {
consumer.Desc = ac.Spec.Description
} else {
consumer.Desc = ""
}

return consumer, nil
}
11 changes: 11 additions & 0 deletions pkg/providers/apisix/translation/apisix_consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestTranslateApisixConsumerV2(t *testing.T) {
Namespace: "qa",
},
Spec: configv2.ApisixConsumerSpec{
Description: "Jack Consumer",
AuthParameter: configv2.ApisixConsumerAuthParameter{
BasicAuth: &configv2.ApisixConsumerBasicAuth{
Value: &configv2.ApisixConsumerBasicAuthValue{
Expand All @@ -43,6 +44,7 @@ func TestTranslateApisixConsumerV2(t *testing.T) {
}
consumer, err := (&translator{}).TranslateApisixConsumerV2(ac)
assert.Nil(t, err)
assert.Equal(t, "Jack Consumer", consumer.Desc)
assert.Len(t, consumer.Plugins, 1)
cfg := consumer.Plugins["basic-auth"].(*apisixv1.BasicAuthConsumerConfig)
assert.Equal(t, "jack", cfg.Username)
Expand All @@ -54,6 +56,7 @@ func TestTranslateApisixConsumerV2(t *testing.T) {
Namespace: "qa",
},
Spec: configv2.ApisixConsumerSpec{
Description: "Jack Consumer",
AuthParameter: configv2.ApisixConsumerAuthParameter{
KeyAuth: &configv2.ApisixConsumerKeyAuth{
Value: &configv2.ApisixConsumerKeyAuthValue{
Expand All @@ -65,6 +68,7 @@ func TestTranslateApisixConsumerV2(t *testing.T) {
}
consumer, err = (&translator{}).TranslateApisixConsumerV2(ac)
assert.Nil(t, err)
assert.Equal(t, "Jack Consumer", consumer.Desc)
assert.Len(t, consumer.Plugins, 1)
cfg2 := consumer.Plugins["key-auth"].(*apisixv1.KeyAuthConsumerConfig)
assert.Equal(t, "qwerty", cfg2.Key)
Expand All @@ -75,6 +79,7 @@ func TestTranslateApisixConsumerV2(t *testing.T) {
Namespace: "qa",
},
Spec: configv2.ApisixConsumerSpec{
Description: "Jack Consumer",
AuthParameter: configv2.ApisixConsumerAuthParameter{
JwtAuth: &configv2.ApisixConsumerJwtAuth{
Value: &configv2.ApisixConsumerJwtAuthValue{
Expand All @@ -92,6 +97,7 @@ func TestTranslateApisixConsumerV2(t *testing.T) {
}
consumer, err = (&translator{}).TranslateApisixConsumerV2(ac)
assert.Nil(t, err)
assert.Equal(t, "Jack Consumer", consumer.Desc)
assert.Len(t, consumer.Plugins, 1)
cfg3 := consumer.Plugins["jwt-auth"].(*apisixv1.JwtAuthConsumerConfig)
assert.Equal(t, "foo", cfg3.Key)
Expand All @@ -108,6 +114,7 @@ func TestTranslateApisixConsumerV2(t *testing.T) {
Namespace: "qa",
},
Spec: configv2.ApisixConsumerSpec{
Description: "Jack Consumer",
AuthParameter: configv2.ApisixConsumerAuthParameter{
WolfRBAC: &configv2.ApisixConsumerWolfRBAC{
Value: &configv2.ApisixConsumerWolfRBACValue{
Expand All @@ -120,6 +127,7 @@ func TestTranslateApisixConsumerV2(t *testing.T) {
}
consumer, err = (&translator{}).TranslateApisixConsumerV2(ac)
assert.Nil(t, err)
assert.Equal(t, "Jack Consumer", consumer.Desc)
assert.Len(t, consumer.Plugins, 1)
cfg4 := consumer.Plugins["wolf-rbac"].(*apisixv1.WolfRBACConsumerConfig)
assert.Equal(t, "https://httpbin.org", cfg4.Server)
Expand All @@ -131,6 +139,7 @@ func TestTranslateApisixConsumerV2(t *testing.T) {
Namespace: "qa",
},
Spec: configv2.ApisixConsumerSpec{
Description: "Jack Consumer",
AuthParameter: configv2.ApisixConsumerAuthParameter{
HMACAuth: &configv2.ApisixConsumerHMACAuth{
Value: &configv2.ApisixConsumerHMACAuthValue{
Expand All @@ -143,6 +152,7 @@ func TestTranslateApisixConsumerV2(t *testing.T) {
}
consumer, err = (&translator{}).TranslateApisixConsumerV2(ac)
assert.Nil(t, err)
assert.Equal(t, "Jack Consumer", consumer.Desc)
assert.Len(t, consumer.Plugins, 1)
cfg5 := consumer.Plugins["hmac-auth"].(*apisixv1.HMACAuthConsumerConfig)
assert.Equal(t, "foo", cfg5.AccessKey)
Expand All @@ -165,6 +175,7 @@ func TestTranslateApisixConsumerV2(t *testing.T) {
}
consumer, err = (&translator{}).TranslateApisixConsumerV2(ac)
assert.Nil(t, err)
assert.Equal(t, "", consumer.Desc)
assert.Len(t, consumer.Plugins, 1)
cfg6 := consumer.Plugins["ldap-auth"].(*apisixv1.LDAPAuthConsumerConfig)
assert.Equal(t, "cn=user01,ou=users,dc=example,dc=org", cfg6.UserDN)
Expand Down
3 changes: 3 additions & 0 deletions samples/deploy/crd/v1/ApisixConsumer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ spec:
properties:
ingressClassName:
type: string
description:
type: string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the apisix consumer description definition schema sets the maxLength of this to 256. https://github.com/apache/apisix/blob/1a45d1da896d22048b08b1bdc1218a84468a98a2/apisix/schema_def.lua#L109
I think it will be good to enforce it at this level as well.

Copy link
Author

@jorgenll jorgenll Jun 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, change applied! Thanks @Revolyssup! Could you or @pottekkat re-launch the workflow?

maxLength: 256
authParameter:
type: object
oneOf:
Expand Down
20 changes: 12 additions & 8 deletions test/e2e/scaffold/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ kind: ApisixConsumer
metadata:
name: %s
spec:
description: %s
authParameter:
basicAuth:
value:
Expand All @@ -37,6 +38,7 @@ kind: ApisixConsumer
metadata:
name: %s
spec:
description: %s
authParameter:
basicAuth:
secretRef:
Expand All @@ -48,6 +50,7 @@ spec:
metadata:
name: %s
spec:
description: %s
authParameter:
keyAuth:
value:
Expand All @@ -59,29 +62,30 @@ spec:
metadata:
name: %s
spec:
description: %s
authParameter:
keyAuth:
secretRef:
name: %s
`
)

func (s *Scaffold) ApisixConsumerBasicAuthCreated(name, username, password string) error {
ac := fmt.Sprintf(_apisixConsumerBasicAuth, s.opts.ApisixResourceVersion, name, username, password)
func (s *Scaffold) ApisixConsumerBasicAuthCreated(name, desc, username, password string) error {
ac := fmt.Sprintf(_apisixConsumerBasicAuth, s.opts.ApisixResourceVersion, name, desc, username, password)
return s.CreateVersionedApisixResource(ac)
}

func (s *Scaffold) ApisixConsumerBasicAuthSecretCreated(name, secret string) error {
ac := fmt.Sprintf(_apisixConsumerBasicAuthSecret, s.opts.ApisixResourceVersion, name, secret)
func (s *Scaffold) ApisixConsumerBasicAuthSecretCreated(name, desc, secret string) error {
ac := fmt.Sprintf(_apisixConsumerBasicAuthSecret, s.opts.ApisixResourceVersion, name, desc, secret)
return s.CreateVersionedApisixResource(ac)
}

func (s *Scaffold) ApisixConsumerKeyAuthCreated(name, key string) error {
ac := fmt.Sprintf(_apisixConsumerKeyAuth, s.opts.ApisixResourceVersion, name, key)
func (s *Scaffold) ApisixConsumerKeyAuthCreated(name, desc, key string) error {
ac := fmt.Sprintf(_apisixConsumerKeyAuth, s.opts.ApisixResourceVersion, name, desc, key)
return s.CreateVersionedApisixResource(ac)
}

func (s *Scaffold) ApisixConsumerKeyAuthSecretCreated(name, secret string) error {
ac := fmt.Sprintf(_apisixConsumerKeyAuthSecret, s.opts.ApisixResourceVersion, name, secret)
func (s *Scaffold) ApisixConsumerKeyAuthSecretCreated(name, desc, secret string) error {
ac := fmt.Sprintf(_apisixConsumerKeyAuthSecret, s.opts.ApisixResourceVersion, name, desc, secret)
return s.CreateVersionedApisixResource(ac)
}
8 changes: 4 additions & 4 deletions test/e2e/suite-annotations/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _ = ginkgo.Describe("suite-annotations: authorization annotations", func() {
s := scaffoldFunc()

ginkgo.It("enable keyAuth in ingress networking/v1", func() {
err := s.ApisixConsumerKeyAuthCreated("foo", "bar")
err := s.ApisixConsumerKeyAuthCreated("foo", "foo consumer", "bar")
assert.Nil(ginkgo.GinkgoT(), err, "creating keyAuth ApisixConsumer")

// Wait until the ApisixConsumer create event was delivered.
Expand Down Expand Up @@ -78,7 +78,7 @@ spec:
})

ginkgo.It("enable keyAuth in ingress networking/v1beta1", func() {
err := s.ApisixConsumerKeyAuthCreated("foo", "bar")
err := s.ApisixConsumerKeyAuthCreated("foo", "foo consumer", "bar")
assert.Nil(ginkgo.GinkgoT(), err, "creating keyAuth ApisixConsumer")

// Wait until the ApisixConsumer create event was delivered.
Expand Down Expand Up @@ -124,7 +124,7 @@ spec:
})

ginkgo.It("enable basicAuth in ingress networking/v1", func() {
err := s.ApisixConsumerBasicAuthCreated("jack1", "jack1-username", "jack1-password")
err := s.ApisixConsumerBasicAuthCreated("jack1", "jack1 consumer", "jack1-username", "jack1-password")
assert.Nil(ginkgo.GinkgoT(), err, "creating keyAuth ApisixConsumer")

// Wait until the ApisixConsumer create event was delivered.
Expand Down Expand Up @@ -172,7 +172,7 @@ spec:
})

ginkgo.It("enable basicAuth in ingress networking/v1beta1", func() {
err := s.ApisixConsumerBasicAuthCreated("jack1", "jack1-username", "jack1-password")
err := s.ApisixConsumerBasicAuthCreated("jack1", "jack1 consumer", "jack1-username", "jack1-password")
assert.Nil(ginkgo.GinkgoT(), err, "creating keyAuth ApisixConsumer")

// Wait until the ApisixConsumer create event was delivered.
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/suite-ingress/suite-ingress-features/ingress-class.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ kind: ApisixConsumer
metadata:
name: jack
spec:
description: jack consumer
authParameter:
keyAuth:
value:
Expand All @@ -444,6 +445,7 @@ spec:
assert.Nil(ginkgo.GinkgoT(), err)
assert.Len(ginkgo.GinkgoT(), acs, 1)
assert.Contains(ginkgo.GinkgoT(), acs[0].Username, "jack")
assert.Contains(ginkgo.GinkgoT(), acs[0].Desc, "jack consumer")
assert.Equal(ginkgo.GinkgoT(), map[string]interface{}{"key": "jack-key"}, acs[0].Plugins["key-auth"])

// delete ApisixConsumer
Expand All @@ -461,6 +463,7 @@ metadata:
name: james
spec:
ingressClassName: apisix
description: james consumer
authParameter:
keyAuth:
value:
Expand All @@ -473,6 +476,7 @@ spec:
assert.Nil(ginkgo.GinkgoT(), err)
assert.Len(ginkgo.GinkgoT(), acs, 1)
assert.Contains(ginkgo.GinkgoT(), acs[0].Username, "james")
assert.Contains(ginkgo.GinkgoT(), acs[0].Desc, "james consumer")
assert.Equal(ginkgo.GinkgoT(), map[string]interface{}{"key": "james-key"}, acs[0].Plugins["key-auth"])
})

Expand Down Expand Up @@ -884,6 +888,7 @@ kind: ApisixConsumer
metadata:
name: jack
spec:
description: jack consumer
authParameter:
keyAuth:
value:
Expand All @@ -896,6 +901,7 @@ spec:
assert.Nil(ginkgo.GinkgoT(), err)
assert.Len(ginkgo.GinkgoT(), acs, 1)
assert.Contains(ginkgo.GinkgoT(), acs[0].Username, "jack")
assert.Contains(ginkgo.GinkgoT(), acs[0].Desc, "jack consumer")
assert.Equal(ginkgo.GinkgoT(), map[string]interface{}{"key": "jack-key"}, acs[0].Plugins["key-auth"])

// delete ApisixConsumer
Expand All @@ -913,6 +919,7 @@ metadata:
name: james
spec:
ingressClassName: apisix
description: james consumer
authParameter:
keyAuth:
value:
Expand All @@ -925,6 +932,7 @@ spec:
assert.Nil(ginkgo.GinkgoT(), err)
assert.Len(ginkgo.GinkgoT(), acs, 1)
assert.Contains(ginkgo.GinkgoT(), acs[0].Username, "james")
assert.Contains(ginkgo.GinkgoT(), acs[0].Desc, "james consumer")
assert.Equal(ginkgo.GinkgoT(), map[string]interface{}{"key": "james-key"}, acs[0].Plugins["key-auth"])

// update ApisixConsumer resource with ingressClassName: watch
Expand All @@ -935,6 +943,7 @@ metadata:
name: james
spec:
ingressClassName: watch
description: james consumer
authParameter:
keyAuth:
value:
Expand All @@ -947,6 +956,7 @@ spec:
assert.Nil(ginkgo.GinkgoT(), err)
assert.Len(ginkgo.GinkgoT(), acs, 1)
assert.Contains(ginkgo.GinkgoT(), acs[0].Username, "james")
assert.Contains(ginkgo.GinkgoT(), acs[0].Desc, "james consumer")
assert.Equal(ginkgo.GinkgoT(), map[string]interface{}{"key": "james-password"}, acs[0].Plugins["key-auth"])
})

Expand Down
5 changes: 3 additions & 2 deletions test/e2e/suite-ingress/suite-ingress-features/resourcesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ spec:
assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixRoutesCreated(3), "Checking number of routes")

// Create ApisixConsumer resource
err := s.ApisixConsumerKeyAuthCreated("foo", "foo-key")
err := s.ApisixConsumerKeyAuthCreated("foo", "foo consumer", "foo-key")
assert.Nil(ginkgo.GinkgoT(), err)
})

Expand Down Expand Up @@ -171,12 +171,13 @@ spec:
_ = s.CreateApisixConsumerByApisixAdmin([]byte(fmt.Sprintf(`
{
"username": "%s",
"desc", "%s",
"plugins": {
"key-auth": {
"key": "auth-one"
}
}
}`, consumer.Username)))
}`, consumer.Username, consumer.Desc)))
}

routes, _ = s.ListApisixRoutes()
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/suite-ingress/suite-ingress-features/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ wrw7im4TNSAdwVX4Y1F4svJ2as5SJn5QYGAzXDixNuwzXYrpP9rzA2s=
//TODO: ApisixGlobal
ginkgo.It("check ApisixConsumer status is recorded", func() {
// create ApisixConsumer resource
assert.Nil(ginkgo.GinkgoT(), s.ApisixConsumerBasicAuthCreated("test-apisix-consumer", "foo", "bar"), "create consumer error")
assert.Nil(ginkgo.GinkgoT(), s.ApisixConsumerBasicAuthCreated("test-apisix-consumer", "test-apisix-consumer consumer", "foo", "bar"), "create consumer error")
time.Sleep(6 * time.Second)

// status should be recorded as successful
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/suite-plugins/suite-plugins-authentication/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _ = ginkgo.Describe("suite-plugins-authentication: ApisixConsumer with basic
s := scaffoldFunc()

ginkgo.It("ApisixRoute with basicAuth consumer", func() {
assert.Nil(ginkgo.GinkgoT(), s.ApisixConsumerBasicAuthCreated("basicvalue", "foo", "bar"), "creating basicAuth ApisixConsumer")
assert.Nil(ginkgo.GinkgoT(), s.ApisixConsumerBasicAuthCreated("basicvalue", "basicvalue consumer", "foo", "bar"), "creating basicAuth ApisixConsumer")

// Wait until the ApisixConsumer create event was delivered.
time.Sleep(6 * time.Second)
Expand Down Expand Up @@ -114,7 +114,7 @@ data:
username: Zm9v
`
assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(secret), "creating basic secret for ApisixConsumer")
assert.Nil(ginkgo.GinkgoT(), s.ApisixConsumerBasicAuthSecretCreated("basicvalue", "basic"), "creating basicAuth ApisixConsumer")
assert.Nil(ginkgo.GinkgoT(), s.ApisixConsumerBasicAuthSecretCreated("basicvalue", "basicvalue consumer", "basic"), "creating basicAuth ApisixConsumer")

// Wait until the ApisixConsumer create event was delivered.
time.Sleep(6 * time.Second)
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/suite-plugins/suite-plugins-authentication/hmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ kind: ApisixConsumer
metadata:
name: hmacvalue
spec:
description: hmacvalue consumer
authParameter:
hmacAuth:
value:
Expand All @@ -51,6 +52,8 @@ spec:
grs, err := s.ListApisixConsumers()
assert.Nil(ginkgo.GinkgoT(), err, "listing consumer")
assert.Len(ginkgo.GinkgoT(), grs, 1)
assert.Contains(ginkgo.GinkgoT(), grs[0].Username, "hmacvalue")
assert.Contains(ginkgo.GinkgoT(), grs[0].Desc, "hmacvalue consumer")
assert.Len(ginkgo.GinkgoT(), grs[0].Plugins, 1)
hmacAuth, _ := grs[0].Plugins["hmac-auth"].(map[string]interface{})
assert.Equal(ginkgo.GinkgoT(), "papa", hmacAuth["access_key"])
Expand Down Expand Up @@ -142,6 +145,7 @@ kind: ApisixConsumer
metadata:
name: hmacvalue
spec:
description: hmacvalue consumer
authParameter:
hmacAuth:
secretRef:
Expand All @@ -155,6 +159,8 @@ spec:
grs, err := s.ListApisixConsumers()
assert.Nil(ginkgo.GinkgoT(), err, "listing consumer")
assert.Len(ginkgo.GinkgoT(), grs, 1)
assert.Contains(ginkgo.GinkgoT(), grs[0].Username, "hmacvalue")
assert.Contains(ginkgo.GinkgoT(), grs[0].Desc, "hmacvalue consumer")
assert.Len(ginkgo.GinkgoT(), grs[0].Plugins, 1)
hmacAuth, _ := grs[0].Plugins["hmac-auth"].(map[string]interface{})
assert.Equal(ginkgo.GinkgoT(), "papa", hmacAuth["access_key"])
Expand Down
Loading
Loading