Newer
Older
AnthosCertManager / cmd / controller / app / start.go
@Yangyang Xie Yangyang Xie on 22 Nov 2022 2 KB fix issues
package app

import (
	"fmt"

	options "gitbucket.jerxie.com/yangyangxie/AnthosCertManager/cmd/controller/app/options"
	_ "gitbucket.jerxie.com/yangyangxie/AnthosCertManager/pkg/controller/issuers"
	_ "gitbucket.jerxie.com/yangyangxie/AnthosCertManager/pkg/issuer/selfsigned"
	logf "gitbucket.jerxie.com/yangyangxie/AnthosCertManager/pkg/logs"
	"gitbucket.jerxie.com/yangyangxie/AnthosCertManager/pkg/util"
	"github.com/spf13/cobra"
	utilerrors "k8s.io/apimachinery/pkg/util/errors"
)

type AnthosCertManagerControllerOptions struct {
	ControllerOptions *options.ControllerOptions
}

func NewAnthosCertManagerControllerOptions() *AnthosCertManagerControllerOptions {
	o := &AnthosCertManagerControllerOptions{
		ControllerOptions: options.NewControllerOptions(),
	}
	return o
}

// NewCommandStartCertManagerController is a CLI handler for starting cert-manager
func NewCommandStartCertManagerController(stopCh <-chan struct{}) *cobra.Command {
	o := NewAnthosCertManagerControllerOptions()
	cmd := &cobra.Command{
		Use:   "cert-manager-controller",
		Short: fmt.Sprintf("Automated TLS controller for Kubernetes (%s) (%s)", util.AppVersion, util.AppGitCommit),
		Long: `
cert-manager is a Kubernetes addon to automate the management and issuance of
TLS certificates from various issuing sources.
It will ensure certificates are valid and up to date periodically, and attempt
to renew certificates at an appropriate time before expiry.`,

		RunE: func(cmd *cobra.Command, args []string) error {
			if err := o.Validate(args); err != nil {
				return fmt.Errorf("error validating options: %s", err)
			}

			logf.Log.V(logf.InfoLevel).Info("starting controller", "version", util.AppVersion, "git-commit", util.AppGitCommit)
			if err := o.RunCertManagerController(stopCh); err != nil {
				cmd.SilenceUsage = true // Don't display usage information when exiting because of an error
				return err
			}

			return nil
		},
		SilenceErrors: true, // Errors are already logged when calling cmd.Execute()
	}

	flags := cmd.Flags()
	o.ControllerOptions.AddFlags(flags)

	return cmd

}
func (o AnthosCertManagerControllerOptions) RunCertManagerController(stopCh <-chan struct{}) error {
	return Run(o.ControllerOptions, stopCh)
}

func (o AnthosCertManagerControllerOptions) Validate(args []string) error {
	errors := []error{}
	errors = append(errors, o.ControllerOptions.Validate())
	return utilerrors.NewAggregate(errors)
}