forked from openfaas/openfaas-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
156 lines (122 loc) · 3.14 KB
/
handler.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
package function
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/openfaas/openfaas-cloud/sdk"
)
// Handle a serverless request
func Handle(req []byte) string {
garbageReq := GarbageRequest{}
err := json.Unmarshal(req, &garbageReq)
if err != nil {
log.Panic(err)
}
gatewayURL := os.Getenv("gateway_url")
deployedFunctions, err := listFunctions(garbageReq.Owner, gatewayURL)
if err != nil {
log.Panic(err)
}
fmt.Printf("Functions owned by %s:\n %s", garbageReq.Owner, deployedFunctions)
deleted := 0
for _, fn := range deployedFunctions {
if fn.GetRepo() == garbageReq.Repo {
if !included(&fn, garbageReq.Owner, garbageReq.Functions) {
err = deleteFunction(fn.Name, gatewayURL)
deleted = deleted + 1
if err != nil {
log.Println(err)
}
}
}
}
return fmt.Sprintf("Garbage collection ran for %s/%s - %d functions deleted.", garbageReq.Owner, garbageReq.Repo, deleted)
}
func formatCloudName(name, owner string) string {
return owner + "-" + name
}
func included(fn *openFaaSFunction, owner string, functionStack []string) bool {
for _, name := range functionStack {
if strings.EqualFold(formatCloudName(name, owner), fn.Name) {
return true
}
}
return false
}
func deleteFunction(target, gatewayURL string) error {
var err error
fmt.Println("Delete ", target)
c := http.Client{
Timeout: time.Second * 3,
}
delReq := struct {
FunctionName string
}{
FunctionName: target,
}
bytesReq, _ := json.Marshal(delReq)
bufferReader := bytes.NewBuffer(bytesReq)
httpReq, _ := http.NewRequest(http.MethodDelete, gatewayURL+"system/functions", bufferReader)
addAuthErr := sdk.AddBasicAuth(httpReq)
if addAuthErr != nil {
log.Printf("Basic auth error %s", addAuthErr)
}
response, err := c.Do(httpReq)
if err == nil {
defer response.Body.Close()
if response.Body != nil {
bodyBytes, bErr := ioutil.ReadAll(response.Body)
if bErr != nil {
log.Fatal(bErr)
}
log.Println(string(bodyBytes))
}
}
return err
}
func listFunctions(owner, gatewayURL string) ([]openFaaSFunction, error) {
var err error
c := http.Client{
Timeout: time.Second * 3,
}
request, _ := http.NewRequest(http.MethodGet, gatewayURL+"/function/list-functions?user="+owner, nil)
response, err := c.Do(request)
if err == nil {
defer response.Body.Close()
if response.Body != nil {
bodyBytes, bErr := ioutil.ReadAll(response.Body)
if bErr != nil {
log.Fatal(bErr)
}
functions := []openFaaSFunction{}
mErr := json.Unmarshal(bodyBytes, &functions)
if mErr != nil {
log.Fatal(mErr)
}
return functions, nil
}
}
return nil, fmt.Errorf("no functions found for user: %s", owner)
}
type GarbageRequest struct {
Functions []string `json:"functions"`
Repo string `json:"repo"`
Owner string `json:"owner"`
}
type openFaaSFunction struct {
Name string `json:"name"`
Image string `json:"image"`
Labels map[string]string `json:"labels"`
}
func (f *openFaaSFunction) GetOwner() string {
return f.Labels["Git-Owner"]
}
func (f *openFaaSFunction) GetRepo() string {
return f.Labels["Git-Repo"]
}