MCPcopy Create free account
hub / github.com/cloudnative-pg/cloudnative-pg / Destroy

Function Destroy

internal/cmd/plugin/destroy/destroy.go:40–116  ·  view source on GitHub ↗

Destroy implements destroy subcommand

(ctx context.Context, clusterName, instanceName string, keepPVC bool)

Source from the content-addressed store, hash-verified

38
39// Destroy implements destroy subcommand
40func Destroy(ctx context.Context, clusterName, instanceName string, keepPVC bool) error {
41 if err := ensurePodIsDeleted(ctx, instanceName, clusterName); err != nil {
42 return err
43 }
44
45 var jobList batchv1.JobList
46 if err := plugin.Client.List(
47 ctx,
48 &jobList,
49 client.InNamespace(plugin.Namespace),
50 client.MatchingLabels{
51 utils.InstanceNameLabelName: instanceName,
52 },
53 ); err != nil {
54 return err
55 }
56 for idx := range jobList.Items {
57 if err := plugin.Client.Delete(
58 ctx,
59 &jobList.Items[idx],
60 client.PropagationPolicy(metav1.DeletePropagationBackground),
61 ); err != nil && !apierrs.IsNotFound(err) {
62 return fmt.Errorf("deleting job %s: %w", jobList.Items[idx].Name, err)
63 }
64 }
65
66 pvcs, err := persistentvolumeclaim.GetInstancePVCs(ctx, plugin.Client, instanceName, plugin.Namespace)
67 if err != nil {
68 return err
69 }
70
71 if keepPVC {
72 // we remove the ownership from the pvcs if present
73 for i := range pvcs {
74 if _, isOwned := controller.IsOwnedByCluster(&pvcs[i]); !isOwned {
75 continue
76 }
77
78 pvcs[i].OwnerReferences = removeOwnerReference(pvcs[i].OwnerReferences, clusterName)
79 pvcs[i].Annotations[utils.PVCStatusAnnotationName] = persistentvolumeclaim.StatusDetached
80 pvcs[i].Labels[utils.InstanceNameLabelName] = instanceName
81 err = plugin.Client.Update(ctx, &pvcs[i])
82 if err != nil {
83 return fmt.Errorf("error updating metadata for persistent volume claim %s: %v",
84 clusterName, err)
85 }
86 }
87 fmt.Printf("Instance %s of cluster %s has been destroyed and the PVC was kept\n",
88 instanceName,
89 clusterName,
90 )
91 return nil
92 }
93
94 for i := range pvcs {
95 if pvcs[i].Labels == nil {
96 pvcs[i].Labels = map[string]string{}
97 }

Callers 1

NewCmdFunction · 0.85

Calls 7

GetInstancePVCsFunction · 0.92
IsOwnedByClusterFunction · 0.92
ensurePodIsDeletedFunction · 0.85
removeOwnerReferenceFunction · 0.85
ListMethod · 0.45
DeleteMethod · 0.45
UpdateMethod · 0.45

Tested by

no test coverage detected