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 519388d
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 75 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
25 changes: 13 additions & 12 deletions examples/test-load-dao-2048.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
source:
intermediateBundlesPath: charts-dir
target:
containerRegistry: release-ci.daocloud.io
# containerPrefixRegistry: release-ci.daocloud.io
containerRepository: charts-syncer-auto
#containerRegistry: release-ci.daocloud.io
containerPrefixRegistry: 10.5.14.100
appendOriginRegistry: true
#containerRepository: charts-syncer-auto
repo:
kind: HARBOR # or as any other supported Helm Chart repository kinds
url: https://release-ci.daocloud.io/chartrepo/charts-syncer-auto
# auth:
# username: [USERNAME]
# password: [PASSWORD]
# ## Container registry authentication
# containers:
# auth:
# username: [USERNAME]
# password: [PASSWORD]
url: https://10.5.14.100//chartrepo/gcr.m.daocloud.io
auth:
username: admin
password: "Harbor123456"
## Container registry authentication
containers:
auth:
username: admin
password: "Harbor123456"
4 changes: 0 additions & 4 deletions examples/test-save-dao-2048.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ target:
intermediateBundlesPath: charts-dir

charts:
- name: coredns-metrics
skipImages: true
versions:
- 0.1.0
- name: dao-2048
versions:
- 1.2.1
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")
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
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,23 @@ 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 != "":
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 519388d

Please sign in to comment.