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

import (
	"fmt"
	"strings"

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

type ControllerOptions struct {
	MetricsBindAddress     string
	HealthProbeBindAddress string
	LeaderElection         bool
	Controllers            []string
}

var (
	allControllers = []string{
		// certificate controllers
		issuing.ControllerName,
		issuers.ControllerName,
	}

	defaultEnabledControllers = []string{
		issuing.ControllerName,
		issuers.ControllerName,
	}
)

func NewControllerOptions() *ControllerOptions {
	return &ControllerOptions{
		Controllers: defaultEnabledControllers,
	}

}

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.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)
}

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
}