Skip to content

Commit

Permalink
feature(main): add tar load feature
Browse files Browse the repository at this point in the history
Signed-off-by: cuisongliu <cuisongliu@qq.com>
  • Loading branch information
cuisongliu committed Sep 27, 2023
1 parent 33663d0 commit b5db3fd
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pkg/buildimage/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ const (
ManifestsDirName = "manifests"
ImagesDirName = "images"
ImageShimDirName = "shim"
ImageTarDirName = "tar"
ImageTarConfigName = "config.txt"
ImageSkopeoDirName = "skopeo"
ImageTarConfigName = "tar.txt"
)
62 changes: 62 additions & 0 deletions pkg/buildimage/tar_images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright © 2021 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package buildimage

import (
"fmt"
"github.com/labring/sreg/pkg/utils/file"
"github.com/labring/sreg/pkg/utils/logger"
"path"
"strings"
)

func TarList(dir string) ([]string, error) {
wrapGetImageErr := func(err error, s string) error {
return fmt.Errorf("failed to get images in %s: %w", s, err)
}
tarDir := path.Join(dir, ImagesDirName, ImageSkopeoDirName)
if !file.IsExist(path.Join(tarDir, ImageTarConfigName)) {
logger.Warn("image tar config %s is not exists,skip", path.Join(tarDir, ImageTarConfigName))
return nil, nil
}

images, err := file.ReadLines(path.Join(tarDir, ImageTarConfigName))
if err != nil {
return nil, wrapGetImageErr(err, tarDir)
}
for i, image := range images {
parts := strings.SplitN(image, "@", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid image format: %s", image)
}
if strings.HasPrefix(parts[0], "docker-archive:") || strings.HasPrefix(parts[0], "oci-archive:") {
partOne := parts[0]

partOneSpilt := strings.SplitN(partOne, ":", 2)
if len(partOneSpilt) == 2 {
originalPath := partOneSpilt[1]
// 将原始路径和路径部分组合
modifiedPath := path.Join(tarDir, originalPath)
// 重新构建 partOne
partOne = partOneSpilt[0] + ":" + modifiedPath
}
parts[0] = partOne
images[i] = strings.Join(parts, "@")
} else {
return nil, fmt.Errorf("invalid image format: %s , must prefix in docker-archive or oci-archive", image)
}
}
return images, nil
}
29 changes: 29 additions & 0 deletions pkg/buildimage/tar_images_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2023 cuisongliu@qq.com.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package buildimage

import (
"testing"
)

func TestTarList(t *testing.T) {
data, err := TarList("test/testregistrytar")
if err != nil {
t.Fatal(err)
}
t.Log(data)
}
1 change: 1 addition & 0 deletions pkg/buildimage/test/testregistrytar/images/shim/image.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker.io/library/busybox:latest
1 change: 1 addition & 0 deletions pkg/buildimage/test/testregistrytar/images/skopeo/tar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-archive:config_main.tar@library/config_main
12 changes: 12 additions & 0 deletions pkg/registry/commands/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ func NewRegistryImageSaveCmd(examplePrefix string) *cobra.Command {
return err
}
logger.Info("images pulled: %+v", outImages)
tarIs := save.NewImageTarSaver(context.Background(), flagsResults.registryPullMaxPullProcs)
tars, err := buildimage.TarList(args[0])
if err != nil {
return err
}
if len(tars) != 0 {
outTars, err := tarIs.SaveImages(tars, flagsResults.registryPullRegistryDir, v1.Platform{OS: "linux", Architecture: flagsResults.registryPullArch})
if err != nil {
return err
}
logger.Info("images tar saved: %+v", outTars)
}
return nil
},
PreRunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/save/save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestSaveTarImages(t *testing.T) {
save := NewImageTarSaver(context.TODO(), 5)
t.Run("no credentials found", func(t *testing.T) {
_ = os.Mkdir("testdata/registry", 0755)
imgs, err := save.SaveImages([]string{"docker-archive:testregistrytar/images/tar/config_main.tar@library/config_main"}, "testdata/registry", v1.Platform{
imgs, err := save.SaveImages([]string{"docker-archive:testregistrytar/images/skopeo/config_main.tar@library/config_main"}, "testdata/registry", v1.Platform{
Architecture: "amd64",
OS: "linux",
})
Expand Down
5 changes: 5 additions & 0 deletions pkg/registry/save/testregistrytar/images/shim/image.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
registry.k8s.io/autoscaling/vpa-admission-controller:0.13.0
registry.k8s.io/autoscaling/vpa-recommender:0.13.0
registry.k8s.io/autoscaling/vpa-updater:0.13.0
registry.k8s.io/redis:e2e
registry.k8s.io/ubuntu-slim:0.1
Binary file not shown.
1 change: 1 addition & 0 deletions pkg/registry/save/testregistrytar/images/skopeo/tar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-archive:config_main.tar@library/config_main

0 comments on commit b5db3fd

Please sign in to comment.