Skip to content

Commit

Permalink
refactoring code base
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivam9268 committed Jun 9, 2022
1 parent ac69886 commit 71f1cd8
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ WORKDIR /kubernetes-pod-monitor

COPY . .

RUN go build -o kubernetes-pod-monitor
RUN go build -o kubernetes-pod-monitor cmd/main.go

FROM golang:1.16.14-alpine

Expand Down
14 changes: 5 additions & 9 deletions main.go → cmd/kubernetes-pod-monitor/run.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package kubernetes_pod_monitor

import (
"fmt"
Expand Down Expand Up @@ -46,7 +46,7 @@ func setup() {
setupApp()
}

func CleanupOnSignal(cleanup func()) {
func cleanupOnSignal(cleanup func()) {
go func() {
sig := <-gracefulStop
log.Info(fmt.Sprintf("caught sig: %+v. waiting for goroutines to finish", sig))
Expand All @@ -60,7 +60,7 @@ func setupApp() {
service.Initialize()
http.Initialize()
sessions.HealthOrPanic()
CleanupOnSignal(cleanup)
cleanupOnSignal(cleanup)
}

func cleanup() {
Expand All @@ -69,13 +69,9 @@ func cleanup() {
done <- true
}

func run() {
func Run() {
setup()
go http.Run()
go service.Run()
<-done
}

func main() {
setup()
run()
}
7 changes: 7 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import kubernetes_pod_monitor "github.com/unacademy/kubernetes-pod-monitor/cmd/kubernetes-pod-monitor"

func main() {
kubernetes_pod_monitor.Run()
}
File renamed without changes.
5 changes: 2 additions & 3 deletions service/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,21 @@ func checkPod(pod *corev1.Pod) {
lastTerminationState = lastState.String()
}

podLogOpts := corev1.PodLogOptions{Container: container.Name, Previous: true}
req := clientset.CoreV1().Pods(namespace).GetLogs(podName, &podLogOpts)
req := clientset.CoreV1().Pods(namespace).GetLogs(podName, &corev1.PodLogOptions{Container: container.Name, Previous: true})

podLogs, err := req.Stream()
if err != nil {
log.Errorln("failed to get pods:", err)
continue
}

buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
log.Errorln("error in copy information from podLogs to buf:", err)
continue
}
logs := buf.String()

containerInfo.Reason = container.LastTerminationState.Terminated.Reason

sendESLogs(&containerInfo, container.RestartCount, &logs, &lastTerminationState)
Expand Down
File renamed without changes.
14 changes: 5 additions & 9 deletions service/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
"github.com/unacademy/kubernetes-pod-monitor/sessions"
)

const createdFormat = "2006-01-02 15:04:05" //"Jan 2, 2006 at 3:04pm (MST)"

// getCurrentTimeInSeconds returns current time in seconds.
func getCurrentTimeInSeconds() int64 {
return time.Now().UnixNano() / int64(time.Second)
Expand Down Expand Up @@ -78,27 +76,26 @@ func notifyOnSlack(containerInfo *ContainerInfo, lastTerminationState *string, p
log.Info("not notifying...")
return
}

channelName := getSlackChannel(containerInfo.ClusterName, containerInfo.Namespace)
api := slack.New(token)

log.Info("notifying...")
log.Infof("Sending to %s for %s namespace in %s cluster", channelName, containerInfo.ClusterName, containerInfo.Namespace)
link := viper.GetString("elasticsearch.dashboard")

link := viper.GetString("elasticsearch.dashboard")
msg := fmt.Sprintf("*Cluster Name*:- %s\n*Namespace*:- %s\n*Container Name*:- %s\n *Reason*:- %s\n `Kibana dashboard`:- <%s|Dashboard>",
containerInfo.ClusterName, containerInfo.Namespace, containerInfo.ContainerName, containerInfo.Reason, link)

err := api.ChatPostMessage(channelName, msg, nil)
if err != nil {
log.Error(err)
}
}

func getSlackChannel(clusterName string, namespace string) string {
sqlClient := sessions.GetSqlClient()
var slackChannel string
var defaultSlackChannel = viper.GetString("slack.channel")

sqlClient := sessions.GetSqlClient()
rows, err := sqlClient.Raw(`select slack_channel FROM k8s_pod_crash_notify WHERE clustername=? AND namespace=?`,
clusterName, namespace).Rows()
if err != nil {
Expand All @@ -119,9 +116,8 @@ func getSlackChannel(clusterName string, namespace string) string {
}

func shouldNotify(clusterName string, namespace string, containername string) bool {
sqlClient := sessions.GetSqlClient()
var exists string

sqlClient := sessions.GetSqlClient()
rows, err := sqlClient.Raw(`select count(*) FROM k8s_crash_ignore_notify WHERE clustername=? AND namespace=? AND containername=?`,
clusterName, namespace, containername).Rows()
if err != nil {
Expand All @@ -145,7 +141,7 @@ func shouldNotify(clusterName string, namespace string, containername string) bo

func persistPodCrash(containerInfo *ContainerInfo, restartCount int32) {
sqlClient := sessions.GetSqlClient()
currTime := time.Unix(getCurrentTimeInSeconds(), 0).Format(createdFormat)
currTime := time.Unix(getCurrentTimeInSeconds(), 0).Format("2006-01-02 15:04:05")
err := sqlClient.Exec(`INSERT INTO k8s_pod_crash (clustername, namespace, containername, restartcount, date) VALUES(?,?,?,?,?)`,
containerInfo.ClusterName, containerInfo.Namespace, containerInfo.ContainerName, restartCount, currTime).Error

Expand Down
2 changes: 1 addition & 1 deletion sessions/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func NewElasticSearchLocalClient(esURL string) (*elastic.Client, error) {
}

func NewElasticSearchAwsClient(esURL string, awsRegion string) (*elastic.Client, error) {

sess := session.Must(session.NewSession(aws.NewConfig().WithRegion(awsRegion)))
svc := sts.New(sess)
creds := GetAWSChainCredentialsV1(svc, sess)
Expand Down Expand Up @@ -54,6 +53,7 @@ func InitElasticsearchClient() {
url := fmt.Sprintf("%s:%s", viper.GetString("elasticsearch.url"),
viper.GetString("elasticsearch.port"))
env := viper.GetString("DEPLOY_ENV")

if env == "local" {
client, err := NewElasticSearchLocalClient(url)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion sessions/irsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func GetAWSChainCredentialsV1(svc *sts.STS, sess *session.Session) *credentials.
},
&credentials.SharedCredentialsProvider{},
}

if irsa {
chain = append(chain, stscreds.NewWebIdentityRoleProvider(svc, roleARN, "", tokenPath))
}

creds := credentials.NewChainCredentials(chain)

creds.Get() //IMPORTANT DO NOT remove otherwise throws error: RequestCanceled: request context canceled
//caused by: context deadline exceeded: no Elasticsearch node available

Expand Down
1 change: 1 addition & 0 deletions sessions/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (

func newClientset() *kubernetes.Clientset {
env := viper.GetString("DEPLOY_ENV")

kubeconfig := ""
if env == "local" {
kubeconfig = filepath.Join(
Expand Down
1 change: 0 additions & 1 deletion sessions/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func newSqlClient() *gorm.DB {
if err != nil {
panic(err)
}
// See "Important settings" section.
sqlClient.DB().SetConnMaxLifetime(time.Duration(viper.GetInt64("sql.connection_lifetime")) * time.Second)
sqlClient.DB().SetMaxOpenConns(80)
sqlClient.DB().SetMaxIdleConns(20)
Expand Down

0 comments on commit 71f1cd8

Please sign in to comment.