/
cleanup.go
81 lines (67 loc) · 1.86 KB
/
cleanup.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
package command
import (
"fmt"
"github.com/echocat/kubor/common"
"github.com/echocat/kubor/kubernetes"
"github.com/echocat/kubor/model"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
func init() {
cmd := &Cleanup{}
cmd.Parent = cmd
RegisterInitializable(cmd)
common.RegisterCliFactory(cmd)
}
type Cleanup struct {
Command
}
func (instance *Cleanup) ConfigureCliCommands(context string, hc common.HasCommands, _ string) error {
if context != "" {
return nil
}
hc.Command("cleanup", "Will delete all orphaned resources which matches the current"+
" project's groupId and artifactId but where not part of the evaluated environment in the configured claim.").
Action(instance.ExecuteFromCli)
return nil
}
func (instance *Cleanup) RunWithArguments(arguments Arguments) error {
ct, err := kubernetes.NewCleanupTask(arguments.Project, arguments.DynamicClient, kubernetes.CleanupModeOrphans)
if err != nil {
return err
}
task := &cleanupTask{
source: instance,
project: arguments.Project,
cleanupTask: &ct,
}
oh, err := model.NewObjectHandler(task.onObject, arguments.Project)
if err != nil {
return err
}
cp, err := arguments.Project.RenderedTemplatesProvider()
if err != nil {
return err
}
err = oh.Handle(cp)
if err != nil {
return err
}
return ct.Execute()
}
type cleanupTask struct {
source *Cleanup
project *model.Project
cleanupTask *kubernetes.CleanupTask
}
func (instance *cleanupTask) onObject(source string, _ runtime.Object, object *unstructured.Unstructured) error {
reference, err := kubernetes.GetObjectReference(object, instance.project.Scheme)
if err != nil {
return err
}
if err := instance.project.Claim.Validate(reference); err != nil {
return fmt.Errorf("%v (source: %s): %w", reference, source, err)
}
instance.cleanupTask.Add(reference)
return nil
}