Skip to content

Commit

Permalink
Introduce log library
Browse files Browse the repository at this point in the history
  • Loading branch information
alenkacz committed Dec 29, 2020
1 parent 6cb6b6b commit 92be7d6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/alenkacz/cert-manager-verifier
go 1.15

require (
github.com/sirupsen/logrus v1.6.0
github.com/spf13/cobra v1.1.1
k8s.io/apimachinery v0.19.4
k8s.io/cli-runtime v0.19.4
Expand Down
18 changes: 14 additions & 4 deletions pkg/cmd/verify/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"os"
"github.com/sirupsen/logrus"
)

type Options struct {
Expand Down Expand Up @@ -51,6 +52,9 @@ func NewCmd() *cobra.Command {
}

func (o *Options) Execute() error {
logrus.SetOutput(o.Streams.Out)
logrus.SetFormatter(SimpleFormatter{})

if o.ConfigFlags.Namespace == nil {
cmn := "cert-manager"
o.ConfigFlags.Namespace = &cmn
Expand All @@ -69,19 +73,19 @@ func (o *Options) Execute() error {
}

deployments := verify.DeploymentDefinitionDefault()
_, _ = fmt.Fprintf(o.Streams.Out, "Waiting for following deployments in namespace %s:\n%s", deployments.Namespace, formatDeploymentNames(deployments.Names))
logrus.Infof("Waiting for following deployments in namespace %s:\n%s", deployments.Namespace, formatDeploymentNames(deployments.Names))
result := verify.DeploymentsReady(kubeClient, deployments)
_, _ = fmt.Fprintf(o.Streams.Out, formatDeploymentResult(result))
logrus.Infof(formatDeploymentResult(result))

if !allReady(result) {
return fmt.Errorf("FAILED! Not all deployments are ready.")
}
err = verify.WaitForTestCertificate(dynamicClient)
if err != nil {
_, _ = fmt.Fprintf(o.Streams.Out, "error when waiting for certificate to be ready: %v", err)
logrus.Infof("error when waiting for certificate to be ready: %v", err)
return err
}
_, _ = fmt.Fprint(o.Streams.Out, "ヽ(•‿•)ノ Cert-manager is READY!")
logrus.Info("ヽ(•‿•)ノ Cert-manager is READY!")
return nil
}

Expand Down Expand Up @@ -114,3 +118,9 @@ func formatDeploymentResult(result []verify.DeploymentResult) string {
}
return formattedResult
}

type SimpleFormatter struct {}

func (SimpleFormatter) Format(entry *logrus.Entry) ([]byte, error) {
return []byte(entry.Message), nil
}
7 changes: 4 additions & 3 deletions pkg/verify/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package verify
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
"strings"

"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -36,7 +37,7 @@ var namespace = &unstructured.Unstructured{
func WaitForTestCertificate(dynamicClient dynamic.Interface) error {
cert := certificate("cert-manager-test", group, version)
resources := []*unstructured.Unstructured{namespace, issuer("cert-manager-test", group, version), cert}
defer cleanupTestResources(dynamicClient, resources)
// defer cleanupTestResources(dynamicClient, resources)

for _, res := range resources {
err := createResource(dynamicClient, res)
Expand Down Expand Up @@ -91,7 +92,7 @@ func createResource(dynamicClient dynamic.Interface, resource *unstructured.Unst
Resource: fmt.Sprintf("%ss", strings.ToLower(gvk.Kind)), // since we know what kinds are we dealing with here, this is OK
}).Namespace(resource.GetNamespace()).Create(context.TODO(), resource, metav1.CreateOptions{})
if errors.IsAlreadyExists(err) {
fmt.Printf("resource %s already exists\n", resource.GetName())
logrus.Debugf("resource %s already exists\n", resource.GetName())
} else if err != nil {
return fmt.Errorf("error when creating resource %s/%s. %v", resource.GetName(), resource.GetNamespace(), err)
}
Expand All @@ -106,7 +107,7 @@ func deleteResource(dynamicClient dynamic.Interface, resource *unstructured.Unst
Resource: fmt.Sprintf("%ss", strings.ToLower(gvk.Kind)), // since we know what kinds are we dealing with here, this is OK
}).Namespace(resource.GetNamespace()).Delete(context.TODO(), resource.GetName(), metav1.DeleteOptions{})
if errors.IsNotFound(err) {
fmt.Printf("resource %s already deleted\n", resource.GetName())
logrus.Debugf("resource %s already deleted\n", resource.GetName())
} else if err != nil {
return fmt.Errorf("error when creating resource %s/%s. %v", resource.GetName(), resource.GetNamespace(), err)
}
Expand Down

0 comments on commit 92be7d6