Skip to content

Commit c268650

Browse files
committed
Upgrade: Go v1.20 and packages
ref: https://issues.redhat.com/browse/ACM-5042 Signed-off-by: Dale Haiducek <[email protected]>
1 parent 069df3c commit c268650

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+572
-896
lines changed

Makefile

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ clean:
8787

8888
.PHONY: fmt-dependencies
8989
fmt-dependencies:
90-
$(call go-get-tool,github.com/daixiang0/gci@v0.2.9)
91-
$(call go-get-tool,mvdan.cc/gofumpt@v0.2.0)
90+
$(call go-get-tool,github.com/daixiang0/gci@v0.10.1)
91+
$(call go-get-tool,mvdan.cc/gofumpt@v0.5.0)
9292

9393
# All available format: format-go format-protos format-python
9494
# Default value will run all formats, override these make target with your requirements:
9595
# eg: fmt: format-go format-protos
9696
fmt: fmt-dependencies
9797
find . -not \( -path "./.go" -prune \) -name "*.go" | xargs gofmt -s -w
98-
find . -not \( -path "./.go" -prune \) -name "*.go" | xargs gci -w -local "$(shell cat go.mod | head -1 | cut -d " " -f 2)"
98+
find . -not \( -path "./.go" -prune \) -name "*.go" | xargs gci write -s standard -s default -s "prefix($(shell cat go.mod | head -1 | cut -d " " -f 2))"
9999
find . -not \( -path "./.go" -prune \) -name "*.go" | xargs gofumpt -l -w
100100

101101
############################################################
@@ -107,7 +107,7 @@ check: lint
107107

108108
.PHONY: lint-dependencies
109109
lint-dependencies:
110-
$(call go-get-tool,github.com/golangci/golangci-lint/cmd/golangci-lint@v1.46.2)
110+
$(call go-get-tool,github.com/golangci/golangci-lint/cmd/golangci-lint@v1.52.2)
111111

112112
# All available linters: lint-dockerfiles lint-scripts lint-yaml lint-copyright-banner lint-go lint-python lint-helm lint-markdown lint-sass lint-typescript lint-protos
113113
# Default value will run all linters, override these make target with your requirements:
@@ -119,19 +119,19 @@ lint: lint-dependencies lint-all
119119
############################################################
120120
GOSEC = $(LOCAL_BIN)/gosec
121121
KUBEBUILDER = $(LOCAL_BIN)/kubebuilder
122-
KBVERSION = 3.2.0
123-
K8S_VERSION = 1.21.2
122+
KBVERSION = 3.12.0
123+
ENVTEST_K8S_VERSION = 1.26.x
124124

125125
.PHONY: test
126126
test: test-dependencies
127-
KUBEBUILDER_ASSETS=$(LOCAL_BIN) go test $(TESTARGS) `go list ./... | grep -v test/e2e`
127+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test $(TESTARGS) `go list ./... | grep -v test/e2e`
128128

129129
.PHONY: test-coverage
130130
test-coverage: TESTARGS = -json -cover -covermode=atomic -coverprofile=coverage_unit.out
131131
test-coverage: test
132132

133133
.PHONY: test-dependencies
134-
test-dependencies: kubebuilder-dependencies kubebuilder
134+
test-dependencies: envtest kubebuilder
135135

136136
.PHONY: kubebuilder
137137
kubebuilder:
@@ -141,17 +141,13 @@ kubebuilder:
141141
chmod +x $(KUBEBUILDER); \
142142
fi
143143

