|
@@ -1,8 +1,11 @@
|
|
|
package clustercache
|
|
package clustercache
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "os"
|
|
|
"sync"
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
+ "k8s.io/klog"
|
|
|
|
|
+
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
|
v1 "k8s.io/api/core/v1"
|
|
v1 "k8s.io/api/core/v1"
|
|
|
stv1 "k8s.io/api/storage/v1"
|
|
stv1 "k8s.io/api/storage/v1"
|
|
@@ -43,20 +46,24 @@ type ClusterCache interface {
|
|
|
|
|
|
|
|
// GetAllStorageClasses returns all the cached storage classes
|
|
// GetAllStorageClasses returns all the cached storage classes
|
|
|
GetAllStorageClasses() []*stv1.StorageClass
|
|
GetAllStorageClasses() []*stv1.StorageClass
|
|
|
|
|
+
|
|
|
|
|
+ // SetConfigMapUpdateFunc sets the configmap update function
|
|
|
|
|
+ SetConfigMapUpdateFunc(func(interface{}))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// KubernetesClusterCache is the implementation of ClusterCache
|
|
// KubernetesClusterCache is the implementation of ClusterCache
|
|
|
type KubernetesClusterCache struct {
|
|
type KubernetesClusterCache struct {
|
|
|
client kubernetes.Interface
|
|
client kubernetes.Interface
|
|
|
|
|
|
|
|
- namespaceWatch WatchController
|
|
|
|
|
- nodeWatch WatchController
|
|
|
|
|
- podWatch WatchController
|
|
|
|
|
- serviceWatch WatchController
|
|
|
|
|
- deploymentsWatch WatchController
|
|
|
|
|
- pvWatch WatchController
|
|
|
|
|
- storageClassWatch WatchController
|
|
|
|
|
- stop chan struct{}
|
|
|
|
|
|
|
+ namespaceWatch WatchController
|
|
|
|
|
+ nodeWatch WatchController
|
|
|
|
|
+ podWatch WatchController
|
|
|
|
|
+ kubecostConfigMapWatch WatchController
|
|
|
|
|
+ serviceWatch WatchController
|
|
|
|
|
+ deploymentsWatch WatchController
|
|
|
|
|
+ pvWatch WatchController
|
|
|
|
|
+ storageClassWatch WatchController
|
|
|
|
|
+ stop chan struct{}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func initializeCache(wc WatchController, wg *sync.WaitGroup, cancel chan struct{}) {
|
|
func initializeCache(wc WatchController, wg *sync.WaitGroup, cancel chan struct{}) {
|
|
@@ -69,26 +76,31 @@ func NewKubernetesClusterCache(client kubernetes.Interface) ClusterCache {
|
|
|
appsRestClient := client.AppsV1().RESTClient()
|
|
appsRestClient := client.AppsV1().RESTClient()
|
|
|
storageRestClient := client.StorageV1().RESTClient()
|
|
storageRestClient := client.StorageV1().RESTClient()
|
|
|
|
|
|
|
|
|
|
+ kubecostNamespace := os.Getenv("KUBECOST_NAMESPACE")
|
|
|
|
|
+ klog.Infof("NAMESPACE: %s", kubecostNamespace)
|
|
|
|
|
+
|
|
|
kcc := &KubernetesClusterCache{
|
|
kcc := &KubernetesClusterCache{
|
|
|
- client: client,
|
|
|
|
|
- namespaceWatch: NewCachingWatcher(coreRestClient, "namespaces", &v1.Namespace{}, "", fields.Everything()),
|
|
|
|
|
- nodeWatch: NewCachingWatcher(coreRestClient, "nodes", &v1.Node{}, "", fields.Everything()),
|
|
|
|
|
- podWatch: NewCachingWatcher(coreRestClient, "pods", &v1.Pod{}, "", fields.Everything()),
|
|
|
|
|
- serviceWatch: NewCachingWatcher(coreRestClient, "services", &v1.Service{}, "", fields.Everything()),
|
|
|
|
|
- deploymentsWatch: NewCachingWatcher(appsRestClient, "deployments", &appsv1.Deployment{}, "", fields.Everything()),
|
|
|
|
|
- pvWatch: NewCachingWatcher(coreRestClient, "persistentvolumes", &v1.PersistentVolume{}, "", fields.Everything()),
|
|
|
|
|
- storageClassWatch: NewCachingWatcher(storageRestClient, "storageclasses", &stv1.StorageClass{}, "", fields.Everything()),
|
|
|
|
|
|
|
+ client: client,
|
|
|
|
|
+ namespaceWatch: NewCachingWatcher(coreRestClient, "namespaces", &v1.Namespace{}, "", fields.Everything()),
|
|
|
|
|
+ nodeWatch: NewCachingWatcher(coreRestClient, "nodes", &v1.Node{}, "", fields.Everything()),
|
|
|
|
|
+ podWatch: NewCachingWatcher(coreRestClient, "pods", &v1.Pod{}, "", fields.Everything()),
|
|
|
|
|
+ kubecostConfigMapWatch: NewCachingWatcher(coreRestClient, "configmaps", &v1.ConfigMap{}, kubecostNamespace, fields.Everything()),
|
|
|
|
|
+ serviceWatch: NewCachingWatcher(coreRestClient, "services", &v1.Service{}, "", fields.Everything()),
|
|
|
|
|
+ deploymentsWatch: NewCachingWatcher(appsRestClient, "deployments", &appsv1.Deployment{}, "", fields.Everything()),
|
|
|
|
|
+ pvWatch: NewCachingWatcher(coreRestClient, "persistentvolumes", &v1.PersistentVolume{}, "", fields.Everything()),
|
|
|
|
|
+ storageClassWatch: NewCachingWatcher(storageRestClient, "storageclasses", &stv1.StorageClass{}, "", fields.Everything()),
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Wait for each caching watcher to initialize
|
|
// Wait for each caching watcher to initialize
|
|
|
var wg sync.WaitGroup
|
|
var wg sync.WaitGroup
|
|
|
- wg.Add(7)
|
|
|
|
|
|
|
+ wg.Add(8)
|
|
|
|
|
|
|
|
cancel := make(chan struct{})
|
|
cancel := make(chan struct{})
|
|
|
|
|
|
|
|
go initializeCache(kcc.namespaceWatch, &wg, cancel)
|
|
go initializeCache(kcc.namespaceWatch, &wg, cancel)
|
|
|
go initializeCache(kcc.nodeWatch, &wg, cancel)
|
|
go initializeCache(kcc.nodeWatch, &wg, cancel)
|
|
|
go initializeCache(kcc.podWatch, &wg, cancel)
|
|
go initializeCache(kcc.podWatch, &wg, cancel)
|
|
|
|
|
+ go initializeCache(kcc.kubecostConfigMapWatch, &wg, cancel)
|
|
|
go initializeCache(kcc.serviceWatch, &wg, cancel)
|
|
go initializeCache(kcc.serviceWatch, &wg, cancel)
|
|
|
go initializeCache(kcc.deploymentsWatch, &wg, cancel)
|
|
go initializeCache(kcc.deploymentsWatch, &wg, cancel)
|
|
|
go initializeCache(kcc.pvWatch, &wg, cancel)
|
|
go initializeCache(kcc.pvWatch, &wg, cancel)
|
|
@@ -109,6 +121,7 @@ func (kcc *KubernetesClusterCache) Run() {
|
|
|
go kcc.nodeWatch.Run(1, stopCh)
|
|
go kcc.nodeWatch.Run(1, stopCh)
|
|
|
go kcc.podWatch.Run(1, stopCh)
|
|
go kcc.podWatch.Run(1, stopCh)
|
|
|
go kcc.serviceWatch.Run(1, stopCh)
|
|
go kcc.serviceWatch.Run(1, stopCh)
|
|
|
|
|
+ go kcc.kubecostConfigMapWatch.Run(1, stopCh)
|
|
|
go kcc.deploymentsWatch.Run(1, stopCh)
|
|
go kcc.deploymentsWatch.Run(1, stopCh)
|
|
|
go kcc.pvWatch.Run(1, stopCh)
|
|
go kcc.pvWatch.Run(1, stopCh)
|
|
|
go kcc.storageClassWatch.Run(1, stopCh)
|
|
go kcc.storageClassWatch.Run(1, stopCh)
|
|
@@ -191,3 +204,7 @@ func (kcc *KubernetesClusterCache) GetAllStorageClasses() []*stv1.StorageClass {
|
|
|
}
|
|
}
|
|
|
return storageClasses
|
|
return storageClasses
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+func (kcc *KubernetesClusterCache) SetConfigMapUpdateFunc(f func(interface{})) {
|
|
|
|
|
+ kcc.kubecostConfigMapWatch.SetUpdateHandler(f)
|
|
|
|
|
+}
|