Skip to content

Commit

Permalink
fix lint error
Browse files Browse the repository at this point in the history
Signed-off-by: yxxhero <[email protected]>
  • Loading branch information
yxxhero committed Apr 17, 2022
1 parent fbe2220 commit c07c24d
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 37 deletions.
7 changes: 5 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ linters-settings:
gocyclo:
min-complexity: 42
gci:
local-prefixes: d7y.io/dragonfly/v2
sections:
- standard
- default
- prefix(d7y.io/dragonfly/v2)

linters:
disable-all: true
enable:
- gci
- gofmt
- golint
- golint
- misspell
- govet
- goconst
Expand Down
82 changes: 49 additions & 33 deletions manager/job/preheat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package job

import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -27,8 +27,11 @@ import (
"time"

machineryv1tasks "github.com/RichardKnop/machinery/v1/tasks"
"github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/manifest/schema2"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/remotes/docker"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"

Expand All @@ -38,9 +41,6 @@ import (
"d7y.io/dragonfly/v2/manager/model"
"d7y.io/dragonfly/v2/manager/types"
"d7y.io/dragonfly/v2/pkg/util/net/httputils"
"github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/remotes/docker"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

var tracer = otel.Tracer("manager")
Expand Down Expand Up @@ -169,13 +169,11 @@ func (p *preheat) getLayers(ctx context.Context, image *preheatImage, preheatArg
ctx, span := tracer.Start(ctx, config.SpanGetLayers, trace.WithSpanKind(trace.SpanKindProducer))
defer span.End()

resp, err := p.getManifests(ctx, image, preheatArgs.Username, preheatArgs.Password)
ocispecManifest, err := p.getManifests(ctx, image, preheatArgs.Username, preheatArgs.Password)
if err != nil {
return nil, err
}
defer resp.Close()

layers, err := p.parseLayers(resp, preheatArgs.URL, preheatArgs.Filter, httputils.MapToHeader(preheatArgs.Headers), image)
layers, err := p.parseLayers(ocispecManifest, preheatArgs.URL, preheatArgs.Filter, httputils.MapToHeader(preheatArgs.Headers), image)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -209,13 +207,7 @@ func (p *preheat) getResolver(ctx context.Context, username, password string) re
creds := func(string) (string, string, error) {
return username, password, nil
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
client := &http.Client{}
options := docker.ResolverOptions{}
options.Hosts = docker.ConfigureDefaultRegistries(
docker.WithClient(client),
Expand All @@ -228,42 +220,66 @@ func (p *preheat) getResolver(ctx context.Context, username, password string) re

}

func (p *preheat) getManifests(ctx context.Context, image *preheatImage, username, password string) (io.ReadCloser, error) {
func (p *preheat) getManifests(ctx context.Context, image *preheatImage, username, password string) (ocispec.Manifest, error) {

resolver := p.getResolver(ctx, username, password)

// combine image url and tag
i := fmt.Sprintf("%s/%s:%s", image.domain, image.name, image.tag)

_, desc, err := resolver.Resolve(ctx, i)
_, imageDesc, err := resolver.Resolve(ctx, i)
if err != nil {
return nil, err
return ocispec.Manifest{}, err
}
f, err := resolver.Fetcher(ctx, i)
if err != nil {
return nil, err
return ocispec.Manifest{}, err
}
r, err := f.Fetch(ctx, desc)
r, err := f.Fetch(ctx, imageDesc)
if err != nil {
return nil, fmt.Errorf("failed to fetch %s: %w", desc.Digest, err)
return ocispec.Manifest{}, fmt.Errorf("failed to fetch %s: %w", imageDesc.Digest, err)
}
defer r.Close()

return r, nil
}

func (p *preheat) parseLayers(r io.ReadCloser, url, filter string, header http.Header, image *preheatImage) ([]*internaljob.PreheatRequest, error) {
body, err := io.ReadAll(r)
descResponse, err := io.ReadAll(r)
if err != nil {
return nil, err
return ocispec.Manifest{}, fmt.Errorf("failed to read %s: %w", imageDesc.Digest, err)
}

manifest, _, err := distribution.UnmarshalManifest(schema2.MediaTypeManifest, body)
if err != nil {
return nil, err
platform := platforms.Only(platforms.DefaultSpec())
switch imageDesc.MediaType {
case images.MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest:

var manifest ocispec.Manifest
if err := json.Unmarshal(descResponse, &manifest); err != nil {
return ocispec.Manifest{}, err
}

if manifest.Config.Digest == imageDesc.Digest && (!platform.Match(*manifest.Config.Platform)) {
return ocispec.Manifest{}, fmt.Errorf("manifest: invalid platform %s: %w", manifest.Config.Platform, err)
}

return manifest, nil
case images.MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex:
var idx ocispec.Index
if err := json.Unmarshal(descResponse, &idx); err != nil {
return ocispec.Manifest{}, err
}
return ocispec.Manifest{
Versioned: idx.Versioned,
MediaType: idx.MediaType,
Layers: idx.Manifests,
Annotations: idx.Annotations,
}, nil
default:
return ocispec.Manifest{}, fmt.Errorf("unsupported manifest type %s", imageDesc.MediaType)
}
}

func (p *preheat) parseLayers(om ocispec.Manifest, url, filter string, header http.Header, image *preheatImage) ([]*internaljob.PreheatRequest, error) {

var layers []*internaljob.PreheatRequest
for _, v := range manifest.References() {
for _, v := range om.Layers {
digest := v.Digest.String()
layer := &internaljob.PreheatRequest{
URL: layerURL(image.protocol, image.domain, image.name, digest),
Expand Down
37 changes: 37 additions & 0 deletions manager/job/preheat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2020 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package job

import (
"context"
"testing"

"github.com/stretchr/testify/require"
)

// func TestGetManifests is a test function for GetManifests()
func TestGetManifests(t *testing.T) {
p := &preheat{}
_, err := p.getManifests(context.Background(), &preheatImage{
protocol: "https",
domain: "registry-1.docker.io",
name: "dragonflyoss/busybox",
tag: "1.35.0",
}, "", "")

require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion manager/types/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ type PreheatArgs struct {
URL string `json:"url" binding:"required"`
Filter string `json:"filter" binding:"omitempty"`
Headers map[string]string `json:"headers" binding:"omitempty"`
Username string `json:"username" binding:"omiempty"`
Username string `json:"username" binding:"omitempty"`
Password string `json:"password" binding:"omitempty"`
}
2 changes: 1 addition & 1 deletion test/e2e/manager/preheat.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var _ = Describe("Preheat with manager", func() {

out, err = fsPod.CurlCommand("POST", map[string]string{"Content-Type": "application/json"}, req,
fmt.Sprintf("http://%s:%s/%s", managerService, managerPort, preheatPath)).CombinedOutput()
fmt.Println(string(out))
fmt.Printf("preheat file %s outout: %s\n", url, string(out))
Expect(err).NotTo(HaveOccurred())

// wait for success
Expand Down

0 comments on commit c07c24d

Please sign in to comment.