Skip to content

Commit 10e0bf9

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 8079389 commit 10e0bf9

File tree

10 files changed

+177
-182
lines changed

10 files changed

+177
-182
lines changed

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

+1-1
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

+1-1
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

+7-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package plugins
33
import (
44
"context"
55
"fmt"
6-
"sigs.k8s.io/controller-runtime/pkg/client"
76
"strings"
87

98
"github.com/operator-framework/api/pkg/operators/v1alpha1"
@@ -12,11 +11,12 @@ import (
1211
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
1312
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/queueinformer"
1413
"github.com/sirupsen/logrus"
15-
v1 "k8s.io/api/core/v1"
14+
corev1 "k8s.io/api/core/v1"
1615
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1716
"k8s.io/apimachinery/pkg/labels"
1817
listerv1 "k8s.io/client-go/listers/core/v1"
1918
"k8s.io/client-go/util/workqueue"
19+
"sigs.k8s.io/controller-runtime/pkg/client"
2020
)
2121

2222
const NamespaceLabelSyncerLabelKey = "security.openshift.io/scc.podSecurityLabelSync"
@@ -42,7 +42,6 @@ type csvNamespaceLabelerPlugin struct {
4242
}
4343

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

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

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

146144
// handle namespace events
147-
case *v1.Namespace:
145+
case *corev1.Namespace:
148146
// ignore namespaces that should be ignored and ones that are already labeled
149147
if ignoreNamespace(eventResource.GetName()) || hasLabelSyncerLabel(eventResource) {
150148
return nil
@@ -177,7 +175,7 @@ func (p *csvNamespaceLabelerPlugin) Sync(ctx context.Context, obj client.Object)
177175
return nil
178176
}
179177

180-
func (p *csvNamespaceLabelerPlugin) getNamespace(namespace string) (*v1.Namespace, error) {
178+
func (p *csvNamespaceLabelerPlugin) getNamespace(namespace string) (*corev1.Namespace, error) {
181179
ns, err := p.namespaceLister.Get(namespace)
182180
if err != nil {
183181
return nil, err
@@ -220,7 +218,7 @@ func ignoreNamespace(namespace string) bool {
220218
return !hasOpenshiftPrefix(namespace) || IsNamespacePSALabelSyncExemptedInVendoredOCPVersion(namespace)
221219
}
222220

223-
func applyLabelSyncerLabel(ctx context.Context, kubeClient operatorclient.ClientInterface, namespace *v1.Namespace) error {
221+
func applyLabelSyncerLabel(ctx context.Context, kubeClient operatorclient.ClientInterface, namespace *corev1.Namespace) error {
224222
if _, ok := namespace.GetLabels()[NamespaceLabelSyncerLabelKey]; !ok {
225223
nsCopy := namespace.DeepCopy()
226224
if nsCopy.GetLabels() == nil {
@@ -234,7 +232,7 @@ func applyLabelSyncerLabel(ctx context.Context, kubeClient operatorclient.Client
234232
return nil
235233
}
236234

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

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

+20-21
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)