diff --git a/cmd/cert-manager/cert-manager.go b/cmd/cert-manager/cert-manager.go index 3ea257d..24e726c 100644 --- a/cmd/cert-manager/cert-manager.go +++ b/cmd/cert-manager/cert-manager.go @@ -40,8 +40,7 @@ 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" @@ -49,6 +48,11 @@ func NewCertManager(checker *cmd.Checker) (*CertManager, error) { 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) diff --git a/cmd/cert-manager/cert-manager_test.go b/cmd/cert-manager/cert-manager_test.go index 8d68969..c51d542 100644 --- a/cmd/cert-manager/cert-manager_test.go +++ b/cmd/cert-manager/cert-manager_test.go @@ -24,7 +24,7 @@ func TestNewCertManager(t *testing.T) { return logrus.NewEntry(logrus.New()) } chatwork := ¬ify.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) @@ -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, diff --git a/cmd/checker.go b/cmd/checker.go index 4631630..d3070d4 100644 --- a/cmd/checker.go +++ b/cmd/checker.go @@ -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, } } diff --git a/cmd/cluster-autoscaler/clusterAutoscaler.go b/cmd/cluster-autoscaler/clusterAutoscaler.go index 9c757d9..b57145a 100644 --- a/cmd/cluster-autoscaler/clusterAutoscaler.go +++ b/cmd/cluster-autoscaler/clusterAutoscaler.go @@ -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)) diff --git a/cmd/datadog-agent/datadog-agent.go b/cmd/datadog-agent/datadog-agent.go index 80605f9..6246c10 100644 --- a/cmd/datadog-agent/datadog-agent.go +++ b/cmd/datadog-agent/datadog-agent.go @@ -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, diff --git a/cmd/datadog-agent/datadog-agent_test.go b/cmd/datadog-agent/datadog-agent_test.go index f32fcb8..3076b13 100644 --- a/cmd/datadog-agent/datadog-agent_test.go +++ b/cmd/datadog-agent/datadog-agent_test.go @@ -17,7 +17,7 @@ func TestNewDatadogAgent(t *testing.T) { return logrus.NewEntry(logrus.New()) } chatwork := ¬ify.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) @@ -36,7 +36,7 @@ func TestCheck(t *testing.T) { chatwork := ¬ify.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: "", @@ -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", diff --git a/cmd/fluent/fluent.go b/cmd/fluent/fluent.go index d868dc9..05504b5 100644 --- a/cmd/fluent/fluent.go +++ b/cmd/fluent/fluent.go @@ -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)) diff --git a/cmd/fluent/fluent_test.go b/cmd/fluent/fluent_test.go index 316d5e8..b2108a6 100644 --- a/cmd/fluent/fluent_test.go +++ b/cmd/fluent/fluent_test.go @@ -16,7 +16,7 @@ func TestNewFluent(t *testing.T) { return logrus.NewEntry(logrus.New()) } chatwork := ¬ify.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) diff --git a/cmd/ingress/ingress.go b/cmd/ingress/ingress.go index f448453..ff5aad6 100644 --- a/cmd/ingress/ingress.go +++ b/cmd/ingress/ingress.go @@ -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 @@ -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) diff --git a/cmd/ingress/ingress_test.go b/cmd/ingress/ingress_test.go index 27a40aa..2c8250f 100644 --- a/cmd/ingress/ingress_test.go +++ b/cmd/ingress/ingress_test.go @@ -20,8 +20,8 @@ func TestNewIngress(t *testing.T) { return logrus.NewEntry(logrus.New()) } chatwork := ¬ify.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) } @@ -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, @@ -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, diff --git a/util/notify/chatwork.go b/util/notify/chatwork.go index fbbc84a..a057633 100644 --- a/util/notify/chatwork.go +++ b/util/notify/chatwork.go @@ -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)