Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(subscriber): delete subscriber by labels #3867

Merged
merged 2 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions litmus-portal/cluster-agents/subscriber/pkg/k8s/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
memory "k8s.io/client-go/discovery/cached"
"k8s.io/client-go/dynamic"
Expand Down Expand Up @@ -243,7 +244,16 @@ 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 {
objLabels := obj.GetLabels()
delete(objLabels, "executed_by")
err = dr.DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: labels.FormatLabels(objLabels)})
logrus.Info("successfully deleted for kind: ", obj.GetKind(), ", resource labels: ", objLabels, ", 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 +263,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
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ rules:

- apiGroups: ["apps"]
resources: ["deployments", "daemonsets", "replicasets", "statefulsets"]
verbs: ["get","list", "delete"]
verbs: ["get","list", "delete","deletecollection"]
ispeakc0de marked this conversation as resolved.
Show resolved Hide resolved

- apiGroups: ["argoproj.io"]
resources: ["workflows","workflows/finalizers","workflowtemplates", "workflowtemplates/finalizers","cronworkflows","cronworkflows/finalizers","clusterworkflowtemplates","clusterworkflowtemplates/finalizers","rollouts"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ rules:

- apiGroups: ["apps"]
resources: ["deployments", "daemonsets", "replicasets", "statefulsets"]
verbs: ["get", "list", "delete"]
verbs: ["get", "list", "delete","deletecollection"]
ispeakc0de marked this conversation as resolved.
Show resolved Hide resolved

- apiGroups: ["argoproj.io"]
resources:
Expand Down
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
2 changes: 1 addition & 1 deletion litmus-portal/manifests/cluster-k8s-manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ rules:
# for checking the app parent resources as deployments or sts and are eligible chaos candidates
- apiGroups: [apps]
resources: [deployments, statefulsets]
verbs: [list, get, patch, update, create, delete]
verbs: [list, get, patch, update, create, delete, deletecollection]

# for checking the app parent resources as replicasets and are eligible chaos candidates
- apiGroups: [apps]
Expand Down
2 changes: 1 addition & 1 deletion litmus-portal/manifests/namespace-k8s-manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ rules:

- apiGroups: [apps]
resources: [deployments, daemonsets, replicasets, statefulsets]
verbs: [get, list, delete]
verbs: [get, list, delete, deletecollection]

- apiGroups: [argoproj.io]
resources: [workflows, workflows/finalizers, workflowtemplates, workflowtemplates/finalizers, cronworkflows, cronworkflows/finalizers, rollouts]
Expand Down