-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
168 lines (130 loc) · 3.71 KB
/
main.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
// Copyright (c) 2023 Alex Ellis, OpenFaaS Ltd
// License: MIT
package main
import (
"context"
b64 "encoding/base64"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/alexellis/go-execute/v2"
"github.com/openfaas/faas-provider/types"
"github.com/openfaas/go-sdk"
)
func main() {
var (
gateway, username, namespace string
action string
name, image, fprocess string
functions, startAt, workers int
)
flag.StringVar(&gateway, "gateway", "http://127.0.0.1:8080", "gateway url")
flag.StringVar(&namespace, "namespace", "openfaas-fn", "namespace for functions")
flag.IntVar(&functions, "functions", 100, "number of functions to create")
flag.IntVar(&startAt, "start-at", 0, "start at function number")
flag.StringVar(&name, "name", "env", "name of function")
flag.StringVar(&image, "image", "", "image to use for function")
flag.StringVar(&fprocess, "fprocess", "env", "fprocess to use for function")
flag.StringVar(&action, "action", "create", "action to perform")
flag.IntVar(&workers, "workers", 1, "number of workers to use")
flag.Parse()
gatewayURL, err := url.Parse(gateway)
if err != nil {
panic(err)
}
password := lookupPasswordViaKubectl()
username = "admin"
auth := &sdk.BasicAuth{
Username: username,
Password: password,
}
if len(image) == 0 {
panic("-image is required")
}
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
wg := sync.WaitGroup{}
wg.Add(workers)
started := time.Now()
workChan := make(chan string)
for i := 0; i < workers; i++ {
go func(worker int) {
for name := range workChan {
if len(name) > 0 {
if err := reconcile(worker, name, image, fprocess, client, namespace, action); err != nil {
panic(err)
}
}
}
wg.Done()
}(i)
}
for i := startAt; i < functions; i++ {
functionName := fmt.Sprintf("%s-%d", name, i+1)
workChan <- functionName
}
close(workChan)
wg.Wait()
log.Printf("Took: %.2f", time.Since(started).Seconds())
}
func reconcile(worker int, name, image, fprocess string, client *sdk.Client, namespace, action string) error {
if action == "create" {
log.Printf("[%d] Creating: %s", worker, name)
spec := types.FunctionDeployment{
Service: name,
Image: image,
Namespace: namespace,
}
if len(fprocess) > 0 {
spec.EnvProcess = fprocess
}
start := time.Now()
code, err := client.Deploy(context.Background(), spec)
if err != nil {
return err
}
if code != http.StatusOK && code != http.StatusAccepted {
return err
}
log.Printf("[%d] Created: %s, status: %d (%dms)", worker, name, code, time.Since(start).Milliseconds())
} else if action == "delete" {
start := time.Now()
log.Printf("[%d] Deleting function: %s", worker, name)
if err := client.DeleteFunction(context.Background(), name, namespace); err != nil {
log.Printf("[%d] Delete %s, error: %s", worker, name, err)
if !strings.Contains(err.Error(), "not found") {
return err
}
}
log.Printf("[%d] Deleted function: %s (%dms)", worker, name, time.Since(start).Milliseconds())
}
return nil
}
func lookupPasswordViaKubectl() string {
cmd := execute.ExecTask{
Command: "kubectl",
Args: []string{"get", "secret", "-n", "openfaas", "basic-auth", "-o", "jsonpath='{.data.basic-auth-password}'"},
StreamStdio: false,
PrintCommand: false,
Env: os.Environ(),
}
res, err := cmd.Execute(context.Background())
if err != nil {
panic(err)
}
if res.ExitCode != 0 {
panic("Non-zero exit code: " + res.Stderr)
}
resOut := strings.Trim(res.Stdout, "\\'")
decoded, err := b64.StdEncoding.DecodeString(resOut)
if err != nil {
panic(err)
}
password := strings.TrimSpace(string(decoded))
return password
}