-
Notifications
You must be signed in to change notification settings - Fork 151
/
remove.go
186 lines (147 loc) · 4.46 KB
/
remove.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package app
import (
"context"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
wego "github.com/weaveworks/weave-gitops/api/v1alpha1"
"github.com/weaveworks/weave-gitops/pkg/gitproviders"
"k8s.io/apimachinery/pkg/types"
)
const (
RemoveCommitMessage = "Remove App manifests"
)
type RemoveParams struct {
Name string
Namespace string
DryRun bool
GitProviderToken string
}
// Remove removes the Weave GitOps automation for an application
func (a *App) Remove(params RemoveParams) error {
ctx := context.Background()
clusterName, err := a.Kube.GetClusterName(ctx)
if err != nil {
return err
}
application, err := a.Kube.GetApplication(ctx, types.NamespacedName{Namespace: params.Namespace, Name: params.Name})
if err != nil {
return err
}
// Find all resources created when adding this app
info, err := getAppResourceInfo(*application, clusterName)
if err != nil {
return err
}
resources := info.clusterResources()
if info.configMode() == ConfigModeClusterOnly {
gvrApp, err := ResourceKindApplication.ToGVR()
if err != nil {
return err
}
if err := a.Kube.DeleteByName(ctx, info.appResourceName(), gvrApp, info.Namespace); err != nil {
return clusterDeleteError(info.appResourceName(), err)
}
gvrSource, err := info.sourceKind().ToGVR()
if err != nil {
return err
}
if err := a.Kube.DeleteByName(ctx, info.appSourceName(), gvrSource, info.Namespace); err != nil {
return clusterDeleteError(info.appResourceName(), err)
}
gvrDeployKind, err := info.deployKind().ToGVR()
if err != nil {
return err
}
if err := a.Kube.DeleteByName(ctx, info.appDeployName(), gvrDeployKind, info.Namespace); err != nil {
return clusterDeleteError(info.appResourceName(), err)
}
return nil
}
cloneURL, branch, err := a.getConfigUrlAndBranch(ctx, info)
if err != nil {
return fmt.Errorf("failed to obtain config URL and branch: %w", err)
}
remover, _, err := a.cloneRepo(a.ConfigGit, cloneURL.String(), branch, params.DryRun)
if err != nil {
return fmt.Errorf("failed to clone configuration repo: %w", err)
}
defer remover()
a.Logger.Actionf("Removing application from cluster and repository")
if !params.DryRun {
for _, resourceRef := range resources {
if resourceRef.repositoryPath != "" { // Some of the automation doesn't get stored
if err := a.ConfigGit.Remove(resourceRef.repositoryPath); err != nil {
return err
}
} else if resourceRef.kind == ResourceKindKustomization ||
resourceRef.kind == ResourceKindHelmRelease {
gvrDeployKind, err := resourceRef.kind.ToGVR()
if err != nil {
return err
}
if err := a.Kube.DeleteByName(ctx, resourceRef.name, gvrDeployKind, info.Namespace); err != nil {
return clusterDeleteError(info.appResourceName(), err)
}
}
}
return a.commitAndPush(a.ConfigGit, RemoveCommitMessage, params.DryRun)
}
return nil
}
func (a *App) getConfigUrlAndBranch(ctx context.Context, info *AppResourceInfo) (gitproviders.RepoURL, string, error) {
configUrl := info.Spec.ConfigURL
if configUrl == string(ConfigTypeUserRepo) {
configUrl = info.Spec.URL
}
repoUrl, err := gitproviders.NewRepoURL(configUrl)
if err != nil {
return gitproviders.RepoURL{}, "", err
}
branch := info.Spec.Branch
if branch != "" {
branch, err = a.GitProvider.GetDefaultBranch(ctx, repoUrl)
if err != nil {
return gitproviders.RepoURL{}, "", err
}
}
return repoUrl, branch, nil
}
func clusterDeleteError(name string, err error) error {
return fmt.Errorf("failed to delete resource: %s with error: %w", name, err)
}
func dirExists(d string) bool {
info, err := os.Stat(d)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}
// findAppManifests locates all manifests associated with a specific application within a repository;
// only used when "RemoveWorkload" is specified
func findAppManifests(application wego.Application, repoDir string) ([][]byte, error) {
root := filepath.Join(repoDir, application.Spec.Path)
if !dirExists(root) {
return nil, fmt.Errorf("application path '%s' not found", application.Spec.Path)
}
manifests := [][]byte{}
err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if !entry.IsDir() && filepath.Ext(path) == ".yaml" {
manifest, err := ioutil.ReadFile(path)
if err != nil {
return err
}
manifests = append(manifests, manifest)
}
return nil
})
if err != nil {
return nil, err
}
return manifests, nil
}