Skip to content

Commit

Permalink
add-keep-old-registry
Browse files Browse the repository at this point in the history
  • Loading branch information
learner0810 committed Mar 25, 2024
1 parent 1cafc62 commit d98d9e2
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 61 deletions.
78 changes: 45 additions & 33 deletions api/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ message Target {
string container_prefix_registry = 7;
string container_repository = 3;
string container_prefix_repository = 8;
bool append_origin_registry = 9;
string repo_name = 4;

Containers containers = 6;
Expand Down
3 changes: 2 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ func setAuthentication(source *api.Source, target *api.Target) error {
target.Containers = &api.Containers{Kind: api.Kind(api.Kind_value[kind]), Auth: &api.Containers_ContainerAuth{Username: username, Password: password, Registry: registry}}
}
} else {
username, password, kind, registry := viper.GetString("target.containers.auth.username"), viper.GetString("target.containers.auth.password"), viper.GetString("target.containers.kind"), viper.GetString("target.containerprefixregistry")
username, password, kind, registry := viper.GetString("target.containers.auth.username"), viper.GetString("target.containers.auth.password"),
viper.GetString("target.containers.kind"), viper.GetString("target.containerprefixregistry")

Check warning on line 130 in internal/config/config.go

View check run for this annotation

Codecov / codecov/patch

internal/config/config.go#L129-L130

Added lines #L129 - L130 were not covered by tests
if username != "" || password != "" {
target.Containers = &api.Containers{Kind: api.Kind(api.Kind_value[kind]), Auth: &api.Containers_ContainerAuth{Username: username, Password: password, Registry: registry}}
}
Expand Down
30 changes: 16 additions & 14 deletions pkg/syncer/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,19 +327,19 @@ func getRelok8sMoveRequest(source *api.Source, target *api.Target, chart *Chart,
return relok8sBundleLoadReq(
chart.TgzPath, outputChartPath,
target.GetContainerRegistry(), target.GetContainerRepository(),
target.GetContainerPrefixRegistry(), target.GetContainerPrefixRepository(), target.GetContainers().GetAuth()), packagedChartPath
target.GetContainerPrefixRegistry(), target.GetContainerPrefixRepository(), target.GetAppendOriginRegistry(), target.GetContainers().GetAuth()), packagedChartPath
} else {
// Direct syncing, SOURCE_REPO => TARGET_REPO
// Once https://github.com/vmware-tanzu/asset-relocation-tool-for-kubernetes/issues/94 is solved, we could
// specify the name we want for the output file. Until then, we should keep using this template thing
outputChartPath := filepath.Join(outdir, "%s-%s.tgz")
packagedChartPath := filepath.Join(outdir, fmt.Sprintf("%s-%s.tgz", chart.Name, chart.Version))
return relok8sMoveReq(chart.TgzPath, outputChartPath, target.GetContainerRegistry(), target.GetContainerRepository(),
target.GetContainerPrefixRegistry(), target.GetContainerPrefixRepository(), source.GetContainers().GetAuth(), target.GetContainers().GetAuth()), packagedChartPath
target.GetContainerPrefixRegistry(), target.GetContainerPrefixRepository(), target.GetAppendOriginRegistry(), source.GetContainers().GetAuth(), target.GetContainers().GetAuth()), packagedChartPath
}
}

