Skip to content

Commit

Permalink
Merge pull request #20 from chatwork/update_parameter
Browse files Browse the repository at this point in the history
change some parameter name
  • Loading branch information
cw-sakamoto authored Dec 7, 2023
2 parents 149689d + 8823205 commit 530cf77
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 63 deletions.
38 changes: 19 additions & 19 deletions cmd/cert-manager/cert-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand All @@ -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
}

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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",
},
},
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/cert-manager/cert-manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 10 additions & 10 deletions cmd/cluster-autoscaler/clusterAutoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type ClusterAutoscaler struct {
*cmd.Checker
Clientset *kubernetes.Clientset
Namespace string
DeploymentName string
ResourceName string
ReplicaCount int
NodeLabelKey string
NodeLabelValue string
Expand All @@ -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 != "" {
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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{
Expand Down Expand Up @@ -188,7 +188,7 @@ func (c *ClusterAutoscaler) createDeploymentObject() *appsv1.Deployment {
{
Key: "app",
Operator: metav1.LabelSelectorOpIn,
Values: []string{c.DeploymentName},
Values: []string{c.ResourceName},
},
},
},
Expand Down
59 changes: 34 additions & 25 deletions cmd/fluent/fluent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 != "" {
Expand All @@ -57,20 +59,27 @@ 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)
return nil, err
}

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
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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),
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -247,7 +256,7 @@ func (f *Fluent) createDeploymentObject() *appsv1.Deployment {
{
Key: "app",
Operator: metav1.LabelSelectorOpIn,
Values: []string{f.DeploymentName},
Values: []string{f.ResourceName},
},
},
},
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.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)
Expand Down
6 changes: 3 additions & 3 deletions cmd/ingress/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestNewIngress(t *testing.T) {
return logrus.NewEntry(logrus.New())
}
chatwork := &notify.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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 530cf77

Please sign in to comment.