forked from openfaas/faas-netes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
132 lines (106 loc) · 3.33 KB
/
delete.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
// Copyright (c) Alex Ellis 2017. All rights reserved.
// 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"
"net/http"
"github.com/innoobijr/faas-provider/types"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// MakeDeleteHandler delete a function
func MakeDeleteHandler(defaultNamespace string, clientset *kubernetes.Clientset) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
q := r.URL.Query()
namespace := q.Get("namespace")
lookupNamespace := defaultNamespace
if len(namespace) > 0 {
lookupNamespace = namespace
}
if lookupNamespace == "kube-system" {
http.Error(w, "unable to list within the kube-system namespace", http.StatusUnauthorized)
return
}
if lookupNamespace != defaultNamespace {
http.Error(w, fmt.Sprintf("namespace must be: %s", defaultNamespace), http.StatusBadRequest)
return
}
body, _ := io.ReadAll(r.Body)
request := types.DeleteFunctionRequest{}
err := json.Unmarshal(body, &request)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if len(request.FunctionName) == 0 {
w.WriteHeader(http.StatusBadRequest)
return
}
getOpts := metav1.GetOptions{}
// This makes sure we don't delete non-labelled deployments
deployment, findDeployErr := clientset.AppsV1().
Deployments(lookupNamespace).
Get(context.TODO(), request.FunctionName, getOpts)
if findDeployErr != nil {
if errors.IsNotFound(findDeployErr) {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
w.Write([]byte(findDeployErr.Error()))
return
}
if isFunction(deployment) {
err := deleteFunction(lookupNamespace, clientset, request, w)
if err != nil {
return
}
} else {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Not a function: " + request.FunctionName))
return
}
w.WriteHeader(http.StatusAccepted)
}
}
func isFunction(deployment *appsv1.Deployment) bool {
if deployment != nil {
if _, found := deployment.Labels["faas_function"]; found {
return true
}
}
return false
}
func deleteFunction(functionNamespace string, clientset *kubernetes.Clientset, request types.DeleteFunctionRequest, w http.ResponseWriter) error {
foregroundPolicy := metav1.DeletePropagationForeground
opts := &metav1.DeleteOptions{PropagationPolicy: &foregroundPolicy}
if deployErr := clientset.AppsV1().Deployments(functionNamespace).
Delete(context.TODO(), request.FunctionName, *opts); deployErr != nil {
if errors.IsNotFound(deployErr) {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
w.Write([]byte(deployErr.Error()))
return fmt.Errorf("error deleting function's deployment")
}
if svcErr := clientset.CoreV1().
Services(functionNamespace).
Delete(context.TODO(), request.FunctionName, *opts); svcErr != nil {
if errors.IsNotFound(svcErr) {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
w.Write([]byte(svcErr.Error()))
return fmt.Errorf("error deleting function's service")
}
return nil
}