forked from openfaas/faas-netes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
212 lines (162 loc) · 5.8 KB
/
update.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
// Copyright 2020 OpenFaaS Author(s)
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package handlers
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
"github.com/innoobijr/faas-netes/pkg/k8s"
types "github.com/innoobijr/faas-provider/types"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// MakeUpdateHandler update specified function
func MakeUpdateHandler(defaultNamespace string, factory k8s.FunctionFactory) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if r.Body != nil {
defer r.Body.Close()
}
body, _ := io.ReadAll(r.Body)
request := types.FunctionDeployment{}
err := json.Unmarshal(body, &request)
if err != nil {
wrappedErr := fmt.Errorf("unable to unmarshal request: %s", err.Error())
http.Error(w, wrappedErr.Error(), http.StatusBadRequest)
return
}
if err := ValidateDeployRequest(&request); err != nil {
wrappedErr := fmt.Errorf("validation failed: %s", err.Error())
http.Error(w, wrappedErr.Error(), http.StatusBadRequest)
return
}
lookupNamespace := defaultNamespace
if len(request.Namespace) > 0 {
lookupNamespace = request.Namespace
}
if lookupNamespace != defaultNamespace {
http.Error(w, fmt.Sprintf("namespace must be: %s", defaultNamespace), http.StatusBadRequest)
return
}
if lookupNamespace == "kube-system" {
http.Error(w, "unable to list within the kube-system namespace", http.StatusUnauthorized)
return
}
annotations, err := buildAnnotations(request)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if status, err := updateDeploymentSpec(ctx, lookupNamespace, factory, request, annotations); err != nil {
if !k8s.IsNotFound(err) {
log.Printf("error updating deployment: %s.%s, error: %s\n", request.Service, lookupNamespace, err)
return
}
wrappedErr := fmt.Errorf("unable update Deployment: %s.%s, error: %s", request.Service, lookupNamespace, err.Error())
http.Error(w, wrappedErr.Error(), status)
return
}
if status, err := updateService(lookupNamespace, factory, request, annotations); err != nil {
if !k8s.IsNotFound(err) {
log.Printf("error updating service: %s.%s, error: %s\n", request.Service, lookupNamespace, err)
}
wrappedErr := fmt.Errorf("unable update Service: %s.%s, error: %s", request.Service, request.Namespace, err.Error())
http.Error(w, wrappedErr.Error(), status)
return
}
w.WriteHeader(http.StatusAccepted)
}
}
func updateDeploymentSpec(
ctx context.Context,
functionNamespace string,
factory k8s.FunctionFactory,
request types.FunctionDeployment,
annotations map[string]string) (int, error) {
getOpts := metav1.GetOptions{}
deployment, findDeployErr := factory.Client.AppsV1().
Deployments(functionNamespace).
Get(context.TODO(), request.Service, getOpts)
if findDeployErr != nil {
return http.StatusNotFound, findDeployErr
}
if err := isAnonymous(request.Image); err != nil {
return http.StatusBadRequest, err
}
if len(deployment.Spec.Template.Spec.Containers) > 0 {
deployment.Spec.Template.Spec.Containers[0].Image = request.Image
deployment.Spec.Template.Spec.Containers[0].ImagePullPolicy = corev1.PullAlways
deployment.Spec.Template.Spec.Containers[0].Env = buildEnvVars(&request)
factory.ConfigureReadOnlyRootFilesystem(request, deployment)
factory.ConfigureContainerUserID(deployment)
deployment.Spec.Template.Spec.NodeSelector = createSelector(request.Constraints)
labels := map[string]string{
"faas_function": request.Service,
"uid": fmt.Sprintf("%d", time.Now().Nanosecond()),
}
if request.Labels != nil {
if min := getMinReplicaCount(*request.Labels); min != nil {
deployment.Spec.Replicas = min
}
for k, v := range *request.Labels {
labels[k] = v
}
}
// deployment.Labels = labels
deployment.Spec.Template.ObjectMeta.Labels = labels
deployment.Annotations = annotations
deployment.Spec.Template.Annotations = annotations
deployment.Spec.Template.ObjectMeta.Annotations = annotations
resources, resourceErr := createResources(request)
if resourceErr != nil {
return http.StatusBadRequest, resourceErr
}
deployment.Spec.Template.Spec.Containers[0].Resources = *resources
secrets := k8s.NewSecretsClient(factory.Client)
existingSecrets, err := secrets.GetSecrets(functionNamespace, request.Secrets)
if err != nil {
return http.StatusBadRequest, err
}
err = factory.ConfigureSecrets(request, deployment, existingSecrets)
if err != nil {
log.Println(err)
return http.StatusBadRequest, err
}
probes, err := factory.MakeProbes(request)
if err != nil {
return http.StatusBadRequest, err
}
deployment.Spec.Template.Spec.Containers[0].LivenessProbe = probes.Liveness
deployment.Spec.Template.Spec.Containers[0].ReadinessProbe = probes.Readiness
}
if _, updateErr := factory.Client.AppsV1().
Deployments(functionNamespace).
Update(context.TODO(), deployment, metav1.UpdateOptions{}); updateErr != nil {
return http.StatusInternalServerError, updateErr
}
return http.StatusAccepted, nil
}
func updateService(
functionNamespace string,
factory k8s.FunctionFactory,
request types.FunctionDeployment,
annotations map[string]string) (int, error) {
getOpts := metav1.GetOptions{}
service, findServiceErr := factory.Client.CoreV1().
Services(functionNamespace).
Get(context.TODO(), request.Service, getOpts)
if findServiceErr != nil {
return http.StatusNotFound, findServiceErr
}
service.Annotations = annotations
if _, updateErr := factory.Client.CoreV1().
Services(functionNamespace).
Update(context.TODO(), service, metav1.UpdateOptions{}); updateErr != nil {
return http.StatusInternalServerError, updateErr
}
return http.StatusAccepted, nil
}