diff --git a/pkg/client/client.go b/pkg/client/client.go index 5dae201b..2ff28377 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -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 == "" { diff --git a/pkg/oci/oci.go b/pkg/oci/oci.go index bc14502b..24e042fa 100644 --- a/pkg/oci/oci.go +++ b/pkg/oci/oci.go @@ -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 { diff --git a/pkg/opt/opt.go b/pkg/opt/opt.go index 0d30b8c0..e1b15d67 100644 --- a/pkg/opt/opt.go +++ b/pkg/opt/opt.go @@ -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'. @@ -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 +} diff --git a/scripts/pkg_in_reg/kcl1/kcl.mod b/scripts/pkg_in_reg/kcl1/kcl.mod index 4d4f2dc5..4c4ac72f 100644 --- a/scripts/pkg_in_reg/kcl1/kcl.mod +++ b/scripts/pkg_in_reg/kcl1/kcl.mod @@ -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" diff --git a/scripts/pkg_in_reg/kcl2/kcl.mod b/scripts/pkg_in_reg/kcl2/kcl.mod index 1cbda44b..428177b7 100644 --- a/scripts/pkg_in_reg/kcl2/kcl.mod +++ b/scripts/pkg_in_reg/kcl2/kcl.mod @@ -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" diff --git a/test/e2e/kpm_test.go b/test/e2e/kpm_test.go index f5fa7c06..ffa0c354 100644 --- a/test/e2e/kpm_test.go +++ b/test/e2e/kpm_test.go @@ -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" @@ -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")) + }) } }) })