-
Notifications
You must be signed in to change notification settings - Fork 0
/
customer.go
214 lines (184 loc) · 6.24 KB
/
customer.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
package khota_handler
import (
"context"
"encoding/json"
"io"
"net/http"
"strings"
v1 "github.com/baazhq/baaz/api/v1/types"
helm "github.com/baazhq/baaz/pkg/helmchartpath"
"github.com/gorilla/mux"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
labelPrefix = "baaz_" // Prefix for labels
dataplaneUnavailable = "unavailable" // Unavailable dataplane status
)
type CustomerListResponse struct {
Name string `json:"name"`
SaaSType string `json:"saas_type"`
CloudType string `json:"cloud_type"`
Status string `json:"status"`
Dataplane string `json:"dataplane"`
Labels map[string]string `json:"labels"`
}
// ListCustomer handles listing customers
func ListCustomer(w http.ResponseWriter, req *http.Request) {
client, _ := getKubeClientset()
nsList, err := client.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{
LabelSelector: "controlplane=baaz",
})
if err != nil {
handleError(w, err, CustomerNamespaceListEmpty, http.StatusInternalServerError)
return
}
var customerListResponse []CustomerListResponse
for _, ns := range nsList.Items {
ns, err := client.CoreV1().Namespaces().Get(context.TODO(), ns.Name, metav1.GetOptions{})
if err != nil {
handleError(w, err, CustomerNamespaceGetFail, http.StatusInternalServerError)
return
}
custLabels := getCustomLabel(ns.Labels)
newCrListResp := CustomerListResponse{
Name: ns.Name,
CloudType: ns.Labels["cloud_type"],
SaaSType: ns.Labels["saas_type"],
Dataplane: ns.Labels["dataplane"],
Status: active,
Labels: custLabels,
}
customerListResponse = append(customerListResponse, newCrListResp)
}
resp, err := json.Marshal(customerListResponse)
if err != nil {
handleError(w, err, JsonMarshallError, http.StatusInternalServerError)
return
}
sendJsonResponse(resp, http.StatusOK, &w)
}
// CreateCustomer handles creating a customer
func CreateCustomer(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
customerName := vars["customer_name"]
body, err := io.ReadAll(io.LimitReader(req.Body, 1048576))
if err != nil {
handleError(w, err, ServerReqSizeExceed, http.StatusBadRequest)
return
}
defer req.Body.Close()
var customer v1.Customer
if err := json.Unmarshal(body, &customer); err != nil {
handleError(w, err, ServerUnmarshallError, http.StatusInternalServerError)
return
}
client, _ := getKubeClientset()
_, err = client.CoreV1().Namespaces().Get(context.TODO(), customerName, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
labels := map[string]string{
"saas_type": string(customer.SaaSType),
"cloud_type": string(customer.CloudType),
"customer_name": string(customerName),
"dataplane": dataplaneUnavailable,
"controlplane": "baaz",
}
if customer.Labels["private_mode"] == "true" {
labels["private_mode"] = "true"
}
allLabels := mergeMaps(labels, setLabelPrefix(customer.Labels))
_, err := client.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: customerName,
Labels: allLabels,
},
}, metav1.CreateOptions{})
if err != nil {
handleError(w, err, CustomerNamespaceCreateFail, http.StatusInternalServerError)
return
}
helmBuild := helm.NewHelm(
customerName+"-customer",
customerName,
customer_chartpath,
nil,
[]string{
"customer.name=" + customerName,
"customer.labels.saas_type=" + string(customer.SaaSType),
"customer.labels.cloud_type=" + string(customer.CloudType),
"customer.labels.customer_name=" + string(customerName),
"customer.labels.dataplane=" + dataplaneUnavailable,
"customer.labels.controlplane=" + "baaz",
},
)
if err := helmBuild.Apply(); err != nil {
handleError(w, err, CustomerNamespaceCreateFail, http.StatusInternalServerError)
return
}
handleSuccess(w, CustomerNamespaceSuccess, http.StatusOK)
sendEventParseable(customersEventStream, customerCreateSuccessEvent, allLabels, map[string]string{"customer_name": customerName})
return
}
handleError(w, err, CustomerNamespaceExists, http.StatusConflict)
}
// UpdateCustomer handles updating a customer
func UpdateCustomer(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
customerName := vars["customer_name"]
body, err := io.ReadAll(io.LimitReader(req.Body, 1048576))
if err != nil {
handleError(w, err, ServerReqSizeExceed, http.StatusBadRequest)
return
}
defer req.Body.Close()
var customer v1.Customer
if err := json.Unmarshal(body, &customer); err != nil {
handleError(w, err, ServerUnmarshallError, http.StatusInternalServerError)
return
}
client, _ := getKubeClientset()
ns, err := client.CoreV1().Namespaces().Get(context.TODO(), customerName, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
handleError(w, err, CustomerNamespaceDoesNotExists, http.StatusNotFound)
return
}
for key, val := range setLabelPrefix(customer.Labels) {
ns.Labels[key] = val
}
if _, err := client.CoreV1().Namespaces().Update(context.TODO(), ns, metav1.UpdateOptions{}); err != nil {
handleError(w, err, CustomerNamespaceUpdateFail, http.StatusInternalServerError)
return
}
handleSuccess(w, CustomerNamespaceUpdateSuccess, http.StatusOK)
}
// handleError logs and handles errors
func handleError(w http.ResponseWriter, err error, msg CustomMsg, code int) {
res := NewResponse(msg, internal_error, err, code)
res.SetResponse(&w)
res.LogResponse()
}
// handleSuccess logs and handles success responses
func handleSuccess(w http.ResponseWriter, msg CustomMsg, code int) {
res := NewResponse(msg, success, nil, code)
res.SetResponse(&w)
res.LogResponse()
}
// setLabelPrefix adds a prefix to all labels
func setLabelPrefix(data map[string]string) map[string]string {
modifiedLabels := make(map[string]string)
for key, val := range data {
modifiedLabels[labelPrefix+key] = val
}
return modifiedLabels
}
// getCustomLabel extracts custom labels
func getCustomLabel(data map[string]string) map[string]string {
filteredMap := make(map[string]string)
for key, val := range data {
if strings.HasPrefix(key, labelPrefix) {
filteredMap[strings.TrimPrefix(key, labelPrefix)] = val
}
}
return filteredMap
}