-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
delete.go
131 lines (107 loc) · 3.9 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package artifact
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client"
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
workflow "github.com/argoproj/argo-workflows/v3/pkg/client/clientset/versioned"
wfv1alpha1 "github.com/argoproj/argo-workflows/v3/pkg/client/clientset/versioned/typed/workflow/v1alpha1"
executor "github.com/argoproj/argo-workflows/v3/workflow/artifacts"
"github.com/argoproj/argo-workflows/v3/workflow/common"
)
func NewArtifactDeleteCommand() *cobra.Command {
return &cobra.Command{
Use: "delete",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
namespace := client.Namespace()
clientConfig := client.GetConfig()
if podName, ok := os.LookupEnv(common.EnvVarArtifactGCPodHash); ok {
config, err := clientConfig.ClientConfig()
workflowInterface := workflow.NewForConfigOrDie(config)
if err != nil {
return err
}
artifactGCTaskInterface := workflowInterface.ArgoprojV1alpha1().WorkflowArtifactGCTasks(namespace)
labelSelector := fmt.Sprintf("%s = %s", common.LabelKeyArtifactGCPodHash, podName)
err = deleteArtifacts(labelSelector, cmd.Context(), artifactGCTaskInterface)
if err != nil {
return err
}
}
return nil
},
}
}
func deleteArtifacts(labelSelector string, ctx context.Context, artifactGCTaskInterface wfv1alpha1.WorkflowArtifactGCTaskInterface) error {
taskList, err := artifactGCTaskInterface.List(context.Background(), metav1.ListOptions{LabelSelector: labelSelector})
if err != nil {
return err
}
for _, task := range taskList.Items {
task.Status.ArtifactResultsByNode = make(map[string]v1alpha1.ArtifactResultNodeStatus)
for nodeName, artifactNodeSpec := range task.Spec.ArtifactsByNode {
var archiveLocation *v1alpha1.ArtifactLocation
artResultNodeStatus := v1alpha1.ArtifactResultNodeStatus{ArtifactResults: make(map[string]v1alpha1.ArtifactResult)}
if artifactNodeSpec.ArchiveLocation != nil {
archiveLocation = artifactNodeSpec.ArchiveLocation
}
var resources resources
resources.Files = make(map[string][]byte) // same resources for every artifact
for _, artifact := range artifactNodeSpec.Artifacts {
if archiveLocation != nil {
err := artifact.Relocate(archiveLocation)
if err != nil {
return err
}
}
drv, err := executor.NewDriver(ctx, &artifact, resources)
if err != nil {
return err
}
err = drv.Delete(&artifact)
if err != nil {
errString := err.Error()
artResultNodeStatus.ArtifactResults[artifact.Name] = v1alpha1.ArtifactResult{Name: artifact.Name, Success: false, Error: &errString}
} else {
artResultNodeStatus.ArtifactResults[artifact.Name] = v1alpha1.ArtifactResult{Name: artifact.Name, Success: true, Error: nil}
}
}
task.Status.ArtifactResultsByNode[nodeName] = artResultNodeStatus
}
patch, err := json.Marshal(map[string]interface{}{"status": v1alpha1.ArtifactGCStatus{ArtifactResultsByNode: task.Status.ArtifactResultsByNode}})
if err != nil {
return err
}
_, err = artifactGCTaskInterface.Patch(context.Background(), task.Name, types.MergePatchType, patch, metav1.PatchOptions{}, "status")
if err != nil {
return err
}
}
return nil
}
type resources struct {
Files map[string][]byte
}
func (r resources) GetSecret(ctx context.Context, name, key string) (string, error) {
path := filepath.Join(common.SecretVolMountPath, name, key)
if file, ok := r.Files[path]; ok {
return string(file), nil
}
file, err := os.ReadFile(path)
if err != nil {
return "", err
} else {
r.Files[path] = file
return string(file), err
}
}
func (r resources) GetConfigMapKey(ctx context.Context, name, key string) (string, error) {
return "", fmt.Errorf("not supported")
}