Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ingress check , cert-manager test #7

Merged
merged 7 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ jobs:
go-version: '1.21.3'
check-latest: true
- name: Creat kind
run: make create-kind
run: make ci:enable:k8s
- name: Apply Cert Manager
run: make apply-cert-manager
- name: Apply Ingress-Nginx
run: make apply-ingress-nginx
- name: Run tests
run: make test
golangci:
Expand Down
31 changes: 23 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ KIND_VERSION = 0.20.0
KUBERNETES_VERSION = 1.27.3
KIND_NODE_HASH = 3966ac761ae0136263ffdb6cfd4db23ef8a83cba8a463690e98317add2c9ba72
CERT_MANAGER_VERSION = 1.13.2
INGRESS_NGINX_VERSION = 1.9.4

GOLANGCI_LINT_VERSION=1.55.1
TAG ?= $(shell git describe --tags --abbrev=0 HEAD || echo dev)
Expand Down Expand Up @@ -37,18 +38,22 @@ goreleaser-snapshot:

.PHONY: create-kind
create-kind:
kind create cluster --image kindest/node:v$(KUBERNETES_VERSION)@sha256:$(KIND_NODE_HASH) --wait 3m;

.PHONY: ci\:enable\:k8s
ci\:enable\:k8s:
@mkdir -p .bin/
@if [ ! -f " ./.bin/kind" ]; then \
curl -sSL -o ./.bin/kind https://github.com/kubernetes-sigs/kind/releases/download/v$(KIND_VERSION)/kind-linux-amd64; \
chmod +x ./.bin/kind; \
fi
@sudo cp ./.bin/kind /usr/local/bin/kind;

#@if [ ! -f "./.bin/kubectl" ]; then \
# curl -sSL -o ./.bin/kubectl https://storage.googleapis.com/kubernetes-release/release/v$(KUBERNETES_VERSION)/bin/linux/amd64/kubectl; \
# chmod +x ./.bin/kubectl; \
#fi
#@sudo cp ./.bin/kubectl /usr/local/bin/kubectl;
@if [ ! -f "./.bin/kubectl" ]; then \
curl -sSL -o ./.bin/kubectl https://storage.googleapis.com/kubernetes-release/release/v$(KUBERNETES_VERSION)/bin/linux/amd64/kubectl; \
chmod +x ./.bin/kubectl; \
fi
@sudo cp ./.bin/kubectl /usr/local/bin/kubectl;
kind create cluster --image kindest/node:v$(KUBERNETES_VERSION)@sha256:$(KIND_NODE_HASH) --wait 3m;

.PHONY: delete-kind
Expand All @@ -61,13 +66,23 @@ delete-kind:
#@sudo cp ./.bin/kind /usr/local/bin/kind;
kind delete cluster

.PHONY: apply-ingress-nginx
apply-ingress-nginx:
@kubectl apply --wait=true -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v${INGRESS_NGINX_VERSION}/deploy/static/provider/cloud/deploy.yaml
@sleep 2
@kubectl -n ingress-nginx wait deploy -l app.kubernetes.io/instance=ingress-nginx --for=condition=available --timeout=60s

.PHONY: delete-ingress-nginx
delete-ingress-nginx:
@kubectl delete --wait=true -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v${INGRESS_NGINX_VERSION}/deploy/static/provider/cloud/deploy.yaml

.PHONY: apply-cert-manager
apply-cert-manager:
@kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v${CERT_MANAGER_VERSION}/cert-manager.yaml
@sleep 90
@kubectl apply --wait=true -f https://github.com/cert-manager/cert-manager/releases/download/v${CERT_MANAGER_VERSION}/cert-manager.yaml
@sleep 2
@kubectl -n cert-manager wait deploy -l app.kubernetes.io/instance=cert-manager --for=condition=available --timeout=60s
@kubectl apply -f ./manifests/ClusterIssuer-SelfSigned.yaml

.PHONY: delete-cert-manager
delete-cert-manager:
@kubectl delete -f ./ClusterIssuer-SelfSigned.yaml
@kubectl delete -f https://github.com/cert-manager/cert-manager/releases/download/v${CERT_MANAGER_VERSION}/cert-manager.yaml
69 changes: 69 additions & 0 deletions cmd/cert-manager/cert-manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package certmanager

import (
"fmt"
"testing"
"time"

cmapiv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
"github.com/chatwork/kibertas/util"
"github.com/chatwork/kibertas/util/notify"
"github.com/sirupsen/logrus"

"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/chatwork/kibertas/cmd"
"github.com/chatwork/kibertas/config"
)

func TestNewCertManager(t *testing.T) {
t.Parallel()
logger := func() *logrus.Entry {
return logrus.NewEntry(logrus.New())
}
chatwork := &notify.Chatwork{}
ingress, err := NewCertManager(true, logger, chatwork)
if err != nil {
t.Fatalf("NewCertManager: %s", err)
}

if ingress == nil {
t.Error("Expected certManager instance, got nil")
}
}

func TestCheck(t *testing.T) {
t.Parallel()
logger := func() *logrus.Entry {
return logrus.NewEntry(logrus.New())
}

k8sclientset, err := config.NewK8sClientset()
if err != nil {
t.Fatalf("NewK8sClientset: %s", err)
}

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

k8sclient, err := config.NewK8sClient(client.Options{Scheme: scheme})
if err != nil {
t.Fatalf("NewK8sClient: %s", err)
}

chatwork := &notify.Chatwork{ApiToken: "token", RoomId: "test", Logger: logger}

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),
CertName: "sample",
Client: k8sclient,
}

err = cm.Check()
if err != nil {
t.Fatalf("Expected No Error, but got error: %s", err)
}
}
12 changes: 9 additions & 3 deletions cmd/datadog-agent/datadog-agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

