diff --git a/pkg/karmadactl/promote.go b/pkg/karmadactl/promote.go index 69fb73924ddf..3aa63fe32528 100644 --- a/pkg/karmadactl/promote.go +++ b/pkg/karmadactl/promote.go @@ -245,58 +245,65 @@ func RunPromote(karmadaConfig KarmadaConfig, opts CommandPromoteOption, args []s return fmt.Errorf("failed to get gvr from %q: %v", opts.gvk, err) } - if opts.deps == true { - interpreter := defaultinterpreter.NewDefaultInterpreter() - dependenciesInterpreter := interpreter.GetDependenciesHandlers() - if _, exist := dependenciesInterpreter[obj.GroupVersionKind()]; exist { - deps, err := interpreter.GetDependencies(obj) + if opts.deps { + err := promoteDeps(obj, opts, f, mapper, gvr, controlPlaneRestConfig) + if err != nil { + return fmt.Errorf("failed to promote relevant resources of gvr %q automatically: %v", opts.gvk, err) + } + } + return promote(controlPlaneRestConfig, obj, gvr, opts) +} + +func promoteDeps(obj *unstructured.Unstructured, opts CommandPromoteOption, f cmdutil.Factory, mapper meta.RESTMapper, gvr schema.GroupVersionResource, controlPlaneRestConfig *rest.Config) error { + interpreter := defaultinterpreter.NewDefaultInterpreter() + dependenciesInterpreter := interpreter.GetDependenciesHandlers() + if _, exist := dependenciesInterpreter[obj.GroupVersionKind()]; exist { + deps, err := interpreter.GetDependencies(obj) + if err != nil { + return fmt.Errorf("failed to customize dependencies for %s(%s), %v", obj.GroupVersionKind(), obj.GetName(), err) + } + + for _, dep := range deps { + args := []string{dep.Kind, dep.Name} + depInfo, err := opts.getObjInfo(f, opts.Cluster, args) if err != nil { - return fmt.Errorf("failed to customize dependencies for %s(%s), %v", obj.GroupVersionKind(), obj.GetName(), err) + return fmt.Errorf("failed to get resource in cluster(%s). err: %v", opts.Cluster, err) } - for _, dep := range deps { - args := []string{dep.Kind, dep.Name} - depInfo, err := opts.getObjInfo(f, opts.Cluster, args) - if err != nil { - return fmt.Errorf("failed to get resource in cluster(%s). err: %v", opts.Cluster, err) - } + depObj := depInfo.Info.Object.(*unstructured.Unstructured) + depGvk := depObj.GetObjectKind().GroupVersionKind() + depGvr, err := restmapper.GetGroupVersionResource(mapper, depGvk) + if err != nil { + return fmt.Errorf("failed to get gvr from %q: %v", depGvk, err) + } - depObj := depInfo.Info.Object.(*unstructured.Unstructured) - depGvk := depObj.GetObjectKind().GroupVersionKind() - depGvr, err := restmapper.GetGroupVersionResource(mapper, depGvk) - if err != nil { - return fmt.Errorf("failed to get gvr from %q: %v", depGvk, err) + if err := preprocessResource(depObj); err != nil { + return fmt.Errorf("failed to preprocess resource %q(%s/%s) in control plane: %v", gvr, opts.Namespace, opts.name, err) + } + + // currently support configmap and secret to be promoted automatically + if len(dep.Namespace) != 0 { + controlPlaneDynamicClient := dynamic.NewForConfigOrDie(controlPlaneRestConfig) + _, err := controlPlaneDynamicClient.Resource(gvr).Namespace(opts.Namespace).Get(context.TODO(), opts.name, metav1.GetOptions{}) + if err == nil { + fmt.Printf("Dependency resource %q(%s/%s) of %q(%s/%s) already exist in karmada control plane, you can edit PropagationPolicy and OverridePolicy to propagate it\n", + depGvr, depObj.GetNamespace(), depObj.GetName(), gvr, opts.Namespace, opts.name) } - if err := preprocessResource(depObj); err != nil { - return fmt.Errorf("failed to preprocess resource %q(%s/%s) in control plane: %v", gvr, opts.Namespace, opts.name, err) + if !apierrors.IsNotFound(err) { + return fmt.Errorf("failed to get dependency resource %q(%s/%s) in control plane: %v", depGvr, depObj.GetNamespace(), depObj.GetName(), err) } - // currently support configmap and secret to be promoted automatically - if len(dep.Namespace) != 0 { - controlPlaneDynamicClient := dynamic.NewForConfigOrDie(controlPlaneRestConfig) - _, err := controlPlaneDynamicClient.Resource(gvr).Namespace(opts.Namespace).Get(context.TODO(), opts.name, metav1.GetOptions{}) - if err == nil { - fmt.Printf("Dependency resource %q(%s/%s) of %q(%s/%s) already exist in karmada control plane, you can edit PropagationPolicy and OverridePolicy to propagate it\n", - depGvr, depObj.GetNamespace(), depObj.GetName(), gvr, opts.Namespace, opts.name) - return nil - } - - if !apierrors.IsNotFound(err) { - return fmt.Errorf("failed to get dependency resource %q(%s/%s) in control plane: %v", depGvr, depObj.GetNamespace(), depObj.GetName(), err) - } - - _, err = controlPlaneDynamicClient.Resource(depGvr).Namespace(depObj.GetNamespace()).Create(context.TODO(), depObj, metav1.CreateOptions{}) - if err != nil { - return fmt.Errorf("failed to create dependency resource %q(%s/%s) in control plane: %v", depGvr, depObj.GetNamespace(), depObj.GetName(), err) - } - - fmt.Printf("Dependency resource %q(%s/%s) is promoted successfully\n", depGvr, depObj.GetNamespace(), depObj.GetName()) + _, err = controlPlaneDynamicClient.Resource(depGvr).Namespace(depObj.GetNamespace()).Create(context.TODO(), depObj, metav1.CreateOptions{}) + if err != nil { + return fmt.Errorf("failed to create dependency resource %q(%s/%s) in control plane: %v", depGvr, depObj.GetNamespace(), depObj.GetName(), err) } + + fmt.Printf("Dependency resource %q(%s/%s) is promoted successfully\n", depGvr, depObj.GetNamespace(), depObj.GetName()) } } } - return promote(controlPlaneRestConfig, obj, gvr, opts) + return nil } func promote(controlPlaneRestConfig *rest.Config, obj *unstructured.Unstructured, gvr schema.GroupVersionResource, opts CommandPromoteOption) error { diff --git a/pkg/resourceinterpreter/defaultinterpreter/default.go b/pkg/resourceinterpreter/defaultinterpreter/default.go index a4c99bb2c5cf..13c3957c7b84 100644 --- a/pkg/resourceinterpreter/defaultinterpreter/default.go +++ b/pkg/resourceinterpreter/defaultinterpreter/default.go @@ -124,6 +124,7 @@ func (e *DefaultInterpreter) ReflectStatus(object *unstructured.Unstructured) (s return reflectWholeStatus(object) } +// GetDependenciesHandlers returns the dependenciesHandlers of the defaultInterpreter. func (e *DefaultInterpreter) GetDependenciesHandlers() map[schema.GroupVersionKind]dependenciesInterpreter { return e.dependenciesHandlers }