Skip to content

Commit

Permalink
Merge pull request #22 from chatwork/fix_log_message
Browse files Browse the repository at this point in the history
change logger level and fix messages
  • Loading branch information
cw-sakamoto authored Dec 11, 2023
2 parents 01ea0db + d31b484 commit 25dba61
Show file tree
Hide file tree
Showing 16 changed files with 227 additions and 172 deletions.
46 changes: 25 additions & 21 deletions cmd/cert-manager/cert-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,31 @@ 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().Fatal("Error NewK8sClientset: ", err)
return nil, fmt.Errorf("error NewK8sClientset: %s", err)
}

scheme := runtime.NewScheme()
_ = cmapiv1.AddToScheme(scheme)

k8sclient, err := config.NewK8sClient(client.Options{Scheme: scheme})
if err != nil {
checker.Logger().Errorf("NewK8sClient: %s", err)
return nil, err
return nil, fmt.Errorf("error NewK8sClient: %s", err)
}

return &CertManager{
Expand Down Expand Up @@ -100,8 +104,8 @@ func (c *CertManager) createResources(cert certificates) error {
ObjectMeta: metav1.ObjectMeta{
Name: c.Namespace,
}}); err != nil {
c.Logger().Error("Error create namespace:", err)
c.Chatwork.AddMessage(fmt.Sprint("Error create namespace:", err))
c.Logger().Error("Error create Namespace:", err)
c.Chatwork.AddMessage(fmt.Sprint("Error create Namespace:", err))
return err
}

Expand All @@ -125,23 +129,23 @@ func (c *CertManager) cleanUpResources(cert certificates) error {
var result *multierror.Error
var err error

c.Logger().Error("Delete Certificate:", cert.certificate.ObjectMeta.Name)
c.Logger().Infof("Delete Certificate: %s", cert.certificate.ObjectMeta.Name)
if err := c.Client.Delete(context.Background(), cert.certificate); err != nil {
c.Chatwork.AddMessage(fmt.Sprintf("Error Delete Certificate: %s\n", err))
c.Logger().Error("Error Delete Certificate:", err)
c.Logger().Errorf("Error Delete Certificate: %s", err)
result = multierror.Append(result, err)
}

c.Logger().Error("Delete Issuer:", cert.certificate.ObjectMeta.Name)
c.Logger().Infof("Delete Issuer: %s", cert.certificate.ObjectMeta.Name)
if err := c.Client.Delete(context.Background(), cert.issuer); err != nil {
c.Chatwork.AddMessage(fmt.Sprintf("Error Delete Issuer: %s\n", err))
c.Logger().Error("Error Delete Issuer:", err)
c.Logger().Errorf("Error Delete Issuer: %s", err)
result = multierror.Append(result, err)
}

c.Logger().Error("Delete RootCA:", cert.certificate.ObjectMeta.Name)
c.Logger().Infof("Delete RootCA: %s", cert.certificate.ObjectMeta.Name)
if err := c.Client.Delete(context.Background(), cert.rootCA); err != nil {
c.Logger().Error("Error Delete RootCA:", err)
c.Logger().Errorf("Error Delete RootCA: %s", err)
c.Chatwork.AddMessage(fmt.Sprintf("Error Delete RootCA: %s\n", err))
result = multierror.Append(result, err)
}
Expand Down Expand Up @@ -221,7 +225,7 @@ func (c *CertManager) createCertificateObject() certificates {
// CRなので、client-goではなく、client-runtimeを使う
// ここでしか作らないリソースなので、utilのほうには入れない
func (c *CertManager) createCert(cert certificates) error {
c.Logger().Infoln("Create RootCA:", cert.rootCA.ObjectMeta.Name)
c.Logger().Infof("Create RootCA: %s", cert.rootCA.ObjectMeta.Name)
c.Chatwork.AddMessage(fmt.Sprintf("Create RootCA: %s\n", cert.rootCA.ObjectMeta.Name))
err := c.Client.Create(c.Ctx, cert.rootCA)
if err != nil {
Expand All @@ -233,10 +237,10 @@ func (c *CertManager) createCert(cert certificates) error {
err = wait.PollUntilContextTimeout(c.Ctx, 5*time.Second, c.Timeout, true, func(ctx context.Context) (bool, error) {
secret, err := secretClient.Get(ctx, cert.rootCA.Spec.SecretName, metav1.GetOptions{})
if err != nil {
c.Logger().WithError(err).Errorf("Waiting for secret %s to be ready", cert.rootCA.Spec.SecretName)
c.Logger().WithError(err).Infof("Waiting for Secret %s to be ready", cert.rootCA.Spec.SecretName)
return false, nil
}
c.Logger().Infof("Created secret:%s at %s", secret.Name, secret.CreationTimestamp)
c.Logger().Infof("Created Secret:%s at %s", secret.Name, secret.CreationTimestamp)
return true, nil
})

Expand All @@ -245,14 +249,14 @@ func (c *CertManager) createCert(cert certificates) error {
}

//Create Issuer
c.Logger().Infoln("Create Issuer:", cert.issuer.ObjectMeta.Name)
c.Logger().Infof("Create Issuer: %s", cert.issuer.ObjectMeta.Name)
c.Chatwork.AddMessage(fmt.Sprintf("Create Issuer: %s\n", cert.issuer.ObjectMeta.Name))
err = c.Client.Create(c.Ctx, cert.issuer)
if err != nil {
return err
}

c.Logger().Infoln("Create Certificate:", cert.certificate.ObjectMeta.Name)
c.Logger().Infof("Create Certificate: %s", cert.certificate.ObjectMeta.Name)
c.Chatwork.AddMessage(fmt.Sprintf("Create Certificate: %s\n", cert.certificate.ObjectMeta.Name))
err = c.Client.Create(c.Ctx, cert.certificate)

Expand All @@ -263,15 +267,15 @@ func (c *CertManager) createCert(cert certificates) error {
err = wait.PollUntilContextTimeout(c.Ctx, 5*time.Second, c.Timeout, true, func(ctx context.Context) (bool, error) {
secret, err := secretClient.Get(ctx, cert.certificate.Spec.SecretName, metav1.GetOptions{})
if err != nil {
c.Logger().WithError(err).Errorf("Waiting for secret %s to be ready", cert.certificate.Spec.SecretName)
c.Logger().WithError(err).Infof("Waiting for Secret %s to be ready", cert.certificate.Spec.SecretName)
return false, nil
}
c.Logger().Infof("Created secret:%s at %s", secret.Name, secret.CreationTimestamp)
c.Logger().Infof("Created Secret:%s at %s", secret.Name, secret.CreationTimestamp)
return true, nil
})

if err != nil {
return fmt.Errorf("waiting for Certificate secret to be ready: %w", err)
return fmt.Errorf("waiting for Certificate Secret to be ready: %w", err)
}

return nil
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,
}
}
8 changes: 5 additions & 3 deletions cmd/cluster-autoscaler/clusterAutoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ 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))

checker.Logger().Infof("cluster-autoscaler check application namespace: %s", namespace)
checker.Chatwork.AddMessage(fmt.Sprintf("cluster-autoscaler check application namespace: %s\n", namespace))
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))

