From b76354fae8f57f6e8e93e542f21e936195fb6418 Mon Sep 17 00:00:00 2001 From: Archit Sharma Date: Fri, 3 Nov 2023 18:24:54 +0530 Subject: [PATCH] temp Signed-off-by: Archit Sharma --- go.mod | 3 +-- pkg/oci/pull.go | 67 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 8e0b5847b..3ee296e95 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,6 @@ require ( github.com/mattn/go-isatty v0.0.19 github.com/mholt/archiver/v3 v3.5.1 github.com/microsoft/go-mssqldb v1.6.0 - github.com/opencontainers/image-spec v1.1.0-rc4 github.com/oras-project/oras-credentials-go v0.3.0 github.com/pkg/errors v0.9.1 github.com/replicatedhq/termui/v3 v3.1.1-0.20200811145416-f40076d26851 @@ -48,7 +47,6 @@ require ( k8s.io/cli-runtime v0.28.2 k8s.io/client-go v0.28.2 k8s.io/klog/v2 v2.100.1 - oras.land/oras-go v1.2.4-0.20230908093003-773868ab83e0 oras.land/oras-go/v2 v2.3.0 sigs.k8s.io/controller-runtime v0.16.2 sigs.k8s.io/e2e-framework v0.3.0 @@ -90,6 +88,7 @@ require ( github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/onsi/ginkgo v1.14.0 // indirect + github.com/opencontainers/image-spec v1.1.0-rc4 // indirect github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect github.com/rubenv/sql-migrate v1.3.1 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect diff --git a/pkg/oci/pull.go b/pkg/oci/pull.go index 8f89b0fbd..167e96579 100644 --- a/pkg/oci/pull.go +++ b/pkg/oci/pull.go @@ -16,15 +16,10 @@ import ( "github.com/replicatedhq/troubleshoot/internal/util" "github.com/replicatedhq/troubleshoot/pkg/version" "k8s.io/klog/v2" - "oras.land/oras-go/pkg/auth" dockerauth "oras.land/oras-go/pkg/auth/docker" - "oras.land/oras-go/pkg/content" - "oras.land/oras-go/pkg/oras" - "oras.land/oras-go/pkg/registry" + oras "oras.land/oras-go/v2" - "oras.land/oras-go/v2/content" "oras.land/oras-go/v2/content/memory" - "oras.land/oras-go/v2/content/oci" "oras.land/oras-go/v2/registry" // "github.com/replicatedhq/troubleshoot/pkg/version" // oras "oras.land/oras-go/v2" @@ -35,6 +30,13 @@ import ( "oras.land/oras-go/v2/registry/remote" "oras.land/oras-go/v2/registry/remote/auth" // "oras.land/oras-go/v2/registry/remote/retry" + credentials "github.com/oras-project/oras-credentials-go" + "oras.land/oras-go/v2" + "oras.land/oras-go/v2/content" + "oras.land/oras-go/v2/content/oci" + "oras.land/oras-go/v2/registry/remote" + "oras.land/oras-go/v2/registry/remote/auth" + "oras.land/oras-go/v2/registry/remote/retry" ) const ( @@ -53,6 +55,59 @@ func PullSupportBundleFromOCI(uri string) ([]byte, error) { return pullFromOCI(context.Background(), uri, "replicated.supportbundle.spec", "replicated-supportbundle") } +func pullImage(uri string, mediaType string, imageName string) ([]byte, error) { + + // 0. Create an OCI layout store + tempDir, err := os.MkdirTemp("", "oci-layout-root") + if err != nil { + return err + } + + store, err := oci.NewLayout(tempDir) + if err != nil { + return err + } + + // 1. Connect to a remote repository + ctx := context.Background() + reg := "registry.replicated.com" + repo, err := remote.NewRepository(reg + "/library/replicated") + if err != nil { + return err + } + + // 2. Get credentials from the docker credential store + storeOpts := credentials.StoreOptions{} + credStore, err := credentials.NewStoreFromDocker(storeOpts) + if err != nil { + return err + } + + // Prepare the auth client for the registry and credential store + repo.Client = &auth.Client{ + Client: retry.DefaultClient, + Cache: auth.DefaultCache, + Credential: credentials.Credential(credStore), // Use the credential store + } + + // 3. Copy from the remote repository to the OCI layout store + tag := "1.0.0-beta.10" + manifestDescriptor, err := oras.Copy(ctx, repo, tag, store, tag, oras.DefaultCopyOptions) + if err != nil { + return err + } + + fmt.Println("manifest pulled:", manifestDescriptor.Digest, manifestDescriptor.MediaType) + + // 3. Fetch from OCI layout store to verify + fetched, err := content.FetchAll(ctx, store, manifestDescriptor) + if err != nil { + return err + } + fmt.Printf("manifest content:\n%s", fetched) + return nil +} + // PullSpecsFromOCI pulls both the preflight and support bundle specs from the given URI // // The URI is expected to be the same as the one used to install your KOTS application