Skip to content

Commit

Permalink
issuse #94 obj to table
Browse files Browse the repository at this point in the history
  • Loading branch information
vijeyash1 committed Jul 19, 2023
1 parent 5240202 commit ee7fee4
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 81 deletions.
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: /Users/vijeshdeepan/Desktop/kubviz/agent/container/api/agent.gen.go
output: agent/container/api/agent.gen.go
26 changes: 19 additions & 7 deletions agent/container/pkg/handler/docker_event_dockerhub.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
package handler

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

// parse errors
var (
ErrReadingBody = errors.New("error reading the request body")
ErrPublishToNats = errors.New("error while publishing to nats")
)

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)
defer func() {
_, _ = io.Copy(io.Discard, r.Body)
_ = r.Body.Close()
}()
payload, err := io.ReadAll(r.Body)
if err != nil || len(payload) == 0 {
log.Printf("%v: %v", ErrReadingBody, err)
return
}

log.Printf("Received event from docker artifactory: %v", string(event))
err = ah.conn.Publish(event, "docker registry")
log.Printf("Received event from docker artifactory: %v", string(payload))
err = ah.conn.Publish(payload, "Dockerhub_Registry")
if err != nil {
log.Printf("Publish failed for event: %v, reason: %v", string(event), err)
log.Printf("%v: %v", ErrPublishToNats, err)
return
}
}
29 changes: 23 additions & 6 deletions agent/container/pkg/handler/docker_event_github.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
package handler

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

var (
ErrMissingGithubEventHeader = errors.New("missing X-GitHub-Event Header")
)

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)
defer func() {
_, _ = io.Copy(io.Discard, r.Body)
_ = r.Body.Close()
}()
event := r.Header.Get("X-GitHub-Event")
if event == "" {
log.Printf("%v", ErrMissingGithubEventHeader)
return
}

payload, err := io.ReadAll(r.Body)
if err != nil || len(payload) == 0 {
log.Printf("%v: %v", ErrReadingBody, err)
return
}

