-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(main): add load tar files (#1)
* feature(main): add tar load feature Signed-off-by: cuisongliu <[email protected]> * feature(main): add tar load feature Signed-off-by: cuisongliu <[email protected]> * feature(main): add tar load feature Signed-off-by: cuisongliu <[email protected]> * feature(main): add tar main cmd Signed-off-by: cuisongliu <[email protected]> * feature(main): add tar main cmd Signed-off-by: cuisongliu <[email protected]> * feature(main): add tar main cmd Signed-off-by: cuisongliu <[email protected]> --------- Signed-off-by: cuisongliu <[email protected]>
- Loading branch information
1 parent
a6fbe3f
commit 2888d8d
Showing
16 changed files
with
283 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 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,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 | ||
} |
This file contains 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,29 @@ | ||
/* | ||
Copyright 2023 [email protected]. | ||
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) | ||
} |
This file contains 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 @@ | ||
docker.io/library/busybox:latest |
Binary file not shown.
This file contains 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 @@ | ||
docker-archive:config_main.tar@library/config_main |
This file contains 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 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 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 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,106 @@ | ||
// Copyright © 2021 sealos. | ||
// | ||
// 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 save | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/containers/image/v5/transports/alltransports" | ||
"strings" | ||
stdsync "sync" | ||
"time" | ||
|
||
"github.com/containers/image/v5/copy" | ||
itype "github.com/containers/image/v5/types" | ||
v1 "github.com/opencontainers/image-spec/specs-go/v1" | ||
"golang.org/x/sync/errgroup" | ||
|
||
"github.com/labring/sreg/pkg/registry/handler" | ||
"github.com/labring/sreg/pkg/registry/sync" | ||
httputils "github.com/labring/sreg/pkg/utils/http" | ||
"github.com/labring/sreg/pkg/utils/logger" | ||
) | ||
|
||
func NewImageTarSaver(ctx context.Context, maxPullProcs int) Registry { | ||
if ctx == nil { | ||
ctx = context.Background() | ||
} | ||
return &tmpTarRegistryImage{ | ||
ctx: ctx, | ||
maxPullProcs: maxPullProcs, | ||
} | ||
} | ||
|
||
type tmpTarRegistryImage struct { | ||
ctx context.Context | ||
maxPullProcs int | ||
} | ||
|
||
func (is *tmpTarRegistryImage) SaveImages(images []string, dir string, platform v1.Platform) ([]string, error) { | ||
logger.Debug("trying to save images: %+v for platform: %s", images, | ||
strings.Join([]string{platform.OS, platform.Architecture, platform.Variant}, ",")) | ||
config, err := handler.NewConfig(dir, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
config.Log.AccessLog.Disabled = true | ||
errCh := handler.Run(is.ctx, config) | ||
|
||
probeCtx, cancel := context.WithTimeout(is.ctx, time.Second*3) | ||
defer cancel() | ||
ep := sync.ParseRegistryAddress(localhost, config.HTTP.Addr) | ||
if err = httputils.WaitUntilEndpointAlive(probeCtx, "http://"+ep); err != nil { | ||
return nil, err | ||
} | ||
|
||
if platform.OS == "" { | ||
platform.OS = "linux" | ||
} | ||
sys := &itype.SystemContext{ | ||
ArchitectureChoice: platform.Architecture, | ||
OSChoice: platform.OS, | ||
VariantChoice: platform.Variant, | ||
DockerInsecureSkipTLSVerify: itype.OptionalBoolTrue, | ||
} | ||
eg, _ := errgroup.WithContext(is.ctx) | ||
numCh := make(chan struct{}, is.maxPullProcs) | ||
var outImages []string | ||
var mu stdsync.Mutex | ||
for index := range images { | ||
img := images[index] | ||
numCh <- struct{}{} | ||
eg.Go(func() error { | ||
mu.Lock() | ||
defer func() { | ||
<-numCh | ||
mu.Unlock() | ||
}() | ||
allImage := strings.Split(img, "@") | ||
srcRef, err := alltransports.ParseImageName(allImage[0]) | ||
if err != nil { | ||
return fmt.Errorf("invalid source name %s: %v", allImage[0], err) | ||
} | ||
err = sync.ArchiveToImage(is.ctx, sys, srcRef, fmt.Sprintf("%s/%s", ep, allImage[1]), copy.CopySystemImage) | ||
if err != nil { | ||
return fmt.Errorf("save image %s: %w", img, err) | ||
} | ||
outImages = append(outImages, img) | ||
return nil | ||
}) | ||
} | ||
err = eg.Wait() | ||
errCh <- err | ||
return outImages, err | ||
} |
This file contains 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 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,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.
This file contains 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 @@ | ||
docker-archive:config_main.tar@library/config_main |
This file contains 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