-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support secret controller (#284)
* feat: add secret controller * fix: e2e test describe * fix: add license headers * fix: add key for types.Event * fix: e2e failed * fix: use key instead of object * fix: bug when delete
- Loading branch information
Showing
8 changed files
with
429 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
// 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 controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
corev1 "k8s.io/api/core/v1" | ||
k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/util/runtime" | ||
"k8s.io/client-go/tools/cache" | ||
"k8s.io/client-go/util/workqueue" | ||
|
||
"github.com/apache/apisix-ingress-controller/pkg/log" | ||
"github.com/apache/apisix-ingress-controller/pkg/seven/state" | ||
"github.com/apache/apisix-ingress-controller/pkg/types" | ||
apisixv1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1" | ||
) | ||
|
||
type secretController struct { | ||
controller *Controller | ||
workqueue workqueue.RateLimitingInterface | ||
workers int | ||
} | ||
|
||
func (c *Controller) newSecretController() *secretController { | ||
ctl := &secretController{ | ||
controller: c, | ||
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.NewItemFastSlowRateLimiter(1*time.Second, 60*time.Second, 5), "Secrets"), | ||
workers: 1, | ||
} | ||
|
||
ctl.controller.secretInformer.AddEventHandler( | ||
cache.ResourceEventHandlerFuncs{ | ||
AddFunc: ctl.onAdd, | ||
UpdateFunc: ctl.onUpdate, | ||
DeleteFunc: ctl.onDelete, | ||
}, | ||
) | ||
|
||
return ctl | ||
} | ||
|
||
func (c *secretController) run(ctx context.Context) { | ||
log.Info("secret controller started") | ||
defer log.Info("secret controller exited") | ||
|
||
if ok := cache.WaitForCacheSync(ctx.Done(), c.controller.secretInformer.HasSynced); !ok { | ||
log.Error("informers sync failed") | ||
return | ||
} | ||
|
||
handler := func() { | ||
for { | ||
obj, shutdown := c.workqueue.Get() | ||
if shutdown { | ||
return | ||
} | ||
err := func(obj interface{}) error { | ||
defer c.workqueue.Done(obj) | ||
event := obj.(*types.Event) | ||
if key, ok := event.Object.(string); !ok { | ||
c.workqueue.Forget(obj) | ||
return fmt.Errorf("expected Secret in workqueue but got %#v", obj) | ||
} else { | ||
if err := c.sync(ctx, event); err != nil { | ||
c.workqueue.AddRateLimited(obj) | ||
log.Errorf("sync secret with ssl %s failed", key) | ||
return fmt.Errorf("error syncing '%s': %s", key, err.Error()) | ||
} | ||
c.workqueue.Forget(obj) | ||
return nil | ||
} | ||
}(obj) | ||
if err != nil { | ||
runtime.HandleError(err) | ||
} | ||
} | ||
} | ||
|
||
for i := 0; i < c.workers; i++ { | ||
go handler() | ||
} | ||
|
||
<-ctx.Done() | ||
c.workqueue.ShutDown() | ||
} | ||
|
||
func (c *secretController) sync(ctx context.Context, ev *types.Event) error { | ||
key := ev.Object.(string) | ||
namespace, name, err := cache.SplitMetaNamespaceKey(key) | ||
if err != nil { | ||
log.Errorf("invalid resource key: %s", key) | ||
return err | ||
} | ||
sec, err := c.controller.secretLister.Secrets(namespace).Get(name) | ||
|
||
secretMapkey := namespace + "_" + name | ||
if err != nil { | ||
if !k8serrors.IsNotFound(err) { | ||
log.Errorw("failed to get Secret", | ||
zap.String("key", secretMapkey), | ||
zap.Error(err), | ||
) | ||
return err | ||
} | ||
|
||
if ev.Type != types.EventDelete { | ||
log.Warnw("Secret was deleted before it can be delivered", | ||
zap.String("key", secretMapkey), | ||
) | ||
return nil | ||
} | ||
} | ||
if ev.Type == types.EventDelete { | ||
if sec != 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. | ||
log.Warnw("discard the stale secret delete event since the resource still exists", | ||
zap.String("key", secretMapkey), | ||
) | ||
return nil | ||
} | ||
sec = ev.Tombstone.(*corev1.Secret) | ||
} | ||
// sync SSL in APISIX which is store in secretSSLMap | ||
// FixMe Need to update the status of CRD ApisixTls | ||
ssls, ok := secretSSLMap.Load(secretMapkey) | ||
if ok { | ||
sslMap := ssls.(sync.Map) | ||
sslMap.Range(func(_, v interface{}) bool { | ||
ssl := v.(*apisixv1.Ssl) | ||
ssl.FullName = ssl.ID | ||
err = state.SyncSsl(ssl, ev.Type.String()) | ||
if err != nil { | ||
return false | ||
} | ||
return true | ||
}) | ||
} | ||
return err | ||
} | ||
|
||
func (c *secretController) onAdd(obj interface{}) { | ||
key, err := cache.MetaNamespaceKeyFunc(obj) | ||
if err != nil { | ||
log.Errorf("found secret object with bad namespace/name: %s, ignore it", err) | ||
return | ||
} | ||
if !c.controller.namespaceWatching(key) { | ||
return | ||
} | ||
|
||
c.workqueue.AddRateLimited(&types.Event{ | ||
Type: types.EventAdd, | ||
Object: key, | ||
}) | ||
} | ||
|
||
func (c *secretController) onUpdate(prev, curr interface{}) { | ||
prevSec := prev.(*corev1.Secret) | ||
currSec := curr.(*corev1.Secret) | ||
|
||
if prevSec.GetResourceVersion() == currSec.GetResourceVersion() { | ||
return | ||
} | ||
key, err := cache.MetaNamespaceKeyFunc(currSec) | ||
if err != nil { | ||
log.Errorf("found secrets object with bad namespace/name: %s, ignore it", err) | ||
return | ||
} | ||
if !c.controller.namespaceWatching(key) { | ||
return | ||
} | ||
c.workqueue.AddRateLimited(&types.Event{ | ||
Type: types.EventUpdate, | ||
Object: key, | ||
}) | ||
} | ||
|
||
func (c *secretController) onDelete(obj interface{}) { | ||
sec, ok := obj.(*corev1.Secret) | ||
if !ok { | ||
tombstone, ok := obj.(cache.DeletedFinalStateUnknown) | ||
if !ok { | ||
log.Errorf("found secrets: %+v in bad tombstone state", obj) | ||
return | ||
} | ||
sec = tombstone.Obj.(*corev1.Secret) | ||
} | ||
|
||
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) | ||
if err != nil { | ||
log.Errorf("found secret resource with bad meta namesapce key: %s", err) | ||
return | ||
} | ||
// FIXME Refactor Controller.namespaceWatching to just use | ||
// namespace after all controllers use the same way to fetch | ||
// the object. | ||
if !c.controller.namespaceWatching(key) { | ||
return | ||
} | ||
c.workqueue.AddRateLimited(&types.Event{ | ||
Type: types.EventDelete, | ||
Object: key, | ||
Tombstone: sec, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.