Skip to content

Commit

Permalink
feat: Add informers and IP map cache
Browse files Browse the repository at this point in the history
feat: Add informers and IP map cache

Signed-off-by: Harisudarsan <[email protected]>

change docker build image

Signed-off-by: Harisudarsan <[email protected]>
  • Loading branch information
harisudarsan1 committed Jun 10, 2024
1 parent 475a203 commit 6305374
Show file tree
Hide file tree
Showing 7 changed files with 435 additions and 80 deletions.
10 changes: 5 additions & 5 deletions relay-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ WORKDIR /usr/src/kubearmor-relay-server

COPY . .

RUN cd relay-server && make
RUN make

### Copy executable image

Expand All @@ -26,14 +26,14 @@ LABEL name="kubearmor-relay-server" \
alerts, and system logs generated by KubeArmor in each node, streamlining log integration with other systems."

RUN microdnf -y update && \
microdnf -y install --nodocs --setopt=install_weak_deps=0 --setopt=keepcache=0 shadow-utils && \
microdnf clean all
microdnf -y install --nodocs --setopt=install_weak_deps=0 --setopt=keepcache=0 shadow-utils && \
microdnf clean all

RUN groupadd --gid 1000 default \
&& useradd --uid 1000 --gid default --shell /bin/bash --create-home default
&& useradd --uid 1000 --gid default --shell /bin/bash --create-home default

COPY LICENSE /licenses/license.txt
COPY --from=builder /usr/src/kubearmor-relay-server/relay-server/kubearmor-relay-server /KubeArmor/kubearmor-relay-server
COPY --from=builder /usr/src/kubearmor-relay-server/kubearmor-relay-server /KubeArmor/kubearmor-relay-server

USER default

Expand Down
25 changes: 25 additions & 0 deletions relay-server/common/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package common

import (
"strings"
)

func Extractdata(body string) map[string]string {

pairs := strings.Split(body, " ")

// Initialize a map to store extracted values
dataMap := make(map[string]string)

// Loop through each key-value pair
for _, pair := range pairs {
// Split each pair by '=' to separate key and value
parts := strings.Split(pair, "=")
if len(parts) == 2 {
key := parts[0]
value := parts[1]
dataMap[key] = value
}
}
return dataMap
}
84 changes: 83 additions & 1 deletion relay-server/elasticsearch/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"github.com/elastic/go-elasticsearch/v7/esutil"
"github.com/google/uuid"
kg "github.com/kubearmor/kubearmor-relay-server/relay-server/log"

kl "github.com/kubearmor/kubearmor-relay-server/relay-server/common"

// kif "github.com/kubearmor/kubearmor-relay-server/relay-server/informers"
"github.com/kubearmor/kubearmor-relay-server/relay-server/server"
)

Expand All @@ -33,6 +37,8 @@ type ElasticsearchClient struct {
bulkIndexer esutil.BulkIndexer
ctx context.Context
alertCh chan interface{}
logCh chan interface{}
// client *kif.Client
}

// NewElasticsearchClient creates a new Elasticsearch client with the given Elasticsearch URL
Expand Down Expand Up @@ -69,8 +75,26 @@ func NewElasticsearchClient(esURL, Endpoint string) (*ElasticsearchClient, error
log.Fatalf("Error creating the indexer: %s", err)
}
alertCh := make(chan interface{}, 10000)
logCh := make(chan interface{}, 10000)
kaClient := server.NewClient(Endpoint)
return &ElasticsearchClient{kaClient: kaClient, bulkIndexer: bi, esClient: esClient, alertCh: alertCh}, nil

// k8sClient := kif.GetK8sClient()
// cc := &kif.ClusterCache{
//
// mu: &sync.RWMutex{},
//
// ipPodCache: make(map[string]PodServiceInfo),
// }
// client := &kif.Client{
// k8sClient: k8sClient,
// ClusterIPCache: cc,
// }

// client := kif.InitializeClient()
// go kif.StartInformers(client)

// TODO: remove this informers
return &ElasticsearchClient{kaClient: kaClient, bulkIndexer: bi, esClient: esClient, alertCh: alertCh, logCh: logCh}, nil
}

// bulkIndex takes an interface and index name and adds the data to the Elasticsearch bulk indexer.
Expand Down Expand Up @@ -157,6 +181,64 @@ func (ecl *ElasticsearchClient) Start() error {
}
}()
}

client.WgServer.Go(func() error {
for client.Running {
res, err := client.LogStream.Recv()
if err != nil {
kg.Warnf("Failed to receive an log (%s)", client.Server)
break
}

if containsKprobe := strings.Contains(res.Data, "kprobe"); containsKprobe {

resourceMap := kl.Extractdata(res.GetResource())
remoteIP := resourceMap["remoteip"]
podserviceInfo, found := ecl.kaClient.Ifclient.ClusterIPCache.Get(remoteIP)

if found {
switch podserviceInfo.Type {
case "POD":
resource := res.GetResource() + fmt.Sprintf(" hostname=%s podname=%s namespace=%s", podserviceInfo.DeploymentName, podserviceInfo.PodName, podserviceInfo.NamespaceName)
data := res.GetData() + fmt.Sprintf(" ownertype=pod")
res.Data = data

res.Resource = resource
// kg.Printf("logData:%s", res.Data)
break
case "SERVICE":
resource := res.GetResource() + fmt.Sprintf(" hostname=%s servicename=%s namespace=%s", podserviceInfo.DeploymentName, podserviceInfo.ServiceName, podserviceInfo.NamespaceName)

data := res.GetData() + fmt.Sprintf(" ownertype=service")
res.Data = data
res.Resource = resource
// kg.Printf("logData:%s", res.Data)

break
}
}

}
tel, _ := json.Marshal(res)
fmt.Printf("%s\n", string(tel))
ecl.logCh <- res
}
return nil
})

for i := 0; i < 5; i++ {
go func() {
for {
select {
case log := <-ecl.logCh:
ecl.bulkIndex(log, "log")
case <-ecl.ctx.Done():
close(ecl.logCh)
return
}
}
}()
}
return nil
}

Expand Down
34 changes: 17 additions & 17 deletions relay-server/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/kubearmor/kubearmor-relay-server/relay-server

go 1.22
go 1.22.0

toolchain go1.22.4

replace (
github.com/kubearmor/kubearmor-relay-server/relay-server => ./
Expand All @@ -20,23 +22,24 @@ require (
go.uber.org/zap v1.27.0
golang.org/x/sync v0.7.0
google.golang.org/grpc v1.63.2
k8s.io/apimachinery v0.29.2
k8s.io/client-go v0.29.2
k8s.io/api v0.30.1
k8s.io/apimachinery v0.30.1
k8s.io/client-go v0.30.1
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.12.0 // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand All @@ -47,7 +50,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/gomega v1.30.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand All @@ -58,23 +60,21 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240314144324-c7f7c6466f7f // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.29.2 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20240310230437-4693a0247e57 // indirect
k8s.io/kube-openapi v0.0.0-20240521193020-835d969ad83a // indirect
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
Expand Down
Loading

0 comments on commit 6305374

Please sign in to comment.