Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to select supported devices in the operator #599

Merged
merged 7 commits into from
Apr 12, 2021
Prev Previous commit
Next Next commit
operator: Add --device command line to operator
Add --device command line to operator's main.go which defines
the controllers/webhooks to set up.

Signed-off-by: Oleg Zhurakivskyy <[email protected]>
  • Loading branch information
ozhuraki committed Apr 8, 2021
commit 2d27602ed03e1ba091c80babaa20d5ffc97b2924
95 changes: 70 additions & 25 deletions cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package main

import (
"flag"
"fmt"
"os"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -54,18 +56,58 @@ func init() {

type devicePluginControllerAndWebhook map[string](func(ctrl.Manager, string, bool) error)

type flagList []string

var supportedDevices = flagList{"dsa", "fpga", "gpu", "qat", "sgx"}
var devices flagList

func (flag *flagList) String() string {
return strings.Join(*flag, ", ")
}

func (flag *flagList) Set(value string) error {
if !contains(supportedDevices, value) {
setupLog.Error(nil, fmt.Sprintf("Unsupported device: %s", value))
os.Exit(1)
}

if contains(devices, value) {
setupLog.Error(nil, fmt.Sprintf("Duplicate device: %s", value))
os.Exit(1)
}

*flag = append(*flag, value)
return nil
}

func contains(arr []string, val string) bool {
for _, s := range arr {
mythi marked this conversation as resolved.
Show resolved Hide resolved
if s == val {
return true
}
}
return false
}

func main() {
var metricsAddr string
var devicePluginNamespace string
var enableLeaderElection bool
var pm *patcher.PatcherManager

ctrl.SetLogger(klogr.New())

flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&devicePluginNamespace, "deviceplugin-namespace", metav1.NamespaceSystem, "The namespace where deviceplugin daemonsets are created")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.Var(&devices, "devices", "Device(s) to set up.")
flag.Parse()

ctrl.SetLogger(klogr.New())
if len(devices) == 0 {
devices = supportedDevices
}

setupControllerAndWebhook := devicePluginControllerAndWebhook{
"dsa": dsa.SetupReconciler,
Expand Down Expand Up @@ -95,40 +137,43 @@ func main() {

withWebhook := true

// TODO(mythi): add a StringVar flag to select which controllers to enable
for _, device := range []string{"dsa", "fpga", "gpu", "qat", "sgx"} {
for _, device := range devices {
if err = setupControllerAndWebhook[device](mgr, ns, withWebhook); err != nil {
bart0sh marked this conversation as resolved.
Show resolved Hide resolved
setupLog.Error(err, "unable to initialize controller", "controller", device)
os.Exit(1)
}
}

pm := patcher.NewPatcherManager(mgr.GetLogger().WithName("webhooks").WithName("Fpga"))
if contains(devices, "sgx") {
mgr.GetWebhookServer().Register("/pods-sgx", &webhook.Admission{
Handler: &sgxwebhook.SgxMutator{Client: mgr.GetClient()},
})
}

mgr.GetWebhookServer().Register("/pods", &webhook.Admission{
Handler: admission.HandlerFunc(pm.GetPodMutator()),
})
if contains(devices, "fpga") {
pm = patcher.NewPatcherManager(mgr.GetLogger().WithName("webhooks").WithName("Fpga"))

mgr.GetWebhookServer().Register("/pods-sgx", &webhook.Admission{
Handler: &sgxwebhook.SgxMutator{Client: mgr.GetClient()},
})
mgr.GetWebhookServer().Register("/pods", &webhook.Admission{
Handler: admission.HandlerFunc(pm.GetPodMutator()),
})

if err = (&fpgacontroller.AcceleratorFunctionReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
PatcherManager: pm,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AcceleratorFunction")
os.Exit(1)
}
if err = (&fpgacontroller.AcceleratorFunctionReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
PatcherManager: pm,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AcceleratorFunction")
os.Exit(1)
}

if err = (&fpgacontroller.FpgaRegionReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
PatcherManager: pm,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FpgaRegion")
os.Exit(1)
if err = (&fpgacontroller.FpgaRegionReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
PatcherManager: pm,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FpgaRegion")
os.Exit(1)
}
}

setupLog.Info("starting manager")
Expand Down