-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sunyanan Choochotkaew <sunyanan.choochotkaew1@ibm.com>
- Loading branch information
Showing
7 changed files
with
203 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2022- IBM Inc. All rights reserved | ||
* SPDX-License-Identifier: Apache2.0 | ||
*/ | ||
|
||
package controllers | ||
|
||
import ( | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/client-go/informers" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// NamespaceWatcher watches new namespace and generate net-attach-def | ||
type NamespaceWatcher struct { | ||
*kubernetes.Clientset | ||
NamespaceQueue chan string | ||
Quit chan struct{} | ||
*MultiNicNetworkReconciler | ||
} | ||
|
||
// NewNamespaceWatcher creates new namespace watcher | ||
func NewNamespaceWatcher(client client.Client, config *rest.Config, multinicnetworkReconciler *MultiNicNetworkReconciler, quit chan struct{}) *NamespaceWatcher { | ||
clientset, _ := kubernetes.NewForConfig(config) | ||
watcher := &NamespaceWatcher{ | ||
Clientset: clientset, | ||
NamespaceQueue: make(chan string), | ||
Quit: quit, | ||
MultiNicNetworkReconciler: multinicnetworkReconciler, | ||
} | ||
factory := informers.NewSharedInformerFactory(clientset, 0) | ||
nsInformer := factory.Core().V1().Namespaces() | ||
|
||
nsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ | ||
AddFunc: func(obj interface{}) { | ||
if ns, ok := obj.(*v1.Namespace); ok { | ||
watcher.NamespaceQueue <- ns.Name | ||
} | ||
}, | ||
}) | ||
factory.Start(watcher.Quit) | ||
|
||
return watcher | ||
} | ||
|
||
// Run executes namespace watcher routine until get quit signal | ||
func (w *NamespaceWatcher) Run() { | ||
defer close(w.NamespaceQueue) | ||
wait.Until(w.ProcessNamespaceQueue, 0, w.Quit) | ||
} | ||
|
||
func (w *NamespaceWatcher) ProcessNamespaceQueue() { | ||
ns := <-w.NamespaceQueue | ||
w.MultiNicNetworkReconciler.HandleNewNamespace(ns) | ||
} |
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
Oops, something went wrong.