/
config.go
246 lines (210 loc) · 7.34 KB
/
config.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package resource
import (
"context"
"crypto/sha1" // nolint: gosec
"fmt"
"path/filepath"
"regexp"
"strings"
"github.com/apache/camel-k/pkg/client"
"github.com/apache/camel-k/pkg/util/camel"
"github.com/apache/camel-k/pkg/util/kubernetes"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
)
// Config represents a config option.
type Config struct {
storageType StorageType
contentType ContentType
resourceName string
resourceKey string
destinationPath string
}
// DestinationPath is the location where the resource will be stored on destination.
func (config *Config) DestinationPath() string {
return config.destinationPath
}
// StorageType is the type of storage used for the configuration.
func (config *Config) StorageType() StorageType {
return config.storageType
}
// ContentType is the type of content used for the configuration.
func (config *Config) ContentType() ContentType {
return config.contentType
}
// Name is the name of the resource.
func (config *Config) Name() string {
return config.resourceName
}
// Key is the key specified for the resource.
func (config *Config) Key() string {
return config.resourceKey
}
// String represents the unparsed value of the resource.
func (config *Config) String() string {
s := fmt.Sprintf("%s:%s", config.storageType, config.resourceName)
if config.resourceKey != "" {
s = fmt.Sprintf("%s/%s", s, config.resourceKey)
}
if config.destinationPath != "" {
s = fmt.Sprintf("%s@%s", s, config.destinationPath)
}
return s
}
// StorageType represent a resource/config type such as configmap, secret or local file.
type StorageType string
const (
// StorageTypeConfigmap --.
StorageTypeConfigmap StorageType = "configmap"
// StorageTypeSecret --.
StorageTypeSecret StorageType = "secret"
// StorageTypeFile --.
StorageTypeFile StorageType = "file"
// StorageTypePVC --.
StorageTypePVC StorageType = "pvc"
)
// ContentType represent what kind of a content is, either data or purely text configuration.
type ContentType string
const (
// ContentTypeData can contain binary content, won't be parsed to look for user properties.
ContentTypeData ContentType = "data"
// ContentTypeText can't contain binary content, will be parsed to look for user properties.
ContentTypeText ContentType = "text"
)
var (
validConfigSecretRegexp = regexp.MustCompile(`^(configmap|secret)\:([\w\.\-\_\:\/@]+)$`)
validFileRegexp = regexp.MustCompile(`^file\:([\w\.\-\_\:\/@" ]+)$`)
validResourceRegexp = regexp.MustCompile(`^([\w\.\-\_\:]+)(\/([\w\.\-\_\:]+))?(\@([\w\.\-\_\:\/]+))?$`)
)
func newConfig(storageType StorageType, contentType ContentType, value string) *Config {
rn, mk, mp := parseResourceValue(storageType, value)
return &Config{
storageType: storageType,
contentType: contentType,
resourceName: rn,
resourceKey: mk,
destinationPath: mp,
}
}
func parseResourceValue(storageType StorageType, value string) (string, string, string) {
if storageType == StorageTypeFile {
resource, maybeDestinationPath := ParseFileValue(value)
return resource, "", maybeDestinationPath
}
return parseCMOrSecretValue(value)
}
// ParseFileValue will parse a file resource/config option to return the local path and the
// destination path expected.
func ParseFileValue(value string) (string, string) {
split := strings.SplitN(value, "@", 2)
if len(split) == 2 {
return split[0], split[1]
}
return value, ""
}
func parseCMOrSecretValue(value string) (string, string, string) {
if !validResourceRegexp.MatchString(value) {
return value, "", ""
}
// Must have 3 values
groups := validResourceRegexp.FindStringSubmatch(value)
return groups[1], groups[3], groups[5]
}
// ParseResource will parse a resource and return a Config.
func ParseResource(item string) (*Config, error) {
return parse(item, ContentTypeData)
}
// ParseVolume will parse a volume and return a Config.
func ParseVolume(item string) (*Config, error) {
configParts := strings.Split(item, ":")
if len(configParts) != 2 {
return nil, fmt.Errorf("could not match pvc as %s", item)
}
return &Config{
storageType: StorageTypePVC,
resourceName: configParts[0],
destinationPath: configParts[1],
}, nil
}
// ParseConfig will parse a config and return a Config.
func ParseConfig(item string) (*Config, error) {
return parse(item, ContentTypeText)
}
func parse(item string, contentType ContentType) (*Config, error) {
var cot StorageType
var value string
switch {
case validConfigSecretRegexp.MatchString(item):
// parse as secret/configmap
groups := validConfigSecretRegexp.FindStringSubmatch(item)
switch groups[1] {
case "configmap":
cot = StorageTypeConfigmap
case "secret":
cot = StorageTypeSecret
}
value = groups[2]
case validFileRegexp.MatchString(item):
// parse as file
groups := validFileRegexp.FindStringSubmatch(item)
cot = StorageTypeFile
value = groups[1]
default:
return nil, fmt.Errorf("could not match config, secret or file configuration as %s", item)
}
return newConfig(cot, contentType, value), nil
}
// ConvertFileToConfigmap convert a local file resource type in a configmap type
// taking care to create the Configmap on the cluster. The method will change the value of config parameter
// to reflect the conversion applied transparently.
func ConvertFileToConfigmap(ctx context.Context, c client.Client, config *Config, namespace string, integrationName string,
content string, rawContent []byte) (*corev1.ConfigMap, error) {
filename := filepath.Base(config.Name())
if config.DestinationPath() == "" {
config.resourceKey = filename
// As we are changing the resource to a configmap type
// we must declare the destination path
if config.ContentType() == ContentTypeData {
config.destinationPath = camel.ResourcesDefaultMountPath + "/" + filename
} else {
config.destinationPath = camel.ConfigResourcesMountPath + "/" + filename
}
} else {
config.resourceKey = filepath.Base(config.DestinationPath())
}
genCmName := fmt.Sprintf("cm-%s", hashFrom([]byte(filename), []byte(integrationName), []byte(content), rawContent))
cm := kubernetes.NewConfigMap(namespace, genCmName, filename, config.Key(), content, rawContent)
err := c.Create(ctx, cm)
if err != nil {
if k8serrors.IsAlreadyExists(err) {
// We'll reuse it, as is
} else {
return cm, err
}
}
config.storageType = StorageTypeConfigmap
config.resourceName = cm.Name
return cm, nil
}
func hashFrom(contents ...[]byte) string {
// SHA1 because we need to limit the length to less than 64 chars
hash := sha1.New() // nolint: gosec
for _, c := range contents {
hash.Write(c)
}
return fmt.Sprintf("%x", hash.Sum(nil))
}