Skip to content

Commit

Permalink
Move to Go 1.20
Browse files Browse the repository at this point in the history
Use errors.Join instead of multierror.
  • Loading branch information
qbarrand committed Nov 8, 2023
1 parent f7501ce commit b794db9
Show file tree
Hide file tree
Showing 25 changed files with 40 additions and 1,526 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
- if: matrix.language == 'go'
uses: actions/setup-go@v4
with:
go-version: '1.19'
go-version: '1.20'

- if: matrix.language == 'go'
run: make manager manager-hub
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gitstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: Run GitStream
runs-on: ubuntu-latest
env:
GO_RELEASE: go1.19.3.linux-amd64.tar.gz
GO_RELEASE: go1.20.11.linux-amd64.tar.gz

container:
image: ghcr.io/qbarrand/gitstream:main
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM registry.access.redhat.com/ubi9/go-toolset:1.19 as builder
FROM registry.access.redhat.com/ubi9/go-toolset:1.20 as builder

# Copy the Go Modules manifests
COPY go.mod go.mod
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.kmm-operator-build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM registry.ci.openshift.org/openshift/release:golang-1.19
FROM registry.ci.openshift.org/openshift/release:golang-1.20

ENV GO111MODULE=on
ENV GOFLAGS=""
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.worker
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM registry.access.redhat.com/ubi9/go-toolset:1.19 as builder
FROM registry.access.redhat.com/ubi9/go-toolset:1.20 as builder

# Add the vendored dependencies
COPY vendor vendor
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/rh-ecosystem-edge/kernel-module-management

go 1.19
go 1.20

