forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkload.go
137 lines (122 loc) · 5.26 KB
/
workload.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
package workload
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/parse"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
"github.com/rancher/types/apis/project.cattle.io/v3/schema"
projectschema "github.com/rancher/types/apis/project.cattle.io/v3/schema"
projectclient "github.com/rancher/types/client/project/v3"
"k8s.io/client-go/rest"
)
const (
workloadRevisions = "revisions"
DeprecatedRollbackTo = "deprecated.deployment.rollback.to"
)
type ActionWrapper struct {
Client rest.Interface
}
func (a ActionWrapper) ActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error {
var deployment projectclient.Workload
accessError := access.ByID(apiContext, &projectschema.Version, "workload", apiContext.ID, &deployment)
if accessError != nil {
return httperror.NewAPIError(httperror.InvalidReference, "Error accessing workload")
}
namespace, name := splitID(deployment.ID)
switch actionName {
case "rollback":
actionInput, err := parse.ReadBody(apiContext.Request)
if err != nil {
return err
}
revision, _ := convert.ToNumber(actionInput["revision"])
data := map[string]interface{}{}
data["kind"] = "DeploymentRollback"
data["apiVersion"] = "extensions/v1beta1"
data["name"] = name
data["rollbackTo"] = map[string]interface{}{"revision": revision}
deploymentRollback, _ := json.Marshal(data)
err = a.Client.Post().Prefix("apis/extensions/v1beta1/").Namespace(namespace).Resource("deployments").Name(name).SubResource("rollback").Body(deploymentRollback).Do().Error()
if err != nil {
return httperror.NewAPIError(httperror.ServerError, fmt.Sprintf("Error updating workload %s by %s", deployment.ID, actionName))
}
case "pause", "resume":
data, err := convert.EncodeToMap(deployment)
if err == nil {
values.PutValue(data, !deployment.DeploymentConfig.Paused, "deploymentConfig", "paused")
err = update(apiContext, data, deployment.ID)
}
if err != nil {
return httperror.NewAPIError(httperror.ServerError, fmt.Sprintf("Error updating workload %s by %s", deployment.ID, actionName))
}
}
return nil
}
func update(apiContext *types.APIContext, data map[string]interface{}, ID string) error {
workloadSchema := apiContext.Schemas.Schema(&schema.Version, "workload")
_, err := workloadSchema.Store.Update(apiContext, workloadSchema, data, ID)
return err
}
func (h Handler) LinkHandler(apiContext *types.APIContext, next types.RequestHandler) error {
if apiContext.Link == workloadRevisions {
var deployment projectclient.Workload
if err := access.ByID(apiContext, &projectschema.Version, "workload", apiContext.ID, &deployment); err == nil {
namespace, deploymentName := splitID(deployment.ID)
data, replicaSets := []map[string]interface{}{}, []map[string]interface{}{}
options := map[string]string{"hidden": "true"}
conditions := []*types.QueryCondition{
types.NewConditionFromString("namespaceId", types.ModifierEQ, []string{namespace}...),
}
if err := access.List(apiContext, &projectschema.Version, projectclient.ReplicaSetType, &types.QueryOptions{Options: options, Conditions: conditions}, &replicaSets); err == nil {
for _, replicaSet := range replicaSets {
ownerReferences := convert.ToMapSlice(replicaSet["ownerReferences"])
for _, ownerReference := range ownerReferences {
kind := convert.ToString(ownerReference["kind"])
name := convert.ToString(ownerReference["name"])
if kind == "Deployment" && name == deploymentName {
data = append(data, replicaSet)
}
}
}
apiContext.Type = projectclient.ReplicaSetType
apiContext.WriteResponse(http.StatusOK, data)
}
}
return nil
}
return httperror.NewAPIError(httperror.NotFound, "Link not found")
}
func Formatter(apiContext *types.APIContext, resource *types.RawResource) {
workloadID := resource.ID
workloadSchema := apiContext.Schemas.Schema(&schema.Version, "workload")
resource.Links["self"] = apiContext.URLBuilder.ResourceLinkByID(workloadSchema, workloadID)
resource.Links["remove"] = apiContext.URLBuilder.ResourceLinkByID(workloadSchema, workloadID)
resource.Links["update"] = apiContext.URLBuilder.ResourceLinkByID(workloadSchema, workloadID)
}
func DeploymentFormatter(apiContext *types.APIContext, resource *types.RawResource) {
workloadID := resource.ID
workloadSchema := apiContext.Schemas.Schema(&schema.Version, "workload")
Formatter(apiContext, resource)
resource.Links["revisions"] = apiContext.URLBuilder.ResourceLinkByID(workloadSchema, workloadID) + "/" + workloadRevisions
resource.Actions["pause"] = apiContext.URLBuilder.ActionLinkByID(workloadSchema, workloadID, "pause")
resource.Actions["resume"] = apiContext.URLBuilder.ActionLinkByID(workloadSchema, workloadID, "resume")
resource.Actions["upgrade"] = apiContext.URLBuilder.ActionLinkByID(workloadSchema, workloadID, "upgrade")
resource.Actions["rollback"] = apiContext.URLBuilder.ActionLinkByID(workloadSchema, workloadID, "rollback")
}
type Handler struct {
}
func splitID(id string) (string, string) {
namespace := ""
parts := strings.SplitN(id, ":", 3)
if len(parts) == 3 {
namespace = parts[1]
id = parts[2]
}
return namespace, id
}