Skip to content

Commit

Permalink
Merge pull request #95 from vijeyash1/dockerhub
Browse files Browse the repository at this point in the history
Dockerhub
  • Loading branch information
jebinjeb authored Jul 19, 2023
2 parents 8bb696b + ee7fee4 commit 45be750
Show file tree
Hide file tree
Showing 12 changed files with 373 additions and 91 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: 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
20 changes: 0 additions & 20 deletions agent/container/pkg/handler/docker_event_api_handler.go

This file was deleted.

32 changes: 32 additions & 0 deletions agent/container/pkg/handler/docker_event_dockerhub.go
Original file line number Diff line number Diff line change
@@ -0,0 +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) {
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(payload))
err = ah.conn.Publish(payload, "Dockerhub_Registry")
if err != nil {
log.Printf("%v: %v", ErrPublishToNats, err)
return
}
}
37 changes: 37 additions & 0 deletions agent/container/pkg/handler/docker_event_github.go
Original file line number Diff line number Diff line change
@@ -0,0 +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) {
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(payload))
err = ah.conn.Publish(payload, "Github_Registry")
if err != nil {
log.Printf("%v: %v", ErrPublishToNats, err)
return
}
}
42 changes: 40 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(model.DockerHubBuild)
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, dockerHubBuildTable}
for _, table := range tables {
if err = splconn.Exec(context.Background(), string(table)); err != nil {
return nil, err
Expand Down Expand Up @@ -377,3 +378,40 @@ func (c *DBClient) RetrieveKubvizEvent() ([]model.DbEvent, error) {
}
return events, 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 := 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")
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)
}
}
15 changes: 14 additions & 1 deletion client/pkg/clickhouse/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,26 @@ 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 (?, ?, ?, ?, ?, ?, ?)"
const InsertDepricatedApi DBStatement = "INSERT INTO DeprecatedAPIs (ClusterName, ObjectName, Description, Kind, Deprecated, Scope) VALUES (?, ?, ?, ?, ?, ?)"
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 (?, ?, ?, ?)"
Loading

0 comments on commit 45be750

Please sign in to comment.