Skip to content

Commit

Permalink
Add all command
Browse files Browse the repository at this point in the history
  • Loading branch information
cw-sakamoto committed Dec 8, 2023
1 parent 91a33ec commit 85d1cb0
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 25 deletions.
8 changes: 6 additions & 2 deletions cmd/cert-manager/cert-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ func NewCertManager(checker *cmd.Checker) (*CertManager, error) {
t := time.Now()

namespace := fmt.Sprintf("cert-manager-test-%d%02d%02d-%s", t.Year(), t.Month(), t.Day(), util.GenerateRandomString(5))
checker.Logger().Infof("cert-manager check application Namespace: %s", namespace)
checker.Chatwork.AddMessage(fmt.Sprintf("cert-manager check application Namespace: %s\n", namespace))
location, _ := time.LoadLocation("Asia/Tokyo")

resourceName := "sample"

if v := os.Getenv("RESOURCE_NAME"); v != "" {
resourceName = v
}

checker.Chatwork.AddMessage(fmt.Sprintf("Start in %s at %s\n", checker.ClusterName, time.Now().In(location).Format("2006-01-02 15:04:05")))

checker.Logger().Infof("cert-manager check application Namespace: %s", namespace)
checker.Chatwork.AddMessage(fmt.Sprintf("cert-manager check application Namespace: %s\n", namespace))

k8sclientset, err := config.NewK8sClientset()
if err != nil {
checker.Logger().Errorf("Error NewK8sClientset: %s ", err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/cert-manager/cert-manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestNewCertManager(t *testing.T) {
return logrus.NewEntry(logrus.New())
}
chatwork := &notify.Chatwork{}
checker := cmd.NewChecker(context.Background(), false, logger, chatwork, 3*time.Minute)
checker := cmd.NewChecker(context.Background(), false, logger, chatwork, "test", 3*time.Minute)
ingress, err := NewCertManager(checker)
if err != nil {
t.Fatalf("NewCertManager: %s", err)
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestCheck(t *testing.T) {
now := time.Now()
namespace := fmt.Sprintf("cert-manager-test-%d%02d%02d-%s", now.Year(), now.Month(), now.Day(), util.GenerateRandomString(5))
cm := &CertManager{
Checker: cmd.NewChecker(context.Background(), true, logger, chatwork, 3*time.Minute),
Checker: cmd.NewChecker(context.Background(), true, logger, chatwork, "test", 3*time.Minute),
Namespace: namespace,
ResourceName: "sample",
Clientset: k8sclientset,
Expand Down
24 changes: 13 additions & 11 deletions cmd/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ import (
)

type Checker struct {
Ctx context.Context
Debug bool
Logger func() *logrus.Entry
Chatwork *notify.Chatwork
Timeout time.Duration
Ctx context.Context
Debug bool
Logger func() *logrus.Entry
Chatwork *notify.Chatwork
ClusterName string
Timeout time.Duration
}

func NewChecker(ctx context.Context, debug bool, logger func() *logrus.Entry, chatwork *notify.Chatwork, timeout time.Duration) *Checker {
func NewChecker(ctx context.Context, debug bool, logger func() *logrus.Entry, chatwork *notify.Chatwork, clusterName string, timeout time.Duration) *Checker {
logger().Info("Checker timeout: ", timeout)

return &Checker{
Ctx: ctx,
Debug: debug,
Logger: logger,
Chatwork: chatwork,
Timeout: timeout,
Ctx: ctx,
Debug: debug,
Logger: logger,
Chatwork: chatwork,
ClusterName: clusterName,
Timeout: timeout,
}
}
2 changes: 2 additions & 0 deletions cmd/cluster-autoscaler/clusterAutoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func NewClusterAutoscaler(checker *cmd.Checker) (*ClusterAutoscaler, error) {

namespace := fmt.Sprintf("cluster-autoscaler-test-%d%02d%02d-%s", t.Year(), t.Month(), t.Day(), util.GenerateRandomString(5))

location, _ := time.LoadLocation("Asia/Tokyo")
checker.Chatwork.AddMessage(fmt.Sprintf("Start in %s at %s\n", checker.ClusterName, time.Now().In(location).Format("2006-01-02 15:04:05")))
checker.Logger().Infof("cluster-autoscaler check application Namespace: %s", namespace)
checker.Chatwork.AddMessage(fmt.Sprintf("cluster-autoscaler check application Namespace: %s\n", namespace))

Expand Down
5 changes: 4 additions & 1 deletion cmd/datadog-agent/datadog-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ func NewDatadogAgent(checker *cmd.Checker) (*DatadogAgent, error) {
appKey = v
}

queryMetrics = "avg:kubernetes.cpu.user.total"
queryMetrics = "avg:kubernetes.cpu.user.total{*}"
if v := os.Getenv("QUERY_METRICS"); v != "" {
queryMetrics = v
}

location, _ := time.LoadLocation("Asia/Tokyo")
checker.Chatwork.AddMessage(fmt.Sprintf("Start in %s at %s\n", checker.ClusterName, time.Now().In(location).Format("2006-01-02 15:04:05")))

return &DatadogAgent{
Checker: checker,
ApiKey: apiKey,
Expand Down
6 changes: 3 additions & 3 deletions cmd/datadog-agent/datadog-agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestNewDatadogAgent(t *testing.T) {
return logrus.NewEntry(logrus.New())
}
chatwork := &notify.Chatwork{}
checker := cmd.NewChecker(context.Background(), false, logger, chatwork, 3*time.Minute)
checker := cmd.NewChecker(context.Background(), false, logger, chatwork, "test", 3*time.Minute)
datadogAgent, err := NewDatadogAgent(checker)
if err != nil {
t.Fatalf("NewDatadogAgent: %s", err)
Expand All @@ -36,7 +36,7 @@ func TestCheck(t *testing.T) {

chatwork := &notify.Chatwork{ApiToken: "token", RoomId: "test", Logger: logger}
datadogAgent := &DatadogAgent{
Checker: cmd.NewChecker(context.Background(), false, logger, chatwork, 1*time.Minute),
Checker: cmd.NewChecker(context.Background(), false, logger, chatwork, "test", 1*time.Minute),
ApiKey: "",
AppKey: "",
QueryMetrics: "",
Expand All @@ -54,7 +54,7 @@ func TestCheck(t *testing.T) {
}

datadogAgent = &DatadogAgent{
Checker: cmd.NewChecker(context.Background(), false, logger, chatwork, 3*time.Minute),
Checker: cmd.NewChecker(context.Background(), false, logger, chatwork, "test", 3*time.Minute),
ApiKey: "test",
AppKey: "test",
QueryMetrics: "avg:kubernetes.cpu.user.total",
Expand Down
4 changes: 4 additions & 0 deletions cmd/fluent/fluent.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func NewFluent(checker *cmd.Checker) (*Fluent, error) {
t := time.Now()

namespace := fmt.Sprintf("fluent-test-%d%02d%02d-%s", t.Year(), t.Month(), t.Day(), util.GenerateRandomString(5))

location, _ := time.LoadLocation("Asia/Tokyo")
checker.Chatwork.AddMessage(fmt.Sprintf("Start in %s at %s\n", checker.ClusterName, time.Now().In(location).Format("2006-01-02 15:04:05")))

checker.Logger().Infof("fluent check application Namespace: %s", namespace)
checker.Chatwork.AddMessage(fmt.Sprintf("fluent check application Namespace: %s\n", namespace))

Expand Down
2 changes: 1 addition & 1 deletion cmd/fluent/fluent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestNewFluent(t *testing.T) {
return logrus.NewEntry(logrus.New())
}
chatwork := &notify.Chatwork{}
checker := cmd.NewChecker(context.Background(), false, logger, chatwork, 3*time.Minute)
checker := cmd.NewChecker(context.Background(), false, logger, chatwork, "test", 3*time.Minute)
fluent, err := NewFluent(checker)
if err != nil {
t.Fatalf("NewFluent: %s", err)
Expand Down
11 changes: 10 additions & 1 deletion cmd/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ type Ingress struct {
ExternalHostname string
}

func NewIngress(checker *cmd.Checker, noDnsCheck bool, ingressClassName string) (*Ingress, error) {
func NewIngress(checker *cmd.Checker, noDnsCheck bool) (*Ingress, error) {
t := time.Now()

namespace := fmt.Sprintf("ingress-test-%d%02d%02d-%s", t.Year(), t.Month(), t.Day(), util.GenerateRandomString(5))

location, _ := time.LoadLocation("Asia/Tokyo")
checker.Chatwork.AddMessage(fmt.Sprintf("Start in %s at %s\n", checker.ClusterName, time.Now().In(location).Format("2006-01-02 15:04:05")))

checker.Logger().Infof("Ingress check application Namespace: %s", namespace)
checker.Chatwork.AddMessage(fmt.Sprintf("Ingress check application Namespace: %s\n", namespace))

resourceName := "sample"
externalHostName := "sample-skmt.cwtest.info"
ingressClassName := "alb"

if v := os.Getenv("RESOURCE_NAME"); v != "" {
resourceName = v
Expand All @@ -50,6 +55,10 @@ func NewIngress(checker *cmd.Checker, noDnsCheck bool, ingressClassName string)
externalHostName = v
}

if v := os.Getenv("INGRESS_CLASS_NAME"); v != "" {
ingressClassName = v
}

k8sclient, err := config.NewK8sClientset()
if err != nil {
checker.Logger().Errorf("NewK8sClientset: %s", err)
Expand Down
8 changes: 4 additions & 4 deletions cmd/ingress/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func TestNewIngress(t *testing.T) {
return logrus.NewEntry(logrus.New())
}
chatwork := &notify.Chatwork{}
checker := cmd.NewChecker(context.Background(), false, logger, chatwork, 3*time.Minute)
ingress, err := NewIngress(checker, false, "alb")
checker := cmd.NewChecker(context.Background(), false, logger, chatwork, "test", 3*time.Minute)
ingress, err := NewIngress(checker, false)
if err != nil {
t.Fatalf("NewIngress: %s", err)
}
Expand Down Expand Up @@ -51,7 +51,7 @@ func TestCheck(t *testing.T) {
// kindとingress-nginxがある前提
// レコードは作れないのでNoDnsCheckをtrueにする
ingress := &Ingress{
Checker: cmd.NewChecker(context.Background(), true, logger, chatwork, 1*time.Minute),
Checker: cmd.NewChecker(context.Background(), true, logger, chatwork, "test", 1*time.Minute),
Namespace: namespace,
Clientset: k8sclient,
NoDnsCheck: true,
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestCheckDNSRecord(t *testing.T) {
// kindとingress-nginxがある前提
// レコードは作れないのでNoDnsCheckをtrueにする
ingress := &Ingress{
Checker: cmd.NewChecker(context.Background(), false, logger, chatwork, 1*time.Minute),
Checker: cmd.NewChecker(context.Background(), false, logger, chatwork, "test", 1*time.Minute),
Namespace: "ingress-test",
Clientset: k8sclient,
NoDnsCheck: true,
Expand Down
1 change: 1 addition & 0 deletions util/notify/chatwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (c *Chatwork) AddMessage(message string) {
// https://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms-room_id-messages
// エラーが起きても問題ないので、エラーはログに出力するだけ
func (c *Chatwork) Send() {
defer c.Messages.Reset()
// APIのURLを作成
apiUrl := fmt.Sprintf("https://api.chatwork.com/v2/rooms/%s/messages", c.RoomId)

Expand Down

0 comments on commit 85d1cb0

Please sign in to comment.