Skip to content

Commit

Permalink
support to promote dependency resources automatically by using '-d true'
Browse files Browse the repository at this point in the history
Signed-off-by: duanmeng <[email protected]>
  • Loading branch information
duanmengkk committed May 22, 2022
1 parent e481fb9 commit 72eff5f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
87 changes: 47 additions & 40 deletions pkg/karmadactl/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/resourceinterpreter/defaultinterpreter/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 72eff5f

Please sign in to comment.