log.Printf("Received docker event from github artifactory: %v", string(event))
err = ah.conn.Publish(event, "Github_Registory")
log.Printf("Received docker event from github artifactory: %v", string(payload))
err = ah.conn.Publish(payload, "Github_Registry")
if err != nil {
log.Printf("Publish failed for event: %v, reason: %v", string(event), err)
log.Printf("%v: %v", ErrPublishToNats, err)
return
}
}
30 changes: 18 additions & 12 deletions client/pkg/clickhouse/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type DBInterface interface {
RetriveOutdatedEvent() ([]model.CheckResultfinal, error)
RetriveKubepugEvent() ([]model.Result, error)
RetrieveKubvizEvent() ([]model.DbEvent, error)
InsertContainerEventDockerHub(string)
InsertContainerEventDockerHub(model.DockerHubBuild)
InsertContainerEventGithub(string)
Close()
}
Expand Down Expand Up @@ -63,7 +63,7 @@ func NewDBClient(conf *config.Config) (DBInterface, error) {
}
return nil, err
}
tables := []DBStatement{kubvizTable, rakeesTable, kubePugDepricatedTable, kubepugDeletedTable, ketallTable, outdateTable, clickhouseExperimental, containerDockerhubTable, containerGithubTable, gitTable, kubescoreTable}
tables := []DBStatement{kubvizTable, rakeesTable, kubePugDepricatedTable, kubepugDeletedTable, ketallTable, outdateTable, clickhouseExperimental, containerDockerhubTable, containerGithubTable, gitTable, kubescoreTable, dockerHubBuildTable}
for _, table := range tables {
if err = splconn.Exec(context.Background(), string(table)); err != nil {
return nil, err
Expand Down Expand Up @@ -379,21 +379,27 @@ 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 {
func (c *DBClient) InsertContainerEventDockerHub(build model.DockerHubBuild) {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(string(InsertDockerHubBuild))
)
defer stmt.Close()
if _, err := stmt.Exec(
build.PushedBy,
build.ImageTag,
build.RepositoryName,
build.DateCreated,
build.Owner,
build.Event,
); err != nil {
log.Fatal(err)
}

if err = batch.Send(); err != nil {
if err := tx.Commit(); err != nil {
log.Fatal(err)
}
}

func (c *DBClient) InsertContainerEventGithub(event string) {
ctx := context.Background()
batch, err := c.splconn.PrepareBatch(ctx, "INSERT INTO container_github")
Expand Down
12 changes: 12 additions & 0 deletions client/pkg/clickhouse/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ const kubescoreTable DBStatement = `
recommendations String
) engine=File(TabSeparated)
`

const dockerHubBuildTable DBStatement = `
CREATE TABLE IF NOT EXISTS dockerhubbuild (
PushedBy String,
ImageTag String,
RepositoryName String,
DateCreated String,
Owner String,
Event String
) engine=File(TabSeparated)
`
const InsertDockerHubBuild DBStatement = "INSERT INTO dockerhubbuild (PushedBy, ImageTag, RepositoryName, DateCreated, Owner, Event) VALUES (?, ?, ?, ?, ?, ?)"
const InsertRakees DBStatement = "INSERT INTO rakkess (ClusterName, Name, Create, Delete, List, Update) VALUES (?, ?, ?, ?, ?, ?)"
const InsertKetall DBStatement = "INSERT INTO getall_resources (ClusterName, Namespace, Kind, Resource, Age) VALUES (?, ?, ?, ?, ?)"
const InsertOutdated DBStatement = "INSERT INTO outdated_images (ClusterName, Namespace, Pod, CurrentImage, CurrentTag, LatestVersion, VersionsBehind) VALUES (?, ?, ?, ?, ?, ?, ?)"
Expand Down
81 changes: 26 additions & 55 deletions client/pkg/clients/container_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ package clients

import (
"encoding/json"
"errors"
"log"
"time"

"github.com/intelops/kubviz/client/pkg/clickhouse"
"github.com/intelops/kubviz/model"
"github.com/nats-io/nats.go"
)

var (
ErrUnmarshalBuildPayload = errors.New("error while unmarshal the dockerhub build payload")
)

type Container string

// constant variables to use with nats stream and
Expand All @@ -18,66 +25,30 @@ const (
containerConsumer Container = "container-event-consumer"
)

// func (n *NATSContext) SubscribeContainerNats(conn clickhouse.DBInterface) {
// n.stream.Subscribe(string(containerSubject), func(msg *nats.Msg) {
// type events struct {
// Events []json.RawMessage `json:"events"`
// }

// eventDocker := &events{}
// err := json.Unmarshal(msg.Data, &eventDocker)
// if err == nil {
// log.Println(eventDocker)
// msg.Ack()
// repoName := msg.Header.Get("REPO_NAME")
// type newEvent struct {
// RepoName string `json:"repoName"`
// Event json.RawMessage `json:"event"`
// }

// for _, event := range eventDocker.Events {
// event := &newEvent{
// RepoName: repoName,
// Event: event,
// }

// eventsJSON, err := json.Marshal(event)
// if err != nil {
// log.Printf("Failed to marshall with repo name going ahead with only event, %v", err)
// eventsJSON = msg.Data
// }
// conn.InsertContainerEvent(string(eventsJSON))
// }
// } else {
// log.Printf("Failed to unmarshal event, %v", err)
// conn.InsertContainerEvent(string(msg.Data))
// }

// log.Println("Inserted metrics:", string(msg.Data))
// }, nats.Durable(string(containerConsumer)), nats.ManualAck())
// }
func (n *NATSContext) SubscribeContainerNats(conn clickhouse.DBInterface) {
n.stream.Subscribe(string(containerSubject), func(msg *nats.Msg) {
type pubData struct {
Metrics json.RawMessage `json:"event"`
Repo string `json:"repoName"`
}
msg.Ack()
repoName := msg.Header.Get("REPO_NAME")
metrics := &pubData{
Metrics: json.RawMessage(msg.Data),
Repo: repoName,
}
data, err := json.Marshal(metrics)
if err != nil {
log.Fatal(err)
}
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))
if repoName == "Dockerhub_Registry" {
var pl model.BuildPayload
err := json.Unmarshal(msg.Data, &pl)
if err != nil {
log.Printf("%v", ErrUnmarshalBuildPayload)
return
}
var hub model.DockerHubBuild
t := time.Unix(int64(pl.Repository.DateCreated), 0)
hub.DateCreated = t.Format("2006-01-02 15:04:05")
hub.PushedBy = pl.PushData.Pusher
hub.ImageTag = pl.PushData.Tag
hub.RepositoryName = pl.Repository.Name
hub.Owner = pl.Repository.Owner
hub.Event = string(msg.Data)
conn.InsertContainerEventDockerHub(hub)
log.Println("Inserted DockerHub Container metrics:", string(msg.Data))
} else if repoName == "Github_Registry" {
conn.InsertContainerEventGithub(string(msg.Data))
log.Println("Inserted Github Container metrics:", string(msg.Data))
}
}, nats.Durable(string(containerConsumer)), nats.ManualAck())
}
37 changes: 37 additions & 0 deletions model/dockerhub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package model

type BuildPayload struct {
CallbackURL string `json:"callback_url"`
PushData struct {
Images []string `json:"images"`
PushedAt float32 `json:"pushed_at"`
Pusher string `json:"pusher"`
Tag string `json:"tag"`
} `json:"push_data"`
Repository struct {
CommentCount int `json:"comment_count"`
DateCreated float32 `json:"date_created"`
Description string `json:"description"`
Dockerfile string `json:"dockerfile"`
FullDescription string `json:"full_description"`
IsOfficial bool `json:"is_official"`
IsPrivate bool `json:"is_private"`
IsTrusted bool `json:"is_trusted"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Owner string `json:"owner"`
RepoName string `json:"repo_name"`
RepoURL string `json:"repo_url"`
StarCount int `json:"star_count"`
Status string `json:"status"`
} `json:"repository"`
}

type DockerHubBuild struct {
PushedBy string
ImageTag string
RepositoryName string
DateCreated string
Owner string
Event string
}
Loading

0 comments on commit ee7fee4

Please sign in to comment.