Skip to content

Commit 5f7f8ee

Browse files
committed
operator: pack controller args into a struct
Signed-off-by: Tuomas Katila <tuomas.katila@intel.com>
1 parent 2fc4d26 commit 5f7f8ee

17 files changed

+106
-103
lines changed

cmd/operator/main.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434

3535
devicepluginv1 "github.com/intel/intel-device-plugins-for-kubernetes/pkg/apis/deviceplugin/v1"
3636
fpgav2 "github.com/intel/intel-device-plugins-for-kubernetes/pkg/apis/fpga/v2"
37+
"github.com/intel/intel-device-plugins-for-kubernetes/pkg/controllers"
3738
"github.com/intel/intel-device-plugins-for-kubernetes/pkg/controllers/dlb"
3839
"github.com/intel/intel-device-plugins-for-kubernetes/pkg/controllers/dsa"
3940
"github.com/intel/intel-device-plugins-for-kubernetes/pkg/controllers/fpga"
@@ -61,7 +62,7 @@ func init() {
6162
// +kubebuilder:scaffold:scheme
6263
}
6364

64-
type devicePluginControllerAndWebhook map[string](func(ctrl.Manager, string, string, bool) error)
65+
type devicePluginControllerAndWebhook map[string](func(ctrl.Manager, controllers.ControllerArgs) error)
6566

6667
type flagList []string
6768

@@ -208,17 +209,17 @@ func main() {
208209
os.Exit(1)
209210
}
210211

211-
ns := os.Getenv("DEVICEPLUGIN_NAMESPACE")
212-
if ns == "" {
213-
ns = devicePluginNamespace
214-
}
212+
cargs := controllers.ControllerArgs{WithWebhook: true}
215213

216-
pluginSecret := os.Getenv("DEVICEPLUGIN_SECRET")
214+
cargs.Namespace = os.Getenv("DEVICEPLUGIN_NAMESPACE")
215+
if cargs.Namespace == "" {
216+
cargs.Namespace = devicePluginNamespace
217+
}
217218

218-
withWebhook := true
219+
cargs.Secret = os.Getenv("DEVICEPLUGIN_SECRET")
219220

