Skip to content

Commit

Permalink
chore(subscriber): delete subscriber by labels
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Chaudhary <shubham.chaudhary@harness.io>
  • Loading branch information
ispeakc0de committed Dec 16, 2022
1 parent 2fc2949 commit b75393f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
26 changes: 23 additions & 3 deletions litmus-portal/cluster-agents/subscriber/pkg/k8s/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,14 @@ func applyRequest(requestType string, obj *unstructured.Unstructured) (*unstruct
logrus.Info("successfully updated for kind: ", response.GetKind(), ", resource name: ", response.GetName(), ", and namespace: ", response.GetNamespace())
return response, nil
} else if requestType == "delete" {
err := dr.Delete(ctx, obj.GetName(), metav1.DeleteOptions{})
var err error
if obj.GetName() != "" {
err = dr.Delete(ctx, obj.GetName(), metav1.DeleteOptions{})
logrus.Info("successfully deleted for kind: ", obj.GetKind(), ", resource name: ", obj.GetName(), ", and namespace: ", obj.GetNamespace())
} else if obj.GetLabels() != nil {
err = dr.DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: convertMapToString(obj.GetLabels())})
logrus.Info("successfully deleted for kind: ", obj.GetKind(), ", resource labels: ", obj.GetLabels(), ", and namespace: ", obj.GetNamespace())
}
if k8s_errors.IsNotFound(err) {
// This doesn't ever happen even if it is already deleted or not found
logrus.Info("%v not found", obj.GetName())
Expand All @@ -253,8 +260,6 @@ func applyRequest(requestType string, obj *unstructured.Unstructured) (*unstruct
if err != nil {
return nil, err
}

logrus.Info("successfully deleted for kind: ", obj.GetKind(), ", resource name: ", obj.GetName(), ", and namespace: ", obj.GetNamespace())
return &unstructured.Unstructured{}, nil
} else if requestType == "get" {
response, err := dr.Get(ctx, obj.GetName(), metav1.GetOptions{})
Expand Down Expand Up @@ -342,3 +347,18 @@ func ClusterConfirm(clusterData map[string]string) ([]byte, error) {
}
return []byte(resp), nil
}

func convertMapToString(m map[string]string) string {
var result string
for k, v := range m {
if k == "executed_by" {
continue
}
if result == "" {
result = fmt.Sprintf("%s=%s", k, v)
continue
}
result = fmt.Sprintf("%s,%s=%s", result, k, v)
}
return result
}
4 changes: 3 additions & 1 deletion litmus-portal/graphql-server/pkg/cluster/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ func DeleteClusters(ctx context.Context, projectID string, clusterIds []*string,
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "subscriber",
"labels": {
"app": "subscriber",
},
"namespace": ` + *cluster.AgentNamespace + `
}
}`,
Expand Down

0 comments on commit b75393f

Please sign in to comment.