forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
125 lines (116 loc) · 3.85 KB
/
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
package app
import (
"net/http"
"fmt"
"reflect"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/parse"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/controllers/management/compose/common"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/types/apis/management.cattle.io/v3"
pv3 "github.com/rancher/types/apis/project.cattle.io/v3"
projectschema "github.com/rancher/types/apis/project.cattle.io/v3/schema"
projectv3 "github.com/rancher/types/client/project/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Wrapper struct {
Clusters v3.ClusterInterface
KubeConfigGetter common.KubeConfigGetter
TemplateContentClient v3.TemplateContentInterface
AppGetter pv3.AppsGetter
}
const appLabel = "io.cattle.field/appId"
func Formatter(apiContext *types.APIContext, resource *types.RawResource) {
resource.AddAction(apiContext, "upgrade")
resource.AddAction(apiContext, "rollback")
resource.Links["revision"] = apiContext.URLBuilder.Link("revision", resource)
if _, ok := resource.Values["status"]; ok {
if status, ok := resource.Values["status"].(map[string]interface{}); ok {
delete(status, "lastAppliedTemplate")
}
}
}
func (w Wrapper) ActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error {
var app projectv3.App
if err := access.ByID(apiContext, &projectschema.Version, projectv3.AppType, apiContext.ID, &app); err != nil {
return err
}
actionInput, err := parse.ReadBody(apiContext.Request)
if err != nil {
return err
}
switch actionName {
case "upgrade":
externalID := actionInput["externalId"]
answers := actionInput["answers"]
_, namespace := ref.Parse(app.ProjectId)
obj, err := w.AppGetter.Apps(namespace).Get(app.Name, metav1.GetOptions{})
if err != nil {
return err
}
if answers != nil {
m, ok := answers.(map[string]interface{})
if ok {
if obj.Spec.Answers == nil {
obj.Spec.Answers = make(map[string]string)
}
for k, v := range m {
obj.Spec.Answers[k] = convert.ToString(v)
}
}
}
obj.Spec.ExternalID = convert.ToString(externalID)
if _, err := w.AppGetter.Apps(namespace).Update(obj); err != nil {
return err
}
return nil
case "rollback":
revision := actionInput["revision"]
if convert.ToString(revision) == "" {
return fmt.Errorf("revision is empty")
}
var appRevision projectv3.AppRevision
_, projectID := ref.Parse(app.ProjectId)
revisionID := fmt.Sprintf("%s:%s", projectID, convert.ToString(revision))
if err := access.ByID(apiContext, &projectschema.Version, projectv3.AppRevisionType, revisionID, &appRevision); err != nil {
return err
}
_, namespace := ref.Parse(app.ProjectId)
obj, err := w.AppGetter.Apps(namespace).Get(app.Name, metav1.GetOptions{})
if err != nil {
return err
}
obj.Spec.Answers = appRevision.Status.Answers
obj.Spec.ExternalID = appRevision.Status.ExternalID
if _, err := w.AppGetter.Apps(namespace).Update(obj); err != nil {
return err
}
return nil
}
return nil
}
func (w Wrapper) LinkHandler(apiContext *types.APIContext, next types.RequestHandler) error {
switch apiContext.Link {
case "revision":
var app projectv3.App
if err := access.ByID(apiContext, &projectschema.Version, projectv3.AppType, apiContext.ID, &app); err != nil {
return err
}
var appRevisions, filtered []map[string]interface{}
if err := access.List(apiContext, &projectschema.Version, projectv3.AppRevisionType, &types.QueryOptions{}, &appRevisions); err != nil {
return err
}
for _, re := range appRevisions {
labels := convert.ToMapInterface(re["labels"])
if reflect.DeepEqual(labels[appLabel], app.Name) {
filtered = append(filtered, re)
}
}
apiContext.Type = projectv3.AppRevisionType
apiContext.WriteResponse(http.StatusOK, filtered)
return nil
}
return nil
}