forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_cluster_app.go
233 lines (222 loc) · 8.08 KB
/
multi_cluster_app.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package multiclusterapp
import (
"encoding/json"
"fmt"
"github.com/rancher/norman/httperror"
"io/ioutil"
"net/http"
"strings"
"github.com/pkg/errors"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/parse"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
gaccess "github.com/rancher/rancher/pkg/api/customization/globalnamespaceaccess"
"github.com/rancher/rancher/pkg/namespace"
"github.com/rancher/types/apis/management.cattle.io/v3"
managementschema "github.com/rancher/types/apis/management.cattle.io/v3/schema"
"github.com/rancher/types/client/management/v3"
"k8s.io/apimachinery/pkg/api/meta"
)
const (
addProjectsAction = "addProjects"
removeProjectsAction = "removeProjects"
)
func (w Wrapper) Formatter(apiContext *types.APIContext, resource *types.RawResource) {
resource.AddAction(apiContext, "rollback")
resource.AddAction(apiContext, addProjectsAction)
resource.AddAction(apiContext, removeProjectsAction)
resource.Links["revisions"] = apiContext.URLBuilder.Link("revisions", resource)
}
func (w Wrapper) ActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error {
var mcApp client.MultiClusterApp
if err := access.ByID(apiContext, &managementschema.Version, client.MultiClusterAppType, apiContext.ID, &mcApp); err != nil {
return err
}
switch actionName {
case "rollback":
data, err := ioutil.ReadAll(apiContext.Request.Body)
if err != nil {
return errors.Wrap(err, "reading request body error")
}
input := client.MultiClusterAppRollbackInput{}
if err = json.Unmarshal(data, &input); err != nil {
return errors.Wrap(err, "unmarshal input error")
}
id := input.RevisionID
splitID := strings.Split(input.RevisionID, ":")
if len(splitID) == 2 {
id = splitID[1]
}
revision, err := w.MultiClusterAppRevisionLister.Get(namespace.GlobalNamespace, id)
if err != nil {
return err
}
obj, err := w.MultiClusterAppLister.Get(namespace.GlobalNamespace, mcApp.Name)
if err != nil {
return err
}
if obj.Status.RevisionName == revision.Name {
return nil
}
toUpdate := obj.DeepCopy()
toUpdate.Spec.TemplateVersionName = revision.TemplateVersionName
toUpdate.Spec.Answers = revision.Answers
_, err = w.MultiClusterApps.Update(toUpdate)
return err
case addProjectsAction:
return w.addProjects(apiContext)
case removeProjectsAction:
return w.removeProjects(apiContext)
default:
return fmt.Errorf("bad action for multiclusterapp %v", actionName)
}
}
func (w Wrapper) addProjects(request *types.APIContext) error {
mcapp, existingProjects, inputProjects, inputAnswers, err := w.modifyProjects(request, addProjectsAction)
if err != nil {
return err
}
var mcappToUpdate *v3.MultiClusterApp
if len(inputProjects) > 0 {
for _, p := range inputProjects {
if existingProjects[p] {
return httperror.NewAPIError(httperror.InvalidBodyContent, fmt.Sprintf("duplicate projects in targets %s", p))
}
existingProjects[p] = true
}
mcappToUpdate = mcapp.DeepCopy()
for _, name := range inputProjects {
mcappToUpdate.Spec.Targets = append(mcappToUpdate.Spec.Targets, v3.Target{ProjectName: name})
}
}
if len(inputAnswers) > 0 {
if mcappToUpdate == nil {
mcappToUpdate = mcapp.DeepCopy()
}
mcappToUpdate.Spec.Answers = append(mcappToUpdate.Spec.Answers, inputAnswers...)
}
if mcappToUpdate != nil {
return w.updateMcApp(mcappToUpdate, request, "addedProjects")
}
request.WriteResponse(http.StatusOK, nil)
return nil
}
func (w Wrapper) removeProjects(request *types.APIContext) error {
mcapp, _, inputProjects, _, err := w.modifyProjects(request, removeProjectsAction)
if err != nil {
return err
}
mcappToUpdate := mcapp.DeepCopy()
toRemoveProjects := make(map[string]bool)
var finalTargets []v3.Target
for _, p := range inputProjects {
toRemoveProjects[p] = true
}
for _, t := range mcapp.Spec.Targets {
if !toRemoveProjects[t.ProjectName] {
finalTargets = append(finalTargets, t)
}
}
mcappToUpdate.Spec.Targets = finalTargets
return w.updateMcApp(mcappToUpdate, request, "removedProjects")
}
func (w Wrapper) modifyProjects(request *types.APIContext, actionName string) (*v3.MultiClusterApp, map[string]bool, []string, []v3.Answer, error) {
split := strings.SplitN(request.ID, ":", 2)
if len(split) != 2 {
return nil, map[string]bool{}, []string{}, []v3.Answer{}, fmt.Errorf("incorrect multi cluster app ID %v", request.ID)
}
var inputProjects []string
var inputAnswers []v3.Answer
existingProjects := make(map[string]bool)
mcapp, err := w.MultiClusterAppLister.Get(split[0], split[1])
if err != nil {
return nil, existingProjects, inputProjects, inputAnswers, err
}
// ensure that caller is not a readonly member of multiclusterapp, else abort
callerID := request.Request.Header.Get(gaccess.ImpersonateUserHeader)
metaAccessor, err := meta.Accessor(mcapp)
if err != nil {
return nil, existingProjects, inputProjects, inputAnswers, err
}
creatorID, ok := metaAccessor.GetAnnotations()[creatorIDAnn]
if !ok {
return nil, existingProjects, inputProjects, inputAnswers, fmt.Errorf("multiclusterapp %v has no creatorId annotation", metaAccessor.GetName())
}
ma := gaccess.MemberAccess{
Users: w.Users,
PrtbLister: w.PrtbLister,
CrtbLister: w.CrtbLister,
RoleTemplateLister: w.RoleTemplateLister,
GrbLister: w.GrbLister,
GrLister: w.GrLister,
Prtbs: w.Prtbs,
Crtbs: w.Crtbs,
ProjectLister: w.ProjectLister,
ClusterLister: w.ClusterLister,
}
accessType, err := ma.GetAccessTypeOfCaller(callerID, creatorID, mcapp.Name, mcapp.Spec.Members)
if err != nil {
return nil, existingProjects, inputProjects, inputAnswers, err
}
if accessType != gaccess.OwnerAccess {
return nil, existingProjects, inputProjects, inputAnswers, fmt.Errorf("only owners can modify projects of multiclusterapp")
}
var updateMultiClusterAppTargetsInput client.UpdateMultiClusterAppTargetsInput
actionInput, err := parse.ReadBody(request.Request)
if err != nil {
return nil, existingProjects, inputProjects, inputAnswers, err
}
if err = convert.ToObj(actionInput, &updateMultiClusterAppTargetsInput); err != nil {
return nil, existingProjects, inputProjects, inputAnswers, err
}
inputProjects = updateMultiClusterAppTargetsInput.Projects
for _, p := range mcapp.Spec.Targets {
existingProjects[p.ProjectName] = true
}
if actionName == addProjectsAction {
if err = ma.EnsureRoleInTargets(inputProjects, mcapp.Spec.Roles, callerID); err != nil {
return nil, existingProjects, inputProjects, inputAnswers, err
}
} else if actionName == removeProjectsAction {
// we want to remove all roles that the mcapp's sys acc has in these projects being removed
if err = ma.RemoveRolesFromTargets(inputProjects, []string{}, mcapp.Name, true); err != nil {
return nil, existingProjects, inputProjects, inputAnswers, err
}
}
for _, a := range updateMultiClusterAppTargetsInput.Answers {
inputAnswers = append(inputAnswers, v3.Answer{
ProjectName: a.ProjectID,
ClusterName: a.ClusterID,
Values: a.Values,
})
}
// check if the input includes answers, and if they are only for the input projects
if len(inputAnswers) > 0 {
inputProjectsMap := make(map[string]bool)
for _, p := range inputProjects {
if !inputProjectsMap[p] {
inputProjectsMap[p] = true
}
}
for _, a := range inputAnswers {
if a.ProjectName == "" {
return nil, existingProjects, inputProjects, inputAnswers, fmt.Errorf("can only provide project-scoped answers for new projects through add/remove projects action")
}
if !inputProjectsMap[a.ProjectName] {
return nil, existingProjects, inputProjects, inputAnswers, fmt.Errorf("the project %v is not among the ones provided in input", a.ProjectName)
}
}
}
return mcapp, existingProjects, inputProjects, inputAnswers, nil
}
func (w Wrapper) updateMcApp(mcappToUpdate *v3.MultiClusterApp, request *types.APIContext, message string) error {
if _, err := w.MultiClusterApps.Update(mcappToUpdate); err != nil {
return err
}
op := map[string]interface{}{
"message": message,
}
request.WriteResponse(http.StatusOK, op)
return nil
}