resourceName := "sample-for-scale"
nodeLabelKey := "eks.amazonaws.com/capacityType"
Expand All @@ -53,7 +55,7 @@ func NewClusterAutoscaler(checker *cmd.Checker) (*ClusterAutoscaler, error) {

k8sclientset, err := config.NewK8sClientset()
if err != nil {
checker.Logger().Fatal("Error NewK8sClientset: ", err)
return nil, fmt.Errorf("error NewK8sClientset: %s", err)
}

return &ClusterAutoscaler{
Expand Down
13 changes: 8 additions & 5 deletions 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 Expand Up @@ -104,18 +107,18 @@ func (d *DatadogAgent) checkMetrics() error {
} else if r != nil && r.StatusCode == 401 {
return true, errors.New("401 Unauthorized")
}
d.Logger().Errorf("Error when querying metrics: %v", err)
d.Logger().Warnf("Error when querying metrics: %v", err)
return false, err
}

if len(resp.GetSeries()) == 0 {
d.Logger().Infof("No results found")
d.Logger().Info("No results found")
return false, nil
} else if len(resp.GetSeries()) > 0 {
d.Logger().Infof("Response from `MetricsApi.QueryMetrics`")
d.Logger().Info("Response from `MetricsApi.QueryMetrics`")
d.Chatwork.AddMessage("Response from `MetricsApi.QueryMetrics`\n")
responseContent, _ := json.MarshalIndent(resp, "", " ")
d.Logger().Debugf("Response:%s", responseContent)
d.Logger().Debugf("Response: %s", responseContent)
return true, nil
}

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
30 changes: 19 additions & 11 deletions cmd/fluent/fluent.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ 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))
checker.Logger().Infof("fluent check application namespace: %s", namespace)
checker.Chatwork.AddMessage(fmt.Sprintf("fluent check application namespace: %s\n", namespace))

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))

resourceName := "burst-log-generator"

Expand Down Expand Up @@ -68,8 +72,12 @@ func NewFluent(checker *cmd.Checker) (*Fluent, error) {

k8sclient, err := config.NewK8sClientset()
if err != nil {
checker.Logger().Errorf("NewK8sClientset: %s", err)
return nil, err
return nil, fmt.Errorf("NewK8sClientset: %s", err)
}

awsConfig, err := config.NewAwsConfig(checker.Ctx)
if err != nil {
return nil, fmt.Errorf("NewAwsConfig: %s", err)
}

return &Fluent{
Expand All @@ -79,7 +87,7 @@ func NewFluent(checker *cmd.Checker) (*Fluent, error) {
ResourceName: resourceName,
LogBucketName: logBucketName,
LogPath: logPath,
Awscfg: config.NewAwsConfig(checker.Ctx),
Awscfg: awsConfig,
}, nil
}

Expand Down Expand Up @@ -179,18 +187,18 @@ func (f *Fluent) checkS3Object() error {

result, err := client.ListObjectsV2(ctx, input)
if err != nil {
f.Logger().Error("Got an error retrieving items:", err)
f.Logger().Warnf("Got an error retrieving items: %s", err)
return false, nil
}

if len(result.Contents) != 0 {
for _, item := range result.Contents {
if item.LastModified.After(t) {
f.Chatwork.AddMessage(fmt.Sprintf("fluentd output to s3://%s/%s/%s\n", targetBucket, targetPrefix, *item.Key))
f.Logger().Println("Name: ", *item.Key)
f.Logger().Println("Last modified: ", *item.LastModified)
f.Logger().Println("Size: ", item.Size)
f.Logger().Println("Storage class: ", item.StorageClass)
f.Logger().Infof("Name: %s ", *item.Key)
f.Logger().Infof("Last modified: %s", *item.LastModified)
f.Logger().Infof("Size: %d", item.Size)
f.Logger().Infof("Storage class: %s", item.StorageClass)
return true, nil
}
}
Expand All @@ -203,7 +211,7 @@ func (f *Fluent) checkS3Object() error {
return fmt.Errorf("error waiting for S3 objects to be ready: %w", err)
}

f.Logger().Infoln("All S3 Objects are available.")
f.Logger().Info("All S3 Objects are available.")

return nil
}
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
Loading

0 comments on commit 25dba61

Please sign in to comment.