Skip to content

Commit cf773f6

Browse files
author
Per Goncalves da Silva
committed
[CARRY] address lint issues in namespace labeler plugin
Signed-off-by: Per Goncalves da Silva <[email protected]>
1 parent 1d7eee7 commit cf773f6

File tree

10 files changed

+179
-184
lines changed

10 files changed

+179
-184
lines changed

staging/operator-lifecycle-manager/pkg/controller/operators/olm/downstream_csv_labeler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212
)
1313

14-
const CsvLabelerPluginId plugins.PluginID = "csv-labeler-plugin"
14+
const CsvLabelerPluginID plugins.PluginID = "csv-labeler-plugin"
1515
const labelSyncerLabelKey = ""
1616

1717
func NewCSVLabelSyncerLabeler(client operatorclient.ClientInterface, logger *logrus.Logger) *CSVLabelSyncerLabeler {

staging/operator-lifecycle-manager/pkg/controller/operators/olm/downstream_plugins.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ func init() {
88
operatorPlugInFactoryFuncs = plugins.OperatorPlugInFactoryMap{
99
// labels unlabeled non-payload openshift-* csv namespaces with
1010
// security.openshift.io/scc.podSecurityLabelSync: true
11-
CsvLabelerPluginId: plugins.NewCsvNamespaceLabelerPluginFunc,
11+
CsvLabelerPluginID: plugins.NewCsvNamespaceLabelerPluginFunc,
1212
}
1313
}
1414

staging/operator-lifecycle-manager/pkg/controller/operators/olm/plugins/downstream_csv_namespace_labeler_plugin.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package plugins
33
import (
44
"context"
55
"fmt"
6-
"k8s.io/apimachinery/pkg/types"
7-
"sigs.k8s.io/controller-runtime/pkg/client"
86
"strings"
97

108
"github.com/operator-framework/api/pkg/operators/v1alpha1"
@@ -13,11 +11,13 @@ import (
1311
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
1412
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/queueinformer"
1513
"github.com/sirupsen/logrus"
16-
v1 "k8s.io/api/core/v1"
14+
corev1 "k8s.io/api/core/v1"
1715
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1816
"k8s.io/apimachinery/pkg/labels"
17+
"k8s.io/apimachinery/pkg/types"
1918
listerv1 "k8s.io/client-go/listers/core/v1"
2019
"k8s.io/client-go/util/workqueue"
20+
"sigs.k8s.io/controller-runtime/pkg/client"
2121
)
2222

2323
const NamespaceLabelSyncerLabelKey = "security.openshift.io/scc.podSecurityLabelSync"
@@ -43,7 +43,6 @@ type csvNamespaceLabelerPlugin struct {
4343
}
4444

4545
func NewCsvNamespaceLabelerPluginFunc(ctx context.Context, config OperatorConfig, hostOperator HostOperator) (OperatorPlugin, error) {
46-
4746
if hostOperator == nil {
4847
return nil, fmt.Errorf("cannot initialize plugin: operator undefined")
4948
}
@@ -126,12 +125,11 @@ func (p *csvNamespaceLabelerPlugin) Shutdown() error {
126125
}
127126

128127
func (p *csvNamespaceLabelerPlugin) Sync(ctx context.Context, obj client.Object) error {
129-
var namespace *v1.Namespace
128+
var namespace *corev1.Namespace
130129
var err error
131130

132131
// get namespace from the event resource
133132
switch eventResource := obj.(type) {
134-
135133
// handle csv events
136134
case *v1alpha1.ClusterServiceVersion:
137135
// ignore copied csvs and namespaces that should be ignored
@@ -145,7 +143,7 @@ func (p *csvNamespaceLabelerPlugin) Sync(ctx context.Context, obj client.Object)
145143
}
146144

147145
// handle namespace events
148-
case *v1.Namespace:
146+
case *corev1.Namespace:
149147
// ignore namespaces that should be ignored and ones that are already labeled
150148
if ignoreNamespace(eventResource.GetName()) || hasLabelSyncerLabel(eventResource) {
151149
return nil
@@ -178,7 +176,7 @@ func (p *csvNamespaceLabelerPlugin) Sync(ctx context.Context, obj client.Object)
178176
return nil
179177
}
180178

181-
func (p *csvNamespaceLabelerPlugin) getNamespace(namespace string) (*v1.Namespace, error) {
179+
func (p *csvNamespaceLabelerPlugin) getNamespace(namespace string) (*corev1.Namespace, error) {
182180
ns, err := p.namespaceLister.Get(namespace)
183181
if err != nil {
184182
return nil, err
@@ -221,7 +219,7 @@ func ignoreNamespace(namespace string) bool {
221219
return !hasOpenshiftPrefix(namespace) || IsNamespacePSALabelSyncExemptedInVendoredOCPVersion(namespace)
222220
}
223221

224-
func applyLabelSyncerLabel(ctx context.Context, kubeClient operatorclient.ClientInterface, namespace *v1.Namespace) error {
222+
func applyLabelSyncerLabel(ctx context.Context, kubeClient operatorclient.ClientInterface, namespace *corev1.Namespace) error {
225223
if _, ok := namespace.GetLabels()[NamespaceLabelSyncerLabelKey]; !ok {
226224
nsCopy := namespace.DeepCopy()
227225
if nsCopy.GetLabels() == nil {
@@ -235,7 +233,7 @@ func applyLabelSyncerLabel(ctx context.Context, kubeClient operatorclient.Client
235233
return nil
236234
}
237235

238-
func hasLabelSyncerLabel(namespace *v1.Namespace) bool {
236+
func hasLabelSyncerLabel(namespace *corev1.Namespace) bool {
239237
_, ok := namespace.GetLabels()[NamespaceLabelSyncerLabelKey]
240238
return ok
241239
}

staging/operator-lifecycle-manager/pkg/controller/operators/olm/plugins/downstream_csv_namespace_labeler_plugin_test.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
listerv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
1414
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
1515
"github.com/stretchr/testify/assert"
16-
v1 "k8s.io/api/core/v1"
16+
corev1 "k8s.io/api/core/v1"
1717
apiextensionsfake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
1818
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1919
"k8s.io/apimachinery/pkg/runtime"
@@ -53,8 +53,7 @@ func init() {
5353
}
5454
}
5555

56-
func NewFakeCSVNamespaceLabelerPlugin(t *testing.T, options ...fakeClientOption) (*csvNamespaceLabelerPlugin, context.CancelFunc) {
57-
56+
func newFakeCSVNamespaceLabelerPlugin(t *testing.T, options ...fakeClientOption) (*csvNamespaceLabelerPlugin, context.CancelFunc) {
5857
resyncPeriod := 5 * time.Minute
5958
clientOptions := &fakeClientOptions{}
6059
for _, applyOption := range options {
@@ -83,9 +82,9 @@ func NewFakeCSVNamespaceLabelerPlugin(t *testing.T, options ...fakeClientOption)
8382
operatorsInformerFactory.Start(stopCtx)
8483

8584
t.Log("waiting for informers to sync")
86-
syncCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
85+
syncCtx, syncCancel := context.WithTimeout(ctx, 10*time.Second)
8786
defer func() {
88-
cancel()
87+
syncCancel()
8988
}()
9089
if ok := cache.WaitForCacheSync(syncCtx.Done(), namespaceInformer.HasSynced); !ok {
9190
t.Fatalf("failed to wait for namespace caches to sync")
@@ -122,15 +121,15 @@ func NewCopiedCsvInNamespace(namespace string) *v1alpha1.ClusterServiceVersion {
122121
return csv
123122
}
124123

125-
func NewNamespace(name string) *v1.Namespace {
126-
return &v1.Namespace{
124+
func NewNamespace(name string) *corev1.Namespace {
125+
return &corev1.Namespace{
127126
ObjectMeta: metav1.ObjectMeta{
128127
Name: name,
129128
},
130129
}
131130
}
132131

133-
func NewLabeledNamespace(name string, labelValue string) *v1.Namespace {
132+
func NewLabeledNamespace(name string, labelValue string) *corev1.Namespace {
134133
ns := NewNamespace(name)
135134
ns.SetLabels(map[string]string{
136135
NamespaceLabelSyncerLabelKey: labelValue,
@@ -141,7 +140,7 @@ func NewLabeledNamespace(name string, labelValue string) *v1.Namespace {
141140
func Test_SyncIgnoresCopiedCsvs(t *testing.T) {
142141
// Sync ignores copied csvs
143142
namespace := "openshift-test"
144-
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace(namespace)))
143+
plugin, shutdown := newFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace(namespace)))
145144
defer shutdown()
146145

147146
assert.Nil(t, plugin.Sync(context.Background(), NewCopiedCsvInNamespace(namespace)))
@@ -154,7 +153,7 @@ func Test_SyncIgnoresCopiedCsvs(t *testing.T) {
154153
func Test_SyncIgnoresNonOpenshiftNamespaces(t *testing.T) {
155154
// Sync ignores non-openshift namespaces
156155
namespace := "test-namespace"
157-
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace(namespace)))
156+
plugin, shutdown := newFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace(namespace)))
158157
defer shutdown()
159158

160159
assert.Nil(t, plugin.Sync(context.Background(), NewCopiedCsvInNamespace(namespace)))
@@ -167,7 +166,7 @@ func Test_SyncIgnoresNonOpenshiftNamespaces(t *testing.T) {
167166
func Test_SyncIgnoresPayloadOpenshiftNamespacesExceptOperators(t *testing.T) {
168167
// Sync ignores payload openshift namespaces, except openshift-operators
169168
// openshift-monitoring sync -> no label
170-
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace("openshift-monitoring"), NewNamespace("openshift-operators")))
169+
plugin, shutdown := newFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace("openshift-monitoring"), NewNamespace("openshift-operators")))
171170
defer shutdown()
172171

173172
assert.Nil(t, plugin.Sync(context.Background(), NewCsvInNamespace("openshift-monitoring")))
@@ -190,7 +189,7 @@ func Test_SyncIgnoresAlreadyLabeledNonPayloadOpenshiftNamespaces(t *testing.T) {
190189

191190
for _, labelValue := range labelValues {
192191
func() {
193-
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewLabeledNamespace(namespace, labelValue)))
192+
plugin, shutdown := newFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewLabeledNamespace(namespace, labelValue)))
194193
defer shutdown()
195194

196195
assert.Nil(t, plugin.Sync(context.Background(), NewCsvInNamespace(namespace)))
@@ -206,7 +205,7 @@ func Test_SyncLabelsNonPayloadUnlabeledOpenshiftNamespaces(t *testing.T) {
206205
// Sync will label non-labeled non-payload openshift- namespaces
207206
namespace := "openshift-test"
208207

209-
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace(namespace)))
208+
plugin, shutdown := newFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace(namespace)))
210209
defer shutdown()
211210

212211
assert.Nil(t, plugin.Sync(context.Background(), NewCsvInNamespace(namespace)))
@@ -218,15 +217,15 @@ func Test_SyncLabelsNonPayloadUnlabeledOpenshiftNamespaces(t *testing.T) {
218217

219218
func Test_SyncFailsIfEventResourceIsNotCSV(t *testing.T) {
220219
// Sync fails if resource is not a csv\
221-
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t)
220+
plugin, shutdown := newFakeCSVNamespaceLabelerPlugin(t)
222221
defer shutdown()
223222

224-
assert.Error(t, plugin.Sync(context.Background(), &v1.ConfigMap{}))
223+
assert.Error(t, plugin.Sync(context.Background(), &corev1.ConfigMap{}))
225224
}
226225

227226
func Test_SyncFailsIfNamespaceNotFound(t *testing.T) {
228227
// Sync fails if the namespace is not found
229-
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t)
228+
plugin, shutdown := newFakeCSVNamespaceLabelerPlugin(t)
230229
defer shutdown()
231230

232231
assert.Error(t, plugin.Sync(context.Background(), NewCsvInNamespace("openshift-test")))
@@ -235,11 +234,11 @@ func Test_SyncFailsIfNamespaceNotFound(t *testing.T) {
235234
func Test_SyncFailsIfCSVCannotBeUpdated(t *testing.T) {
236235
// Sync fails if the namespace cannot be updated
237236
namespace := "openshift-test"
238-
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace(namespace)))
237+
plugin, shutdown := newFakeCSVNamespaceLabelerPlugin(t, withK8sResources(NewNamespace(namespace)))
239238
defer shutdown()
240239

241240
updateNsError := func(action clienttesting.Action) (handled bool, ret runtime.Object, err error) {
242-
return true, &v1.Namespace{}, errors.New("error updating namespace")
241+
return true, &corev1.Namespace{}, errors.New("error updating namespace")
243242
}
244243
plugin.kubeClient.KubernetesInterface().CoreV1().(*v1fake.FakeCoreV1).PrependReactor("update", "namespaces", updateNsError)
245244
assert.Error(t, plugin.Sync(context.Background(), NewCsvInNamespace(namespace)))
@@ -251,7 +250,7 @@ func Test_SyncLabelsNamespaceWithCSV(t *testing.T) {
251250
// Sync should apply the label syncer label to the namespace
252251
namespace := NewNamespace("openshift-test")
253252
csv := NewCsvInNamespace(namespace.GetName())
254-
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(namespace), withExtendedResources(csv))
253+
plugin, shutdown := newFakeCSVNamespaceLabelerPlugin(t, withK8sResources(namespace), withExtendedResources(csv))
255254
defer shutdown()
256255

257256
assert.NoError(t, plugin.Sync(context.Background(), namespace))
@@ -266,7 +265,7 @@ func Test_SyncDoesNotLabelNamespaceWithoutCSVs(t *testing.T) {
266265
// that contains zero non-copied csvs
267266
// Sync should *NOT* apply the label syncer label to the namespace
268267
namespace := NewNamespace("openshift-test")
269-
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(namespace))
268+
plugin, shutdown := newFakeCSVNamespaceLabelerPlugin(t, withK8sResources(namespace))
270269
defer shutdown()
271270

272271
assert.NoError(t, plugin.Sync(context.Background(), namespace))
@@ -282,7 +281,7 @@ func Test_SyncDoesNotLabelNamespacesWithCopiedCSVs(t *testing.T) {
282281
// Sync should *NOT* apply the label syncer label to the namespace
283282
namespace := NewNamespace("openshift-test")
284283
csv := NewCopiedCsvInNamespace(namespace.GetName())
285-
plugin, shutdown := NewFakeCSVNamespaceLabelerPlugin(t, withK8sResources(namespace), withExtendedResources(csv))
284+
plugin, shutdown := newFakeCSVNamespaceLabelerPlugin(t, withK8sResources(namespace), withExtendedResources(csv))
286285
defer shutdown()
287286

288287
assert.NoError(t, plugin.Sync(context.Background(), namespace))

0 commit comments

Comments
 (0)