mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-10-05 07:16:54 +08:00
feat: add options target-registry for duplicate mode
This commit is contained in:
@@ -76,6 +76,8 @@ func CmdDuplicate(f cmdutil.Factory) *cobra.Command {
|
|||||||
}
|
}
|
||||||
return cmdutil.UsageErrorf(cmd, usageString)
|
return cmdutil.UsageErrorf(cmd, usageString)
|
||||||
}
|
}
|
||||||
|
// special empty string, eg: --target-registry ""
|
||||||
|
duplicateOptions.IsChangeTargetRegistry = cmd.Flags().Changed("target-registry")
|
||||||
|
|
||||||
connectOptions := handler.ConnectOptions{
|
connectOptions := handler.ConnectOptions{
|
||||||
Namespace: duplicateOptions.Namespace,
|
Namespace: duplicateOptions.Namespace,
|
||||||
|
@@ -9,7 +9,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/distribution/reference"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/resource"
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@@ -43,11 +45,12 @@ type DuplicateOptions struct {
|
|||||||
Workloads []string
|
Workloads []string
|
||||||
ExtraCIDR []string
|
ExtraCIDR []string
|
||||||
|
|
||||||
TargetKubeconfig string
|
TargetKubeconfig string
|
||||||
TargetNamespace string
|
TargetNamespace string
|
||||||
TargetContainer string
|
TargetContainer string
|
||||||
TargetImage string
|
TargetImage string
|
||||||
TargetRegistry string
|
TargetRegistry string
|
||||||
|
IsChangeTargetRegistry bool
|
||||||
|
|
||||||
isSame bool
|
isSame bool
|
||||||
|
|
||||||
@@ -157,7 +160,6 @@ func (d *DuplicateOptions) DoDuplicate(ctx context.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
d.replaceRegistry(u)
|
|
||||||
|
|
||||||
labelsMap := map[string]string{
|
labelsMap := map[string]string{
|
||||||
config.ManageBy: config.ConfigMapPodTrafficManager,
|
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 {
|
if err = unstructured.SetNestedField(u.Object, append(containers, v.Object), containersPath...); err != nil {
|
||||||
return err
|
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 := 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)
|
//_, 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
|
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 {
|
func (d *DuplicateOptions) replaceRegistry(u *unstructured.Unstructured) error {
|
||||||
if d.TargetRegistry == "" {
|
// not pass this options, do nothing
|
||||||
|
if !d.IsChangeTargetRegistry {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -657,9 +663,29 @@ func (d *DuplicateOptions) replaceRegistry(u *unstructured.Unstructured) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//for i, container := range temp.Spec.InitContainers {
|
for i, container := range temp.Spec.InitContainers {
|
||||||
// if container.Image
|
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
|
var marshal []byte
|
||||||
if marshal, err = json.Marshal(temp.Spec); err != nil {
|
if marshal, err = json.Marshal(temp.Spec); err != nil {
|
||||||
|
@@ -302,17 +302,12 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestName(t *testing.T) {
|
func TestName(t *testing.T) {
|
||||||
name := "alpine@sha256:b733d4a32c4da6a00a84df2ca32791bb03df95400243648d8c539e7b4cce329c"
|
name := "docker.io/naison/alpine@sha256:b733d4a32c4da6a00a84df2ca32791bb03df95400243648d8c539e7b4cce329c"
|
||||||
named, err := reference.ParseNormalizedNamed(name)
|
named, err := reference.ParseNormalizedNamed(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
named = reference.TagNameOnly(named)
|
|
||||||
domain := reference.Domain(named)
|
domain := reference.Domain(named)
|
||||||
path := reference.Path(named)
|
path := reference.Path(named)
|
||||||
tagged, ok := named.(reference.Tagged)
|
fmt.Println(domain, path)
|
||||||
if !ok {
|
|
||||||
t.Fail()
|
|
||||||
}
|
|
||||||
fmt.Println(domain, path, tagged)
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user