Skip to content

Commit

Permalink
支持arm架构
Browse files Browse the repository at this point in the history
  • Loading branch information
togettoyou committed Feb 1, 2024
1 parent 412405a commit ada3bd0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
6 changes: 4 additions & 2 deletions .github/ISSUE_TEMPLATE/hub-mirror.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ labels: ["hub-mirror"]
---

{
"platform":"",
"hub-mirror": [
"格式:你需要转换的原始镜像$自定义镜像名:自定义标签名",
"其中 $自定义镜像名:自定义标签名 是可选的",
"若对OS/ARCH无要求,platform请留空,不要加任何值,默认就是linux/amd64",
"如需切换arm架构,请修改platform为arm64或linux/arm64/v8",
"格式:你需要转换的原始镜像$自定义镜像名:自定义标签名 (其中 $自定义镜像名:自定义标签名 是可选的)",
"以下是三个正确示例",
"registry.k8s.io/kube-apiserver:v1.27.4",
"registry.k8s.io/kube-apiserver:v1.27.4$my-kube-apiserver",
Expand Down
21 changes: 13 additions & 8 deletions pkg/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Output struct {
Target string
}

func (c *Cli) Source2Target(source string) (*Output, error) {
func (c *Cli) Source2Target(source string, platform string) (*Output, error) {
if source == "" {
return nil, errors.New("source is nil")
}
Expand All @@ -72,16 +72,21 @@ func (c *Cli) Source2Target(source string) (*Output, error) {

if strings.Contains(source, "$") {
parts := strings.Split(source, "$")
if len(parts) > 1 {
source = parts[0]
target = parts[1]
}
source = parts[0]
target = parts[1]
}

if !strings.Contains(target, ":") && strings.Contains(source, ":") {
parts := strings.Split(source, ":")
if len(parts) > 1 {
target += ":" + parts[1]
target += ":" + parts[1]
}

if platform != "" {
if strings.Contains(target, ":") {
parts := strings.Split(target, ":")
target = parts[0] + "-" + strings.ReplaceAll(platform, "/", "-") + ":" + parts[1]
} else {
target += "-" + strings.ReplaceAll(platform, "/", "-")
}
}

Expand All @@ -98,7 +103,7 @@ func (c *Cli) Source2Target(source string) (*Output, error) {
}

func (c *Cli) PullTagPushImage(ctx context.Context, source, platform string) (*Output, error) {
output, err := c.Source2Target(source)
output, err := c.Source2Target(source, platform)
if err != nil {
return nil, err
}
Expand Down
46 changes: 38 additions & 8 deletions pkg/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,78 @@ func TestSource2Target(t *testing.T) {
repository: "",
}

output, err := cli.Source2Target("")
output, err := cli.Source2Target("", "")
assert.Nil(t, output)

source := "registry.k8s.io/kube-apiserver"
output, err = cli.Source2Target(source)
output, err = cli.Source2Target(source, "")
assert.Nil(t, err)
assert.Equal(t, source, output.Source)
assert.Equal(t, "docker.io/togettoyou/registry.k8s.io.kube-apiserver", output.Target)

source = "registry.k8s.io/kube-apiserver:v1.27.4"
output, err = cli.Source2Target(source)
output, err = cli.Source2Target(source, "")
assert.Nil(t, err)
assert.Equal(t, source, output.Source)
assert.Equal(t, "docker.io/togettoyou/registry.k8s.io.kube-apiserver:v1.27.4", output.Target)

source = "registry.k8s.io/kube-apiserver$kube-apiserver"
output, err = cli.Source2Target(source)
output, err = cli.Source2Target(source, "")
assert.Nil(t, err)
assert.Equal(t, "registry.k8s.io/kube-apiserver", output.Source)
assert.Equal(t, "docker.io/togettoyou/kube-apiserver", output.Target)

source = "registry.k8s.io/kube-apiserver:v1.27.4$kube-apiserver"
output, err = cli.Source2Target(source)
output, err = cli.Source2Target(source, "")
assert.Nil(t, err)
assert.Equal(t, "registry.k8s.io/kube-apiserver:v1.27.4", output.Source)
assert.Equal(t, "docker.io/togettoyou/kube-apiserver:v1.27.4", output.Target)

source = "registry.k8s.io/kube-apiserver:v1.27.4$kube-apiserver:mytag"
output, err = cli.Source2Target(source)
output, err = cli.Source2Target(source, "")
assert.Nil(t, err)
assert.Equal(t, "registry.k8s.io/kube-apiserver:v1.27.4", output.Source)
assert.Equal(t, "docker.io/togettoyou/kube-apiserver:mytag", output.Target)

source = "nginx@sha256:123456$nginx"
output, err = cli.Source2Target(source)
output, err = cli.Source2Target(source, "")
assert.Nil(t, err)
assert.Equal(t, "nginx@sha256:123456", output.Source)
assert.Equal(t, "docker.io/togettoyou/nginx:123456", output.Target)

source = "nginx@sha256:123456$nginx:mytag"
output, err = cli.Source2Target(source)
output, err = cli.Source2Target(source, "")
assert.Nil(t, err)
assert.Equal(t, "nginx@sha256:123456", output.Source)
assert.Equal(t, "docker.io/togettoyou/nginx:mytag", output.Target)

source = "golang"
output, err = cli.Source2Target(source, "linux/arm64/v8")
assert.Nil(t, err)
assert.Equal(t, "golang", output.Source)
assert.Equal(t, "docker.io/togettoyou/golang-linux-arm64-v8", output.Target)

source = "golang:1.21.6"
output, err = cli.Source2Target(source, "linux/arm64/v8")
assert.Nil(t, err)
assert.Equal(t, "golang:1.21.6", output.Source)
assert.Equal(t, "docker.io/togettoyou/golang-linux-arm64-v8:1.21.6", output.Target)

source = "golang:1.21.6$mygolang"
output, err = cli.Source2Target(source, "linux/arm64/v8")
assert.Nil(t, err)
assert.Equal(t, "golang:1.21.6", output.Source)
assert.Equal(t, "docker.io/togettoyou/mygolang-linux-arm64-v8:1.21.6", output.Target)

source = "golang:1.21.6$mygolang:1.21.6arm64"
output, err = cli.Source2Target(source, "linux/arm64/v8")
assert.Nil(t, err)
assert.Equal(t, "golang:1.21.6", output.Source)
assert.Equal(t, "docker.io/togettoyou/mygolang-linux-arm64-v8:1.21.6arm64", output.Target)

source = "registry.k8s.io/kube-apiserver:v1.27.4"
output, err = cli.Source2Target(source, "arm64")
assert.Nil(t, err)
assert.Equal(t, "registry.k8s.io/kube-apiserver:v1.27.4", output.Source)
assert.Equal(t, "docker.io/togettoyou/registry.k8s.io.kube-apiserver-arm64:v1.27.4", output.Target)
}

0 comments on commit ada3bd0

Please sign in to comment.