Newer
Older
AnthosCertManager / cmd / controller / app / options / options.go
package options

import (
	"fmt"
	"strings"

	"gitbucket.jerxie.com/yangyangxie/AnthosCertManager/pkg/controller/certificaterequests/selfsigned"
	"gitbucket.jerxie.com/yangyangxie/AnthosCertManager/pkg/controller/certificates/issuing"
	"gitbucket.jerxie.com/yangyangxie/AnthosCertManager/pkg/controller/certificates/keymanager"
	"gitbucket.jerxie.com/yangyangxie/AnthosCertManager/pkg/controller/certificates/requestmanager"
	"gitbucket.jerxie.com/yangyangxie/AnthosCertManager/pkg/controller/certificates/trigger"
	"gitbucket.jerxie.com/yangyangxie/AnthosCertManager/pkg/controller/issuers"
	"github.com/spf13/pflag"
	"k8s.io/apimachinery/pkg/util/sets"
)

const (
	defaultAPIServerHost = ""
	defaultKubeconfig    = ""

	defaultClusterResourceNamespace = "kube-system"
	defaultNamespace                = ""
)

type ControllerOptions struct {
	APIServerHost            string
	Kubeconfig               string
	ClusterResourceNamespace string
	Namespace                string

	MetricsBindAddress     string
	HealthProbeBindAddress string
	LeaderElection         bool
	Controllers            []string
}

var (
	allControllers = []string{
		// certificate controllers
		issuing.ControllerName,
		issuers.ControllerName,
		trigger.ControllerName,
		requestmanager.ControllerName,
		keymanager.ControllerName,
		selfsigned.CRControllerName,
	}

	defaultEnabledControllers = []string{
		issuing.ControllerName,
		issuers.ControllerName,
		trigger.ControllerName,
		requestmanager.ControllerName,
		keymanager.ControllerName,
		selfsigned.CRControllerName,
	}
)

func NewControllerOptions() *ControllerOptions {
	return &ControllerOptions{
		APIServerHost:            defaultAPIServerHost,
		ClusterResourceNamespace: defaultClusterResourceNamespace,
		Controllers:              defaultEnabledControllers,
		Namespace:                defaultNamespace,
	}

}

func getStrSlice(s string) []string {
	var arr = strings.Split(s, ",")
	ret := make([]string, len(arr))
	for _, str := range arr {
		if v := strings.TrimSpace(str); v != "" {
			ret = append(ret, v)
		}

	}
	return ret
}

func (s *ControllerOptions) AddFlags(fs *pflag.FlagSet) {
	fs.StringVar(&s.APIServerHost, "master", defaultAPIServerHost, ""+
		"Optional apiserver host address to connect to. If not specified, autoconfiguration "+
		"will be attempted.")
	fs.StringVar(&s.Kubeconfig, "kubeconfig", defaultKubeconfig, ""+
		"Paths to a kubeconfig. Only required if out-of-cluster.")

	fs.StringVar(&s.HealthProbeBindAddress, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
	fs.StringVar(&s.HealthProbeBindAddress, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
	fs.BoolVar(&s.LeaderElection, "leader-elect", false,
		"Enable leader election for controller manager. "+
			"Enabling this will ensure there is only one active controller manager.")
	var controllerSet string
	fs.StringVar(&controllerSet, "controllers", "*", fmt.Sprintf(""+
		"A list of controllers to enable. '--controllers=*' enables all "+
		"on-by-default controllers, '--controllers=foo' enables just the controller "+
		"named 'foo', '--controllers=*,-foo' disables the controller named "+
		"'foo'.\nAll controllers: %s",
		strings.Join(allControllers, ", ")))
	s.Controllers = getStrSlice(controllerSet)

	fs.StringVar(&s.ClusterResourceNamespace, "cluster-resource-namespace", defaultClusterResourceNamespace, ""+
		"Namespace to store resources owned by cluster scoped resources such as ClusterIssuer in. "+
		"This must be specified if ClusterIssuers are enabled.")
	fs.StringVar(&s.Namespace, "namespace", defaultNamespace, ""+
		"If set, this limits the scope of cert-manager to a single namespace and ClusterIssuers are disabled. "+
		"If not specified, all namespaces will be watched")

}

func (o *ControllerOptions) EnabledControllers() sets.String {
	enabled := sets.NewString()

	for _, controller := range o.Controllers {
		switch {
		// Enable all controllers
		case controller == "*":
			enabled = enabled.Insert(defaultEnabledControllers...)
		default:
			enabled = enabled.Insert(controller)

		}
	}
	return enabled
}

// TODO: Implement validation
func (o *ControllerOptions) Validate() error {
	return nil
}