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: support secret plugin config #1486

Merged
merged 4 commits into from
Dec 12, 2022
Merged
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
47 changes: 47 additions & 0 deletions docs/en/latest/concepts/apisix_route.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,53 @@ spec:
enable: true
```

### Config with secretRef

Plugins are supported to be configured from kubernetes secret with `secretRef`.

The priority is `plugins.secretRef > plugins.config`. That is, the duplicated key in `plugins.config` are replaced by `plugins.secretRef`.

Example below configures echo plugin. The final values of `before_body`, `body` and `after_body` are "This is the replaced preface", "my custom body" and "This is the epilogue", respectively.

```yaml
apiVersion: v1
kind: Secret
metadata:
name: echo
data:
# content is "This is the replaced preface"
before_body: IlRoaXMgaXMgdGhlIHJlcGxhY2VkIHByZWZhY2Ui
# content is "my custom body"
body: Im15IGN1c3RvbSBib2R5Ig==
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: httpbin-route
spec:
http:
- name: rule1
match:
hosts:
- httpbin.org
paths:
- /ip
backends:
- serviceName: %s
servicePort: %d
weight: 10
plugins:
- name: echo
enable: true
config:
before_body: "This is the preface"
after_body: "This is the epilogue"
headers:
X-Foo: v1
X-Foo2: v2
secretRef: echo
```

## Websocket proxy

You can route requests to [WebSocket](https://en.wikipedia.org/wiki/WebSocket#:~:text=WebSocket%20is%20a%20computer%20communications,WebSocket%20is%20distinct%20from%20HTTP.) services by setting the `websocket` attribute to `true` as shown below:
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 @@ -170,6 +170,8 @@ type ApisixRoutePlugin struct {
Enable bool `json:"enable" yaml:"enable"`
// Plugin configuration.
Config ApisixRoutePluginConfig `json:"config" yaml:"config"`
// Plugin configuration secretRef.
SecretRef string `json:"secretRef" yaml:"secretRef"`
}

// ApisixRoutePluginConfig is the configuration for
Expand Down
15 changes: 15 additions & 0 deletions pkg/providers/apisix/translation/apisix_pluginconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ func (t *translator) TranslatePluginConfigV2(config *configv2.ApisixPluginConfig
zap.Any("new", plugin.Config),
)
}
if plugin.SecretRef != "" {
sec, err := t.SecretLister.Secrets(config.Namespace).Get(plugin.SecretRef)
if err != nil {
log.Errorw("The config secretRef is invalid",
zap.Any("plugin", plugin.Name),
zap.String("secretRef", plugin.SecretRef))
break
}
log.Debugw("Add new items, then override items with the same plugin key",
zap.Any("plugin", plugin.Name),
zap.String("secretRef", plugin.SecretRef))
for key, value := range sec.Data {
plugin.Config[key] = string(value)
}
}
pluginMap[plugin.Name] = plugin.Config
} else {
pluginMap[plugin.Name] = make(map[string]interface{})
Expand Down
30 changes: 30 additions & 0 deletions pkg/providers/apisix/translation/apisix_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ func (t *translator) translateHTTPRouteV2(ctx *translation.TranslateContext, ar
continue
}
if plugin.Config != nil {
if plugin.SecretRef != "" {
sec, err := t.SecretLister.Secrets(ar.Namespace).Get(plugin.SecretRef)
if err != nil {
log.Errorw("The config secretRef is invalid",
zap.Any("plugin", plugin.Name),
zap.String("secretRef", plugin.SecretRef))
break
}
log.Debugw("Add new items, then override items with the same plugin key",
zap.Any("plugin", plugin.Name),
zap.String("secretRef", plugin.SecretRef))
for key, value := range sec.Data {
plugin.Config[key] = string(value)
}
}
pluginMap[plugin.Name] = plugin.Config
} else {
pluginMap[plugin.Name] = make(map[string]interface{})
Expand Down Expand Up @@ -753,6 +768,21 @@ func (t *translator) translateStreamRouteV2(ctx *translation.TranslateContext, a
continue
}
if plugin.Config != nil {
if plugin.SecretRef != "" {
sec, err := t.SecretLister.Secrets(ar.Namespace).Get(plugin.SecretRef)
if err != nil {
log.Errorw("The config secretRef is invalid",
zap.Any("plugin", plugin.Name),
zap.String("secretRef", plugin.SecretRef))
break
}
log.Debugw("Add new items, then override items with the same plugin key",
zap.Any("plugin", plugin.Name),
zap.String("secretRef", plugin.SecretRef))
for key, value := range sec.Data {
plugin.Config[key] = string(value)
}
}
pluginMap[plugin.Name] = plugin.Config
} else {
pluginMap[plugin.Name] = make(map[string]interface{})
Expand Down
2 changes: 2 additions & 0 deletions samples/deploy/crd/v1/ApisixPluginConfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ spec:
config:
type: object
x-kubernetes-preserve-unknown-fields: true # we have to enable it since plugin config
secretRef:
type: string
required:
- name
- enable
Expand Down
4 changes: 4 additions & 0 deletions samples/deploy/crd/v1/ApisixRoute.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ spec:
config:
type: object
x-kubernetes-preserve-unknown-fields: true # we have to enable it since plugin config
secretRef:
type: string
required:
- name
- enable
Expand Down Expand Up @@ -592,6 +594,8 @@ spec:
config:
type: object
x-kubernetes-preserve-unknown-fields: true # we have to enable it since plugin config
secretRef:
type: string
required:
- name
- enable
Expand Down
93 changes: 93 additions & 0 deletions test/e2e/suite-plugins/suite-plugins-general/secret_ref.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package plugins

import (
"fmt"
ginkgo "github.com/onsi/ginkgo/v2"
"github.com/stretchr/testify/assert"
"net/http"

"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
)

var _ = ginkgo.Describe("suite-plugins-general: config plugin with secretRef", func() {
suites := func(scaffoldFunc func() *scaffold.Scaffold) {
s := scaffoldFunc()
ginkgo.It("suite-plugins-general: echo plugin config with secretRef", func() {
backendSvc, backendPorts := s.DefaultHTTPBackend()
secret := `
apiVersion: v1
kind: Secret
metadata:
name: echo
data:
# content is "This is the replaced preface"
before_body: IlRoaXMgaXMgdGhlIHJlcGxhY2VkIHByZWZhY2Ui
# content is "my custom body"
body: Im15IGN1c3RvbSBib2R5Ig==

