-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add command to load images into custom registry #531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NautiluX
wants to merge
2
commits into
main
Choose a base branch
from
pull-bom-images
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright (c) Codesphere Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/codesphere-cloud/cs-go/pkg/io" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // LoadCmd represents the load command. | ||
| type LoadCmd struct { | ||
| cmd *cobra.Command | ||
| } | ||
|
|
||
| func AddLoadCmd(rootCmd *cobra.Command, opts *GlobalOptions) { | ||
| load := LoadCmd{ | ||
| cmd: &cobra.Command{ | ||
| Use: "load", | ||
| Short: "Load resources into a local or custom registry", | ||
| Long: io.Long(`Load resources from external sources into a local or custom registry, | ||
| e.g. mirror images from GHCR.`), | ||
| }, | ||
| } | ||
|
|
||
| AddCmd(rootCmd, load.cmd) | ||
| AddLoadImagesCmd(load.cmd, opts) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Copyright (c) Codesphere Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| packageio "github.com/codesphere-cloud/cs-go/pkg/io" | ||
| "github.com/codesphere-cloud/oms/internal/env" | ||
| "github.com/codesphere-cloud/oms/internal/installer" | ||
| "github.com/codesphere-cloud/oms/internal/installer/bom" | ||
| "github.com/codesphere-cloud/oms/internal/registry" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const packageBomJSON = "bom.json" | ||
|
|
||
| type LoadImagesCmd struct { | ||
| cmd *cobra.Command | ||
| Opts *LoadImagesOpts | ||
| Copier registry.ImageCopier | ||
| Env env.Env | ||
| } | ||
|
|
||
| type LoadImagesOpts struct { | ||
| *GlobalOptions | ||
| DryRun bool | ||
| Force bool | ||
| } | ||
|
|
||
| func AddLoadImagesCmd(load *cobra.Command, opts *GlobalOptions) { | ||
| c := &LoadImagesCmd{ | ||
| cmd: &cobra.Command{ | ||
| Use: "images <package> <target-registry>", | ||
| Short: "Mirror all Codesphere OCI images required to install package from Codesphere's registry", | ||
| Long: packageio.Long(`Mirror all Codesphere OCI images required to install package from Codesphere's registry into a target registry. | ||
| This is required for installations that require a custom registry, such as air-gapped environments. | ||
|
|
||
| Ensure that the target registry is reachable and that you have permission to push images to it. | ||
| Registry authentication is read from local container registry credentials, such as Docker config, | ||
| Docker credential helpers, or Podman auth files. | ||
|
|
||
| Logging in to the source and target registry before running this command is required. | ||
|
|
||
| To use the custom registry, it must be configured using Codesphere's configuration file before installing Codesphere.`), | ||
| Example: formatExamples("load images", []packageio.Example{ | ||
| { | ||
| Cmd: "codesphere-v1.68.0.tar.gz registry.internal.example.com", | ||
| Desc: "Mirror every Codesphere OCI image required for Codesphere 1.68.0 into the target registry", | ||
| }, | ||
| }), | ||
| Args: cobra.ExactArgs(2), | ||
| }, | ||
| Opts: &LoadImagesOpts{ | ||
| GlobalOptions: opts, | ||
| }, | ||
| Env: env.NewEnv(), | ||
| } | ||
|
|
||
| c.cmd.Flags().BoolVar(&c.Opts.DryRun, "dry-run", false, "Print planned copy operations without copying images") | ||
| c.cmd.Flags().BoolVarP(&c.Opts.Force, "force", "f", false, "Force new package extraction even if already extracted") | ||
|
|
||
| AddCmd(load, c.cmd) | ||
| c.cmd.RunE = c.RunE | ||
| } | ||
|
|
||
| func (c *LoadImagesCmd) RunE(cmd *cobra.Command, args []string) error { | ||
| pm := installer.NewPackage(c.Env.GetOmsWorkdir(), args[0]) | ||
| return c.LoadImagesFromPackage(cmd.Context(), pm, args[1]) | ||
| } | ||
|
|
||
| func (c *LoadImagesCmd) LoadImagesFromPackage(ctx context.Context, pm installer.PackageManager, targetRegistry string) error { | ||
| bomPath, err := c.extractPackageBom(pm) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return c.LoadImagesFromBomPath(ctx, bomPath, targetRegistry) | ||
| } | ||
|
|
||
| func (c *LoadImagesCmd) LoadImagesFromBomPath(ctx context.Context, bomPath string, targetRegistry string) error { | ||
| config, err := bom.Parse(bomPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| copier := c.Copier | ||
| if !c.Opts.DryRun && copier == nil { | ||
| copier = registry.NewRegistryImageCopier(ctx) | ||
| } | ||
|
|
||
| mirror := registry.Mirror{ | ||
| Copier: copier, | ||
| DryRun: c.Opts.DryRun, | ||
| } | ||
| _, err = mirror.MirrorImages(config.ImageReferencesForCodesphereRegistry(), targetRegistry) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to load images from %s: %w", bomPath, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (c *LoadImagesCmd) extractPackageBom(pm installer.PackageManager) (string, error) { | ||
| if err := pm.ExtractDependency(packageBomJSON, c.Opts.Force); err != nil { | ||
| return "", fmt.Errorf("failed to extract %s from package: %w", packageBomJSON, err) | ||
| } | ||
|
|
||
| return pm.GetDependencyPath(packageBomJSON), nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| // Copyright (c) Codesphere Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package cmd_test | ||
|
|
||
| import ( | ||
| "archive/tar" | ||
| "bytes" | ||
| "compress/gzip" | ||
| "context" | ||
| "errors" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/codesphere-cloud/oms/cli/cmd" | ||
| "github.com/codesphere-cloud/oms/internal/installer" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| type fakeImageCopier struct { | ||
| calls [][2]string | ||
| err error | ||
| } | ||
|
|
||
| func (r *fakeImageCopier) Copy(sourceRef string, destinationRef string) error { | ||
| r.calls = append(r.calls, [2]string{sourceRef, destinationRef}) | ||
| return r.err | ||
| } | ||
|
|
||
| var _ = Describe("LoadImages", func() { | ||
| var ( | ||
| tempDir string | ||
| newPackage func() installer.PackageManager | ||
| ) | ||
|
|
||
| BeforeEach(func() { | ||
| tempDir = GinkgoT().TempDir() | ||
| packagePath := filepath.Join(tempDir, "codesphere-test.tar.gz") | ||
| Expect(createLoadImagesTestPackage(packagePath, "bom.json", `{ | ||
| "components": { | ||
| "clusterPki": { | ||
| "files": { | ||
| "chart": { | ||
| "ociRef": "ghcr.io/codesphere-cloud/charts/cluster-pki:0.1.6" | ||
| } | ||
| }, | ||
| "containerImages": { | ||
| "cronjob": "ghcr.io/codesphere-cloud/docker/alpine/kubectl:1.34.2" | ||
| } | ||
| } | ||
| } | ||
| }`)).To(Succeed()) | ||
|
|
||
| newPackage = func() installer.PackageManager { | ||
| return installer.NewPackage(filepath.Join(tempDir, "oms-workdir"), packagePath) | ||
| } | ||
| }) | ||
|
|
||
| It("copies refs with fake copier", func() { | ||
| copier := &fakeImageCopier{} | ||
| c := &cmd.LoadImagesCmd{ | ||
| Opts: &cmd.LoadImagesOpts{}, | ||
| Copier: copier, | ||
| } | ||
|
|
||
| err := c.LoadImagesFromPackage(context.TODO(), newPackage(), "registry.internal.example.com") | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(copier.calls).To(Equal([][2]string{ | ||
| { | ||
| "ghcr.io/codesphere-cloud/charts/cluster-pki:0.1.6", | ||
| "registry.internal.example.com/codesphere-cloud/charts/cluster-pki:0.1.6", | ||
| }, | ||
| { | ||
| "ghcr.io/codesphere-cloud/docker/alpine/kubectl:1.34.2", | ||
| "registry.internal.example.com/codesphere-cloud/docker/alpine/kubectl:1.34.2", | ||
| }, | ||
| })) | ||
| }) | ||
|
|
||
| It("does not call the copier during dry-run", func() { | ||
| copier := &fakeImageCopier{} | ||
| c := &cmd.LoadImagesCmd{ | ||
| Opts: &cmd.LoadImagesOpts{DryRun: true}, | ||
| Copier: copier, | ||
| } | ||
|
|
||
| err := c.LoadImagesFromPackage(context.TODO(), newPackage(), "registry.internal.example.com") | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(copier.calls).To(BeEmpty()) | ||
| }) | ||
|
|
||
| It("propagates copy errors", func() { | ||
| copier := &fakeImageCopier{err: errors.New("copy failed")} | ||
| c := &cmd.LoadImagesCmd{ | ||
| Opts: &cmd.LoadImagesOpts{}, | ||
| Copier: copier, | ||
| } | ||
|
|
||
| err := c.LoadImagesFromPackage(context.TODO(), newPackage(), "registry.internal.example.com") | ||
| Expect(err).To(MatchError(ContainSubstring("copy failed"))) | ||
| }) | ||
| }) | ||
|
|
||
| func createLoadImagesTestPackage(filename string, bomName string, bomContent string) error { | ||
| depsContent, err := createLoadImagesDepsArchive(map[string]string{ | ||
| bomName: bomContent, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| file, err := os.Create(filename) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer func() { _ = file.Close() }() | ||
|
|
||
| gzw := gzip.NewWriter(file) | ||
| defer func() { _ = gzw.Close() }() | ||
|
|
||
| tw := tar.NewWriter(gzw) | ||
| defer func() { _ = tw.Close() }() | ||
|
|
||
| header := &tar.Header{ | ||
| Name: "deps.tar.gz", | ||
| Mode: 0o600, | ||
| Size: int64(len(depsContent)), | ||
| } | ||
| if err := tw.WriteHeader(header); err != nil { | ||
| return err | ||
| } | ||
| _, err = tw.Write(depsContent) | ||
| return err | ||
| } | ||
|
|
||
| func createLoadImagesDepsArchive(files map[string]string) ([]byte, error) { | ||
| var buf bytes.Buffer | ||
| gzw := gzip.NewWriter(&buf) | ||
| tw := tar.NewWriter(gzw) | ||
|
|
||
| for name, content := range files { | ||
| header := &tar.Header{ | ||
| Name: name, | ||
| Mode: 0o600, | ||
| Size: int64(len(content)), | ||
| } | ||
| if err := tw.WriteHeader(header); err != nil { | ||
| return nil, err | ||
| } | ||
| if _, err := tw.Write([]byte(content)); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| if err := tw.Close(); err != nil { | ||
| return nil, err | ||
| } | ||
| if err := gzw.Close(); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return buf.Bytes(), nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated fix that was necessary to run the tests locally.Seems they aren't executed in the CI