forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
201 lines (173 loc) · 5.55 KB
/
store.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
package ingress
import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"github.com/rancher/norman/store/transform"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
"github.com/rancher/rancher/pkg/api/store/workload"
"github.com/rancher/rancher/pkg/controllers/user/ingress"
"github.com/rancher/rancher/pkg/ref"
"github.com/sirupsen/logrus"
)
const (
ingressStateAnnotation = "field.cattle.io/ingressState"
)
func Wrap(store types.Store) types.Store {
modify := &Store{
store,
}
return New(modify)
}
type Store struct {
types.Store
}
func (p *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
name, _ := data["name"].(string)
namespace, _ := data["namespaceId"].(string)
id := ref.FromStrings(namespace, name)
formatData(id, data, false)
data, err := p.Store.Create(apiContext, schema, data)
return data, err
}
func (p *Store) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) {
formatData(id, data, false)
data, err := p.Store.Update(apiContext, schema, data, id)
return data, err
}
func formatData(id string, data map[string]interface{}, forFrontend bool) {
oldState := getState(data)
newState := map[string]string{}
// transform default backend
if target, ok := values.GetValue(data, "defaultBackend"); ok && target != nil {
updateRule(convert.ToMapInterface(target), id, "", "/", forFrontend, data, oldState, newState)
}
// transform rules
if paths, ok := getPaths(data); ok {
for hostpath, target := range paths {
updateRule(target, id, hostpath.host, hostpath.path, forFrontend, data, oldState, newState)
}
}
updateCerts(data, forFrontend, oldState, newState)
setState(data, newState)
workload.SetPublicEnpointsFields(data)
}
func updateRule(target map[string]interface{}, id, host, path string, forFrontend bool, data map[string]interface{}, oldState map[string]string, newState map[string]string) {
targetData := convert.ToMapInterface(target)
port, _ := targetData["targetPort"]
serviceID, _ := targetData["serviceId"].(string)
namespace, name := ref.Parse(id)
stateKey := ingress.GetStateKey(name, namespace, host, path, convert.ToString(port))
if forFrontend {
isService := true
if serviceValue, ok := oldState[stateKey]; ok && !convert.IsEmpty(serviceValue) {
targetData["workloadIds"] = strings.Split(serviceValue, "/")
isService = false
}
if isService {
targetData["serviceId"] = fmt.Sprintf("%s:%s", data["namespaceId"].(string), serviceID)
} else {
delete(targetData, "serviceId")
}
} else {
workloadIDs := convert.ToStringSlice(targetData["workloadIds"])
if serviceID != "" {
splitted := strings.Split(serviceID, ":")
if len(splitted) > 1 {
serviceID = splitted[1]
}
} else {
serviceID = getServiceID(stateKey)
}
newState[stateKey] = strings.Join(workloadIDs, "/")
targetData["serviceId"] = serviceID
}
}
func getServiceID(stateKey string) string {
bytes, err := base64.URLEncoding.DecodeString(stateKey)
if err != nil {
return ""
}
sum := md5.Sum(bytes)
hex := "ingress-" + hex.EncodeToString(sum[:])
return hex
}
func getCertKey(key string) string {
return base64.URLEncoding.EncodeToString([]byte(key))
}
type hostPath struct {
host string
path string
}
func getPaths(data map[string]interface{}) (map[hostPath]map[string]interface{}, bool) {
v, ok := values.GetValue(data, "rules")
if !ok {
return nil, false
}
result := make(map[hostPath]map[string]interface{})
for _, rule := range convert.ToMapSlice(v) {
converted := convert.ToMapInterface(rule)
paths, ok := converted["paths"]
if ok {
for path, target := range convert.ToMapInterface(paths) {
result[hostPath{host: convert.ToString(converted["host"]), path: path}] = convert.ToMapInterface(target)
}
}
}
return result, true
}
func setState(data map[string]interface{}, stateMap map[string]string) {
content, err := json.Marshal(stateMap)
if err != nil {
logrus.Errorf("failed to save state on ingress: %v", data["id"])
return
}
values.PutValue(data, string(content), "annotations", ingressStateAnnotation)
}
func getState(data map[string]interface{}) map[string]string {
state := map[string]string{}
v, ok := values.GetValue(data, "annotations", ingressStateAnnotation)
if ok {
json.Unmarshal([]byte(convert.ToString(v)), &state)
}
return state
}
func updateCerts(data map[string]interface{}, forFrontend bool, oldState map[string]string, newState map[string]string) {
if forFrontend {
if certs, _ := values.GetSlice(data, "tls"); len(certs) > 0 {
for _, cert := range certs {
certName := convert.ToString(cert["certificateId"])
certKey := getCertKey(certName)
cert["certificateId"] = oldState[certKey]
}
}
} else {
if certs, _ := values.GetSlice(data, "tls"); len(certs) > 0 {
for _, cert := range certs {
certificateID := convert.ToString(cert["certificateId"])
id := strings.Split(certificateID, ":")
if len(id) == 2 {
certName := id[1]
certKey := getCertKey(certName)
newState[certKey] = certificateID
cert["certificateId"] = certName
}
}
}
}
}
func New(store types.Store) types.Store {
return &transform.Store{
Store: store,
Transformer: func(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error) {
id, _ := data["id"].(string)
formatData(id, data, true)
return data, nil
},
}
}