forked from apache/camel-k
/
run_help.go
162 lines (146 loc) · 5.75 KB
/
run_help.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
/*
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 cmd
import (
"context"
"crypto/sha1" //nolint
"fmt"
"path"
"strings"
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/client"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/apache/camel-k/pkg/util/resource"
"github.com/magiconair/properties"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
)
//nolint
func hashFrom(contents ...[]byte) string {
// SHA1 because we need to limit the length to less than 64 chars
hash := sha1.New()
for _, c := range contents {
hash.Write(c)
}
return fmt.Sprintf("%x", hash.Sum(nil))
}
func parseConfigAndGenCm(ctx context.Context, cmd *cobra.Command, c client.Client, config *resource.Config, integration *v1.Integration, enableCompression bool) (*corev1.ConfigMap, error) {
switch config.StorageType() {
case resource.StorageTypeConfigmap:
cm := kubernetes.LookupConfigmap(ctx, c, integration.Namespace, config.Name())
if cm == nil {
fmt.Fprintln(cmd.ErrOrStderr(), "Warn:", config.Name(), "Configmap not found in", integration.Namespace, "namespace, make sure to provide it before the Integration can run")
} else if config.ContentType() != resource.ContentTypeData && cm.BinaryData != nil {
return nil, fmt.Errorf("you cannot provide a binary config, use a text file instead")
}
case resource.StorageTypeSecret:
secret := kubernetes.LookupSecret(ctx, c, integration.Namespace, config.Name())
if secret == nil {
fmt.Fprintln(cmd.ErrOrStderr(), "Warn:", config.Name(), "Secret not found in", integration.Namespace, "namespace, make sure to provide it before the Integration can run")
}
case resource.StorageTypeFile:
// Don't allow a binary non compressed resource
rawData, contentType, err := loadRawContent(ctx, config.Name())
if err != nil {
return nil, err
}
if config.ContentType() != resource.ContentTypeData && !enableCompression && isBinary(contentType) {
return nil, fmt.Errorf("you cannot provide a binary config, use a text file or check --resource flag instead")
}
resourceType := v1.ResourceTypeData
if config.ContentType() == resource.ContentTypeText {
resourceType = v1.ResourceTypeConfig
}
resourceSpec, err := binaryOrTextResource(path.Base(config.Name()), rawData, contentType, enableCompression, resourceType, config.DestinationPath())
if err != nil {
return nil, err
}
return resource.ConvertFileToConfigmap(ctx, c, config, integration.Namespace, integration.Name, resourceSpec.Content, resourceSpec.RawContent)
default:
// Should never reach this
return nil, fmt.Errorf("invalid option type %s", config.StorageType())
}
return nil, nil
}
func binaryOrTextResource(fileName string, data []byte, contentType string, base64Compression bool, resourceType v1.ResourceType, destinationPath string) (v1.ResourceSpec, error) {
resourceSpec := v1.ResourceSpec{
DataSpec: v1.DataSpec{
Name: fileName,
Path: destinationPath,
ContentKey: fileName,
ContentType: contentType,
Compression: false,
},
Type: resourceType,
}
if !base64Compression && isBinary(contentType) {
resourceSpec.RawContent = data
return resourceSpec, nil
}
// either is a text resource or base64 compression is enabled
if base64Compression {
content, err := compressToString(data)
if err != nil {
return resourceSpec, err
}
resourceSpec.Content = content
resourceSpec.Compression = true
} else {
resourceSpec.Content = string(data)
}
return resourceSpec, nil
}
func filterFileLocation(maybeFileLocations []string) []string {
filteredOptions := make([]string, 0)
for _, option := range maybeFileLocations {
if strings.HasPrefix(option, "file:") {
localPath, _ := resource.ParseFileValue(strings.Replace(option, "file:", "", 1))
filteredOptions = append(filteredOptions, localPath)
}
}
return filteredOptions
}
func mergePropertiesWithPrecedence(items []string) (*properties.Properties, error) {
loPrecedenceProps := properties.NewProperties()
hiPrecedenceProps := properties.NewProperties()
for _, item := range items {
prop, err := extractProperties(item)
if err != nil {
return nil, err
}
// We consider file props to have a lower priority versus single properties
if strings.HasPrefix(item, "file:") {
loPrecedenceProps.Merge(prop)
} else {
hiPrecedenceProps.Merge(prop)
}
}
// Any property contained in both collections will be merged
// giving precedence to the ones in hiPrecedenceProps
loPrecedenceProps.Merge(hiPrecedenceProps)
return loPrecedenceProps, nil
}
// The function parse the value and if it is a file (file:/path/), it will parse as property file
// otherwise return a single property built from the item passed as `key=value`.
func extractProperties(value string) (*properties.Properties, error) {
if !strings.HasPrefix(value, "file:") {
return keyValueProps(value)
}
// we already validated the existence of files during validate()
return loadPropertyFile(strings.Replace(value, "file:", "", 1))
}
func keyValueProps(value string) (*properties.Properties, error) {
return properties.Load([]byte(value), properties.UTF8)
}