Skip to content

Commit

Permalink
fixed a bug where not-namespaced resource could not be promoted
Browse files Browse the repository at this point in the history
Signed-off-by: duanmeng <duanmeng_yewu@cmss.chinamobile.com>
  • Loading branch information
duanmengkk committed May 12, 2022
1 parent dfd1582 commit e7ba433
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions pkg/karmadactl/promote.go
Expand Up @@ -256,36 +256,56 @@ func promote(controlPlaneRestConfig *rest.Config, obj *unstructured.Unstructured

controlPlaneDynamicClient := dynamic.NewForConfigOrDie(controlPlaneRestConfig)

_, err := controlPlaneDynamicClient.Resource(gvr).Namespace(opts.Namespace).Get(context.TODO(), opts.name, metav1.GetOptions{})
if err == nil {
fmt.Printf("Resource %q(%s/%s) already exist in karmada control plane, you can edit PropagationPolicy and OverridePolicy to propagate it\n",
gvr, opts.Namespace, opts.name)
karmadaClient := karmadaclientset.NewForConfigOrDie(controlPlaneRestConfig)

return nil
}
if !apierrors.IsNotFound(err) {
return fmt.Errorf("failed to get resource %q(%s/%s) in control plane: %v", gvr, opts.Namespace, opts.name, err)
}
if len(obj.GetNamespace()) == 0 {
_, err := controlPlaneDynamicClient.Resource(gvr).Get(context.TODO(), opts.name, metav1.GetOptions{})
if err == nil {
fmt.Printf("Resource %q(%s) already exist in karmada control plane, you can edit PropagationPolicy and OverridePolicy to propagate it\n",
gvr, opts.name)
return nil
}

_, err = controlPlaneDynamicClient.Resource(gvr).Namespace(opts.Namespace).Create(context.TODO(), obj, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("failed to create resource %q(%s/%s) in control plane: %v", gvr, opts.Namespace, opts.name, err)
}
if !apierrors.IsNotFound(err) {
return fmt.Errorf("failed to get resource %q(%s) in control plane: %v", gvr, opts.name, err)
}

karmadaClient := karmadaclientset.NewForConfigOrDie(controlPlaneRestConfig)
_, err = controlPlaneDynamicClient.Resource(gvr).Create(context.TODO(), obj, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("failed to create resource %q(%s) in control plane: %v", gvr, opts.name, err)
}

if len(obj.GetNamespace()) == 0 {
err = createOrUpdateClusterPropagationPolicy(karmadaClient, gvr, opts)
if err != nil {
return err
}

fmt.Printf("Resource %q(%s) is promoted successfully\n", gvr, opts.name)
} else {
_, err := controlPlaneDynamicClient.Resource(gvr).Namespace(opts.Namespace).Get(context.TODO(), opts.name, metav1.GetOptions{})
if err == nil {
fmt.Printf("Resource %q(%s/%s) already exist in karmada control plane, you can edit PropagationPolicy and OverridePolicy to propagate it\n",
gvr, opts.Namespace, opts.name)
return nil
}

if !apierrors.IsNotFound(err) {
return fmt.Errorf("failed to get resource %q(%s/%s) in control plane: %v", gvr, opts.Namespace, opts.name, err)
}

_, err = controlPlaneDynamicClient.Resource(gvr).Namespace(opts.Namespace).Create(context.TODO(), obj, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("failed to create resource %q(%s/%s) in control plane: %v", gvr, opts.Namespace, opts.name, err)
}

err = createOrUpdatePropagationPolicy(karmadaClient, gvr, opts)
}
if err != nil {
return err
}

if err != nil {
return err
fmt.Printf("Resource %q(%s/%s) is promoted successfully\n", gvr, opts.Namespace, opts.name)
}

fmt.Printf("Resource %q(%s/%s) is promoted successfully\n", gvr, opts.Namespace, opts.name)

return nil
}

Expand Down

0 comments on commit e7ba433

Please sign in to comment.