Skip to content

Commit

Permalink
fix: support psuh http registry image. (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
kycheng authored Oct 30, 2023
1 parent 59fa5af commit 8f71e81
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
30 changes: 19 additions & 11 deletions apis/artifacts/v1alpha1/image.go → registry/image.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 The Katanomi Authors.
Copyright 2023 The Katanomi Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,13 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1
package registry

import (
"context"
"fmt"
"net/http"

"github.com/go-resty/resty/v2"
artifactv1 "github.com/katanomi/pkg/apis/artifacts/v1alpha1"
"github.com/katanomi/pkg/client"
pclient "github.com/katanomi/pkg/plugin/client"
"github.com/katanomi/pkg/restclient"
Expand All @@ -35,7 +37,7 @@ import (
// PushEmptyImage an empty image will be created and pushed.
// The function will read the credentials from the context. If the credentials
// are empty, only the credentials will not be used for push.
func PushEmptyImage(ctx context.Context, uri URI) error {
func PushEmptyImage(ctx context.Context, uri artifactv1.URI) error {
log := logging.FromContext(ctx).Named("PushImage").With("ref", uri.String())
if uri.Host == "" || uri.Path == "" || uri.Tag == "" {
err := fmt.Errorf("repository host, path and tag must be set")
Expand All @@ -60,25 +62,31 @@ func PushEmptyImage(ctx context.Context, uri URI) error {
return err
}

var httpCli *http.Client
restyClient := restclient.RESTClient(ctx)
if restyClient != nil {
httpCli = restyClient.GetClient()
} else {
httpCli = client.NewHTTPClient()
}

repo, err := remote.NewRepository(uri.Repository())
if err != nil {
log.Errorw("failed to new repository", "err", err)
return err
}

schemeDeter := NewDefaultRegistrySchemeDetection(resty.NewWithClient(httpCli), true, true)
scheme, _ := schemeDeter.DetectScheme(ctx, uri.Host)
if scheme == HTTP {
repo.PlainHTTP = true
}

credential, err := extraAuthFromContext(ctx)
if err != nil {
log.Warnw("failed to extra auth, will try to push without credentials", "err", err)
}

var httpCli *http.Client
restyClient := restclient.RESTClient(ctx)
if restyClient != nil {
httpCli = restyClient.GetClient()
} else {
httpCli = client.NewHTTPClient()
}

// need to make sure ignore authentication is set.
// Warn: may pollute the global client.
client.InsecureSkipVerifyOption(httpCli)
Expand Down
15 changes: 8 additions & 7 deletions apis/artifacts/v1alpha1/image_test.go → registry/image_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 The Katanomi Authors.
Copyright 2023 The Katanomi Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1
package registry

import (
"bytes"
Expand All @@ -27,6 +27,7 @@ import (
"strings"
"testing"

artifactv1 "github.com/katanomi/pkg/apis/artifacts/v1alpha1"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

Expand Down Expand Up @@ -77,22 +78,22 @@ func mockRegistry(t *testing.T) *httptest.Server {

func TestPushEmptyImage(t *testing.T) {
tests := map[string]struct {
uri URI
uri artifactv1.URI
wantErr bool
}{
"input is not satisfied": {
uri: URI{Path: "repo"},
uri: artifactv1.URI{Path: "repo"},
wantErr: true,
},
"invalid repo": {
uri: URI{Path: "rep:^o", Tag: "tag"},
uri: artifactv1.URI{Path: "rep:^o", Tag: "tag"},
wantErr: true,
},
"normal push": {
uri: URI{Path: "repo", Tag: "tag"},
uri: artifactv1.URI{Path: "repo", Tag: "tag"},
},
"push failed": {
uri: URI{Path: "failed", Tag: "tag"},
uri: artifactv1.URI{Path: "failed", Tag: "tag"},
wantErr: true,
},
}
Expand Down

0 comments on commit 8f71e81

Please sign in to comment.