`
assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(secret), "creating echo secret for ApisixRoute")
ar := fmt.Sprintf(`
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: httpbin-route
spec:
http:
- name: rule1
match:
hosts:
- httpbin.org
paths:
- /ip
backends:
- serviceName: %s
servicePort: %d
weight: 10
plugins:
- name: echo
enable: true
config:
before_body: "This is the preface"
after_body: "This is the epilogue"
headers:
X-Foo: v1
X-Foo2: v2
secretRef: echo

`, backendSvc, backendPorts[0])

assert.Nil(ginkgo.GinkgoT(), s.CreateVersionedApisixResource(ar))

err := s.EnsureNumApisixUpstreamsCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of upstreams")
err = s.EnsureNumApisixRoutesCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of routes")

resp := s.NewAPISIXClient().GET("/ip").WithHeader("Host", "httpbin.org").Expect()
resp.Status(http.StatusOK)
resp.Header("X-Foo").Equal("v1")
resp.Header("X-Foo2").Equal("v2")
resp.Body().Contains("This is the replaced preface")
resp.Body().Contains("This is the epilogue")
resp.Body().Contains("my custom body")
})
}
ginkgo.Describe("suite-plugins-general: scaffold v2", func() {
suites(scaffold.NewDefaultV2Scaffold)
})
})