Skip to content

Commit

Permalink
add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
harisudarsan1 committed Jul 28, 2024
1 parent 4679b3a commit b150c80
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
1 change: 0 additions & 1 deletion relay-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ require (
github.com/kubearmor/KubeArmor/protobuf v0.0.0-20240315075053-fee50c9428b9
github.com/spf13/viper v1.18.2
go.uber.org/zap v1.27.0
golang.org/x/sync v0.7.0
google.golang.org/grpc v1.63.2
k8s.io/api v0.30.1
k8s.io/apimachinery v0.30.1
Expand Down
2 changes: 0 additions & 2 deletions relay-server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbht
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
48 changes: 32 additions & 16 deletions relay-server/informers/informercache.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ func StartInformers(client *Client) {
podInformer.AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
pod, ok := obj.(*v1.Pod)
if !ok {
kg.Errf("Failed to cast object to *v1.Pod in AddFunc: received type %T", obj)
return
}

pod := obj.(*v1.Pod)
deploymentName := getDeploymentNamefromPod(pod)
podInfo := PodServiceInfo{
Type: "POD",
Expand All @@ -103,29 +107,30 @@ func StartInformers(client *Client) {
}

client.ClusterIPCache.Set(pod.Status.PodIP, podInfo)

// kg.Printf("POD Added: %s/%s, remoteIP %s\n", pod.Name, deploymentName, pod.Status.PodIP)
},
UpdateFunc: func(oldObj, newObj interface{}) {
pod, ok := newObj.(*v1.Pod)
if !ok {
kg.Errf("Failed to cast object to *v1.Pod in UpdateFunc: received type %T", newObj)
return
}

pod := newObj.(*v1.Pod)
deploymentName := getDeploymentNamefromPod(pod)
podInfo := PodServiceInfo{

Type: "POD",
PodName: pod.Name,
DeploymentName: deploymentName,

NamespaceName: pod.Namespace,
NamespaceName: pod.Namespace,
}

client.ClusterIPCache.Set(pod.Status.PodIP, podInfo)
// kg.Printf("POD Updated: %s/%s, remoteIP %s\n", pod.Name, deploymentName, pod.Status.PodIP)

},
DeleteFunc: func(obj interface{}) {

pod := obj.(*v1.Pod)
pod, ok := obj.(*v1.Pod)
if !ok {
kg.Errf("Failed to cast object to *v1.Pod in DeleteFunc: received type %T", obj)
return
}

client.ClusterIPCache.Delete(pod.Status.PodIP)
},
Expand All @@ -139,7 +144,11 @@ func StartInformers(client *Client) {
serviceInformer.AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
service := obj.(*v1.Service)
service, ok := obj.(*v1.Service)
if !ok {
kg.Errf("Failed to cast object to *v1.Service in AddFunc: received type %T", obj)
return
}

svcInfo := PodServiceInfo{

Expand All @@ -154,8 +163,11 @@ func StartInformers(client *Client) {
// kg.Printf("Service Added: %s/%s, remoteIP %s\n", service.Namespace, service.Name, service.Spec.ClusterIP)
},
UpdateFunc: func(oldObj, newObj interface{}) {
service := newObj.(*v1.Service)

service, ok := newObj.(*v1.Service)
if !ok {
kg.Errf("Failed to cast object to *v1.Service in UpdateFunc: received type %T", newObj)
return
}
svcInfo := PodServiceInfo{

Type: "SERVICE",
Expand All @@ -164,10 +176,14 @@ func StartInformers(client *Client) {
NamespaceName: service.Namespace,
}
client.ClusterIPCache.Set(service.Spec.ClusterIP, svcInfo)
kg.Printf("Service Updated: %s/%s\n", service.Namespace, service.Name)
// kg.Printf("Service Updated: %s/%s\n", service.Namespace, service.Name)
},
DeleteFunc: func(obj interface{}) {
service := obj.(*v1.Service)
service, ok := obj.(*v1.Service)
if !ok {
kg.Errf("Failed to cast object to *v1.Service in DeleteFunc: received type %T", obj)
return
}

client.ClusterIPCache.Delete(service.Spec.ClusterIP)
// kg.Printf("Service Deleted: %s/%s\n", service.Namespace, service.Name)
Expand Down
2 changes: 1 addition & 1 deletion relay-server/server/relayServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ func NewRelayServer(port string) *RelayServer {

if enableEsDashboards {
ESLogChannel = make(chan *pb.Log, 10000)
AlertBufferChannel = make(chan *pb.Alert, 1000)
ESAlertChannel = make(chan *pb.Alert, 1000)
}

// listen to gRPC port
Expand Down

0 comments on commit b150c80

Please sign in to comment.