-
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
d0daa59
commit 2a58e9d
Showing
9 changed files
with
366 additions
and
55 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,106 @@ | ||
package controllers | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/supporttools/KubeWatchman/monitoring" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/client-go/informers" | ||
"k8s.io/client-go/tools/cache" | ||
) | ||
|
||
func NewPodMonitorController(clientset KubernetesClientset, logger *logrus.Logger) *PodMonitorController { | ||
return &PodMonitorController{ | ||
Clientset: clientset, | ||
Logger: logger.WithField("controller", "PodMonitorController"), | ||
podStatuses: make(map[string]int), | ||
} | ||
} | ||
|
||
func (n *PodMonitorController) Start() error { | ||
monitoring.ControllerStatus("PodMonitorController", true) | ||
factory := informers.NewSharedInformerFactory(n.Clientset, time.Minute) | ||
PodInformer := factory.Core().V1().Pods().Informer() | ||
|
||
PodInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{ | ||
AddFunc: n.onAdd, | ||
UpdateFunc: n.onUpdate, | ||
DeleteFunc: n.onDelete, | ||
}) | ||
|
||
n.stopCh = make(chan struct{}) | ||
go PodInformer.Run(n.stopCh) | ||
|
||
if !cache.WaitForCacheSync(n.stopCh, PodInformer.HasSynced) { | ||
logrus.Error("Timed out waiting for caches to sync") | ||
return fmt.Errorf("timed out waiting for caches to sync") | ||
} | ||
|
||
logrus.Debug("Pod informer synced successfully") | ||
return nil | ||
} | ||
|
||
func (n *PodMonitorController) Stop() error { | ||
monitoring.ControllerStatus("PodMonitorController", false) | ||
close(n.stopCh) | ||
return nil | ||
} | ||
|
||
func (n *PodMonitorController) logPodDetails(Pod *v1.Pod, event string) { | ||
n.Logger.Infof("Pod %s: %s (status: %s)", event, Pod.Name, Pod.Status.Phase) | ||
n.Logger.Debug("Pod Details: ", Pod.Status, Pod.Status.Conditions, Pod.Annotations, Pod.Labels, Pod.Finalizers, Pod.Status.PodIP) | ||
} | ||
|
||
func (n *PodMonitorController) onAdd(obj interface{}) { | ||
Pod := obj.(*v1.Pod) | ||
n.logPodDetails(Pod, "added") | ||
monitoring.PodCountChange("inc") | ||
status := string(Pod.Status.Phase) | ||
n.podStatuses[status]++ | ||
monitoring.UpdatePodStatus(status, float64(n.podStatuses[status])) | ||
} | ||
|
||
func (n *PodMonitorController) onUpdate(oldObj, newObj interface{}) { | ||
oldPod := oldObj.(*v1.Pod) | ||
newPod := newObj.(*v1.Pod) | ||
n.logPodDetails(newPod, "updated") | ||
|
||
oldStatus := string(oldPod.Status.Phase) | ||
newStatus := string(newPod.Status.Phase) | ||
n.podStatuses[oldStatus]-- | ||
n.podStatuses[newStatus]++ | ||
monitoring.UpdatePodStatus(oldStatus, float64(n.podStatuses[oldStatus])) | ||
monitoring.UpdatePodStatus(newStatus, float64(n.podStatuses[newStatus])) | ||
|
||
oldSummary := monitoring.CreatePodSummary(oldPod) | ||
newSummary := monitoring.CreatePodSummary(newPod) | ||
|
||
for _, container := range oldSummary.Containers { | ||
if container.IsCrashLooping { | ||
n.CrashLoopingCount-- | ||
} | ||
} | ||
|
||
for _, container := range newSummary.Containers { | ||
if container.IsCrashLooping { | ||
n.CrashLoopingCount++ | ||
} | ||
} | ||
|
||
monitoring.UpdatePodStatus("CrashLoopBackOff", float64(n.CrashLoopingCount)) | ||
} | ||
|
||
func (n *PodMonitorController) onDelete(obj interface{}) { | ||
Pod, ok := obj.(*v1.Pod) | ||
if !ok { | ||
fmt.Println("Delete event with incorrect type:", obj) | ||
return | ||
} | ||
n.logPodDetails(Pod, "deleted") | ||
monitoring.PodCountChange("dec") | ||
status := string(Pod.Status.Phase) | ||
n.podStatuses[status]-- | ||
monitoring.UpdatePodStatus(status, float64(n.podStatuses[status])) | ||
} |
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,39 @@ | ||
package controllers_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus/hooks/test" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/supporttools/KubeWatchman/controllers" | ||
"k8s.io/client-go/kubernetes/fake" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
func TestPodMonitorControllerStart(t *testing.T) { | ||
// Create a fake clientset | ||
fakeClientset := fake.NewSimpleClientset() | ||
|
||
// Create a mock factory | ||
mockFactory := new(MockClusterConnectionFactory) | ||
mockFactory.On("NewForConfig", mock.Anything).Return(fakeClientset, nil) | ||
|
||
// Use the mock factory to create a Clientset | ||
clientset, err := mockFactory.NewForConfig(&rest.Config{}) | ||
if err != nil { | ||
t.Errorf("Failed to create clientset: %v", err) | ||
} | ||
|
||
// Create a logger and hook for testing | ||
logger, _ := test.NewNullLogger() | ||
|
||
// Create the PodMonitorController using the clientset | ||
controller := controllers.NewPodMonitorController(clientset, logger) // Use clientset here directly | ||
|
||
// Start the controller | ||
if err := controller.Start(); err != nil { | ||
t.Errorf("Failed to start controller: %v", err) | ||
} | ||
|
||
// Additional testing logic needed here | ||
} |
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,16 @@ | ||
package controllers_test | ||
|
||
import ( | ||
"github.com/stretchr/testify/mock" | ||
"k8s.io/client-go/kubernetes/fake" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
type MockClusterConnectionFactory struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockClusterConnectionFactory) NewForConfig(config *rest.Config) (*fake.Clientset, error) { | ||
args := m.Called(config) | ||
return args.Get(0).(*fake.Clientset), args.Error(1) | ||
} |
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.