Skip to content

Commit

Permalink
Make it usable as a library, namespace can be configured
Browse files Browse the repository at this point in the history
  • Loading branch information
alenkacz committed Jan 3, 2021
1 parent 0186d2c commit 3ef4a14
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 42 deletions.
58 changes: 18 additions & 40 deletions pkg/cmd/verify/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,30 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
)

const defaultTimeout = 2 * time.Minute
var defaultInstallationNamespace = "cert-manager"

type Options struct {
ConfigFlags *genericclioptions.ConfigFlags
Streams *genericclioptions.IOStreams
DebugLogs bool
CertManagerNamespace string
}

func NewOptions() *Options {
return &Options{
opt := &Options{
ConfigFlags: genericclioptions.NewConfigFlags(true),
Streams: &genericclioptions.IOStreams{
In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr,
},
}
// this is necessary so that the namespace flag is not inherited from ConfigFlags and we can redefine it
opt.ConfigFlags.Namespace = nil
return opt
}

func NewCmd() *cobra.Command {
Expand All @@ -49,11 +52,11 @@ func NewCmd() *cobra.Command {
}

rootCmd.Flags().BoolVar(&options.DebugLogs, "debug", false, "If true, will print out debug logs (default false)")
rootCmd.Flags().StringVarP(&options.CertManagerNamespace, "namespace", "n", defaultInstallationNamespace, "Namespace in which cert-manager is installed")

options.ConfigFlags.AddFlags(rootCmd.Flags())
rootCmd.SetOut(options.Streams.Out)
rootCmd.SilenceUsage = true
// TODO add flag to configure cm namespace
// TODO add flag to specify CM version and verify version
// TODO make timeout configurable

Expand All @@ -70,58 +73,33 @@ func (o *Options) Execute() error {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()

if o.ConfigFlags.Namespace == nil {
cmn := "cert-manager"
o.ConfigFlags.Namespace = &cmn
}
config, err := o.ConfigFlags.ToRESTConfig()
if err != nil {
return fmt.Errorf("unable to get kubernetes rest config: %v", err)
}
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf("unable to get kubernetes client: %v", err)
}
dynamicClient, err := dynamic.NewForConfig(config)

logrus.Infof("Waiting for deployments in namespace %s:\n", o.CertManagerNamespace)
result, err := verify.Verify(ctx, config, &verify.Options{
o.CertManagerNamespace,
})
if err != nil {
return fmt.Errorf("unable to get kubernetes client: %v", err)
return err
}

deployments := verify.DeploymentDefinitionDefault()
logrus.Infof("Waiting for following deployments in namespace %s:\n%s", deployments.Namespace, formatDeploymentNames(deployments.Names))
result := verify.DeploymentsReady(ctx, kubeClient, deployments)
logrus.Infof(formatDeploymentResult(result))
logrus.Infof(formatDeploymentResult(result.DeploymentsResult))

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

func allReady(result []verify.DeploymentResult) bool {
for _, r := range result {
if !r.Ready {
return false
}
}
return true
}

func formatDeploymentNames(names []string) string {
var formattedNames string
for _, n := range names {
formattedNames += fmt.Sprintf("\t- %s\n", n)
}
return formattedNames

}

func formatDeploymentResult(result []verify.DeploymentResult) string {
var formattedResult string
for _, r := range result {
Expand Down
4 changes: 2 additions & 2 deletions pkg/verify/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ type DeploymentDefinition struct {
Names []string
}

func DeploymentDefinitionDefault() DeploymentDefinition {
func DeploymentDefinitionDefault(namespace string) DeploymentDefinition {
// TODO make sure these Names work also with helm chart installation
// TODO make sure we support cert-manager that does not have all these deployments
return DeploymentDefinition{
Namespace: "cert-manager",
Namespace: namespace,
Names: []string{"cert-manager", "cert-manager-cainjector", "cert-manager-webhook"},
}
}
Expand Down
69 changes: 69 additions & 0 deletions pkg/verify/verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package verify

import (
"context"
"fmt"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

type VerifyResult struct {
Success bool

DeploymentsSuccess bool
CertificateSuccess bool

DeploymentsResult []DeploymentResult
CertificateError error
}

type CertificateResult struct {
Success bool
Error error
}

type Options struct {
CertManagerNamespace string
}

func Verify(ctx context.Context, config *rest.Config, options *Options) (*VerifyResult, error) {
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("unable to get kubernetes client: %v", err)
}
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("unable to get kubernetes client: %v", err)
}

deployments := DeploymentDefinitionDefault(options.CertManagerNamespace)
deploymentResult := DeploymentsReady(ctx, kubeClient, deployments)

result := &VerifyResult{
Success: false,
DeploymentsResult: deploymentResult,
}

if !allReady(deploymentResult) {
return result, nil
}
result.DeploymentsSuccess = true
err = WaitForTestCertificate(ctx, dynamicClient)
if err != nil {
result.CertificateError = err
} else {
result.CertificateSuccess = true
}

return result, nil
}

func allReady(result []DeploymentResult) bool {
for _, r := range result {
if !r.Ready {
return false
}
}
return true
}

0 comments on commit 3ef4a14

Please sign in to comment.