144-
.PHONY: kubebuilder-dependencies
145-
kubebuilder-dependencies: $(LOCAL_BIN)
146-
@if [ ! -f $(LOCAL_BIN)/etcd ] || [ ! -f $(LOCAL_BIN)/kube-apiserver ] || [ ! -f $(LOCAL_BIN)/kubectl ] || \
147-
[ "$$($(KUBEBUILDER) version 2>/dev/null | grep -o KubeBuilderVersion:\"[0-9]*\.[0-9]\.[0-9]*\")" != "KubeBuilderVersion:\"$(KBVERSION)\"" ]; then \
148-
echo "Installing envtest Kubebuilder assets"; \
149-
curl -L "https://go.kubebuilder.io/test-tools/$(K8S_VERSION)/$(GOOS)/$(GOARCH)" | tar xz --strip-components=2 -C $(LOCAL_BIN); \
150-
fi
144+
.PHONY: envtest
145+
envtest:
146+
$(call go-get-tool,sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
151147

152148
.PHONY: gosec
153149
gosec:
154-
$(call go-get-tool,github.com/securego/gosec/v2/cmd/gosec@v2.9.6)
150+
$(call go-get-tool,github.com/securego/gosec/v2/cmd/gosec@v2.15.0)
155151

156152
.PHONY: gosec-scan
157153
gosec-scan: gosec
@@ -212,7 +208,7 @@ controller-gen: ## Download controller-gen locally if necessary.
212208

213209
.PHONY: kustomize
214210
kustomize: ## Download kustomize locally if necessary.
215-
$(call go-get-tool,sigs.k8s.io/kustomize/kustomize/[email protected])
211+
$(call go-get-tool,sigs.k8s.io/kustomize/kustomize/[email protected])
216212

217213
############################################################
218214
# e2e test section

build/common/config/.golangci.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
service:
2-
# When updating this, also update the version stored in docker/build-tools/Dockerfile in the multicloudlab/tools repo.
3-
golangci-lint-version: 1.18.x # use the fixed version to not introduce new linters unexpectedly
41
run:
52
# timeout for analysis, e.g. 30s, 5m, default is 1m
63
deadline: 20m
7-
go: '1.17'
84
timeout: 20m
95

106
# which dirs to skip: they won't be analyzed;

controllers/automation/PolicyAutomationPredicate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
// we only want to watch for pb contains policy as subjects
1414
var policyAuomtationPredicateFuncs = predicate.Funcs{
1515
UpdateFunc: func(e event.UpdateEvent) bool {
16-
// nolint: forcetypeassert
16+
//nolint:forcetypeassert
1717
policyAutomationNew := e.ObjectNew.(*policyv1beta1.PolicyAutomation)
18-
// nolint: forcetypeassert
18+
//nolint:forcetypeassert
1919
policyAutomationOld := e.ObjectOld.(*policyv1beta1.PolicyAutomation)
2020

2121
if policyAutomationNew.Spec.PolicyRef == "" {
@@ -29,7 +29,7 @@ var policyAuomtationPredicateFuncs = predicate.Funcs{
2929
return !equality.Semantic.DeepEqual(policyAutomationNew.Spec, policyAutomationOld.Spec)
3030
},
3131
CreateFunc: func(e event.CreateEvent) bool {
32-
// nolint: forcetypeassert
32+
//nolint:forcetypeassert
3333
policyAutomationNew := e.Object.(*policyv1beta1.PolicyAutomation)
3434

3535
return policyAutomationNew.Spec.PolicyRef != ""

controllers/automation/policyMapper.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
func policyMapper(c client.Client) handler.MapFunc {
1818
return func(obj client.Object) []reconcile.Request {
19-
// nolint: forcetypeassert
19+
//nolint:forcetypeassert
2020
policy := obj.(*policiesv1.Policy)
2121

2222
var result []reconcile.Request
@@ -42,9 +42,8 @@ func policyMapper(c client.Client) handler.MapFunc {
4242

4343
if found {
4444
modeType := policyAutomation.Spec.Mode
45-
if modeType == "scan" {
46-
// scan mode, do not queue
47-
} else if modeType == policyv1beta1.Once || modeType == policyv1beta1.EveryEvent {
45+
// Do not queue during scan mode
46+
if modeType != "scan" && modeType == policyv1beta1.Once || modeType == policyv1beta1.EveryEvent {
4847
// The same policyAutomation mapping logic for once and everyEvent mode
4948
request := reconcile.Request{NamespacedName: types.NamespacedName{
5049
Name: policyAutomation.GetName(),

controllers/automation/policyPredicate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
// we only want to watch for pb contains policy as subjects
1313
var policyPredicateFuncs = predicate.Funcs{
1414
UpdateFunc: func(e event.UpdateEvent) bool {
15-
// nolint: forcetypeassert
15+
//nolint:forcetypeassert
1616
plcObjNew := e.ObjectNew.(*policiesv1.Policy)
1717
if _, ok := plcObjNew.Labels["policy.open-cluster-management.io/root-policy"]; ok {
1818
return false
1919
}
2020

21-
// nolint: forcetypeassert
21+
//nolint:forcetypeassert
2222
plcObjOld := e.ObjectOld.(*policiesv1.Policy)
2323

2424
return plcObjNew.Status.ComplianceState != plcObjOld.Status.ComplianceState

controllers/automation/policyautomation_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ func (r *PolicyAutomationReconciler) Reconcile(
520520

521521
policyAutomation.Status.ClustersWithEvent = eventMap
522522
// use StatusWriter to update status subresource of a Kubernetes object
523-
err = r.Status().Update(ctx, policyAutomation, &client.UpdateOptions{})
523+
err = r.Status().Update(ctx, policyAutomation)
524524
if err != nil {
525525
log.Error(err, "Failed to update ClustersWithEvent in policyAutomation status")
526526

controllers/encryptionkeys/encryptionkeys_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var _ reconcile.Reconciler = &EncryptionKeysReconciler{}
5656

5757
// EncryptionKeysReconciler is responsible for rotating the AES encryption key in the "policy-encryption-key" Secrets
5858
// for all managed clusters.
59-
type EncryptionKeysReconciler struct { // nolint:golint,revive
59+
type EncryptionKeysReconciler struct { //nolint:golint,revive
6060
client.Client
6161
KeyRotationDays uint
6262
MaxConcurrentReconciles uint
@@ -192,7 +192,7 @@ func (r *EncryptionKeysReconciler) getNextRotationFromNow(secret *corev1.Secret)
192192

193193
lastRotated, err := time.Parse(time.RFC3339, lastRotatedTS)
194194
if err != nil {
195-
return 0, fmt.Errorf(`%w with value "%s": %v`, errLastRotationParseError, lastRotatedTS, err)
195+
return 0, fmt.Errorf(`%w with value "%s": %w`, errLastRotationParseError, lastRotatedTS, err)
196196
}
197197

198198
nextRotation := lastRotated.Add(time.Hour * 24 * time.Duration(r.KeyRotationDays))

controllers/encryptionkeys/encryptionkeys_controller_test.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ type erroringFakeClient struct {
4444
UpdateError bool
4545
}
4646

47-
func (c *erroringFakeClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object) error {
47+
func (c *erroringFakeClient) Get(
48+
ctx context.Context, key client.ObjectKey, obj client.Object, _ ...client.GetOption,
49+
) error {
4850
if c.GetError {
4951
return errors.New("some get error")
5052
}
@@ -81,7 +83,7 @@ func (c *erroringFakeClient) Update(ctx context.Context, obj client.Object, opts
8183
func generateSecret() *corev1.Secret {
8284
key := make([]byte, keySize/8)
8385
_, err := rand.Read(key)
84-
Expect(err).To(BeNil())
86+
Expect(err).ToNot(HaveOccurred())
8587

8688
encryptionSecret := &corev1.Secret{
8789
ObjectMeta: metav1.ObjectMeta{
@@ -93,7 +95,7 @@ func generateSecret() *corev1.Secret {
9395

9496
prevKey := make([]byte, keySize/8)
9597
_, err = rand.Read(prevKey)
96-
Expect(err).To(BeNil())
98+
Expect(err).ToNot(HaveOccurred())
9799

98100
encryptionSecret.Data["previousKey"] = prevKey
99101

@@ -190,9 +192,9 @@ func getReconciler(encryptionSecret *corev1.Secret) *EncryptionKeysReconciler {
190192

191193
scheme := k8sruntime.NewScheme()
192194
err := clientgoscheme.AddToScheme(scheme)
193-
Expect(err).To(BeNil())
195+
Expect(err).ToNot(HaveOccurred())
194196
err = v1.AddToScheme(scheme)
195-
Expect(err).To(BeNil())
197+
Expect(err).ToNot(HaveOccurred())
196198

197199
builder := fake.NewClientBuilder().WithObjects(policies...).WithScheme(scheme)
198200

@@ -213,7 +215,7 @@ func getReconciler(encryptionSecret *corev1.Secret) *EncryptionKeysReconciler {
213215
func assertTriggerUpdate(r *EncryptionKeysReconciler) {
214216
policyList := v1.PolicyList{}
215217
err := r.List(context.TODO(), &policyList)
216-
Expect(err).To(BeNil())
218+
Expect(err).ToNot(HaveOccurred())
217219

218220
for _, policy := range policyList.Items {
219221
annotation := policy.Annotations[propagator.TriggerUpdateAnnotation]
@@ -230,7 +232,7 @@ func assertTriggerUpdate(r *EncryptionKeysReconciler) {
230232
func assertNoTriggerUpdate(r *EncryptionKeysReconciler) {
231233
policyList := v1.PolicyList{}
232234
err := r.List(context.TODO(), &policyList)
233-
Expect(err).To(BeNil())
235+
Expect(err).ToNot(HaveOccurred())
234236

235237
for _, policy := range policyList.Items {
236238
annotation := policy.Annotations[propagator.TriggerUpdateAnnotation]
@@ -267,12 +269,12 @@ func TestReconcileRotateKey(t *testing.T) {
267269
request := ctrl.Request{NamespacedName: secretID}
268270
result, err := r.Reconcile(context.TODO(), request)
269271

270-
Expect(err).To(BeNil())
272+
Expect(err).ToNot(HaveOccurred())
271273
Expect(result.Requeue).To(BeFalse())
272274
Expect(getRequeueAfterDays(result)).To(Equal(30))
273275

274276
err = r.Get(context.TODO(), secretID, encryptionSecret)
275-
Expect(err).To(BeNil())
277+
Expect(err).ToNot(HaveOccurred())
276278
Expect(bytes.Equal(encryptionSecret.Data["key"], originalKey)).To(BeFalse())
277279
Expect(bytes.Equal(encryptionSecret.Data["previousKey"], originalKey)).To(BeTrue())
278280

@@ -301,11 +303,11 @@ func TestReconcileNoRotation(t *testing.T) {
301303
request := ctrl.Request{NamespacedName: secretID}
302304
result, err := r.Reconcile(context.TODO(), request)
303305

304-
Expect(err).To(BeNil())
306+
Expect(err).ToNot(HaveOccurred())
305307
Expect(getRequeueAfterDays(result)).To(Equal(30))
306308

307309
err = r.Get(context.TODO(), secretID, encryptionSecret)
308-
Expect(err).To(BeNil())
310+
Expect(err).ToNot(HaveOccurred())
309311
Expect(bytes.Equal(encryptionSecret.Data["key"], originalKey)).To(BeTrue())
310312
Expect(bytes.Equal(encryptionSecret.Data["previousKey"], originalKey)).To(BeFalse())
311313

@@ -324,12 +326,12 @@ func TestReconcileNotFound(t *testing.T) {
324326
request := ctrl.Request{NamespacedName: secretID}
325327
result, err := r.Reconcile(context.TODO(), request)
326328

327-
Expect(err).To(BeNil())
329+
Expect(err).ToNot(HaveOccurred())
328330
Expect(result.RequeueAfter).To(Equal(time.Duration(0)))
329331

330332
policyList := v1.PolicyList{}
331333
err = r.List(context.TODO(), &policyList)
332-
Expect(err).To(BeNil())
334+
Expect(err).ToNot(HaveOccurred())
333335

334336
for _, policy := range policyList.Items {
335337
annotation := policy.Annotations[propagator.TriggerUpdateAnnotation]
@@ -358,12 +360,12 @@ func TestReconcileManualRotation(t *testing.T) {
358360
request := ctrl.Request{NamespacedName: secretID}
359361
result, err := r.Reconcile(context.TODO(), request)
360362

361-
Expect(err).To(BeNil())
363+
Expect(err).ToNot(HaveOccurred())
362364
Expect(result.Requeue).To(BeFalse())
363365
Expect(result.RequeueAfter).To(Equal(time.Duration(0)))
364366

365367
err = r.Get(context.TODO(), secretID, encryptionSecret)
366-
Expect(err).To(BeNil())
368+
Expect(err).ToNot(HaveOccurred())
367369
Expect(bytes.Equal(encryptionSecret.Data["key"], originalKey)).To(BeTrue())
368370
Expect(bytes.Equal(encryptionSecret.Data["previousKey"], originalPrevKey)).To(BeTrue())
369371

@@ -390,14 +392,14 @@ func TestReconcileInvalidKey(t *testing.T) {
390392
request := ctrl.Request{NamespacedName: secretID}
391393
result, err := r.Reconcile(context.TODO(), request)
392394

393-
Expect(err).To(BeNil())
395+
Expect(err).ToNot(HaveOccurred())
394396
Expect(result.Requeue).To(BeFalse())
395397
Expect(getRequeueAfterDays(result)).To(Equal(30))
396398

397399
err = r.Get(context.TODO(), secretID, encryptionSecret)
398-
Expect(err).To(BeNil())
400+
Expect(err).ToNot(HaveOccurred())
399401
Expect(bytes.Equal(encryptionSecret.Data["key"], originalKey)).To(BeFalse())
400-
Expect(len(encryptionSecret.Data["previousKey"])).To(Equal(0))
402+
Expect(encryptionSecret.Data["previousKey"]).To(BeEmpty())
401403

402404
assertTriggerUpdate(r)
403405
}
@@ -422,14 +424,14 @@ func TestReconcileInvalidPreviousKey(t *testing.T) {
422424
request := ctrl.Request{NamespacedName: secretID}
423425
result, err := r.Reconcile(context.TODO(), request)
424426

425-
Expect(err).To(BeNil())
427+
Expect(err).ToNot(HaveOccurred())
426428
Expect(result.Requeue).To(BeFalse())
427429
Expect(getRequeueAfterDays(result)).To(Equal(30))
428430

429431
err = r.Get(context.TODO(), secretID, encryptionSecret)
430-
Expect(err).To(BeNil())
432+
Expect(err).ToNot(HaveOccurred())
431433
Expect(bytes.Equal(encryptionSecret.Data["key"], originalKey)).To(BeTrue())
432-
Expect(len(encryptionSecret.Data["previousKey"])).To(Equal(0))
434+
Expect(encryptionSecret.Data["previousKey"]).To(BeEmpty())
433435

434436
assertNoTriggerUpdate(r)
435437
}
@@ -451,7 +453,7 @@ func TestReconcileSecretNotFiltered(t *testing.T) {
451453
request := ctrl.Request{NamespacedName: secretID}
452454
result, err := r.Reconcile(context.TODO(), request)
453455

454-
Expect(err).To(BeNil())
456+
Expect(err).ToNot(HaveOccurred())
455457
Expect(result.Requeue).To(BeFalse())
456458
Expect(result.RequeueAfter).To(Equal(time.Duration(0)))
457459

@@ -504,10 +506,10 @@ func TestReconcileAPIFails(t *testing.T) {
504506
result, err := r.Reconcile(context.TODO(), request)
505507

506508
if !test.ExpectedRotation {
507-
Expect(err).ShouldNot(BeNil())
509+
Expect(err).Should(HaveOccurred())
508510
Expect(result.RequeueAfter).Should(Equal(time.Duration(0)))
509511
} else {
510-
Expect(err).Should(BeNil())
512+
Expect(err).ShouldNot(HaveOccurred())
511513
Expect(result.RequeueAfter).ShouldNot(Equal(time.Duration(0)))
512514
}
513515

@@ -517,7 +519,7 @@ func TestReconcileAPIFails(t *testing.T) {
517519
r.Client = erroringClient.Client
518520

519521
err = r.Get(context.TODO(), client.ObjectKeyFromObject(encryptionSecret), encryptionSecret)
520-
Expect(err).Should(BeNil())
522+
Expect(err).ShouldNot(HaveOccurred())
521523

522524
if test.ExpectedRotation {
523525
Expect(bytes.Equal(originalKey, encryptionSecret.Data["key"])).Should(BeFalse())

controllers/policyset/placementBindingMapper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212
policiesv1 "open-cluster-management.io/governance-policy-propagator/api/v1"
1313
)
1414

15-
func placementBindingMapper(c client.Client) handler.MapFunc {
15+
func placementBindingMapper(_ client.Client) handler.MapFunc {
1616
return func(obj client.Object) []reconcile.Request {
17-
// nolint: forcetypeassert
17+
//nolint:forcetypeassert
1818
object := obj.(*policiesv1.PlacementBinding)
1919
var result []reconcile.Request
2020

0 commit comments

Comments
 (0)