Skip to content

Commit

Permalink
Merge pull request #19 from chatwork/implement_sigterm
Browse files Browse the repository at this point in the history
Implementation SIGTERM
  • Loading branch information
cw-sakamoto authored Dec 7, 2023
2 parents d900e14 + cd2886e commit 149689d
Show file tree
Hide file tree
Showing 15 changed files with 262 additions and 257 deletions.
89 changes: 39 additions & 50 deletions cmd/cert-manager/cert-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"os"
"strconv"
"time"

cmapiv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
Expand All @@ -14,20 +13,21 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/chatwork/kibertas/cmd"
"github.com/chatwork/kibertas/config"
"github.com/chatwork/kibertas/util"
"github.com/chatwork/kibertas/util/k8s"
"github.com/chatwork/kibertas/util/notify"
"github.com/sirupsen/logrus"
)

type CertManager struct {
*cmd.Checker
CertName string
Client client.Client
Namespace string
CertName string
Clientset *kubernetes.Clientset
Client client.Client
}

type certificates struct {
Expand All @@ -36,67 +36,56 @@ type certificates struct {
certificate *cmapiv1.Certificate
}

func NewCertManager(debug bool, logger func() *logrus.Entry, chatwork *notify.Chatwork) (*CertManager, error) {
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))
logger().Infof("cert-manager check application namespace: %s", namespace)
chatwork.AddMessage(fmt.Sprintf("cert-manager check application namespace: %s\n", namespace))
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"
timeout := 20

if v := os.Getenv("CERT_NAME"); v != "" {
certName = v
}
scheme := runtime.NewScheme()
_ = cmapiv1.AddToScheme(scheme)

var err error
if v := os.Getenv("CHECK_TIMEOUT"); v != "" {
timeout, err = strconv.Atoi(v)
if err != nil {
logger().Errorf("strconv.Atoi: %s", err)
return nil, err
}
}

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

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

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

return &CertManager{
Checker: cmd.NewChecker(namespace, k8sclientset, debug, logger, chatwork, time.Duration(timeout)*time.Minute),
CertName: certName,
Client: k8sclient,
Checker: checker,
Namespace: namespace,
CertName: certName,
Clientset: k8sclientset,
Client: k8sclient,
}, nil
}

func (c *CertManager) Check() error {
cert := c.createCertificateObject()

c.Chatwork.AddMessage("cert-manager check start\n")
defer c.Chatwork.Send()

cert := c.createCertificateObject()

if err := c.createResources(cert); err != nil {
if err := c.cleanUpResources(cert); err != nil {
c.Chatwork.AddMessage(fmt.Sprintf("Error Delete Resources: %s\n", err))
}
return err
}
defer func() {
if err := c.cleanUpResources(cert); err != nil {
c.Chatwork.AddMessage(fmt.Sprintf("Error Delete Resources: %s\n", err))
}
}()
if err := c.createResources(cert); err != nil {
return err
}

c.Chatwork.AddMessage("cert-manager check finished\n")
return nil
Expand All @@ -105,10 +94,12 @@ func (c *CertManager) Check() error {
func (c *CertManager) createResources(cert certificates) error {
k := k8s.NewK8s(c.Namespace, c.Clientset, c.Logger)

if err := k.CreateNamespace(&apiv1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: c.Namespace,
}}); err != nil {
if err := k.CreateNamespace(
c.Ctx,
&apiv1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: c.Namespace,
}}); err != nil {
c.Logger().Error("Error create namespace:", err)
c.Chatwork.AddMessage(fmt.Sprint("Error create namespace:", err))
return err
Expand Down Expand Up @@ -232,14 +223,14 @@ func (c *CertManager) createCertificateObject() certificates {
func (c *CertManager) createCert(cert certificates) error {
c.Logger().Infoln("Create RootCA:", cert.rootCA.ObjectMeta.Name)
c.Chatwork.AddMessage(fmt.Sprintf("Create RootCA: %s\n", cert.rootCA.ObjectMeta.Name))
err := c.Client.Create(context.Background(), cert.rootCA)
err := c.Client.Create(c.Ctx, cert.rootCA)
if err != nil {
return err
}

secretClient := c.Clientset.CoreV1().Secrets(c.Namespace)

err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, c.Timeout, true, func(ctx context.Context) (bool, 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)
Expand All @@ -248,41 +239,39 @@ func (c *CertManager) createCert(cert certificates) error {
c.Logger().Infof("Created secret:%s at %s", secret.Name, secret.CreationTimestamp)
return true, nil
})

if err != nil {
c.Logger().Error("Timed out waiting for RootCA secret to be ready:", err)
c.Chatwork.AddMessage(fmt.Sprintf("Timed out waiting for RootCA secret to be ready: %s\n", err))
return err
return fmt.Errorf("waiting for RootCA secret to be ready: %w", err)
}

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

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

if err != nil {
return err
}

err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, c.Timeout, true, func(ctx context.Context) (bool, 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\n", cert.certificate.Spec.SecretName)
c.Logger().WithError(err).Errorf("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)
return true, nil
})

if err != nil {
c.Logger().Error("Timed out waiting for Certificate secret to be ready:", err)
c.Chatwork.AddMessage(fmt.Sprintf("Timed out waiting for Certificate secret to be ready: %s\n", err))
return err
return fmt.Errorf("waiting for Certificate secret to be ready: %w", err)
}

return nil
Expand Down
12 changes: 8 additions & 4 deletions cmd/cert-manager/cert-manager_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package certmanager

import (
"context"
"fmt"
"testing"
"time"
Expand All @@ -23,7 +24,8 @@ func TestNewCertManager(t *testing.T) {
return logrus.NewEntry(logrus.New())
}
chatwork := &notify.Chatwork{}
ingress, err := NewCertManager(true, logger, chatwork)
checker := cmd.NewChecker(context.Background(), false, logger, chatwork, 3*time.Minute)
ingress, err := NewCertManager(checker)
if err != nil {
t.Fatalf("NewCertManager: %s", err)
}
Expand Down Expand Up @@ -57,9 +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(namespace, k8sclientset, true, logger, chatwork, 3*time.Minute),
CertName: "sample",
Client: k8sclient,
Checker: cmd.NewChecker(context.Background(), true, logger, chatwork, 3*time.Minute),
Namespace: namespace,
CertName: "sample",
Clientset: k8sclientset,
Client: k8sclient,
}

err = cm.Check()
Expand Down
27 changes: 13 additions & 14 deletions cmd/checker.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
package cmd

import (
"context"
"time"

"github.com/chatwork/kibertas/util/notify"
"github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
)

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

func NewChecker(namespace string, clientset *kubernetes.Clientset, 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, timeout time.Duration) *Checker {
logger().Info("Checker timeout: ", timeout)

return &Checker{
Namespace: namespace,
Clientset: clientset,
Debug: debug,
Logger: logger,
Chatwork: chatwork,
Timeout: timeout,
Ctx: ctx,
Debug: debug,
Logger: logger,
Chatwork: chatwork,
Timeout: timeout,
}
}
Loading

0 comments on commit 149689d

Please sign in to comment.