Skip to content

Commit

Permalink
chore: change variables's name and fix Update func
Browse files Browse the repository at this point in the history
Signed-off-by: Jintao Zhang <zhangjintao9020@gmail.com>
  • Loading branch information
tao12345666333 committed Dec 29, 2021
1 parent e23bda8 commit 91ab406
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 49 deletions.
46 changes: 23 additions & 23 deletions pkg/ingress/apisix_pluginconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ func (c *apisixPluginConfigController) sync(ctx context.Context, ev *types.Event
return err
}
var (
ar kube.ApisixPluginConfig
apc kube.ApisixPluginConfig
tctx *translation.TranslateContext
)
switch obj.GroupVersion {
case kube.ApisixPluginConfigV2beta3:
ar, err = c.controller.apisixPluginConfigLister.V2beta3(namespace, name)
apc, err = c.controller.apisixPluginConfigLister.V2beta3(namespace, name)
}
if err != nil {
if !k8serrors.IsNotFound(err) {
Expand All @@ -116,7 +116,7 @@ func (c *apisixPluginConfigController) sync(ctx context.Context, ev *types.Event
}
}
if ev.Type == types.EventDelete {
if ar != nil {
if apc != nil {
// We still find the resource while we are processing the DELETE event,
// that means object with same namespace and name was created, discarding
// this stale DELETE event.
Expand All @@ -125,20 +125,20 @@ func (c *apisixPluginConfigController) sync(ctx context.Context, ev *types.Event
)
return nil
}
ar = ev.Tombstone.(kube.ApisixPluginConfig)
apc = ev.Tombstone.(kube.ApisixPluginConfig)
}

switch obj.GroupVersion {
case kube.ApisixPluginConfigV2beta3:
if ev.Type != types.EventDelete {
tctx, err = c.controller.translator.TranslatePluginConfigV2beta3(ar.V2beta3())
tctx, err = c.controller.translator.TranslatePluginConfigV2beta3(apc.V2beta3())
} else {
tctx, err = c.controller.translator.TranslatePluginConfigV2beta3NotStrictly(ar.V2beta3())
tctx, err = c.controller.translator.TranslatePluginConfigV2beta3NotStrictly(apc.V2beta3())
}
if err != nil {
log.Errorw("failed to translate ApisixPluginConfig v2beta3",
zap.Error(err),
zap.Any("object", ar),
zap.Any("object", apc),
)
return err
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func (c *apisixPluginConfigController) sync(ctx context.Context, ev *types.Event
zap.String("version", obj.GroupVersion),
zap.String("event", "update"),
zap.Error(err),
zap.Any("ApisixPluginConfig", ar),
zap.Any("ApisixPluginConfig", apc),
)
return err
}
Expand All @@ -196,18 +196,18 @@ func (c *apisixPluginConfigController) handleSyncErr(obj interface{}, errOrigin
c.controller.MetricsCollector.IncrSyncOperation("PluginConfig", "failure")
return
}
var ar kube.ApisixPluginConfig
var apc kube.ApisixPluginConfig
switch event.GroupVersion {
case kube.ApisixPluginConfigV2beta3:
ar, errLocal = c.controller.apisixPluginConfigLister.V2beta3(namespace, name)
apc, errLocal = c.controller.apisixPluginConfigLister.V2beta3(namespace, name)
}
if errOrigin == nil {
if ev.Type != types.EventDelete {
if errLocal == nil {
switch ar.GroupVersion() {
switch apc.GroupVersion() {
case kube.ApisixPluginConfigV2beta3:
c.controller.recorderEvent(ar.V2beta3(), v1.EventTypeNormal, _resourceSynced, nil)
c.controller.recordStatus(ar.V2beta3(), _resourceSynced, nil, metav1.ConditionTrue, ar.V2beta3().GetGeneration())
c.controller.recorderEvent(apc.V2beta3(), v1.EventTypeNormal, _resourceSynced, nil)
c.controller.recordStatus(apc.V2beta3(), _resourceSynced, nil, metav1.ConditionTrue, apc.V2beta3().GetGeneration())
}
} else {
log.Errorw("failed list ApisixPluginConfig",
Expand All @@ -226,10 +226,10 @@ func (c *apisixPluginConfigController) handleSyncErr(obj interface{}, errOrigin
zap.Error(errOrigin),
)
if errLocal == nil {
switch ar.GroupVersion() {
switch apc.GroupVersion() {
case kube.ApisixPluginConfigV2beta3:
c.controller.recorderEvent(ar.V2beta3(), v1.EventTypeWarning, _resourceSyncAborted, errOrigin)
c.controller.recordStatus(ar.V2beta3(), _resourceSyncAborted, errOrigin, metav1.ConditionFalse, ar.V2beta3().GetGeneration())
c.controller.recorderEvent(apc.V2beta3(), v1.EventTypeWarning, _resourceSyncAborted, errOrigin)
c.controller.recordStatus(apc.V2beta3(), _resourceSyncAborted, errOrigin, metav1.ConditionFalse, apc.V2beta3().GetGeneration())
}
} else {
log.Errorw("failed list ApisixPluginConfig",
Expand All @@ -254,12 +254,12 @@ func (c *apisixPluginConfigController) onAdd(obj interface{}) {
log.Debugw("ApisixPluginConfig add event arrived",
zap.Any("object", obj))

ar := kube.MustNewApisixPluginConfig(obj)
apc := kube.MustNewApisixPluginConfig(obj)
c.workqueue.Add(&types.Event{
Type: types.EventAdd,
Object: kube.ApisixPluginConfigEvent{
Key: key,
GroupVersion: ar.GroupVersion(),
GroupVersion: apc.GroupVersion(),
},
})

Expand Down Expand Up @@ -297,13 +297,13 @@ func (c *apisixPluginConfigController) onUpdate(oldObj, newObj interface{}) {
}

func (c *apisixPluginConfigController) onDelete(obj interface{}) {
ar, err := kube.NewApisixPluginConfig(obj)
apc, err := kube.NewApisixPluginConfig(obj)
if err != nil {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
return
}
ar = kube.MustNewApisixPluginConfig(tombstone)
apc = kube.MustNewApisixPluginConfig(tombstone)
}
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
Expand All @@ -314,15 +314,15 @@ func (c *apisixPluginConfigController) onDelete(obj interface{}) {
return
}
log.Debugw("ApisixPluginConfig delete event arrived",
zap.Any("final state", ar),
zap.Any("final state", apc),
)
c.workqueue.Add(&types.Event{
Type: types.EventDelete,
Object: kube.ApisixPluginConfigEvent{
Key: key,
GroupVersion: ar.GroupVersion(),
GroupVersion: apc.GroupVersion(),
},
Tombstone: ar,
Tombstone: apc,
})

c.controller.MetricsCollector.IncrEvents("PluginConfig", "delete")
Expand Down
16 changes: 8 additions & 8 deletions pkg/ingress/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,15 @@ func (c *Controller) syncManifests(ctx context.Context, added, updated, deleted
}
}
}
for _, u := range deleted.pluginConfigs {
if err := c.apisix.Cluster(clusterName).PluginConfig().Delete(ctx, u); err != nil {
for _, pc := range deleted.pluginConfigs {
if err := c.apisix.Cluster(clusterName).PluginConfig().Delete(ctx, pc); err != nil {
// pluginConfig might be referenced by other routes.
if err != cache.ErrStillInUse {
merr = multierror.Append(merr, err)
} else {
log.Infow("plugin_config was referenced by other routes",
zap.String("plugin_config_id", u.ID),
zap.String("plugin_config_name", u.Name),
zap.String("plugin_config_id", pc.ID),
zap.String("plugin_config_name", pc.Name),
)
}
}
Expand All @@ -269,8 +269,8 @@ func (c *Controller) syncManifests(ctx context.Context, added, updated, deleted
merr = multierror.Append(merr, err)
}
}
for _, u := range added.pluginConfigs {
if _, err := c.apisix.Cluster(clusterName).PluginConfig().Create(ctx, u); err != nil {
for _, pc := range added.pluginConfigs {
if _, err := c.apisix.Cluster(clusterName).PluginConfig().Create(ctx, pc); err != nil {
merr = multierror.Append(merr, err)
}
}
Expand All @@ -296,8 +296,8 @@ func (c *Controller) syncManifests(ctx context.Context, added, updated, deleted
merr = multierror.Append(merr, err)
}
}
for _, sr := range updated.pluginConfigs {
if _, err := c.apisix.Cluster(clusterName).PluginConfig().Create(ctx, sr); err != nil {
for _, pc := range updated.pluginConfigs {
if _, err := c.apisix.Cluster(clusterName).PluginConfig().Update(ctx, pc); err != nil {
merr = multierror.Append(merr, err)
}
}
Expand Down
26 changes: 13 additions & 13 deletions pkg/kube/apisix_plugin_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,44 +66,44 @@ type apisixPluginConfig struct {
v2beta3 *configv2beta3.ApisixPluginConfig
}

func (ar *apisixPluginConfig) V2beta3() *configv2beta3.ApisixPluginConfig {
if ar.groupVersion != ApisixPluginConfigV2beta3 {
func (apc *apisixPluginConfig) V2beta3() *configv2beta3.ApisixPluginConfig {
if apc.groupVersion != ApisixPluginConfigV2beta3 {
panic("not a apisix.apache.org/v2beta3 pluginConfig")
}
return ar.v2beta3
return apc.v2beta3
}

func (ar *apisixPluginConfig) GroupVersion() string {
return ar.groupVersion
func (apc *apisixPluginConfig) GroupVersion() string {
return apc.groupVersion
}

func (ar *apisixPluginConfig) ResourceVersion() string {
return ar.V2beta3().ResourceVersion
func (apc *apisixPluginConfig) ResourceVersion() string {
return apc.V2beta3().ResourceVersion
}

type apisixPluginConfigLister struct {
v2beta3Lister listersv2beta3.ApisixPluginConfigLister
}

func (l *apisixPluginConfigLister) V2beta3(namespace, name string) (ApisixPluginConfig, error) {
ar, err := l.v2beta3Lister.ApisixPluginConfigs(namespace).Get(name)
apc, err := l.v2beta3Lister.ApisixPluginConfigs(namespace).Get(name)
if err != nil {
return nil, err
}
return &apisixPluginConfig{
groupVersion: ApisixPluginConfigV2beta3,
v2beta3: ar,
v2beta3: apc,
}, nil
}

// MustNewApisixPluginConfig creates a kube.ApisixPluginConfig object according to the
// type of obj.
func MustNewApisixPluginConfig(obj interface{}) ApisixPluginConfig {
switch ar := obj.(type) {
switch apc := obj.(type) {
case *configv2beta3.ApisixPluginConfig:
return &apisixPluginConfig{
groupVersion: ApisixPluginConfigV2beta3,
v2beta3: ar,
v2beta3: apc,
}
default:
panic("invalid ApisixPluginConfig type")
Expand All @@ -114,11 +114,11 @@ func MustNewApisixPluginConfig(obj interface{}) ApisixPluginConfig {
// type of obj. It returns nil and the error reason when the
// type assertion fails.
func NewApisixPluginConfig(obj interface{}) (ApisixPluginConfig, error) {
switch ar := obj.(type) {
switch apc := obj.(type) {
case *configv2beta3.ApisixPluginConfig:
return &apisixPluginConfig{
groupVersion: ApisixPluginConfigV2beta3,
v2beta3: ar,
v2beta3: apc,
}, nil
default:
return nil, errors.New("invalid ApisixPluginConfig type")
Expand Down
18 changes: 13 additions & 5 deletions test/e2e/plugins/plugin_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ package plugins

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

"github.com/onsi/ginkgo"
"github.com/stretchr/testify/assert"

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

Expand All @@ -40,7 +41,7 @@ var _ = ginkgo.Describe("ApisixPluginConfig", func() {
apiVersion: apisix.apache.org/v2beta3
kind: ApisixPluginConfig
metadata:
name: test-apc-1
name: echo-and-cors-apc
spec:
plugins:
- name: echo
Expand All @@ -51,6 +52,8 @@ spec:
headers:
X-Foo: v1
X-Foo2: v2
- name: cors
enable: true
`)
assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(apc))

Expand All @@ -76,7 +79,7 @@ spec:
- serviceName: %s
servicePort: %d
weight: 10
plugin_config_name: test-apc-1
plugin_config_name: echo-and-cors-apc
`, backendSvc, backendPorts[0])
assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(ar))

Expand All @@ -87,12 +90,17 @@ spec:
pcs, err := s.ListApisixPluginConfig()
assert.Nil(ginkgo.GinkgoT(), err, nil, "listing pluginConfigs")
assert.Len(ginkgo.GinkgoT(), pcs, 1)
assert.Len(ginkgo.GinkgoT(), pcs[0].Plugins, 1)
assert.Len(ginkgo.GinkgoT(), pcs[0].Plugins, 2)

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.Header("Access-Control-Allow-Origin").Equal("*")
resp.Header("Access-Control-Allow-Methods").Equal("*")
resp.Header("Access-Control-Allow-Headers").Equal("*")
resp.Header("Access-Control-Expose-Headers").Equal("*")
resp.Header("Access-Control-Max-Age").Equal("5")
resp.Body().Contains("This is the preface")
resp.Body().Contains("origin")
resp.Body().Contains("This is the epilogue")
Expand Down

0 comments on commit 91ab406

Please sign in to comment.