require (
github.com/a8m/envsubst v1.4.2
Expand All @@ -10,7 +10,6 @@ require (
github.com/google/go-cmp v0.6.0
github.com/google/go-containerregistry v0.16.1
github.com/google/go-containerregistry/pkg/authn/kubernetes v0.0.0-20230523181351-c3f8a49229d3
github.com/hashicorp/go-multierror v1.1.1
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/onsi/ginkgo/v2 v2.13.0
github.com/onsi/gomega v1.29.0
Expand Down Expand Up @@ -61,7 +60,6 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,6 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
Expand Down
18 changes: 9 additions & 9 deletions internal/controllers/module_nmc_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"reflect"
"strings"

"github.com/hashicorp/go-multierror"
buildv1 "github.com/openshift/api/build/v1"
kmmv1beta1 "github.com/rh-ecosystem-edge/kernel-module-management/api/v1beta1"
"github.com/rh-ecosystem-edge/kernel-module-management/internal/api"
Expand Down Expand Up @@ -117,8 +116,8 @@ func (mnr *ModuleNMCReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

sdMap, prepareErrs := mnr.reconHelper.prepareSchedulingData(ctx, mod, targetedNodes, currentNMCs)
var sumErr *multierror.Error
sumErr = multierror.Append(sumErr, prepareErrs...)
errs := make([]error, 0, len(sdMap)+1)
errs = append(errs, prepareErrs...)

for nodeName, sd := range sdMap {
if sd.action == actionAdd {
Expand All @@ -127,13 +126,14 @@ func (mnr *ModuleNMCReconciler) Reconcile(ctx context.Context, req ctrl.Request)
if sd.action == actionDelete {
err = mnr.reconHelper.disableModuleOnNode(ctx, mod.Namespace, mod.Name, nodeName)
}
sumErr = multierror.Append(sumErr, err)

errs = append(errs, err)
}

err = mnr.reconHelper.moduleUpdateWorkerPodsStatus(ctx, mod, targetedNodes)
sumErr = multierror.Append(sumErr, err)
errs = append(errs, err)

err = sumErr.ErrorOrNil()
err = errors.Join(errs...)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to reconcile module %s/%s config: %v", mod.Namespace, mod.Name, err)
}
Expand Down Expand Up @@ -246,13 +246,13 @@ func (mnrh *moduleNMCReconcilerHelper) finalizeModule(ctx context.Context, mod *
return fmt.Errorf("failed to list NMCs with %s configured in the cluster: %v", modNSN, err)
}

var sumErr *multierror.Error
errs := make([]error, 0, len(nmcList.Items))
for _, nmc := range nmcList.Items {
err := mnrh.removeModuleFromNMC(ctx, &nmc, mod.Namespace, mod.Name)
sumErr = multierror.Append(sumErr, err)
errs = append(errs, err)
}

err := sumErr.ErrorOrNil()
err := errors.Join(errs...)
if err != nil {
return fmt.Errorf("failed to remove %s module from some of NMCs: %v", modNSN, err)
}
Expand Down
36 changes: 15 additions & 21 deletions internal/controllers/nmc_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"reflect"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/mitchellh/hashstructure/v2"
kmmv1beta1 "github.com/rh-ecosystem-edge/kernel-module-management/api/v1beta1"
"github.com/rh-ecosystem-edge/kernel-module-management/internal/config"
Expand Down Expand Up @@ -120,17 +119,15 @@ func (r *NMCReconciler) Reconcile(ctx context.Context, req reconcile.Request) (r
statusMap[status.Namespace+"/"+status.Name] = &nmcObj.Status.Modules[i]
}

// TODO move to errors.Join() when we move to Go 1.20
// errs := make([]error, 0, len(nmcObj.Spec.Modules)+len(nmcObj.Status.Modules))
var errs *multierror.Error
errs := make([]error, 0, len(nmcObj.Spec.Modules)+len(nmcObj.Status.Modules))

for _, mod := range nmcObj.Spec.Modules {
moduleNameKey := mod.Namespace + "/" + mod.Name

logger := logger.WithValues("module", moduleNameKey)

if err := r.helper.ProcessModuleSpec(ctrl.LoggerInto(ctx, logger), &nmcObj, &mod, statusMap[moduleNameKey]); err != nil {
errs = multierror.Append(
errs = append(
errs,
fmt.Errorf("error processing Module %s: %v", moduleNameKey, err),
)
Expand All @@ -148,22 +145,22 @@ func (r *NMCReconciler) Reconcile(ctx context.Context, req reconcile.Request) (r
logger := logger.WithValues("status", statusNameKey)

if err := r.helper.ProcessUnconfiguredModuleStatus(ctrl.LoggerInto(ctx, logger), &nmcObj, status); err != nil {
errs = multierror.Append(
errs = append(
errs,
fmt.Errorf("erorr processing orphan status for Module %s: %v", statusNameKey, err),
)
}
}

if err := r.helper.GarbageCollectInUseLabels(ctx, &nmcObj); err != nil {
errs = multierror.Append(errs, fmt.Errorf("failed to GC in-use labels for NMC %s: %v", req.NamespacedName, err))
errs = append(errs, fmt.Errorf("failed to GC in-use labels for NMC %s: %v", req.NamespacedName, err))
}

if err := r.helper.UpdateNodeLabelsAndRecordEvents(ctx, &nmcObj); err != nil {
errs = multierror.Append(errs, fmt.Errorf("could not update node's labels for NMC %s: %v", req.NamespacedName, err))
errs = append(errs, fmt.Errorf("could not update node's labels for NMC %s: %v", req.NamespacedName, err))
}

return ctrl.Result{}, errs.ErrorOrNil()
return ctrl.Result{}, errors.Join(errs...)
}

func (r *NMCReconciler) SetupWithManager(ctx context.Context, mgr manager.Manager) error {
Expand Down Expand Up @@ -431,9 +428,7 @@ func (h *nmcReconcilerHelperImpl) RemovePodFinalizers(ctx context.Context, nodeN
return fmt.Errorf("could not delete orphan worker Pods on node %s: %v", nodeName, err)
}

// TODO move to errors.Join() when we move to Go 1.20
//errs := make([]error, 0, len(pods))
var errs *multierror.Error
errs := make([]error, 0, len(pods))

for i := 0; i < len(pods); i++ {
pod := &pods[i]
Expand All @@ -442,7 +437,7 @@ func (h *nmcReconcilerHelperImpl) RemovePodFinalizers(ctx context.Context, nodeN

if controllerutil.RemoveFinalizer(pod, nodeModulesConfigFinalizer) {
if err = h.client.Patch(ctx, pod, mergeFrom); err != nil {
errs = multierror.Append(
errs = append(
errs,
fmt.Errorf("could not patch Pod %s/%s: %v", pod.Namespace, pod.Name, err),
)
Expand All @@ -452,7 +447,7 @@ func (h *nmcReconcilerHelperImpl) RemovePodFinalizers(ctx context.Context, nodeN
}
}

return errs.ErrorOrNil()
return errors.Join(errs...)
}

func (h *nmcReconcilerHelperImpl) SyncStatus(ctx context.Context, nmcObj *kmmv1beta1.NodeModulesConfig) error {
Expand All @@ -478,8 +473,7 @@ func (h *nmcReconcilerHelperImpl) SyncStatus(ctx context.Context, nmcObj *kmmv1b
}

patchFrom := client.MergeFrom(nmcObj.DeepCopy())
// TODO move to errors.Join() when we move to Go 1.20
var errs *multierror.Error
errs := make([]error, 0, len(pods))
podsToDelete := make([]v1.Pod, 0, len(pods))

for _, p := range pods {
Expand Down Expand Up @@ -522,7 +516,7 @@ func (h *nmcReconcilerHelperImpl) SyncStatus(ctx context.Context, nmcObj *kmmv1b
}

if err = yaml.UnmarshalStrict([]byte(p.Annotations[configAnnotationKey]), &status.Config); err != nil {
errs = multierror.Append(
errs = append(
errs,
fmt.Errorf("%s: could not unmarshal the ModuleConfig from YAML: %v", podNSN, err),
)
Expand Down Expand Up @@ -554,8 +548,8 @@ func (h *nmcReconcilerHelperImpl) SyncStatus(ctx context.Context, nmcObj *kmmv1b
}

err = h.client.Status().Patch(ctx, nmcObj, patchFrom)
errs = multierror.Append(errs, err)
if err = errs.ErrorOrNil(); err != nil {
errs = append(errs, err)
if err = errors.Join(errs...); err != nil {
return fmt.Errorf("encountered errors while reconciling NMC %s status: %v", nmcObj.Name, err)
}

Expand All @@ -565,10 +559,10 @@ func (h *nmcReconcilerHelperImpl) SyncStatus(ctx context.Context, nmcObj *kmmv1b
// in the reconcile loop, and in that case we can always try to delete the pod manually, and after that the flow will be able to continue
for _, pod := range podsToDelete {
err = h.pm.DeletePod(ctx, &pod)
errs = multierror.Append(errs, err)
errs = append(errs, err)
}

return errs.ErrorOrNil()
return errors.Join(errs...)
}

func (h *nmcReconcilerHelperImpl) UpdateNodeLabelsAndRecordEvents(ctx context.Context, nmc *kmmv1beta1.NodeModulesConfig) error {
Expand Down
11 changes: 5 additions & 6 deletions internal/worker/cmdlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package worker

import (
"bufio"
"errors"
"fmt"
"io"
"os/exec"
"sync"

"github.com/go-logr/logr"
"github.com/hashicorp/go-multierror"
)

type CommandLogger struct {
Expand Down Expand Up @@ -50,14 +50,13 @@ func (cl *CommandLogger) Wait() error {
cl.wg.Wait()
close(errs)

// TODO move to errors.Join() when we move to Go 1.20
var err *multierror.Error
chErrs := make([]error, 0, goroutines)

for chErr := range errs {
err = multierror.Append(err, chErr)
for err := range errs {
chErrs = append(chErrs, err)
}

return err.ErrorOrNil()
return errors.Join(chErrs...)
}

func (cl *CommandLogger) write(r io.Reader, name string, errs chan<- error) {
Expand Down
13 changes: 5 additions & 8 deletions internal/worker/imagepuller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package worker
import (
"archive/tar"
"context"
"errors"
"fmt"
"io"
"os"
Expand All @@ -12,7 +13,6 @@ import (
"github.com/go-logr/logr"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/hashicorp/go-multierror"
"github.com/rh-ecosystem-edge/kernel-module-management/internal/utils"
)

Expand Down Expand Up @@ -152,19 +152,16 @@ func (i *imagePuller) PullAndExtract(ctx context.Context, imageName string, inse
wg.Wait()
close(errs)

// TODO move to errors.Join() when we move to Go 1.20
var sumErr *multierror.Error
chErrs := make([]error, 0)

for chErr := range errs {
sumErr = multierror.Append(sumErr, chErr)
chErrs = append(chErrs, chErr)
}

err = sumErr.ErrorOrNil()

if err != nil {
if err = errors.Join(chErrs...); err != nil {
return res, fmt.Errorf("got one or more errors while writing the image: %v", err)
}

logger.V(1).Info("Image written to the filesystem")

if err = ctx.Err(); err != nil {
Expand Down
Loading

0 comments on commit b794db9

Please sign in to comment.