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

nydusify: utilize golangci-lint to perform static check #258

Merged
merged 2 commits into from
Jan 7, 2022
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
23 changes: 23 additions & 0 deletions contrib/nydusify/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# https://golangci-lint.run/usage/configuration#config-file

linters:
enable:
- structcheck
- varcheck
- staticcheck
- unconvert
- gofmt
- goimports
- revive
- ineffassign
- vet
- unused
- misspell
disable:
- errcheck

run:
deadline: 4m
skip-dirs:
- misc

1 change: 1 addition & 0 deletions contrib/nydusify/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ static-release:
.PHONY: test
test: build build-smoke
@go vet $(PACKAGES)
golangci-lint run
@go test -count=1 -v -timeout 20m -race ./pkg/...

build-smoke:
Expand Down
7 changes: 5 additions & 2 deletions contrib/nydusify/cmd/nydusify.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/dragonflyoss/image-service/contrib/nydusify/pkg/converter"
"github.com/dragonflyoss/image-service/contrib/nydusify/pkg/converter/provider"
"github.com/dragonflyoss/image-service/contrib/nydusify/pkg/metrics"
"github.com/dragonflyoss/image-service/contrib/nydusify/pkg/metrics/file_exporter"
"github.com/dragonflyoss/image-service/contrib/nydusify/pkg/metrics/fileexporter"
"github.com/dragonflyoss/image-service/contrib/nydusify/pkg/remote"
"github.com/dragonflyoss/image-service/contrib/nydusify/pkg/utils"
)
Expand Down Expand Up @@ -273,7 +273,7 @@ func main() {
return err
}

metrics.Register(file_exporter.New(filepath.Join(opt.WorkDir, "conversion_metrics.prom")))
metrics.Register(fileexporter.New(filepath.Join(opt.WorkDir, "conversion_metrics.prom")))
defer metrics.Export()

return cvt.Convert(context.Background())
Expand Down Expand Up @@ -312,6 +312,9 @@ func main() {
}

_, arch, err := provider.ExtractOsArch(c.String("platform"))
if err != nil {
return err
}

