From 8823205378fc63a0101b96375ef828f1bf68015d Mon Sep 17 00:00:00 2001 From: cw-sakamoto Date: Thu, 7 Dec 2023 15:25:27 +0900 Subject: [PATCH] change some parameter name --- cmd/cert-manager/cert-manager.go | 38 ++++++------- cmd/cert-manager/cert-manager_test.go | 10 ++-- cmd/cluster-autoscaler/clusterAutoscaler.go | 20 +++---- cmd/fluent/fluent.go | 59 ++++++++++++--------- cmd/fluent/fluent_test.go | 2 +- cmd/ingress/ingress_test.go | 6 +-- 6 files changed, 72 insertions(+), 63 deletions(-) diff --git a/cmd/cert-manager/cert-manager.go b/cmd/cert-manager/cert-manager.go index f06e330..bbdc883 100644 --- a/cmd/cert-manager/cert-manager.go +++ b/cmd/cert-manager/cert-manager.go @@ -24,10 +24,10 @@ import ( type CertManager struct { *cmd.Checker - Namespace string - CertName string - Clientset *kubernetes.Clientset - Client client.Client + Namespace string + ResourceName string + Clientset *kubernetes.Clientset + Client client.Client } type certificates struct { @@ -43,10 +43,10 @@ func NewCertManager(checker *cmd.Checker) (*CertManager, error) { checker.Logger().Infof("cert-manager check application namespace: %s", namespace) checker.Chatwork.AddMessage(fmt.Sprintf("cert-manager check application namespace: %s\n", namespace)) - certName := "sample" + resourceName := "sample" - if v := os.Getenv("CERT_NAME"); v != "" { - certName = v + if v := os.Getenv("RESOURCE_NAME"); v != "" { + resourceName = v } k8sclientset, err := config.NewK8sClientset() @@ -64,11 +64,11 @@ func NewCertManager(checker *cmd.Checker) (*CertManager, error) { } return &CertManager{ - Checker: checker, - Namespace: namespace, - CertName: certName, - Clientset: k8sclientset, - Client: k8sclient, + Checker: checker, + Namespace: namespace, + ResourceName: resourceName, + Clientset: k8sclientset, + Client: k8sclient, }, nil } @@ -154,10 +154,10 @@ func (c *CertManager) cleanUpResources(cert certificates) error { } func (c *CertManager) createCertificateObject() certificates { - caName := c.CertName + "-ca" - caSecretName := c.CertName + "-tls" - issuerName := c.CertName + "-issuer" - certificateName := c.CertName + "-cert" + caName := c.ResourceName + "-ca" + caSecretName := c.ResourceName + "-tls" + issuerName := c.ResourceName + "-issuer" + certificateName := c.ResourceName + "-cert" certificateSecretName := certificateName rootCA := &cmapiv1.Certificate{ @@ -208,9 +208,9 @@ func (c *CertManager) createCertificateObject() certificates { Kind: "Issuer", }, DNSNames: []string{ - c.CertName, - c.CertName + "." + c.Namespace + ".svc", - c.CertName + "." + c.Namespace + ".svc.cluster.local", + c.ResourceName, + c.ResourceName + "." + c.Namespace + ".svc", + c.ResourceName + "." + c.Namespace + ".svc.cluster.local", }, }, } diff --git a/cmd/cert-manager/cert-manager_test.go b/cmd/cert-manager/cert-manager_test.go index fa65d33..8d68969 100644 --- a/cmd/cert-manager/cert-manager_test.go +++ b/cmd/cert-manager/cert-manager_test.go @@ -59,11 +59,11 @@ 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), - Namespace: namespace, - CertName: "sample", - Clientset: k8sclientset, - Client: k8sclient, + Checker: cmd.NewChecker(context.Background(), true, logger, chatwork, 3*time.Minute), + Namespace: namespace, + ResourceName: "sample", + Clientset: k8sclientset, + Client: k8sclient, } err = cm.Check() diff --git a/cmd/cluster-autoscaler/clusterAutoscaler.go b/cmd/cluster-autoscaler/clusterAutoscaler.go index 5b049a6..73d16a7 100644 --- a/cmd/cluster-autoscaler/clusterAutoscaler.go +++ b/cmd/cluster-autoscaler/clusterAutoscaler.go @@ -21,7 +21,7 @@ type ClusterAutoscaler struct { *cmd.Checker Clientset *kubernetes.Clientset Namespace string - DeploymentName string + ResourceName string ReplicaCount int NodeLabelKey string NodeLabelValue string @@ -35,12 +35,12 @@ func NewClusterAutoscaler(checker *cmd.Checker) (*ClusterAutoscaler, error) { checker.Logger().Infof("cluster-autoscaler check application namespace: %s", namespace) checker.Chatwork.AddMessage(fmt.Sprintf("cluster-autoscaler check application namespace: %s\n", namespace)) - deploymentName := "sample-for-scale" + resourceName := "sample-for-scale" nodeLabelKey := "eks.amazonaws.com/capacityType" nodeLabelValue := "SPOT" - if v := os.Getenv("DEPLOYMENT_NAME"); v != "" { - deploymentName = v + if v := os.Getenv("RESOURCE_NAME"); v != "" { + resourceName = v } if v := os.Getenv("NODE_LABEL_KEY"); v != "" { @@ -60,7 +60,7 @@ func NewClusterAutoscaler(checker *cmd.Checker) (*ClusterAutoscaler, error) { Checker: checker, Clientset: k8sclientset, Namespace: namespace, - DeploymentName: deploymentName, + ResourceName: resourceName, NodeLabelKey: nodeLabelKey, NodeLabelValue: nodeLabelValue, }, nil @@ -132,7 +132,7 @@ func (c *ClusterAutoscaler) cleanUpResources() error { k := k8s.NewK8s(c.Namespace, c.Clientset, c.Logger) var result *multierror.Error var err error - if err = k.DeleteDeployment(c.DeploymentName); err != nil { + if err = k.DeleteDeployment(c.ResourceName); err != nil { c.Chatwork.AddMessage(fmt.Sprintf("Error Delete Deployment: %s", err)) result = multierror.Append(result, err) } @@ -147,19 +147,19 @@ func (c *ClusterAutoscaler) cleanUpResources() error { func (c *ClusterAutoscaler) createDeploymentObject() *appsv1.Deployment { deployment := &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ - Name: c.DeploymentName, + Name: c.ResourceName, }, Spec: appsv1.DeploymentSpec{ Replicas: util.Int32Ptr(int32(c.ReplicaCount)), Selector: &metav1.LabelSelector{ MatchLabels: map[string]string{ - "app": c.DeploymentName, + "app": c.ResourceName, }, }, Template: apiv1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: map[string]string{ - "app": c.DeploymentName, + "app": c.ResourceName, }, }, Spec: apiv1.PodSpec{ @@ -188,7 +188,7 @@ func (c *ClusterAutoscaler) createDeploymentObject() *appsv1.Deployment { { Key: "app", Operator: metav1.LabelSelectorOpIn, - Values: []string{c.DeploymentName}, + Values: []string{c.ResourceName}, }, }, }, diff --git a/cmd/fluent/fluent.go b/cmd/fluent/fluent.go index ff20e5a..f7de247 100644 --- a/cmd/fluent/fluent.go +++ b/cmd/fluent/fluent.go @@ -25,13 +25,13 @@ import ( type Fluent struct { *cmd.Checker - Namespace string - Clientset *kubernetes.Clientset - Env string - LogBucketName string - DeploymentName string - ReplicaCount int - Awscfg aws.Config + Namespace string + Clientset *kubernetes.Clientset + LogBucketName string + LogPath string + ResourceName string + ReplicaCount int + Awscfg aws.Config } func NewFluent(checker *cmd.Checker) (*Fluent, error) { @@ -41,12 +41,14 @@ func NewFluent(checker *cmd.Checker) (*Fluent, error) { checker.Logger().Infof("fluent check application namespace: %s", namespace) checker.Chatwork.AddMessage(fmt.Sprintf("fluent check application namespace: %s\n", namespace)) - deploymentName := "burst-log-generator" + resourceName := "burst-log-generator" + env := "cwtest" + logBucketName := "cwtest-kubernetes-logs" - if v := os.Getenv("DEPLOYMENT_NAME"); v != "" { - deploymentName = v + if v := os.Getenv("RESOURCE_NAME"); v != "" { + resourceName = v } if v := os.Getenv("ENV"); v != "" { @@ -57,6 +59,13 @@ func NewFluent(checker *cmd.Checker) (*Fluent, error) { logBucketName = v } + // path s3bucket/fluentd/env(test,stg,etc...)/namespace/dt=yyyymmdd + logPath := fmt.Sprintf("fluentd/%s/%s/dt=%d%02d%02d", env, namespace, t.UTC().Year(), t.UTC().Month(), t.UTC().Day()) + + if v := os.Getenv("LOG_PATH"); v != "" { + logPath = v + } + k8sclient, err := config.NewK8sClientset() if err != nil { checker.Logger().Errorf("NewK8sClientset: %s", err) @@ -64,13 +73,13 @@ func NewFluent(checker *cmd.Checker) (*Fluent, error) { } return &Fluent{ - Checker: checker, - Namespace: namespace, - Clientset: k8sclient, - Env: env, - DeploymentName: deploymentName, - LogBucketName: logBucketName, - Awscfg: config.NewAwsConfig(checker.Ctx), + Checker: checker, + Namespace: namespace, + Clientset: k8sclient, + ResourceName: resourceName, + LogBucketName: logBucketName, + LogPath: logPath, + Awscfg: config.NewAwsConfig(checker.Ctx), }, nil } @@ -90,8 +99,8 @@ func (f *Fluent) Check() error { } f.ReplicaCount = (len(nodes.Items) / 3) + 1 - f.Logger().Infof("%s replica counts: %d", f.DeploymentName, f.ReplicaCount) - f.Chatwork.AddMessage(fmt.Sprintf("%s replica counts: %d\n", f.DeploymentName, f.ReplicaCount)) + f.Logger().Infof("%s replica counts: %d", f.ResourceName, f.ReplicaCount) + f.Chatwork.AddMessage(fmt.Sprintf("%s replica counts: %d\n", f.ResourceName, f.ReplicaCount)) defer func() { if err := f.cleanUpResources(); err != nil { @@ -142,7 +151,7 @@ func (f *Fluent) cleanUpResources() error { var result *multierror.Error var err error - if err = k.DeleteDeployment(f.DeploymentName); err != nil { + if err = k.DeleteDeployment(f.ResourceName); err != nil { f.Chatwork.AddMessage(fmt.Sprintf("Error Delete Deployment: %s", err)) result = multierror.Append(result, err) } @@ -158,7 +167,7 @@ func (f *Fluent) checkS3Object() error { client := s3.NewFromConfig(f.Awscfg) t := time.Now() targetBucket := f.LogBucketName - targetPrefix := fmt.Sprintf("fluentd/%s/%s/dt=%d%02d%02d", f.Env, f.Namespace, t.UTC().Year(), t.UTC().Month(), t.UTC().Day()) + targetPrefix := f.LogPath input := &s3.ListObjectsV2Input{ Bucket: aws.String(targetBucket), @@ -204,19 +213,19 @@ func (f *Fluent) createDeploymentObject() *appsv1.Deployment { deployment := &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ - Name: f.DeploymentName, + Name: f.ResourceName, }, Spec: appsv1.DeploymentSpec{ Replicas: util.Int32Ptr(int32(desireReplicacount)), Selector: &metav1.LabelSelector{ MatchLabels: map[string]string{ - "app": f.DeploymentName, + "app": f.ResourceName, }, }, Template: apiv1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: map[string]string{ - "app": f.DeploymentName, + "app": f.ResourceName, }, }, Spec: apiv1.PodSpec{ @@ -247,7 +256,7 @@ func (f *Fluent) createDeploymentObject() *appsv1.Deployment { { Key: "app", Operator: metav1.LabelSelectorOpIn, - Values: []string{f.DeploymentName}, + Values: []string{f.ResourceName}, }, }, }, diff --git a/cmd/fluent/fluent_test.go b/cmd/fluent/fluent_test.go index dd02184..316d5e8 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.TODO(), false, logger, chatwork, 3*time.Minute) + checker := cmd.NewChecker(context.Background(), false, logger, chatwork, 3*time.Minute) fluent, err := NewFluent(checker) if err != nil { t.Fatalf("NewFluent: %s", err) diff --git a/cmd/ingress/ingress_test.go b/cmd/ingress/ingress_test.go index 62f5850..27a40aa 100644 --- a/cmd/ingress/ingress_test.go +++ b/cmd/ingress/ingress_test.go @@ -20,7 +20,7 @@ func TestNewIngress(t *testing.T) { return logrus.NewEntry(logrus.New()) } chatwork := ¬ify.Chatwork{} - checker := cmd.NewChecker(context.TODO(), false, logger, chatwork, 3*time.Minute) + checker := cmd.NewChecker(context.Background(), false, logger, chatwork, 3*time.Minute) ingress, err := NewIngress(checker, false, "alb") 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.TODO(), true, logger, chatwork, 1*time.Minute), + Checker: cmd.NewChecker(context.Background(), true, logger, chatwork, 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.TODO(), false, logger, chatwork, 1*time.Minute), + Checker: cmd.NewChecker(context.Background(), false, logger, chatwork, 1*time.Minute), Namespace: "ingress-test", Clientset: k8sclient, NoDnsCheck: true,