Skip to content

Commit

Permalink
feat: Add a method to KpmClient to fetch the oci manifest
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe committed Oct 23, 2023
1 parent 0de32be commit ba2be89
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,20 @@ func (c *KpmClient) pullTarFromOci(localPath string, ociOpts *opt.OciOptions) er
return nil
}

// FetchOciManifestConfIntoJsonStr will fetch the oci manifest config of the kcl package from the oci registry and return it into json string.
func (c *KpmClient) FetchOciManifestIntoJsonStr(opts opt.OciFetchOptions) (string, error) {
ociCli, err := oci.NewOciClient(opts.Reg, opts.Repo, &c.settings)
if err != nil {
return "", err
}

manifestJson, err := ociCli.FetchManifestIntoJsonStr(opts)
if err != nil {
return "", err
}
return manifestJson, nil
}

// check sum for a Dependency.
func check(dep pkg.Dependency, newDepPath string) bool {
if dep.Sum == "" {
Expand Down
11 changes: 11 additions & 0 deletions pkg/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ func (ociClient *OciClient) PushWithOciManifest(localPath, tag string, opts *opt
return nil
}

// FetchManifestByRef will fetch the manifest and return it into json string.
func (ociClient *OciClient) FetchManifestIntoJsonStr(opts opt.OciFetchOptions) (string, error) {
fetchOpts := opts.FetchBytesOptions
_, manifestContent, err := oras.FetchBytes(*ociClient.ctx, ociClient.repo, opts.Tag, fetchOpts)
if err != nil {
return "", err
}

return string(manifestContent), nil
}

func loadCredential(hostName string, settings *settings.Settings) (*remoteauth.Credential, error) {
authClient, err := dockerauth.NewClientWithDockerFallback(settings.CredentialsFile)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/opt/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/errors"
"kcl-lang.io/kpm/pkg/reporter"
"oras.land/oras-go/v2"
)

// CompileOptions is the input options of 'kpm run'.
Expand Down Expand Up @@ -218,3 +219,9 @@ func (oci *OciOptions) AddStoragePathSuffix(pathPrefix string) string {
type OciManifestOptions struct {
Annotations map[string]string
}

// OciFetchOptions is the input options of the api to fetch oci manifest.
type OciFetchOptions struct {
oras.FetchBytesOptions
OciOptions
}
1 change: 1 addition & 0 deletions scripts/pkg_in_reg/kcl1/kcl.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "kcl1"
edition = "0.0.1"
version = "0.0.1"
description = "This is the kcl package named kcl1"

[dependencies]
k8s = "1.27"
1 change: 1 addition & 0 deletions scripts/pkg_in_reg/kcl2/kcl.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "kcl2"
edition = "0.0.1"
version = "0.0.1"
description = "This is the kcl package named kcl2"

[dependencies]
kcl1 = "0.0.1"
Expand Down
23 changes: 23 additions & 0 deletions test/e2e/kpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/constants"
"kcl-lang.io/kpm/pkg/opt"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
Expand Down Expand Up @@ -208,6 +210,27 @@ var _ = ginkgo.Describe("Kpm CLI Testing", func() {
gomega.Expect(manifest_expect.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_DESCRIPTION]).
To(gomega.Equal(manifest_got.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_DESCRIPTION]))
})

ginkgo.It("testing 'fetch api '", func() {
kpmcli, err := client.NewKpmClient()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
jsonstr, err := kpmcli.FetchOciManifestIntoJsonStr(opt.OciFetchOptions{
FetchBytesOptions: oras.DefaultFetchBytesOptions,
OciOptions: opt.OciOptions{
Reg: "localhost:5001",
Repo: "test/kcl2",
Tag: "0.0.1",
},
})
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
var manifest_expect v1.Manifest
err = json.Unmarshal([]byte(jsonstr), &manifest_expect)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(len(manifest_expect.Annotations)).To(gomega.Equal(4))
gomega.Expect(manifest_expect.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_NAME]).To(gomega.Equal("kcl2"))
gomega.Expect(manifest_expect.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_VERSION]).To(gomega.Equal("0.0.1"))
gomega.Expect(manifest_expect.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_DESCRIPTION]).To(gomega.Equal("This is the kcl package named kcl2"))
})
}
})
})

0 comments on commit ba2be89

Please sign in to comment.