checker, err := checker.New(checker.Opt{
WorkDir: c.String("work-dir"),
Expand Down
10 changes: 5 additions & 5 deletions contrib/nydusify/pkg/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ type Backend interface {
// file handle and file path.
Upload(ctx context.Context, blobID, blobPath string, blobSize int64, forcePush bool) (*ocispec.Descriptor, error)
Check(blobID string) (bool, error)
Type() BackendType
Type() Type
}

// TODO: Directly forward blob data to storage backend

type BackendType = int
type Type = int

const (
OssBackend BackendType = iota
OssBackend Type = iota
RegistryBackend
)

func blobDesc(size int64, blobId string) ocispec.Descriptor {
blobDigest := digest.NewDigestFromEncoded(digest.SHA256, blobId)
func blobDesc(size int64, blobID string) ocispec.Descriptor {
blobDigest := digest.NewDigestFromEncoded(digest.SHA256, blobID)
desc := ocispec.Descriptor{
Digest: blobDigest,
Size: size,
Expand Down
6 changes: 3 additions & 3 deletions contrib/nydusify/pkg/backend/oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ func (b *OSSBackend) Upload(ctx context.Context, blobID, blobPath string, size i
}
blobSize := stat.Size()

var needMultiparts bool = false
var needMultiparts = false
// Blob size bigger than 100MB, apply multiparts upload.
if blobSize >= multipartsUploadThreshold {
needMultiparts = true
}

start := time.Now()
var crc64 uint64 = 0
var crc64 uint64
crc64ErrChan := make(chan error, 1)
go func() {
var e error
Expand Down Expand Up @@ -237,6 +237,6 @@ func (b *OSSBackend) Check(blobID string) (bool, error) {
return b.bucket.IsObjectExist(blobID)
}

func (r *OSSBackend) Type() BackendType {
func (b *OSSBackend) Type() Type {
return OssBackend
}
2 changes: 1 addition & 1 deletion contrib/nydusify/pkg/backend/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r *Registry) Check(blobID string) (bool, error) {
return true, nil
}

func (r *Registry) Type() BackendType {
func (r *Registry) Type() Type {
return RegistryBackend
}

Expand Down
32 changes: 16 additions & 16 deletions contrib/nydusify/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ type Cache struct {
// Remote is responsible for pulling & pushing cache image
remote *remote.Remote
// Store the pulled records from registry
pulledRecords map[digest.Digest]*CacheRecord
pulledRecords map[digest.Digest]*Record
// Store the records prepared to push to registry
pushedRecords []*CacheRecord
pushedRecords []*Record
}

// New creates Nydus cache instance,
Expand All @@ -67,14 +67,14 @@ func New(remote *remote.Remote, opt Opt) (*Cache, error) {
opt: opt,
remote: remote,
// source_layer_chain_id -> cache_record
pulledRecords: make(map[digest.Digest]*CacheRecord),
pushedRecords: []*CacheRecord{},
pulledRecords: make(map[digest.Digest]*Record),
pushedRecords: []*Record{},
}

return cache, nil
}

func (cache *Cache) recordToLayer(record *CacheRecord) (*ocispec.Descriptor, *ocispec.Descriptor) {
func (cache *Cache) recordToLayer(record *Record) (*ocispec.Descriptor, *ocispec.Descriptor) {
bootstrapCacheMediaType := ocispec.MediaTypeImageLayerGzip
if cache.opt.DockerV2Format {
bootstrapCacheMediaType = images.MediaTypeDockerSchema2LayerGzip
Expand Down Expand Up @@ -129,7 +129,7 @@ func (cache *Cache) exportRecordsToLayers() []ocispec.Descriptor {
return layers
}

func (cache *Cache) layerToRecord(layer *ocispec.Descriptor) *CacheRecord {
func (cache *Cache) layerToRecord(layer *ocispec.Descriptor) *Record {
sourceChainIDStr, ok := layer.Annotations[utils.LayerAnnotationNydusSourceChainID]
if !ok {
return nil
Expand Down Expand Up @@ -181,7 +181,7 @@ func (cache *Cache) layerToRecord(layer *ocispec.Descriptor) *CacheRecord {
},
}
}
return &CacheRecord{
return &Record{
SourceChainID: sourceChainID,
NydusBootstrapDesc: &bootstrapDesc,
NydusBlobDesc: nydusBlobDesc,
Expand All @@ -199,7 +199,7 @@ func (cache *Cache) layerToRecord(layer *ocispec.Descriptor) *CacheRecord {
utils.LayerAnnotationNydusBlob: "true",
},
}
return &CacheRecord{
return &Record{
SourceChainID: sourceChainID,
NydusBlobDesc: nydusBlobDesc,
}
Expand All @@ -208,9 +208,9 @@ func (cache *Cache) layerToRecord(layer *ocispec.Descriptor) *CacheRecord {
return nil
}

func mergeRecord(old, new *CacheRecord) *CacheRecord {
func mergeRecord(old, new *Record) *Record {
if old == nil {
old = &CacheRecord{
old = &Record{
SourceChainID: new.SourceChainID,
}
}
Expand All @@ -228,8 +228,8 @@ func mergeRecord(old, new *CacheRecord) *CacheRecord {
}

func (cache *Cache) importRecordsFromLayers(layers []ocispec.Descriptor) {
pulledRecords := make(map[digest.Digest]*CacheRecord)
pushedRecords := []*CacheRecord{}
pulledRecords := make(map[digest.Digest]*Record)
pushedRecords := []*Record{}

for _, layer := range layers {
record := cache.layerToRecord(&layer)
Expand Down Expand Up @@ -308,7 +308,7 @@ func (cache *Cache) Export(ctx context.Context) error {
mediaType = images.MediaTypeDockerSchema2Manifest
}

manifest := CacheManifest{
manifest := Manifest{
MediaType: mediaType,
Manifest: ocispec.Manifest{
Versioned: specs.Versioned{
Expand Down Expand Up @@ -355,7 +355,7 @@ func (cache *Cache) Import(ctx context.Context) error {
return errors.Wrap(err, "Read cache manifest")
}

var manifest CacheManifest
var manifest Manifest
if err := json.Unmarshal(manifestBytes, &manifest); err != nil {
return errors.Wrap(err, "Unmarshal cache manifest")
}
Expand All @@ -374,7 +374,7 @@ func (cache *Cache) Import(ctx context.Context) error {
}

// Check checks bootstrap & blob layer exists in registry or storage backend
func (cache *Cache) Check(ctx context.Context, layerChainID digest.Digest) (*CacheRecord, io.ReadCloser, io.ReadCloser, error) {
func (cache *Cache) Check(ctx context.Context, layerChainID digest.Digest) (*Record, io.ReadCloser, io.ReadCloser, error) {
record, ok := cache.pulledRecords[layerChainID]
if !ok {
return nil, nil, nil, nil
Expand Down Expand Up @@ -409,7 +409,7 @@ func (cache *Cache) Check(ctx context.Context, layerChainID digest.Digest) (*Cac
}

// Record puts new bootstrap & blob layer to cache record, it's a limited queue.
func (cache *Cache) Record(records []*CacheRecord) {
func (cache *Cache) Record(records []*Record) {
moveFront := map[digest.Digest]bool{}
for _, record := range records {
moveFront[record.SourceChainID] = true
Expand Down
8 changes: 4 additions & 4 deletions contrib/nydusify/pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/dragonflyoss/image-service/contrib/nydusify/pkg/utils"
)

func makeRecord(id int64, hashBlob bool) *CacheRecord {
func makeRecord(id int64, hashBlob bool) *Record {
var blobDesc *ocispec.Descriptor
idStr := strconv.FormatInt(id, 10)
if hashBlob {
Expand All @@ -27,7 +27,7 @@ func makeRecord(id int64, hashBlob bool) *CacheRecord {
Size: id,
}
}
return &CacheRecord{
return &Record{
SourceChainID: digest.FromString("chain-" + idStr),
NydusBootstrapDesc: &ocispec.Descriptor{
MediaType: ocispec.MediaTypeImageLayerGzip,
Expand Down Expand Up @@ -79,7 +79,7 @@ func testWithBackend(t *testing.T, _backend backend.Backend) {
})
assert.Nil(t, err)

exported := []*CacheRecord{
exported := []*Record{
makeRecord(1, true),
makeRecord(2, true),
makeRecord(3, false),
Expand All @@ -105,7 +105,7 @@ func testWithBackend(t *testing.T, _backend backend.Backend) {
}

cache.importRecordsFromLayers(layers)
cache.Record([]*CacheRecord{
cache.Record([]*Record{
makeRecord(4, true),
makeRecord(5, true),
})
Expand Down
4 changes: 2 additions & 2 deletions contrib/nydusify/pkg/cache/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

type CacheManifest struct {
type Manifest struct {
MediaType string `json:"mediaType,omitempty"`
ocispec.Manifest
}

type CacheRecord struct {
type Record struct {
SourceChainID digest.Digest
NydusBlobDesc *ocispec.Descriptor
NydusBootstrapDesc *ocispec.Descriptor
Expand Down
3 changes: 1 addition & 2 deletions contrib/nydusify/pkg/checker/tool/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ func NewBuilder(binaryPath string) *Builder {
// Check calls `nydus-image check` to parse Nydus bootstrap
// and output debug information to specified JSON file.
func (builder *Builder) Check(option BuilderOption) error {
var args []string
args = []string{
args := []string{
"check",
"--bootstrap",
option.BootstrapPath,
Expand Down
3 changes: 1 addition & 2 deletions contrib/nydusify/pkg/checker/tool/nydusd.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ func NewNydusd(conf NydusdConfig) (*Nydusd, error) {
func (nydusd *Nydusd) Mount() error {
nydusd.Umount()

var args []string
args = []string{
args := []string{
"--config",
nydusd.ConfigPath,
"--mountpoint",
Expand Down
8 changes: 4 additions & 4 deletions contrib/nydusify/pkg/converter/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func newCacheGlue(

func (cg *cacheGlue) Pull(
ctx context.Context, sourceLayerChainID digest.Digest,
) (*cache.CacheRecord, error) {
) (*cache.Record, error) {
if cg.cache == nil {
return nil, nil
}

var cacheRecord *cache.CacheRecord
var cacheRecord *cache.Record

// Using ChainID to ensure we can find corresponding overlayed
// Nydus blob/bootstrap layer in cache records.
Expand All @@ -78,7 +78,7 @@ func (cg *cacheGlue) Pull(
"ChainID": sourceLayerChainID,
})
// Pull the cached layer from cache image, then push to target namespace/repo,
// because the blob data is not shared between diffrent namespaces in registry,
// because the blob data is not shared between different namespaces in registry,
// this operation ensures that Nydus image owns these layers.
cacheRecord = _cacheRecord
defer bootstrapReader.Close()
Expand Down Expand Up @@ -176,7 +176,7 @@ func (cg *cacheGlue) Export(
// conversion progress as much as possible
cg.cache.Import(ctx)

cacheRecords := []*cache.CacheRecord{}
cacheRecords := []*cache.Record{}
for _, layer := range buildLayers {
record := layer.GetCacheRecord()
cacheRecords = append(cacheRecords, &record)
Expand Down
6 changes: 3 additions & 3 deletions contrib/nydusify/pkg/converter/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type buildLayer struct {
bootstrapsDir string
dockerV2Format bool

cacheRecord *cache.CacheRecord
cacheRecord *cache.Record
blobDesc *ocispec.Descriptor
bootstrapDesc *ocispec.Descriptor
bootstrapDiffID *digest.Digest
Expand Down Expand Up @@ -304,11 +304,11 @@ func (layer *buildLayer) Build(ctx context.Context) error {
return buildDone(nil)
}

func (layer *buildLayer) GetCacheRecord() cache.CacheRecord {
func (layer *buildLayer) GetCacheRecord() cache.Record {
if layer.cacheRecord != nil {
return *layer.cacheRecord
}
return cache.CacheRecord{
return cache.Record{
SourceChainID: layer.source.ChainID(),
NydusBlobDesc: layer.blobDesc,
NydusBootstrapDesc: layer.bootstrapDesc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: Apache-2.0

package file_exporter
package fileexporter

import (
"github.com/prometheus/client_golang/prometheus"
Expand Down
4 changes: 2 additions & 2 deletions contrib/nydusify/pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ func (parser *Parser) PullNydusBootstrap(ctx context.Context, image *Image) (io.
return reader, nil
}

func (p *Parser) matchImagePlatform(desc *ocispec.Descriptor) bool {
if p.interestedArch == desc.Platform.Architecture && desc.Platform.OS == "linux" {
func (parser *Parser) matchImagePlatform(desc *ocispec.Descriptor) bool {
if parser.interestedArch == desc.Platform.Architecture && desc.Platform.OS == "linux" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this suggestion from golangci-lint, we are never using single char as a variable name.

return true
}
return false
Expand Down
Loading