-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathclient.go
155 lines (127 loc) · 4.39 KB
/
client.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
// MIT License
//
// Copyright (c) 2020 Dmitrii Ustiugov and EASE lab
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"encoding/json"
"flag"
"fmt"
"os/exec"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/vhive-serverless/vSwarm/tools/endpoint"
)
// Functions is an object for unmarshalled JSON with functions to deploy.
type Functions struct {
Functions []functionType `json:"functions"`
}
type functionType struct {
Name string `json:"name"`
File string `json:"file"`
// number of functions to deploy from the same file (with different names)
Count int `json:"count"`
Eventing bool `json:"eventing"`
ApplyScript string `json:"applyScript"`
}
var (
gatewayURL = flag.String("gatewayURL", "192.168.1.240.sslip.io", "URL of the gateway")
namespaceName = flag.String("namespace", "default", "name of namespace in which services exists")
)
func main() {
funcPath := flag.String("funcPath", "./configs/knative_workloads", "Path to the folder with *.yml files")
funcJSONFile := flag.String("jsonFile", "./tools/deployer/functions.json", "Path to the JSON file with functions to deploy")
endpointsFile := flag.String("endpointsFile", "endpoints.json", "File with endpoints' metadata")
deploymentConcurrency := flag.Int("conc", 5, "Number of functions to deploy concurrently (for serving)")
flag.Parse()
log.Debug("Function files are taken from ", *funcPath)
funcSlice := getFuncSlice(*funcJSONFile)
urls := deploy(*funcPath, funcSlice, *deploymentConcurrency)
writeEndpoints(*endpointsFile, urls)
log.Infoln("Deployment finished")
}
func getFuncSlice(file string) []functionType {
log.Debug("Opening JSON file with functions: ", file)
byteValue, err := os.ReadFile(file)
if err != nil {
log.Fatal(err)
}
var functions Functions
if err := json.Unmarshal(byteValue, &functions); err != nil {
log.Fatal(err)
}
return functions.Functions
}
func deploy(funcPath string, funcSlice []functionType, deploymentConcurrency int) []string {
var urls []string
sem := make(chan bool, deploymentConcurrency) // limit the number of parallel deployments
for _, fType := range funcSlice {
for i := 0; i < fType.Count; i++ {
sem <- true
funcName := fmt.Sprintf("%s-%d", fType.Name, i)
url := fmt.Sprintf("%s.%s.%s", funcName, *namespaceName, *gatewayURL)
urls = append(urls, url)
filePath := filepath.Join(funcPath, fType.File)
go func(funcName, filePath string) {
defer func() { <-sem }()
deployFunction(funcName, filePath)
}(funcName, filePath)
}
}
for i := 0; i < cap(sem); i++ {
sem <- true
}
return urls
}
func deployFunction(funcName, filePath string) {
cmd := exec.Command(
"kn",
"service",
"apply",
funcName,
"-f",
filePath,
"--concurrency-target",
"1",
)
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
log.Warnf("Failed to deploy function %s, %s: %v\n%s\n", funcName, filePath, err, stdoutStderr)
}
log.Info("Deployed function ", funcName)
}
func writeEndpoints(filePath string, urls []string) {
var endpoints []endpoint.Endpoint
for _, url := range urls {
endpoints = append(endpoints, endpoint.Endpoint{
Hostname: url,
Eventing: false,
Matchers: nil,
})
}
data, err := json.MarshalIndent(endpoints, "", "\t")
if err != nil {
log.Fatalln("failed to marshal", err)
}
if err := os.WriteFile(filePath, data, 0644); err != nil {
log.Fatalln("failed to write", err)
}
}