forked from submariner-io/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25c3102
commit 5ea9465
Showing
5 changed files
with
144 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
SPDX-License-Identifier: Apache-2.0 | ||
Copyright Contributors to the Submariner project. | ||
Licensed 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 service | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/cache" | ||
"k8s.io/klog" | ||
) | ||
|
||
type Controller struct { | ||
// Indirection hook for unit tests to supply fake client sets | ||
NewClientset func(kubeConfig *rest.Config) (kubernetes.Interface, error) | ||
namespaceInformer cache.Controller | ||
namespaceStore cache.Store | ||
stopCh chan struct{} | ||
} | ||
|
||
func NewController() *Controller { | ||
return &Controller{ | ||
NewClientset: func(c *rest.Config) (kubernetes.Interface, error) { | ||
return kubernetes.NewForConfig(c) | ||
}, | ||
stopCh: make(chan struct{}), | ||
} | ||
} | ||
|
||
func (c *Controller) Start(kubeConfig *rest.Config) error { | ||
klog.Infof("Starting Namespace Controller") | ||
|
||
clientSet, err := c.NewClientset(kubeConfig) | ||
if err != nil { | ||
return fmt.Errorf("error creating client set: %v", err) | ||
} | ||
|
||
c.namespaceStore, c.namespaceInformer = cache.NewInformer( | ||
&cache.ListWatch{ | ||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { | ||
return clientSet.CoreV1().Namespaces().List(context.TODO(), options) | ||
}, | ||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { | ||
return clientSet.CoreV1().Namespaces().Watch(context.TODO(), options) | ||
}, | ||
}, | ||
&v1.Namespace{}, | ||
0, | ||
cache.ResourceEventHandlerFuncs{ | ||
AddFunc: func(obj interface{}) {}, | ||
UpdateFunc: func(old interface{}, new interface{}) {}, | ||
DeleteFunc: func(obj interface{}) {}, | ||
}, | ||
) | ||
|
||
go c.namespaceInformer.Run(c.stopCh) | ||
|
||
return nil | ||
} | ||
|
||
func (c *Controller) Stop() { | ||
close(c.stopCh) | ||
|
||
klog.Infof("Namespace Controller stopped") | ||
} | ||
|
||
func (c *Controller) LocalNamespacesContains(namespace string) bool { | ||
namespaces := c.namespaceStore.List() | ||
|
||
for _, ns := range namespaces { | ||
if ns.(*v1.Namespace).ObjectMeta.Name == ns { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
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