Skip to content

Commit

Permalink
issue #89
Browse files Browse the repository at this point in the history
  • Loading branch information
vijeyash1 committed Jul 17, 2023
1 parent 8bb696b commit 5240202
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 23 deletions.
48 changes: 35 additions & 13 deletions agent/container/api/agent.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion agent/container/cfg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ generate:
chi-server: true
models: true
embedded-spec: true
output: agent/api/agent.gen.go
output: /Users/vijeshdeepan/Desktop/kubviz/agent/container/api/agent.gen.go
15 changes: 13 additions & 2 deletions agent/container/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,22 @@ paths:
'200':
description: OK

/event/docker:
/event/docker/hub:
post:
tags:
- public
summary: Post Docker artifactory events
summary: Post Dockerhub artifactory events
responses:
'200':
description: OK

/event/docker/github:
post:
tags:
- public
summary: Post github Docker artifactory events
responses:
'200':
description: OK

# oapi-codegen -config ./cfg.yaml ./openapi.yaml
5 changes: 4 additions & 1 deletion agent/container/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func New() *Application {

mux := chi.NewMux()
apiServer.BindRequest(mux)

chi.Walk(mux, func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
fmt.Printf("[%s]: '%s' has %d middlewares.\n", method, route, len(middlewares))
return nil
})
httpServer := &http.Server{
// TODO: remove hardcoding
// Addr: fmt.Sprintf("0.0.0.0:%d", cfg.Port),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"
)

func (ah *APIHandler) PostEventDocker(w http.ResponseWriter, r *http.Request) {
func (ah *APIHandler) PostEventDockerHub(w http.ResponseWriter, r *http.Request) {
event, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("Event body read failed: %v", err)
Expand Down
20 changes: 20 additions & 0 deletions agent/container/pkg/handler/docker_event_github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package handler

import (
"io"
"log"
"net/http"
)

func (ah *APIHandler) PostEventDockerGithub(w http.ResponseWriter, r *http.Request) {
event, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("Event body read failed: %v", err)
}

log.Printf("Received docker event from github artifactory: %v", string(event))
err = ah.conn.Publish(event, "Github_Registory")
if err != nil {
log.Printf("Publish failed for event: %v, reason: %v", string(event), err)
}
}
36 changes: 34 additions & 2 deletions client/pkg/clickhouse/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ type DBInterface interface {
InsertDeletedAPI(model.DeletedAPI)
InsertKubvizEvent(model.Metrics)
InsertGitEvent(string)
InsertContainerEvent(string)
InsertKubeScoreMetrics(model.KubeScoreRecommendations)
RetriveKetallEvent() ([]model.Resource, error)
RetriveOutdatedEvent() ([]model.CheckResultfinal, error)
RetriveKubepugEvent() ([]model.Result, error)
RetrieveKubvizEvent() ([]model.DbEvent, error)
InsertContainerEventDockerHub(string)
InsertContainerEventGithub(string)
Close()
}

Expand Down Expand Up @@ -62,7 +63,7 @@ func NewDBClient(conf *config.Config) (DBInterface, error) {
}
return nil, err
}
tables := []DBStatement{kubvizTable, rakeesTable, kubePugDepricatedTable, kubepugDeletedTable, ketallTable, outdateTable, clickhouseExperimental, containerTable, gitTable, kubescoreTable}
tables := []DBStatement{kubvizTable, rakeesTable, kubePugDepricatedTable, kubepugDeletedTable, ketallTable, outdateTable, clickhouseExperimental, containerDockerhubTable, containerGithubTable, gitTable, kubescoreTable}
for _, table := range tables {
if err = splconn.Exec(context.Background(), string(table)); err != nil {
return nil, err
Expand Down Expand Up @@ -377,3 +378,34 @@ func (c *DBClient) RetrieveKubvizEvent() ([]model.DbEvent, error) {
}
return events, nil
}

func (c *DBClient) InsertContainerEventDockerHub(event string) {
ctx := context.Background()
batch, err := c.splconn.PrepareBatch(ctx, "INSERT INTO container_dockerhub")
if err != nil {
log.Fatal(err)
}

if err = batch.Append(event); err != nil {
log.Fatal(err)
}

if err = batch.Send(); err != nil {
log.Fatal(err)
}
}
func (c *DBClient) InsertContainerEventGithub(event string) {
ctx := context.Background()
batch, err := c.splconn.PrepareBatch(ctx, "INSERT INTO container_github")
if err != nil {
log.Fatal(err)
}

if err = batch.Append(event); err != nil {
log.Fatal(err)
}

if err = batch.Send(); err != nil {
log.Fatal(err)
}
}
3 changes: 2 additions & 1 deletion client/pkg/clickhouse/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const InsertDepricatedApi DBStatement = "INSERT INTO DeprecatedAPIs (ClusterName
const InsertDeletedApi DBStatement = "INSERT INTO DeletedAPIs (ClusterName, ObjectName, Group, Kind, Version, Name, Deleted, Scope) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
const InsertKubvizEvent DBStatement = "INSERT INTO events (ClusterName, Id, EventTime, OpType, Name, Namespace, Kind, Message, Reason, Host, Event, FirstTime, LastTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
const clickhouseExperimental DBStatement = `SET allow_experimental_object_type=1;`
const containerTable DBStatement = `CREATE table IF NOT EXISTS container_bridge(event JSON) ENGINE = MergeTree ORDER BY tuple();`
const containerDockerhubTable DBStatement = `CREATE table IF NOT EXISTS container_dockerhub(event JSON) ENGINE = MergeTree ORDER BY tuple();`
const gitTable DBStatement = `CREATE table IF NOT EXISTS git_json(event JSON) ENGINE = MergeTree ORDER BY tuple();`
const containerGithubTable DBStatement = `CREATE table IF NOT EXISTS container_github(event JSON) ENGINE = MergeTree ORDER BY tuple();`
const InsertKubeScore string = "INSERT INTO kubescore (id, namespace, cluster_name, recommendations) VALUES (?, ?, ?, ?)"
9 changes: 7 additions & 2 deletions client/pkg/clients/container_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ func (n *NATSContext) SubscribeContainerNats(conn clickhouse.DBInterface) {
if err != nil {
log.Fatal(err)
}
conn.InsertContainerEvent(string(data))
log.Println("Inserted Container metrics:", string(msg.Data))
if repoName == "Github_Registory" {
conn.InsertContainerEventGithub(string(data))
log.Println("Inserted Github Container metrics:", string(msg.Data))
} else if repoName == "Dockerhub_Registry" {
conn.InsertContainerEventDockerHub(string(data))
log.Println("Inserted DockerHub Container metrics:", string(msg.Data))
}
}, nats.Durable(string(containerConsumer)), nats.ManualAck())
}

0 comments on commit 5240202

Please sign in to comment.