Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove ioutil and deprecated errors, update deps #11

Merged
merged 1 commit into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM metalstack/builder:latest as builder

FROM alpine:3.13
FROM alpine:3.14
RUN apk add --no-cache tini ca-certificates
COPY --from=builder /work/bin/metal-image-cache-sync /metal-image-cache-sync
CMD ["/metal-image-cache-sync"]
15 changes: 7 additions & 8 deletions cmd/internal/determine-sync-images/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/metal-stack/metal-image-cache-sync/cmd/internal/metrics"
"github.com/metal-stack/metal-image-cache-sync/pkg/api"
"github.com/metal-stack/metal-image-cache-sync/pkg/utils"
"github.com/pkg/errors"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -44,12 +43,12 @@ func NewSyncLister(logger *zap.SugaredLogger, driver *metalgo.Driver, s3 *s3.S3,
func (s *SyncLister) DetermineImageSyncList() ([]api.OS, error) {
s3Images, err := s.retrieveImagesFromS3()
if err != nil {
return nil, errors.Wrap(err, "error listing images in s3")
return nil, fmt.Errorf("error listing images in s3:%w", err)
}

resp, err := s.driver.ImageList()
if err != nil {
return nil, errors.Wrap(err, "error listing images")
return nil, fmt.Errorf("error listing images:%w", err)
}

s.imageCollector.SetMetalAPIImageCount(len(resp.Image))
Expand Down Expand Up @@ -170,7 +169,7 @@ func (s *SyncLister) isExcluded(url string) bool {
func (s *SyncLister) DetermineKernelSyncList() ([]api.Kernel, error) {
resp, err := s.driver.PartitionList()
if err != nil {
return nil, errors.Wrap(err, "error listing partitions")
return nil, fmt.Errorf("error listing partitions:%w", err)
}

var result []api.Kernel
Expand Down Expand Up @@ -217,7 +216,7 @@ func (s *SyncLister) DetermineKernelSyncList() ([]api.Kernel, error) {
func (s *SyncLister) DetermineBootImageSyncList() ([]api.BootImage, error) {
resp, err := s.driver.PartitionList()
if err != nil {
return nil, errors.Wrap(err, "error listing partitions")
return nil, fmt.Errorf("error listing partitions:%w", err)
}

var result []api.BootImage
Expand Down Expand Up @@ -271,7 +270,7 @@ func (s *SyncLister) DetermineBootImageSyncList() ([]api.BootImage, error) {
func retrieveContentLength(ctx context.Context, c *http.Client, url string) (int64, error) {
req, err := http.NewRequest(http.MethodHead, url, nil)
if err != nil {
return 0, errors.Wrap(err, "unable to create head request")
return 0, fmt.Errorf("unable to create head request:%w", err)
}

req = req.WithContext(ctx)
Expand All @@ -288,7 +287,7 @@ func retrieveContentLength(ctx context.Context, c *http.Client, url string) (int

size, err := strconv.Atoi(resp.Header.Get("Content-Length"))
if err != nil {
return 0, errors.Wrap(err, "content-length header value could not be converted to integer")
return 0, fmt.Errorf("content-length header value could not be converted to integer:%w", err)
}

return int64(size), nil
Expand Down Expand Up @@ -347,7 +346,7 @@ func (s *SyncLister) retrieveImagesFromS3() (map[string]s3.Object, error) {
return true
})
if err != nil {
return nil, errors.Wrap(err, "cannot list s3 objects")
return nil, fmt.Errorf("cannot list s3 objects:%w", err)
}

return res, nil
Expand Down
5 changes: 3 additions & 2 deletions cmd/internal/metrics/boot-image-collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metrics

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -42,8 +43,8 @@ func MustBootImageMetrics(logger *zap.SugaredLogger, rootPath string) *BootImage
})
c.cacheDownloads = cacheDownloads.Inc

c.reg.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
c.reg.MustRegister(prometheus.NewGoCollector())
c.reg.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
c.reg.MustRegister(collectors.NewGoCollector())
c.reg.MustRegister(cacheSize)
c.reg.MustRegister(cacheImageCount)
c.reg.MustRegister(cacheMisses)
Expand Down
5 changes: 3 additions & 2 deletions cmd/internal/metrics/image-collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metrics

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -70,8 +71,8 @@ func MustImageMetrics(logger *zap.SugaredLogger, rootPath string) *ImageCollecto
})
c.cacheDownloadsInc = cacheDownloadsInc.Inc

c.reg.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
c.reg.MustRegister(prometheus.NewGoCollector())
c.reg.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
c.reg.MustRegister(collectors.NewGoCollector())
c.reg.MustRegister(cacheSize)
c.reg.MustRegister(cacheImageCount)
c.reg.MustRegister(cacheUnsyncedImageCount)
Expand Down
5 changes: 3 additions & 2 deletions cmd/internal/metrics/kernel-collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -44,8 +45,8 @@ func MustKernelMetrics(logger *zap.SugaredLogger, rootPath string) *KernelCollec
})
c.cacheDownloads = cacheDownloads.Inc

c.reg.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
c.reg.MustRegister(prometheus.NewGoCollector())
c.reg.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
c.reg.MustRegister(collectors.NewGoCollector())
c.reg.MustRegister(cacheSize)
c.reg.MustRegister(cacheImageCount)
c.reg.MustRegister(cacheMisses)
Expand Down
23 changes: 11 additions & 12 deletions cmd/internal/sync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/metal-stack/metal-image-cache-sync/cmd/internal/metrics"
"github.com/metal-stack/metal-image-cache-sync/pkg/api"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
"github.com/spf13/afero"
"go.uber.org/zap"
)
Expand All @@ -36,15 +35,15 @@ type Syncer struct {
func NewSyncer(logger *zap.SugaredLogger, fs afero.Fs, s3 *s3manager.Downloader, config *api.Config, collector *metrics.ImageCollector, stop context.Context) (*Syncer, error) {
err := fs.MkdirAll(config.GetImageRootPath(), 0755)
if err != nil {
return nil, errors.Wrap(err, "error creating image subdirectory in cache root")
return nil, fmt.Errorf("error creating image subdirectory in cache root:%w", err)
}
err = fs.MkdirAll(config.GetKernelRootPath(), 0755)
if err != nil {
return nil, errors.Wrap(err, "error creating kernel subdirectory in cache root")
return nil, fmt.Errorf("error creating kernel subdirectory in cache root:%w", err)
}
err = fs.MkdirAll(config.GetBootImageRootPath(), 0755)
if err != nil {
return nil, errors.Wrap(err, "error creating boot image subdirectory in cache root")
return nil, fmt.Errorf("error creating boot image subdirectory in cache root:%w", err)
}

return &Syncer{
Expand All @@ -62,12 +61,12 @@ func NewSyncer(logger *zap.SugaredLogger, fs afero.Fs, s3 *s3manager.Downloader,
func (s *Syncer) Sync(rootPath string, entitiesToSync api.CacheEntities) error {
current, err := currentFileIndex(s.fs, rootPath)
if err != nil {
return errors.Wrap(err, "error creating file index")
return fmt.Errorf("error creating file index:%w", err)
}

remove, keep, add, err := s.defineDiff(rootPath, current, entitiesToSync)
if err != nil {
return errors.Wrap(err, "error creating cache diff")
return fmt.Errorf("error creating cache diff:%w", err)
}

s.printSyncPlan(remove, keep, add)
Expand All @@ -93,7 +92,7 @@ func (s *Syncer) Sync(rootPath string, entitiesToSync api.CacheEntities) error {

err = cleanEmptyDirs(s.fs, rootPath)
if err != nil {
return errors.Wrap(err, "error cleaning up empty directories")
return fmt.Errorf("error cleaning up empty directories:%w", err)
}

return nil
Expand All @@ -103,7 +102,7 @@ func currentFileIndex(fs afero.Fs, rootPath string) (api.CacheEntities, error) {
var result api.CacheEntities
err := afero.Walk(fs, rootPath, func(p string, info os.FileInfo, innerErr error) error {
if innerErr != nil {
return errors.Wrap(innerErr, fmt.Sprintf("error while walking through root path %s", rootPath))
return fmt.Errorf("error while walking through root path %s error:%w", rootPath, innerErr)
}

if info.IsDir() {
Expand Down Expand Up @@ -158,7 +157,7 @@ func (s *Syncer) defineDiff(rootPath string, currentEntities api.CacheEntities,

hash, err := s.fileMD5(strings.Join([]string{rootPath, existing.GetSubPath()}, string(os.PathSeparator)))
if err != nil {
return nil, nil, nil, errors.Wrap(err, "error calculating hash sum of local file")
return nil, nil, nil, fmt.Errorf("error calculating hash sum of local file:%w", err)
}

if hash != expected {
Expand Down Expand Up @@ -211,12 +210,12 @@ func (s *Syncer) download(rootPath string, e api.CacheEntity) error {

err := s.fs.MkdirAll(path.Dir(tmpTargetPath), 0755)
if err != nil {
return errors.Wrap(err, "error creating tmp download path in cache root")
return fmt.Errorf("error creating tmp download path in cache root:%w", err)
}

err = s.fs.MkdirAll(path.Dir(targetPath), 0755)
if err != nil {
return errors.Wrap(err, "error creating path in cache root")
return fmt.Errorf("error creating path in cache root:%w", err)
}

f, err := s.fs.Create(tmpTargetPath)
Expand Down Expand Up @@ -247,7 +246,7 @@ func (s *Syncer) download(rootPath string, e api.CacheEntity) error {

err = s.fs.Rename(tmpTargetPath, targetPath)
if err != nil {
return errors.Wrap(err, "error moving downloaded file to final destination")
return fmt.Errorf("error moving downloaded file to final destination:%w", err)
}

if !e.HasMD5() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/internal/sync/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"path"
"regexp"
Expand Down Expand Up @@ -134,7 +134,7 @@ func dlLoggingSvc(data []byte) (*s3.S3, *[]string, *[]string) {
bodyBytes := data[start:fin]
r.HTTPResponse = &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader(bodyBytes)),
Body: io.NopCloser(bytes.NewReader(bodyBytes)),
Header: http.Header{},
}
r.HTTPResponse.Header.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d",
Expand Down
19 changes: 10 additions & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"
"errors"
"fmt"
"log"
"net/http"
"strings"
Expand All @@ -20,7 +22,6 @@ import (
"github.com/metal-stack/metal-image-cache-sync/pkg/api"
"github.com/metal-stack/metal-image-cache-sync/pkg/utils"
"github.com/metal-stack/v"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/robfig/cron/v3"
"github.com/spf13/afero"
Expand Down Expand Up @@ -223,7 +224,7 @@ func run() error {
}
})
if err != nil {
return errors.Wrap(err, "could not initialize cron schedule")
return fmt.Errorf("could not initialize cron schedule:%w", err)
}

handlers := []cacheFileHandler{newCacheFileHandler(c.ImageCacheBindAddress, c.GetImageRootPath(), imageCollector)}
Expand Down Expand Up @@ -330,7 +331,7 @@ func runSync(c *api.Config) error {
err := func() error {
syncImages, err := lister.DetermineImageSyncList()
if err != nil {
return errors.Wrap(err, "cannot gather images")
return fmt.Errorf("cannot gather images:%w", err)
}

var converted api.CacheEntities
Expand All @@ -340,7 +341,7 @@ func runSync(c *api.Config) error {

err = syncer.Sync(c.GetImageRootPath(), converted)
if err != nil {
return errors.Wrap(err, "error during image sync")
return fmt.Errorf("error during image sync:%w", err)
}

return nil
Expand All @@ -352,7 +353,7 @@ func runSync(c *api.Config) error {
err = func() error {
syncKernels, err := lister.DetermineKernelSyncList()
if err != nil {
return errors.Wrap(err, "cannot kernel images")
return fmt.Errorf("cannot kernel images:%w", err)
}

var converted api.CacheEntities
Expand All @@ -362,7 +363,7 @@ func runSync(c *api.Config) error {

err = syncer.Sync(c.GetKernelRootPath(), converted)
if err != nil {
return errors.Wrap(err, "error during kernel sync")
return fmt.Errorf("error during kernel sync:%w", err)
}

return nil
Expand All @@ -374,7 +375,7 @@ func runSync(c *api.Config) error {
err = func() error {
syncImages, err := lister.DetermineBootImageSyncList()
if err != nil {
return errors.Wrap(err, "cannot gather boot images")
return fmt.Errorf("cannot gather boot images:%w", err)
}

var converted api.CacheEntities
Expand All @@ -384,7 +385,7 @@ func runSync(c *api.Config) error {

err = syncer.Sync(c.GetBootImageRootPath(), converted)
if err != nil {
return errors.Wrap(err, "error during boot image sync")
return fmt.Errorf("error during boot image sync:%w", err)
}

return nil
Expand All @@ -394,7 +395,7 @@ func runSync(c *api.Config) error {
}

if len(errs) > 0 {
return errors.Errorf("errors occurred during sync: %v", errs)
return fmt.Errorf("errors occurred during sync: %v", errs)
}

return nil
Expand Down
30 changes: 11 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,25 @@ go 1.16

require (
github.com/Masterminds/semver v1.5.0
github.com/aws/aws-sdk-go v1.38.28
github.com/aws/aws-sdk-go v1.39.4
github.com/docker/go-units v0.4.0
github.com/go-openapi/spec v0.20.3 // indirect
github.com/go-openapi/strfmt v0.20.1
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.5
github.com/gopherjs/gopherjs v0.0.0-20210420193930-a4630ec28c79 // indirect
github.com/metal-stack/metal-go v0.14.0
github.com/go-playground/validator/v10 v10.7.0
github.com/google/go-cmp v0.5.6
github.com/gopherjs/gopherjs v0.0.0-20210707094841-eea289f08d45 // indirect
github.com/metal-stack/metal-go v0.15.1
github.com/metal-stack/metal-lib v0.8.0 // indirect
github.com/metal-stack/v v1.0.3
github.com/olekukonko/tablewriter v0.0.5
github.com/pelletier/go-toml v1.9.0 // indirect
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.10.0
github.com/prometheus/client_golang v1.11.0
github.com/robfig/cron/v3 v3.0.1
github.com/smartystreets/assertions v1.2.0 // indirect
github.com/spf13/afero v1.6.0
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.1.3
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.7.1
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.16.0
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887 // indirect
go.uber.org/zap v1.18.1
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/validator.v9 v9.31.0
gopkg.in/ini.v1 v1.62.0 // indirect
sigs.k8s.io/controller-runtime v0.8.3
sigs.k8s.io/controller-runtime v0.9.2
)
Loading