diff --git a/cmd/kubevpn/cmds/duplicate.go b/cmd/kubevpn/cmds/duplicate.go index b2181cff..a8706620 100644 --- a/cmd/kubevpn/cmds/duplicate.go +++ b/cmd/kubevpn/cmds/duplicate.go @@ -76,6 +76,8 @@ func CmdDuplicate(f cmdutil.Factory) *cobra.Command { } return cmdutil.UsageErrorf(cmd, usageString) } + // special empty string, eg: --target-registry "" + duplicateOptions.IsChangeTargetRegistry = cmd.Flags().Changed("target-registry") connectOptions := handler.ConnectOptions{ Namespace: duplicateOptions.Namespace, diff --git a/pkg/handler/duplicate.go b/pkg/handler/duplicate.go index acb34b05..8ad38a55 100644 --- a/pkg/handler/duplicate.go +++ b/pkg/handler/duplicate.go @@ -9,7 +9,9 @@ import ( "strings" "time" + "github.com/docker/distribution/reference" "github.com/google/uuid" + log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -43,11 +45,12 @@ type DuplicateOptions struct { Workloads []string ExtraCIDR []string - TargetKubeconfig string - TargetNamespace string - TargetContainer string - TargetImage string - TargetRegistry string + TargetKubeconfig string + TargetNamespace string + TargetContainer string + TargetImage string + TargetRegistry string + IsChangeTargetRegistry bool isSame bool @@ -157,7 +160,6 @@ func (d *DuplicateOptions) DoDuplicate(ctx context.Context) error { return err } } - d.replaceRegistry(u) labelsMap := map[string]string{ config.ManageBy: config.ConfigMapPodTrafficManager, @@ -304,6 +306,9 @@ func (d *DuplicateOptions) DoDuplicate(ctx context.Context) error { if err = unstructured.SetNestedField(u.Object, append(containers, v.Object), containersPath...); err != nil { return err } + if err = d.replaceRegistry(u); err != nil { + return err + } _, createErr := client.Resource(object.Mapping.Resource).Namespace(d.TargetNamespace).Create(context.Background(), u, metav1.CreateOptions{}) //_, createErr := runtimeresource.NewHelper(object.Client, object.Mapping).Create(d.TargetNamespace, true, u) @@ -646,9 +651,10 @@ func (d *DuplicateOptions) setEnv(u *unstructured.Unstructured) error { return nil } -// todo replace origin registry with special registry for pulling image +// replace origin registry with special registry for pulling image func (d *DuplicateOptions) replaceRegistry(u *unstructured.Unstructured) error { - if d.TargetRegistry == "" { + // not pass this options, do nothing + if !d.IsChangeTargetRegistry { return nil } @@ -657,9 +663,29 @@ func (d *DuplicateOptions) replaceRegistry(u *unstructured.Unstructured) error { return err } - //for i, container := range temp.Spec.InitContainers { - // if container.Image - //} + for i, container := range temp.Spec.InitContainers { + oldImage := container.Image + named, err := reference.ParseNormalizedNamed(oldImage) + if err != nil { + return err + } + domain := reference.Domain(named) + newImage := strings.TrimPrefix(strings.ReplaceAll(oldImage, domain, d.TargetRegistry), "/") + temp.Spec.InitContainers[i].Image = newImage + log.Debugf("update init container: %s image: %s --> %s", container.Name, oldImage, newImage) + } + + for i, container := range temp.Spec.Containers { + oldImage := container.Image + named, err := reference.ParseNormalizedNamed(oldImage) + if err != nil { + return err + } + domain := reference.Domain(named) + newImage := strings.TrimPrefix(strings.ReplaceAll(oldImage, domain, d.TargetRegistry), "/") + temp.Spec.Containers[i].Image = newImage + log.Debugf("update container: %s image: %s --> %s", container.Name, oldImage, newImage) + } var marshal []byte if marshal, err = json.Marshal(temp.Spec); err != nil { diff --git a/pkg/handler/function_test.go b/pkg/handler/function_test.go index 49392714..c22b4a23 100644 --- a/pkg/handler/function_test.go +++ b/pkg/handler/function_test.go @@ -302,17 +302,12 @@ func init() { } func TestName(t *testing.T) { - name := "alpine@sha256:b733d4a32c4da6a00a84df2ca32791bb03df95400243648d8c539e7b4cce329c" + name := "docker.io/naison/alpine@sha256:b733d4a32c4da6a00a84df2ca32791bb03df95400243648d8c539e7b4cce329c" named, err := reference.ParseNormalizedNamed(name) if err != nil { t.Error(err) } - named = reference.TagNameOnly(named) domain := reference.Domain(named) path := reference.Path(named) - tagged, ok := named.(reference.Tagged) - if !ok { - t.Fail() - } - fmt.Println(domain, path, tagged) + fmt.Println(domain, path) }