220221
for _, device := range devices {
221-
if err = setupControllerAndWebhook[device](mgr, ns, pluginSecret, withWebhook); err != nil {
222+
if err = setupControllerAndWebhook[device](mgr, cargs); err != nil {
222223
setupLog.Error(err, "unable to initialize controller", "controller", device)
223224
os.Exit(1)
224225
}

pkg/controllers/dlb/controller.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ var defaultNodeSelector map[string]string = deployments.DLBPluginDaemonSet().Spe
4343
// +kubebuilder:rbac:groups=deviceplugin.intel.com,resources=dlbdeviceplugins/finalizers,verbs=update
4444

4545
// SetupReconciler creates a new reconciler for DlbDevicePlugin objects.
46-
func SetupReconciler(mgr ctrl.Manager, namespace, registrySecret string, withWebhook bool) error {
47-
c := &controller{scheme: mgr.GetScheme(), ns: namespace, secret: registrySecret}
46+
func SetupReconciler(mgr ctrl.Manager, args controllers.ControllerArgs) error {
47+
c := &controller{scheme: mgr.GetScheme(), args: args}
4848
if err := controllers.SetupWithManager(mgr, c, devicepluginv1.GroupVersion.String(), "DlbDevicePlugin", ownerKey); err != nil {
4949
return err
5050
}
5151

52-
if withWebhook {
52+
if args.WithWebhook {
5353
return (&devicepluginv1.DlbDevicePlugin{}).SetupWebhookWithManager(mgr)
5454
}
5555

@@ -59,8 +59,7 @@ func SetupReconciler(mgr ctrl.Manager, namespace, registrySecret string, withWeb
5959
type controller struct {
6060
controllers.DefaultServiceAccountFactory
6161
scheme *runtime.Scheme
62-
ns string
63-
secret string
62+
args controllers.ControllerArgs
6463
}
6564

6665
func (c *controller) CreateEmptyObject() client.Object {
@@ -93,11 +92,11 @@ func (c *controller) NewDaemonSet(rawObj client.Object) *apps.DaemonSet {
9392
setInitContainer(&ds.Spec.Template.Spec, devicePlugin.Spec)
9493
}
9594

96-
ds.ObjectMeta.Namespace = c.ns
95+
ds.ObjectMeta.Namespace = c.args.Namespace
9796

98-
if len(c.secret) > 0 {
97+
if len(c.args.Secret) > 0 {
9998
ds.Spec.Template.Spec.ImagePullSecrets = []v1.LocalObjectReference{
100-
{Name: c.secret},
99+
{Name: c.args.Secret},
101100
}
102101
}
103102

pkg/controllers/dlb/controller_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (c *controller) newDaemonSetExpected(rawObj client.Object) *apps.DaemonSet
4545
APIVersion: "apps/v1",
4646
},
4747
ObjectMeta: metav1.ObjectMeta{
48-
Namespace: c.ns,
48+
Namespace: c.args.Namespace,
4949
Name: appLabel + "-" + devicePlugin.Name,
5050
Labels: map[string]string{
5151
"app": appLabel,
@@ -155,9 +155,9 @@ func (c *controller) newDaemonSetExpected(rawObj client.Object) *apps.DaemonSet
155155
},
156156
}
157157

158-
if len(c.secret) > 0 {
158+
if len(c.args.Secret) > 0 {
159159
daemonSet.Spec.Template.Spec.ImagePullSecrets = []v1.LocalObjectReference{
160-
{Name: c.secret},
160+
{Name: c.args.Secret},
161161
}
162162
}
163163

@@ -178,7 +178,7 @@ func TestNewDaemonSetDLB(t *testing.T) {
178178
t.Errorf("expected and actuall daemonsets differ: %+s", diff.ObjectGoPrintDiff(expected, actual))
179179
}
180180

181-
c.secret = "mysecret"
181+
c.args.Secret = "mysecret"
182182

183183
expected = c.newDaemonSetExpected(plugin)
184184
actual = c.NewDaemonSet(plugin)

pkg/controllers/dsa/controller.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ var defaultNodeSelector = deployments.DSAPluginDaemonSet().Spec.Template.Spec.No
4747
// +kubebuilder:rbac:groups=deviceplugin.intel.com,resources=dsadeviceplugins/finalizers,verbs=update
4848

4949
// SetupReconciler creates a new reconciler for DsaDevicePlugin objects.
50-
func SetupReconciler(mgr ctrl.Manager, namespace, registrySecret string, withWebhook bool) error {
51-
c := &controller{scheme: mgr.GetScheme(), ns: namespace, secret: registrySecret}
50+
func SetupReconciler(mgr ctrl.Manager, args controllers.ControllerArgs) error {
51+
c := &controller{scheme: mgr.GetScheme(), args: args}
5252
if err := controllers.SetupWithManager(mgr, c, devicepluginv1.GroupVersion.String(), "DsaDevicePlugin", ownerKey); err != nil {
5353
return err
5454
}
5555

56-
if withWebhook {
56+
if args.WithWebhook {
5757
return (&devicepluginv1.DsaDevicePlugin{}).SetupWebhookWithManager(mgr)
5858
}
5959

@@ -63,8 +63,7 @@ func SetupReconciler(mgr ctrl.Manager, namespace, registrySecret string, withWeb
6363
type controller struct {
6464
controllers.DefaultServiceAccountFactory
6565
scheme *runtime.Scheme
66-
ns string
67-
secret string
66+
args controllers.ControllerArgs
6867
}
6968

7069
func (c *controller) CreateEmptyObject() client.Object {
@@ -201,17 +200,17 @@ func (c *controller) NewDaemonSet(rawObj client.Object) *apps.DaemonSet {
201200
daemonSet.Spec.Template.Spec.Tolerations = devicePlugin.Spec.Tolerations
202201
}
203202

204-
daemonSet.ObjectMeta.Namespace = c.ns
203+
daemonSet.ObjectMeta.Namespace = c.args.Namespace
205204
daemonSet.Spec.Template.Spec.Containers[0].Args = getPodArgs(devicePlugin)
206205
daemonSet.Spec.Template.Spec.Containers[0].Image = devicePlugin.Spec.Image
207206

208207
if devicePlugin.Spec.InitImage != "" {
209208
addInitContainer(daemonSet, devicePlugin)
210209
}
211210

212-
if len(c.secret) > 0 {
211+
if len(c.args.Secret) > 0 {
213212
daemonSet.Spec.Template.Spec.ImagePullSecrets = []v1.LocalObjectReference{
214-
{Name: c.secret},
213+
{Name: c.args.Secret},
215214
}
216215
}
217216

pkg/controllers/dsa/controller_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (c *controller) newDaemonSetExpected(rawObj client.Object) *apps.DaemonSet
4747
APIVersion: "apps/v1",
4848
},
4949
ObjectMeta: metav1.ObjectMeta{
50-
Namespace: c.ns,
50+
Namespace: c.args.Namespace,
5151
Name: appLabel + "-" + devicePlugin.Name,
5252
Labels: map[string]string{
5353
"app": appLabel,
@@ -177,9 +177,9 @@ func (c *controller) newDaemonSetExpected(rawObj client.Object) *apps.DaemonSet
177177
addInitContainer(&daemonSet, devicePlugin)
178178
}
179179

180-
if len(c.secret) > 0 {
180+
if len(c.args.Secret) > 0 {
181181
daemonSet.Spec.Template.Spec.ImagePullSecrets = []v1.LocalObjectReference{
182-
{Name: c.secret},
182+
{Name: c.args.Secret},
183183
}
184184
}
185185

@@ -200,7 +200,7 @@ func TestNewDaemonSetDSA(t *testing.T) {
200200
t.Errorf("expected and actuall daemonsets differ: %+s", diff.ObjectGoPrintDiff(expected, actual))
201201
}
202202

203-
c.secret = "mysecret"
203+
c.args.Secret = "mysecret"
204204

205205
expected = c.newDaemonSetExpected(plugin)
206206
actual = c.NewDaemonSet(plugin)

pkg/controllers/fpga/controller.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ var defaultNodeSelector = deployments.FPGAPluginDaemonSet().Spec.Template.Spec.N
4343
// +kubebuilder:rbac:groups=deviceplugin.intel.com,resources=fpgadeviceplugins/finalizers,verbs=update
4444

4545
// SetupReconciler creates a new reconciler for FpgaDevicePlugin objects.
46-
func SetupReconciler(mgr ctrl.Manager, namespace, registrySecret string, withWebhook bool) error {
47-
c := &controller{scheme: mgr.GetScheme(), ns: namespace, secret: registrySecret}
46+
func SetupReconciler(mgr ctrl.Manager, args controllers.ControllerArgs) error {
47+
c := &controller{scheme: mgr.GetScheme(), args: args}
4848
if err := controllers.SetupWithManager(mgr, c, devicepluginv1.GroupVersion.String(), "FpgaDevicePlugin", ownerKey); err != nil {
4949
return err
5050
}
5151

52-
if withWebhook {
52+
if args.WithWebhook {
5353
return (&devicepluginv1.FpgaDevicePlugin{}).SetupWebhookWithManager(mgr)
5454
}
5555

@@ -59,8 +59,7 @@ func SetupReconciler(mgr ctrl.Manager, namespace, registrySecret string, withWeb
5959
type controller struct {
6060
controllers.DefaultServiceAccountFactory
6161
scheme *runtime.Scheme
62-
ns string
63-
secret string
62+
args controllers.ControllerArgs
6463
}
6564

6665
func (c *controller) CreateEmptyObject() client.Object {
@@ -86,11 +85,11 @@ func (c *controller) NewDaemonSet(rawObj client.Object) *apps.DaemonSet {
8685
daemonSet.Spec.Template.Spec.Tolerations = devicePlugin.Spec.Tolerations
8786
}
8887

89-
daemonSet.ObjectMeta.Namespace = c.ns
88+
daemonSet.ObjectMeta.Namespace = c.args.Namespace
9089

91-
if len(c.secret) > 0 {
90+
if len(c.args.Secret) > 0 {
9291
daemonSet.Spec.Template.Spec.ImagePullSecrets = []v1.LocalObjectReference{
93-
{Name: c.secret},
92+
{Name: c.args.Secret},
9493
}
9594
}
9695

pkg/controllers/fpga/controller_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (c *controller) newDaemonSetExpected(rawObj client.Object) *apps.DaemonSet
4848
APIVersion: "apps/v1",
4949
},
5050
ObjectMeta: metav1.ObjectMeta{
51-
Namespace: c.ns,
51+
Namespace: c.args.Namespace,
5252
Name: appLabel + "-" + devicePlugin.Name,
5353
Labels: map[string]string{
5454
"app": appLabel,
@@ -199,9 +199,9 @@ func (c *controller) newDaemonSetExpected(rawObj client.Object) *apps.DaemonSet
199199
},
200200
}
201201

202-
if len(c.secret) > 0 {
202+
if len(c.args.Secret) > 0 {
203203
ds.Spec.Template.Spec.ImagePullSecrets = []v1.LocalObjectReference{
204-
{Name: c.secret},
204+
{Name: c.args.Secret},
205205
}
206206
}
207207

@@ -227,7 +227,7 @@ func TestNewDaemonSetFPGA(t *testing.T) {
227227
t.Errorf("expected and actuall daemonsets differ: %+s", diff.ObjectGoPrintDiff(expected, actual))
228228
}
229229

230-
c.secret = "mysecret"
230+
c.args.Secret = "mysecret"
231231

232232
expected = c.newDaemonSetExpected(plugin)
233233
actual = c.NewDaemonSet(plugin)

pkg/controllers/gpu/controller.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ var defaultNodeSelector = deployments.GPUPluginDaemonSet().Spec.Template.Spec.No
4949
// +kubebuilder:rbac:groups=deviceplugin.intel.com,resources=gpudeviceplugins/finalizers,verbs=update
5050

5151
// SetupReconciler creates a new reconciler for GpuDevicePlugin objects.
52-
func SetupReconciler(mgr ctrl.Manager, namespace, registrySecret string, withWebhook bool) error {
53-
c := &controller{scheme: mgr.GetScheme(), ns: namespace, secret: registrySecret}
52+
func SetupReconciler(mgr ctrl.Manager, args controllers.ControllerArgs) error {
53+
c := &controller{scheme: mgr.GetScheme(), args: args}
5454
if err := controllers.SetupWithManager(mgr, c, devicepluginv1.GroupVersion.String(), "GpuDevicePlugin", ownerKey); err != nil {
5555
return err
5656
}
5757

58-
if withWebhook {
58+
if args.WithWebhook {
5959
return (&devicepluginv1.GpuDevicePlugin{}).SetupWebhookWithManager(mgr)
6060
}
6161

@@ -64,8 +64,7 @@ func SetupReconciler(mgr ctrl.Manager, namespace, registrySecret string, withWeb
6464

6565
type controller struct {
6666
scheme *runtime.Scheme
67-
ns string
68-
secret string
67+
args controllers.ControllerArgs
6968
}
7069

7170
func (c *controller) CreateEmptyObject() client.Object {
@@ -81,7 +80,7 @@ func (c *controller) NewSharedServiceAccount() *v1.ServiceAccount {
8180
return &v1.ServiceAccount{
8281
ObjectMeta: metav1.ObjectMeta{
8382
Name: serviceAccountName,
84-
Namespace: c.ns,
83+
Namespace: c.args.Namespace,
8584
},
8685
}
8786
}
@@ -90,13 +89,13 @@ func (c *controller) NewSharedClusterRoleBinding() *rbacv1.ClusterRoleBinding {
9089
return &rbacv1.ClusterRoleBinding{
9190
ObjectMeta: metav1.ObjectMeta{
9291
Name: roleBindingName,
93-
Namespace: c.ns,
92+
Namespace: c.args.Namespace,
9493
},
9594
Subjects: []rbacv1.Subject{
9695
{
9796
Kind: "ServiceAccount",
9897
Name: serviceAccountName,
99-
Namespace: c.ns,
98+
Namespace: c.args.Namespace,
10099
},
101100
},
102101
RoleRef: rbacv1.RoleRef{
@@ -141,13 +140,13 @@ func (c *controller) NewDaemonSet(rawObj client.Object) *apps.DaemonSet {
141140
daemonSet.Spec.Template.Spec.Tolerations = devicePlugin.Spec.Tolerations
142141
}
143142

144-
daemonSet.ObjectMeta.Namespace = c.ns
143+
daemonSet.ObjectMeta.Namespace = c.args.Namespace
145144
daemonSet.Spec.Template.Spec.Containers[0].Args = getPodArgs(devicePlugin)
146145
daemonSet.Spec.Template.Spec.Containers[0].Image = devicePlugin.Spec.Image
147146

148-
if len(c.secret) > 0 {
147+
if len(c.args.Secret) > 0 {
149148
daemonSet.Spec.Template.Spec.ImagePullSecrets = []v1.LocalObjectReference{
150-
{Name: c.secret},
149+
{Name: c.args.Secret},
151150
}
152151
}
153152

pkg/controllers/gpu/controller_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"sigs.k8s.io/controller-runtime/pkg/client"
3030

3131
devicepluginv1 "github.com/intel/intel-device-plugins-for-kubernetes/pkg/apis/deviceplugin/v1"
32+
"github.com/intel/intel-device-plugins-for-kubernetes/pkg/controllers"
3233
)
3334

3435
const appLabel = "intel-gpu-plugin"
@@ -50,7 +51,7 @@ func (c *controller) newDaemonSetExpected(rawObj client.Object) *apps.DaemonSet
5051
APIVersion: "apps/v1",
5152
},
5253
ObjectMeta: metav1.ObjectMeta{
53-
Namespace: c.ns,
54+
Namespace: c.args.Namespace,
5455
Name: appLabel + "-" + devicePlugin.Name,
5556
Labels: map[string]string{
5657
"app": appLabel,
@@ -196,9 +197,9 @@ func (c *controller) newDaemonSetExpected(rawObj client.Object) *apps.DaemonSet
196197
addVolumeMountIfMissing(&daemonSet.Spec.Template.Spec, "sysfsdevices", "/sys/devices", true)
197198
}
198199

199-
if len(c.secret) > 0 {
200+
if len(c.args.Secret) > 0 {
200201
daemonSet.Spec.Template.Spec.ImagePullSecrets = []v1.LocalObjectReference{
201-
{Name: c.secret},
202+
{Name: c.args.Secret},
202203
}
203204
}
204205

@@ -280,7 +281,9 @@ func TestNewDamonSetGPU(t *testing.T) {
280281

281282
func TestNewDamonSetGPUWithSecret(t *testing.T) {
282283
c := &controller{
283-
secret: "mysecret",
284+
args: controllers.ControllerArgs{
285+
Secret: "mysecret",
286+
},
284287
}
285288

286289
plugin := &devicepluginv1.GpuDevicePlugin{}

0 commit comments

Comments
 (0)