package ktest import ( "fmt" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/manager" "edge-infra.dev/pkg/k8s/runtime/controller" "edge-infra.dev/test/f2/x/ktest/envtest" ) // options for creating a ktest framework type options struct { konfigkonnector bool certmgr bool skipNamespaceCreation bool skipNamespaceDeletion bool metricsAddress string mgrCreator func(...controller.Option) (manager.Manager, error) gracefulTimeout string envtestOpts []envtest.Option clientScheme *runtime.Scheme } // Option is a ktest framework extension option. The behavior of individual // options may be modified by providing test flags exposed by this package. type Option func(*options) // SkipNamespaceCreation will make the framework skip creation of a namespace // for each test case. Useful for tests that don't operate against any // Namespaced resources. func SkipNamespaceCreation() Option { return func(o *options) { o.skipNamespaceCreation = true } } // SkipNamespaceDeletion will make the framework skip deletion of a namespace // for each test case. func SkipNamespaceDeletion() Option { return func(o *options) { o.skipNamespaceDeletion = true } } // WithKonfigKonnector controls whether or not K8s config connector is installed // and configured for the test run. func WithKonfigKonnector() Option { return func(o *options) { o.konfigkonnector = true } } // WithCertManager ensures that cert-manager is installed for the test run. func WithCertManager() Option { return func(o *options) { o.certmgr = true } } // WithCtrlManager sets up the framework for testing a controller-runtime Manager func WithCtrlManager(fn func(...controller.Option) (manager.Manager, error)) Option { return func(o *options) { o.mgrCreator = fn } } // WithGracefulTimeout allows an optional timeout duration to be passed during ktest setup. // The desired timeout is passed as a string and parsed using time.ParseDuration. // e.g. WithGracefulTimeout("1m30s") // Passing "0" disables graceful shutdown while passing a negative value // allows the graceful shutdown to complete with no timeout. // Defaults to defaultGracefulShutdownPeriod (30s) func WithGracefulTimeout(timeout string) Option { return func(o *options) { o.gracefulTimeout = timeout } } func WithEnvtestOptions(opts ...envtest.Option) Option { return func(o *options) { o.envtestOpts = opts } } // WithMetricsAddress enables the manager's metrics server. // The passed port will be used if available. // Otherwise, a random available port will be used. // Passing 0 will allow a random available port to be used by default. func WithMetricsAddress(addr int) Option { return func(o *options) { o.metricsAddress = fmt.Sprintf(":%d", addr) } } // WithScheme sets the Scheme that will be used when creating the // [K8s.Client]. If it is not set, the Scheme provided by [K8s.Manager] // will be used. Ie, WithScheme takes precedence over [WithCtrlManager] when // setting the [client.Client] Scheme func WithScheme(scheme *runtime.Scheme) Option { return func(o *options) { o.clientScheme = scheme } } func makeOptions(opts ...Option) *options { options := &options{} for _, o := range opts { o(options) } return options }