Skip to content

Commit

Permalink
Update pre-go1.21 code
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
  • Loading branch information
helsaawy committed Mar 5, 2024
1 parent 060de7c commit e1587a9
Show file tree
Hide file tree
Showing 27 changed files with 47 additions and 1,886 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Expand Up @@ -495,8 +495,7 @@ jobs:
working-directory: src/github.com/containerd/containerd
run: |
# TODO: when https://github.com/containerd/containerd/pull/8691 makes it into the next release (container v1.6.22?), remove the skip
# `-skip` is only available in go1.20
export EXTRA_TESTFLAGS='-timeout=20m -run="[^(TestConvert)]"'
export EXTRA_TESTFLAGS='-timeout=20m -skip="TestConvert"'
export GOTEST='gotestsum --format=standard-verbose --debug --'
make integration
Expand Down
4 changes: 1 addition & 3 deletions cmd/containerd-shim-runhcs-v1/rootfs.go
Expand Up @@ -63,9 +63,7 @@ func parseLegacyRootfsMount(m *types.Mount) (string, []string, error) {
if strings.HasPrefix(option, mount.ParentLayerPathsFlag) {
err := json.Unmarshal([]byte(option[len(mount.ParentLayerPathsFlag):]), &parentLayerPaths)
if err != nil {
// TODO (go1.20): use multierror via fmt.Errorf("...: %w; ...: %w", ...)
//nolint:errorlint // non-wrapping format verb for fmt.Errorf
return "", nil, fmt.Errorf("unmarshal parent layer paths from mount: %v: %w", err, errdefs.ErrFailedPrecondition)
return "", nil, fmt.Errorf("unmarshal parent layer paths from mount: %w: %w", err, errdefs.ErrFailedPrecondition)
}
// Would perhaps be worthwhile to check for unrecognized options and return an error,
// but since this is a legacy layer mount we don't do that to avoid breaking anyone.
Expand Down
12 changes: 3 additions & 9 deletions ext4/dmverity/dmverity.go
Expand Up @@ -185,19 +185,15 @@ func ReadDMVerityInfoReader(r io.Reader) (*VerityInfo, error) {
block := make([]byte, blockSize)
if s, err := r.Read(block); err != nil || s != blockSize {
if err != nil {
// TODO (go1.20): use multierror via fmt.Errorf("...: %w; ...: %w", ...)
//nolint:errorlint // non-wrapping format verb for fmt.Errorf
return nil, fmt.Errorf("%s: %w", ErrSuperBlockReadFailure, err)
return nil, fmt.Errorf("%w: %w", ErrSuperBlockReadFailure, err)
}
return nil, fmt.Errorf("unexpected bytes read expected=%d actual=%d: %w", blockSize, s, ErrSuperBlockReadFailure)
}

dmvSB := &dmveritySuperblock{}
b := bytes.NewBuffer(block)
if err := binary.Read(b, binary.LittleEndian, dmvSB); err != nil {
// TODO (go1.20): use multierror via fmt.Errorf("...: %w; ...: %w", ...)
//nolint:errorlint // non-wrapping format verb for fmt.Errorf
return nil, fmt.Errorf("%s: %w", ErrSuperBlockParseFailure, err)
return nil, fmt.Errorf("%w: %w", ErrSuperBlockParseFailure, err)
}

if string(bytes.Trim(dmvSB.Signature[:], "\x00")[:]) != VeritySignature {
Expand All @@ -206,9 +202,7 @@ func ReadDMVerityInfoReader(r io.Reader) (*VerityInfo, error) {

if s, err := r.Read(block); err != nil || s != blockSize {
if err != nil {
// TODO (go1.20): use multierror via fmt.Errorf("...: %w; ...: %w", ...)
//nolint:errorlint // non-wrapping format verb for fmt.Errorf
return nil, fmt.Errorf("%s: %w", ErrRootHashReadFailure, err)
return nil, fmt.Errorf("%w: %w", ErrRootHashReadFailure, err)
}
return nil, fmt.Errorf("unexpected bytes read expected=%d, actual=%d: %w", blockSize, s, ErrRootHashReadFailure)
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Expand Up @@ -33,7 +33,6 @@ require (
go.etcd.io/bbolt v1.3.9
go.opencensus.io v0.24.0
go.uber.org/mock v0.4.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/sync v0.6.0
golang.org/x/sys v0.17.0
google.golang.org/grpc v1.62.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Expand Up @@ -1148,8 +1148,6 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
22 changes: 12 additions & 10 deletions internal/cmd/cmd.go
Expand Up @@ -261,7 +261,7 @@ func (c *Cmd) Start() error {
if ctx == nil {
ctx = context.Background()
}
kctx := log.Copy(context.Background(), ctx)
kctx := context.WithoutCancel(ctx)
_, _ = c.Process.Kill(kctx)
case <-c.allDoneCh:
}
Expand Down Expand Up @@ -313,24 +313,26 @@ func (c *Cmd) Wait() error {
close(timeoutErrCh)
}

// TODO (go1.20): use multierror for these
ioErr := c.ioGrp.Wait()
if ioErr == nil {
ioErr, _ = c.stdinErr.Load().(error)
var errs []error
errs = append(errs, c.ioGrp.Wait())
if err, _ := c.stdinErr.Load().(error); err != nil {
errs = append(errs, err)
}
close(c.allDoneCh)
if tErr := <-timeoutErrCh; ioErr == nil {
ioErr = tErr
if err := <-timeoutErrCh; err != nil {
errs = append(errs, err)
}

c.Process.Close()
c.ExitState = state
if exitErr != nil {
return exitErr
errs = append(errs, exitErr)
}
if state.exited && state.code != 0 {
return &ExitError{state}
errs = append(errs, &ExitError{state})
}
return ioErr

return errors.Join(errs...)
}

// Run is equivalent to Start followed by Wait.
Expand Down
8 changes: 2 additions & 6 deletions internal/guest/runtime/hcsv2/container.go
Expand Up @@ -206,19 +206,15 @@ func (c *Container) Delete(ctx context.Context) error {

if err := os.RemoveAll(c.scratchDirPath); err != nil {
if retErr != nil {
// TODO (go1.20): use multierror via fmt.Errorf("...: %w; ...: %w", ...)
//nolint:errorlint // non-wrapping format verb for fmt.Errorf
retErr = fmt.Errorf("errors deleting container state, %s & %w", retErr, err)
retErr = fmt.Errorf("errors deleting container state: %w; %w", retErr, err)
} else {
retErr = err
}
}

if err := os.RemoveAll(c.ociBundlePath); err != nil {
if retErr != nil {
// TODO (go1.20): use multierror via fmt.Errorf("...: %w; ...: %w", ...)
//nolint:errorlint // non-wrapping format verb for fmt.Errorf
retErr = fmt.Errorf("errors deleting container oci bundle dir, %s & %w", retErr, err)
retErr = fmt.Errorf("errors deleting container oci bundle dir: %w; %w", retErr, err)
} else {
retErr = err
}
Expand Down
5 changes: 1 addition & 4 deletions internal/guest/runtime/hcsv2/spec.go
Expand Up @@ -290,13 +290,10 @@ func addDevSev(ctx context.Context, spec *oci.Spec) error {
devSev, err := devices.DeviceFromPath("/dev/sev", "rwm")
if err != nil {
// try adding /dev/guest-sev, which should be present for 6.x kernel
sevErr := fmt.Errorf("failed to add SEV device to spec: %w", err)
var errSevGuest error
devSev, errSevGuest = devices.DeviceFromPath("/dev/sev-guest", "rwm")
if errSevGuest != nil {
// TODO (go1.20): use multierror via fmt.Errorf("...: %w; ...: %w", ...)
//nolint:errorlint // non-wrapping format verb for fmt.Errorf
return fmt.Errorf("%s: %w", sevErr, errSevGuest)
return fmt.Errorf("failed to add SEV device to spec: %w: %w", err, errSevGuest)
}
}
addLinuxDeviceToSpec(ctx, devSev, spec, true)
Expand Down
20 changes: 0 additions & 20 deletions internal/log/context.go
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
)

type entryContextKeyType int
Expand Down Expand Up @@ -93,25 +92,6 @@ func WithContext(ctx context.Context, entry *logrus.Entry) (context.Context, *lo
return ctx, entry
}

// Copy extracts the tracing Span and logging entry from the src Context, if they
// exist, and adds them to the dst Context.
//
// This is useful to share tracing and logging between contexts, but not the
// cancellation. For example, if the src Context has been cancelled but cleanup
// operations triggered by the cancellation require a non-cancelled context to
// execute.
func Copy(dst context.Context, src context.Context) context.Context {
if s := trace.FromContext(src); s != nil {
dst = trace.NewContext(dst, s)
}

if e := fromContext(src); e != nil {
dst, _ = WithContext(dst, e)
}

return dst
}

func fromContext(ctx context.Context) *logrus.Entry {
e, _ := ctx.Value(_entryContextKey).(*logrus.Entry)
return e
Expand Down
4 changes: 1 addition & 3 deletions internal/log/format.go
Expand Up @@ -103,9 +103,7 @@ func encode(v interface{}) (_ []byte, err error) {

if jErr := enc.Encode(v); jErr != nil {
if err != nil {
// TODO (go1.20): use multierror via fmt.Errorf("...: %w; ...: %w", ...)
//nolint:errorlint // non-wrapping format verb for fmt.Errorf
return nil, fmt.Errorf("protojson encoding: %v; json encoding: %w", err, jErr)
return nil, fmt.Errorf("protojson encoding: %w; json encoding: %w", err, jErr)
}
return nil, fmt.Errorf("json encoding: %w", jErr)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/oci/annotations.go
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"encoding/json"
"errors"
"slices"
"strconv"
"strings"

"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"

iannotations "github.com/Microsoft/hcsshim/internal/annotations"
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
Expand Down
2 changes: 1 addition & 1 deletion internal/uvm/start.go
Expand Up @@ -230,7 +230,7 @@ func (uvm *UtilityVM) Start(ctx context.Context) (err error) {
go func() {
// the original context may have timeout or propagate a cancellation
// copy the original to prevent it affecting the background wait go routine
cCtx := log.Copy(context.Background(), pCtx)
cCtx := context.WithoutCancel(pCtx)
err := uvm.hcsSystem.WaitCtx(cCtx)
if err == nil {
err = uvm.hcsSystem.ExitError()
Expand Down
16 changes: 5 additions & 11 deletions internal/uvm/wait.go
Expand Up @@ -4,6 +4,7 @@ package uvm

import (
"context"
"errors"
"fmt"

"github.com/sirupsen/logrus"
Expand All @@ -18,22 +19,15 @@ func (uvm *UtilityVM) Wait() error { return uvm.WaitCtx(context.Background()) }
func (uvm *UtilityVM) WaitCtx(ctx context.Context) (err error) {
err = uvm.hcsSystem.WaitCtx(ctx)

e := logrus.WithField(logfields.UVMID, uvm.id)
e.Debug("uvm exited, waiting for output processing to complete")
logrus.WithField(logfields.UVMID, uvm.id).Debug("uvm exited, waiting for output processing to complete")
var outputErr error
if uvm.outputProcessingDone != nil {
select {
case <-uvm.outputProcessingDone:
case <-ctx.Done():
err2 := ctx.Err()
// TODO (go1.20): use multierror for err & err2 and remove log Warning
if err == nil {
err = fmt.Errorf("failed to wait on uvm output processing: %w", err2)
} else {
// log err2 since it won't get returned to user
e.WithError(err2).Warning("failed to wait on uvm output processing")
}
outputErr = fmt.Errorf("failed to wait on uvm output processing: %w", ctx.Err())
}
}

return err
return errors.Join(err, outputErr)
}
1 change: 0 additions & 1 deletion test/go.mod
Expand Up @@ -19,7 +19,6 @@ require (
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.3
go.opencensus.io v0.24.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/sync v0.6.0
golang.org/x/sys v0.17.0
google.golang.org/grpc v1.62.0
Expand Down
2 changes: 0 additions & 2 deletions test/go.sum
Expand Up @@ -2581,8 +2581,6 @@ golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EH
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20220827204233-334a2380cb91/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
Expand Down
7 changes: 5 additions & 2 deletions test/internal/util/util.go
Expand Up @@ -9,6 +9,7 @@ import (
"runtime"
"strconv"
"strings"
"testing"
"time"
"unicode"

Expand All @@ -35,8 +36,10 @@ func CleanName(n string) string {
//
// Should not be called from init() or global variable initialization since there is no guarantee on
// if the testing flags have been defined yet (and ideally should be called after [flag.Parse]).
func RunningBenchmarks() (b bool) {
// TODO (go1.21): use [testing.Testing] (ref: https://github.com/golang/go/issues/52600) to shortcircut
func RunningBenchmarks() bool {
if !testing.Testing() {
return false
}

// go doesn't run benchmarks unless -test.bench flag is passed, so thats our cue.
// ref: https://pkg.go.dev/testing#hdr-Benchmarks
Expand Down
18 changes: 13 additions & 5 deletions test/pkg/flag/flag.go
Expand Up @@ -2,11 +2,10 @@ package flag

import (
"flag"
"slices"
"strings"

"github.com/sirupsen/logrus"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)

const (
Expand Down Expand Up @@ -116,8 +115,17 @@ func NewStringSet(name, usage string, caseSensitive bool) *StringSet {
return ss
}

// Strings returns a string slice of the flags provided to the flag
func (ss *StringSet) Strings() []string { return maps.Keys(ss.s) }
// Strings returns a string slice of the flags provided to the flag.
func (ss *StringSet) Strings() []string {
// given flux around [maps.Keys], inline it directly
//
// see: https://github.com/golang/go/issues/61538
r := make([]string, 0, len(ss.s))
for k := range ss.s {
r = append(r, k)
}
return r
}

func (ss *StringSet) String() string {
if ss == nil || ss.Len() == 0 { // may be called by flag package on nil receiver
Expand All @@ -138,7 +146,7 @@ func (ss *StringSet) Set(s string) error {
return nil
}

// standardize formats the feature flag s to be consistent (ie, trim and to lowercase)
// standardize formats the feature flag s to be consistent (ie, trim and to lowercase).
func (ss *StringSet) standardize(s string) string {
s = strings.TrimSpace(s)
if !ss.cs {
Expand Down
3 changes: 1 addition & 2 deletions test/pkg/flag/flag_test.go
Expand Up @@ -2,9 +2,8 @@ package flag

import (
"fmt"
"slices"
"testing"

"golang.org/x/exp/slices"
)

// tests for testing fixtures ...
Expand Down
27 changes: 0 additions & 27 deletions vendor/golang.org/x/exp/LICENSE

This file was deleted.

22 changes: 0 additions & 22 deletions vendor/golang.org/x/exp/PATENTS

This file was deleted.

0 comments on commit e1587a9

Please sign in to comment.