func relok8sMoveReq(sourcePath, targetPath, containerRegistry, containerRepository, containerPrefixRegistry, containerPrefixRepository string, sourceAuth, targetAuth *api.Containers_ContainerAuth) *mover.ChartMoveRequest {
func relok8sMoveReq(sourcePath, targetPath, containerRegistry, containerRepository, containerPrefixRegistry, containerPrefixRepository string, appendOriginRegistry bool, sourceAuth, targetAuth *api.Containers_ContainerAuth) *mover.ChartMoveRequest {
req := &mover.ChartMoveRequest{
Source: mover.Source{
Chart: mover.ChartSpec{
Expand All @@ -351,11 +351,12 @@ func relok8sMoveReq(sourcePath, targetPath, containerRegistry, containerReposito
},
Target: mover.Target{
Rules: mover.RewriteRules{
Registry: containerRegistry,
PrefixRegistry: containerPrefixRegistry,
Repository: containerRepository,
PrefixRepository: containerPrefixRepository,
ForcePush: true,
Registry: containerRegistry,
PrefixRegistry: containerPrefixRegistry,
AppendOriginRegistry: appendOriginRegistry,
Repository: containerRepository,
PrefixRepository: containerPrefixRepository,
ForcePush: true,
},
Chart: mover.ChartSpec{
Local: &mover.LocalChart{
Expand Down Expand Up @@ -390,7 +391,7 @@ func relok8sBundleSaveReq(sourcePath, targetPath string, containerSourceAuth *ap
return req
}

func relok8sBundleLoadReq(sourcePath, targetPath, containerRegistry, containerRepository, containerPrefixRegistry, containerPrefixRepository string, containerTargetAuth *api.Containers_ContainerAuth) *mover.ChartMoveRequest {
func relok8sBundleLoadReq(sourcePath, targetPath, containerRegistry, containerRepository, containerPrefixRegistry, containerPrefixRepository string, appendOriginRegistry bool, containerTargetAuth *api.Containers_ContainerAuth) *mover.ChartMoveRequest {
req := &mover.ChartMoveRequest{
Source: mover.Source{
Chart: mover.ChartSpec{
Expand All @@ -401,11 +402,12 @@ func relok8sBundleLoadReq(sourcePath, targetPath, containerRegistry, containerRe
},
Target: mover.Target{
Rules: mover.RewriteRules{
Registry: containerRegistry,
PrefixRegistry: containerPrefixRegistry,
Repository: containerRepository,
PrefixRepository: containerPrefixRepository,
ForcePush: true,
Registry: containerRegistry,
PrefixRegistry: containerPrefixRegistry,
AppendOriginRegistry: appendOriginRegistry,
Repository: containerRepository,
PrefixRepository: containerPrefixRepository,
ForcePush: true,
},
Chart: mover.ChartSpec{
Local: &mover.LocalChart{
Expand Down
4 changes: 2 additions & 2 deletions pkg/syncer/sync_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func TestRelok8sMoveReq(t *testing.T) {
ContainersAuth: &mover.ContainersAuth{Credentials: &mover.OCICredentials{Server: "treg", Username: "tuser", Password: "tpass"}},
},
}
got := relok8sMoveReq("/tmp/source-dir", "/tmp/target-dir", "gcr.io", "my-project/containers", "", "",
got := relok8sMoveReq("/tmp/source-dir", "/tmp/target-dir", "gcr.io", "my-project/containers", "", "", false,
&api.Containers_ContainerAuth{Username: "suser", Password: "spass", Registry: "sreg"}, &api.Containers_ContainerAuth{Username: "tuser", Password: "tpass", Registry: "treg"},
)
if !reflect.DeepEqual(want, got) {
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestRelok8sBundleLoadReq(t *testing.T) {
ContainersAuth: &mover.ContainersAuth{Credentials: &mover.OCICredentials{Server: "reg", Username: "user", Password: "pass"}},
},
}
got := relok8sBundleLoadReq("/tmp/source-dir", "/tmp/target-dir", "gcr.io", "my-project/containers", "", "", &api.Containers_ContainerAuth{Username: "user", Password: "pass", Registry: "reg"})
got := relok8sBundleLoadReq("/tmp/source-dir", "/tmp/target-dir", "gcr.io", "my-project/containers", "", "", false, &api.Containers_ContainerAuth{Username: "user", Password: "pass", Registry: "reg"})
if !reflect.DeepEqual(want, got) {
t.Errorf("unexpected relok8s bundle load request. got: %v, want: %v", got, want)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import (
)

type OCIImageLocation struct {
Registry string
PrefixRegistry string
Repository string
PrefixRepository string
Registry string
PrefixRegistry string
Repository string
PrefixRepository string
AppendOriginRegistry bool
}
type RewriteAction struct {
Path string `json:"path"`
Expand Down Expand Up @@ -195,17 +196,25 @@ func (t *ImageTemplate) Apply(originalImage name.Repository, imageDigest string,
var rewrites []*RewriteAction

registry := originalImage.Registry.Name()
if rules.PrefixRegistry != "" {
switch {
case rules.PrefixRegistry != "" && rules.AppendOriginRegistry:
registry = fmt.Sprintf("%s/%s", rules.PrefixRegistry, registry)
case rules.PrefixRegistry != "":
registry = rules.PrefixRegistry
} else if rules.Registry != "" {
case rules.Registry != "":
registry = rules.Registry
}

// Repository path should contain the repositoryPrefix + imageName
repository := originalImage.RepositoryStr()
switch {
case rules.AppendOriginRegistry && rules.PrefixRegistry != "":
case rules.AppendOriginRegistry && rules.PrefixRepository != "" && rules.PrefixRegistry != "":
fallthrough
case rules.AppendOriginRegistry && rules.PrefixRepository != "":
repository = fmt.Sprintf("%s/%s", rules.PrefixRepository, repository)
case rules.PrefixRepository != "" && rules.PrefixRegistry != "":
repository = fmt.Sprintf("%s/%s/%s", rules.PrefixRepository, originalImage.Registry.Name(), repository)
repository = fmt.Sprintf("%s/%s/%s", originalImage.Registry.Name(), rules.PrefixRepository, repository)
case rules.PrefixRepository != "":
repository = fmt.Sprintf("%s/%s", rules.PrefixRepository, repository)
case rules.PrefixRegistry != "":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,10 +571,11 @@ func (cm *ChartMover) computeChanges(imageChanges []*internal.ImageChange, regis
var chartChanges []*internal.RewriteAction
imageCache := map[string]bool{}
rewriteRules := &internal.OCIImageLocation{
Registry: registryRules.Registry,
PrefixRegistry: registryRules.PrefixRegistry,
Repository: registryRules.Repository,
PrefixRepository: registryRules.PrefixRepository,
Registry: registryRules.Registry,
PrefixRegistry: registryRules.PrefixRegistry,
Repository: registryRules.Repository,
PrefixRepository: registryRules.PrefixRepository,
AppendOriginRegistry: registryRules.AppendOriginRegistry,
}

for _, change := range imageChanges {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package mover

import (
"errors"
"fmt"
"strings"

Expand All @@ -20,6 +21,9 @@ type RewriteRules struct {
Repository string
// PrefixRegistry add prefix of the registry
PrefixRepository string
// AppendOriginRegistry when setting up a PrefixRegistry, ture means that the original registry is appended to the
// new registry, and fasle means that it is moved to the Repositoy field.
AppendOriginRegistry bool
// Push the image even if there is already an image with a different digest
ForcePush bool
}
Expand All @@ -30,6 +34,10 @@ func (r *RewriteRules) Validate() error {
repository string
)

if r.PrefixRegistry == "" && r.AppendOriginRegistry {
return errors.New("AppendOriginRegistry can only be set to ture if PrefixRegistry is set.")
}

switch {
case r.Registry != "" && r.PrefixRegistry != "":
registry = fmt.Sprintf("%s/%s", r.PrefixRepository, r.Repository)
Expand Down

0 comments on commit d98d9e2

Please sign in to comment.