/
stack.go
189 lines (157 loc) · 4.31 KB
/
stack.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
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package stack
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"regexp"
"time"
envsubst "github.com/drone/envsubst"
glob "github.com/ryanuber/go-glob"
yaml "gopkg.in/yaml.v2"
)
const legacyProviderName = "faas"
const providerName = "openfaas"
const defaultSchemaVersion = "1.0"
// ValidSchemaVersions available schema versions
var ValidSchemaVersions = []string{
"1.0",
}
// ParseYAMLFile parse YAML file into a stack of "services".
func ParseYAMLFile(yamlFile, regex, filter string, envsubst bool) (*Services, error) {
var err error
var fileData []byte
urlParsed, err := url.Parse(yamlFile)
if err == nil && len(urlParsed.Scheme) > 0 {
fmt.Println("Parsed: " + urlParsed.String())
fileData, err = fetchYAML(urlParsed)
if err != nil {
return nil, err
}
} else {
fileData, err = ioutil.ReadFile(yamlFile)
if err != nil {
return nil, err
}
}
return ParseYAMLData(fileData, regex, filter, envsubst)
}
func substituteEnvironment(data []byte) ([]byte, error) {
ret, err := envsubst.Parse(string(data))
if err != nil {
return nil, err
}
res, resErr := ret.Execute(func(input string) string {
if val, ok := os.LookupEnv(input); ok {
return val
}
return ""
})
return []byte(res), resErr
}
// ParseYAMLData parse YAML data into a stack of "services".
func ParseYAMLData(fileData []byte, regex string, filter string, envsubst bool) (*Services, error) {
var services Services
regexExists := len(regex) > 0
filterExists := len(filter) > 0
var source []byte
if envsubst {
substData, substErr := substituteEnvironment(fileData)
if substErr != nil {
return &services, substErr
}
source = substData
} else {
source = fileData
}
err := yaml.Unmarshal(source, &services)
if err != nil {
fmt.Printf("Error with YAML file\n")
return nil, err
}
for _, f := range services.Functions {
if f.Language == "Dockerfile" {
f.Language = "dockerfile"
}
}
if services.Provider.Name != providerName {
return nil, fmt.Errorf(`['%s'] is the only valid "provider.name" for the OpenFaaS CLI, but you gave: %s`, providerName, services.Provider.Name)
}
if len(services.Version) > 0 && !IsValidSchemaVersion(services.Version) {
return nil, fmt.Errorf("%s are the only valid versions for the stack file - found: %s", ValidSchemaVersions, services.Version)
}
if regexExists && filterExists {
return nil, fmt.Errorf("pass in a regex or a filter, not both")
}
if regexExists || filterExists {
for k, function := range services.Functions {
var match bool
var err error
function.Name = k
if regexExists {
match, err = regexp.MatchString(regex, function.Name)
if err != nil {
return nil, err
}
} else {
match = glob.Glob(filter, function.Name)
}
if !match {
delete(services.Functions, function.Name)
}
}
if len(services.Functions) == 0 {
return nil, fmt.Errorf("no functions matching --filter/--regex were found in the YAML file")
}
}
return &services, nil
}
func makeHTTPClient(timeout *time.Duration) http.Client {
if timeout != nil {
return http.Client{
Timeout: *timeout,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: *timeout,
// KeepAlive: 0,
}).DialContext,
// MaxIdleConns: 1,
// DisableKeepAlives: true,
IdleConnTimeout: 120 * time.Millisecond,
ExpectContinueTimeout: 1500 * time.Millisecond,
},
}
}
// This should be used for faas-cli invoke etc.
return http.Client{}
}
// fetchYAML pulls in file from remote location such as GitHub raw file-view
func fetchYAML(address *url.URL) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, address.String(), nil)
if err != nil {
return nil, err
}
timeout := 120 * time.Second
client := makeHTTPClient(&timeout)
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
resBytes, err := ioutil.ReadAll(res.Body)
return resBytes, err
}
// IsValidSchemaVersion validates schema version
func IsValidSchemaVersion(schemaVersion string) bool {
for _, validVersion := range ValidSchemaVersions {
if schemaVersion == validVersion {
return true
}
}
return false
}