func TestNewDatadogAgent(t *testing.T) {
t.Parallel()
logger := func() *logrus.Entry {
return logrus.NewEntry(logrus.New())
}
Expand All @@ -28,6 +29,7 @@ func TestNewDatadogAgent(t *testing.T) {
}

func TestCheck(t *testing.T) {
t.Parallel()
logger := func() *logrus.Entry {
return logrus.NewEntry(logrus.New())
}
Expand Down Expand Up @@ -69,9 +71,13 @@ func TestCheck(t *testing.T) {
}

os.Setenv("CLUSTER_NAME", "test")
datadogAgent, err = NewDatadogAgent(true, logger, chatwork)
if err != nil {
t.Fatalf("NewDatadogAgent: %s", err)

datadogAgent = &DatadogAgent{
Checker: cmd.NewChecker("test", k8sclient, true, logger, chatwork),
ApiKey: "test",
AppKey: "test",
ClusterName: "test",
WaitTime: 1 * time.Second,
}

err = datadogAgent.Check()
Expand Down
24 changes: 24 additions & 0 deletions cmd/fluent/fluent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fluent

import (
"testing"

"github.com/chatwork/kibertas/util/notify"

"github.com/sirupsen/logrus"
)

func TestNewFluent(t *testing.T) {
logger := func() *logrus.Entry {
return logrus.NewEntry(logrus.New())
}
chatwork := &notify.Chatwork{}
fluent, err := NewFluent(true, logger, chatwork)
if err != nil {
t.Fatalf("NewFluent: %s", err)
}

if fluent == nil {
t.Error("Expected fluent instance, got nil")
}
}
91 changes: 91 additions & 0 deletions cmd/ingress/ingressCheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package ingress

import (
"fmt"
"testing"
"time"

"github.com/chatwork/kibertas/cmd"
"github.com/chatwork/kibertas/config"
"github.com/chatwork/kibertas/util"
"github.com/chatwork/kibertas/util/notify"

"github.com/sirupsen/logrus"
)

func TestNewIngress(t *testing.T) {
t.Parallel()
logger := func() *logrus.Entry {
return logrus.NewEntry(logrus.New())
}
chatwork := &notify.Chatwork{}
ingress, err := NewIngress(true, logger, chatwork, false, "alb")
if err != nil {
t.Fatalf("NewIngress: %s", err)
}

if ingress == nil {
t.Error("Expected ingress instance, got nil")
}
}

func TestCheck(t *testing.T) {
t.Parallel()
logger := func() *logrus.Entry {
return logrus.NewEntry(logrus.New())
}

k8sclient, err := config.NewK8sClientset()
if err != nil {
t.Fatalf("NewK8sClientset: %s", err)
}

chatwork := &notify.Chatwork{ApiToken: "token", RoomId: "test", Logger: logger}

now := time.Now()

namespace := fmt.Sprintf("ingress-test-%d%02d%02d-%s", now.Year(), now.Month(), now.Day(), util.GenerateRandomString(5))

// kindとingress-nginxがある前提
// レコードは作れないのでNoDnsCheckをtrueにする
ingress := &Ingress{
Checker: cmd.NewChecker(namespace, k8sclient, true, logger, chatwork),
NoDnsCheck: true,
IngressClassName: "nginx",
ResourceName: "sample",
ExternalHostname: "sample.example.com",
}

err = ingress.Check()
if err != nil {
t.Fatalf("Expected No Error, but got error: %s", err)
}
}

func TestCheckDNSRecord(t *testing.T) {
logger := func() *logrus.Entry {
return logrus.NewEntry(logrus.New())
}

k8sclient, err := config.NewK8sClientset()
if err != nil {
t.Fatalf("NewK8sClientset: %s", err)
}

chatwork := &notify.Chatwork{ApiToken: "token", RoomId: "test", Logger: logger}

// kindとingress-nginxがある前提
// レコードは作れないのでNoDnsCheckをtrueにする
ingress := &Ingress{
Checker: cmd.NewChecker("test", k8sclient, true, logger, chatwork),
NoDnsCheck: true,
IngressClassName: "nginx",
ResourceName: "sample",
ExternalHostname: "go.chatwork.com",
}

err = ingress.checkDNSRecord()
if err != nil {
t.Fatalf("Expected No Error, but got error: %s", err)
}
}
35 changes: 18 additions & 17 deletions util/k8s/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,27 +168,28 @@ func (k *K8s) CreateIngress(ingress *networkingv1.Ingress) error {
return err
}

err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 5*time.Minute, false, func(ctx context.Context) (bool, error) {
ingress, err := ingressClient.Get(ctx, ingress.Name, metav1.GetOptions{})
if err != nil {
return false, err
}
if *ingress.Spec.IngressClassName == "alb" {
err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 5*time.Minute, false, func(ctx context.Context) (bool, error) {
ingress, err := ingressClient.Get(ctx, ingress.Name, metav1.GetOptions{})
if err != nil {
return false, err
}

for _, address := range ingress.Status.LoadBalancer.Ingress {
if address.Hostname != "" {
k.logger().Infof("Ingress is now available at Hostname: %s", address.Hostname)
return true, nil
for _, address := range ingress.Status.LoadBalancer.Ingress {
if address.Hostname != "" {
k.logger().Infof("Ingress is now available at Hostname: %s", address.Hostname)
return true, nil
}
}
}
k.logger().Infoln("Ingress is not yet available, retrying...")
return false, nil
})
k.logger().Infoln("Ingress is not yet available, retrying...")
return false, nil
})

if err != nil {
k.logger().Error("Timed out waiting for ingress to be ready:", err)
return err
if err != nil {
k.logger().Error("Timed out waiting for ingress to be ready:", err)
return err
}
}

return nil
}

Expand Down
Loading