• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes / kompose / 4217081086

19 Feb 2023 04:20PM UTC coverage: 53.126%. Remained the same
4217081086

push

github

GitHub
Bump golang.org/x/net from 0.5.0 to 0.7.0 (#1592)

1963 of 3695 relevant lines covered (53.13%)

7.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.7
/pkg/loader/compose/utils.go
1
/*
2
Copyright 2017 The Kubernetes Authors All rights reserved.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package compose
18

19
import (
20
        "io"
21
        "os"
22
        "path/filepath"
23
        "regexp"
24
        "strings"
25

26
        "github.com/kubernetes/kompose/pkg/kobject"
27
        "github.com/pkg/errors"
28

29
        api "k8s.io/api/core/v1"
30
)
31

32
const (
33
        // LabelServiceType defines the type of service to be created
34
        LabelServiceType = "kompose.service.type"
35
        // LabelServiceExternalTrafficPolicy defines the external policy traffic of service to be created
36
        LabelServiceExternalTrafficPolicy = "kompose.service.external-traffic-policy"
37
        // LabelServiceGroup defines the group of services in a single pod
38
        LabelServiceGroup = "kompose.service.group"
39
        // LabelNodePortPort defines the port value for NodePort service
40
        LabelNodePortPort = "kompose.service.nodeport.port"
41
        // LabelServiceExpose defines if the service needs to be made accessible from outside the cluster or not
42
        LabelServiceExpose = "kompose.service.expose"
43
        // LabelServiceExposeTLSSecret provides the name of the TLS secret to use with the Kubernetes ingress controller
44
        LabelServiceExposeTLSSecret = "kompose.service.expose.tls-secret"
45
        // LabelServiceExposeIngressClassName provides the name of ingress class to use with the Kubernetes ingress controller
46
        LabelServiceExposeIngressClassName = "kompose.service.expose.ingress-class-name"
47
        // LabelServiceAccountName defines the service account name to provide the credential info of the pod.
48
        LabelServiceAccountName = "kompose.serviceaccount-name"
49
        // LabelControllerType defines the type of controller to be created
50
        LabelControllerType = "kompose.controller.type"
51
        // LabelImagePullSecret defines a secret name for kubernetes ImagePullSecrets
52
        LabelImagePullSecret = "kompose.image-pull-secret"
53
        // LabelImagePullPolicy defines Kubernetes PodSpec imagePullPolicy.
54
        LabelImagePullPolicy = "kompose.image-pull-policy"
55
        // HealthCheckReadinessDisable defines readiness health check disable
56
        HealthCheckReadinessDisable = "kompose.service.healthcheck.readiness.disable"
57
        // HealthCheckReadinessTest defines readiness health check test
58
        HealthCheckReadinessTest = "kompose.service.healthcheck.readiness.test"
59
        // HealthCheckReadinessInterval defines readiness health check interval
60
        HealthCheckReadinessInterval = "kompose.service.healthcheck.readiness.interval"
61
        // HealthCheckReadinessTimeout defines readiness health check timeout
62
        HealthCheckReadinessTimeout = "kompose.service.healthcheck.readiness.timeout"
63
        // HealthCheckReadinessRetries defines readiness health check retries
64
        HealthCheckReadinessRetries = "kompose.service.healthcheck.readiness.retries"
65
        // HealthCheckReadinessStartPeriod defines readiness health check start period
66
        HealthCheckReadinessStartPeriod = "kompose.service.healthcheck.readiness.start_period"
67
        // HealthCheckReadinessHTTPGetPath defines readiness health check HttpGet path
68
        HealthCheckReadinessHTTPGetPath = "kompose.service.healthcheck.readiness.http_get_path"
69
        // HealthCheckReadinessHTTPGetPort defines readiness health check HttpGet port
70
        HealthCheckReadinessHTTPGetPort = "kompose.service.healthcheck.readiness.http_get_port"
71
        // HealthCheckReadinessTCPPort defines readiness health check tcp port
72
        HealthCheckReadinessTCPPort = "kompose.service.healthcheck.readiness.tcp_port"
73
        // HealthCheckLivenessHTTPGetPath defines liveness health check HttpGet path
74
        HealthCheckLivenessHTTPGetPath = "kompose.service.healthcheck.liveness.http_get_path"
75
        // HealthCheckLivenessHTTPGetPort defines liveness health check HttpGet port
76
        HealthCheckLivenessHTTPGetPort = "kompose.service.healthcheck.liveness.http_get_port"
77
        // HealthCheckLivenessTCPPort defines liveness health check tcp port
78
        HealthCheckLivenessTCPPort = "kompose.service.healthcheck.liveness.tcp_port"
79

80
        // ServiceTypeHeadless ...
81
        ServiceTypeHeadless = "Headless"
82
)
83

84
// load environment variables from compose file
85
func loadEnvVars(envars []string) []kobject.EnvVar {
8✔
86
        envs := []kobject.EnvVar{}
8✔
87
        for _, e := range envars {
16✔
88
                character := ""
8✔
89
                equalPos := strings.Index(e, "=")
8✔
90
                colonPos := strings.Index(e, ":")
8✔
91
                switch {
8✔
92
                case equalPos == -1 && colonPos == -1:
2✔
93
                        character = ""
2✔
94
                case equalPos == -1 && colonPos != -1:
2✔
95
                        character = ":"
2✔
96
                case equalPos != -1 && colonPos == -1:
2✔
97
                        character = "="
2✔
98
                case equalPos != -1 && colonPos != -1:
2✔
99
                        if equalPos > colonPos {
3✔
100
                                character = ":"
1✔
101
                        } else {
2✔
102
                                character = "="
1✔
103
                        }
1✔
104
                }
105

106
                if character == "" {
10✔
107
                        envs = append(envs, kobject.EnvVar{
2✔
108
                                Name:  e,
2✔
109
                                Value: os.Getenv(e),
2✔
110
                        })
2✔
111
                } else {
8✔
112
                        values := strings.SplitN(e, character, 2)
6✔
113
                        // try to get value from os env
6✔
114
                        if values[1] == "" {
8✔
115
                                values[1] = os.Getenv(values[0])
2✔
116
                        }
2✔
117
                        envs = append(envs, kobject.EnvVar{
6✔
118
                                Name:  values[0],
6✔
119
                                Value: values[1],
6✔
120
                        })
6✔
121
                }
122
        }
123

124
        return envs
8✔
125
}
126

127
// getComposeFileDir returns compose file directory
128
// Assume all the docker-compose files are in the same directory
129
func getComposeFileDir(inputFiles []string) (string, error) {
×
130
        inputFile := inputFiles[0]
×
131
        if strings.Index(inputFile, "/") != 0 {
×
132
                workDir, err := os.Getwd()
×
133
                if err != nil {
×
134
                        return "", errors.Wrap(err, "Unable to retrieve compose file directory")
×
135
                }
×
136
                inputFile = filepath.Join(workDir, inputFile)
×
137
        }
138
        return filepath.Dir(inputFile), nil
×
139
}
140

141
func handleServiceType(ServiceType string) (string, error) {
7✔
142
        switch strings.ToLower(ServiceType) {
7✔
143
        case "", "clusterip":
3✔
144
                return string(api.ServiceTypeClusterIP), nil
3✔
145
        case "nodeport":
2✔
146
                return string(api.ServiceTypeNodePort), nil
2✔
147
        case "loadbalancer":
2✔
148
                return string(api.ServiceTypeLoadBalancer), nil
2✔
149
        case "headless":
×
150
                return ServiceTypeHeadless, nil
×
151
        default:
×
152
                return "", errors.New("Unknown value " + ServiceType + " , supported values are 'nodeport, clusterip, headless or loadbalancer'")
×
153
        }
154
}
155

156
func handleServiceExternalTrafficPolicy(ServiceExternalTrafficPolicyType string) (string, error) {
×
157
        switch strings.ToLower(ServiceExternalTrafficPolicyType) {
×
158
        case "", "cluster":
×
159
                return string(api.ServiceExternalTrafficPolicyTypeCluster), nil
×
160
        case "local":
×
161
                return string(api.ServiceExternalTrafficPolicyTypeLocal), nil
×
162
        default:
×
163
                return "", errors.New("Unknown value " + ServiceExternalTrafficPolicyType + " , supported values are 'local, cluster'")
×
164
        }
165
}
166

167
func normalizeContainerNames(svcName string) string {
×
168
        return strings.ToLower(svcName)
×
169
}
×
170

171
func normalizeServiceNames(svcName string) string {
3✔
172
        re := regexp.MustCompile("[._]")
3✔
173
        return strings.ToLower(re.ReplaceAllString(svcName, "-"))
3✔
174
}
3✔
175

176
func normalizeVolumes(svcName string) string {
×
177
        return strings.Replace(svcName, "_", "-", -1)
×
178
}
×
179

180
func normalizeNetworkNames(netName string) (string, error) {
4✔
181
        netval := strings.ToLower(strings.Replace(netName, "_", "-", -1))
4✔
182
        regString := "[^A-Za-z0-9.-]+"
4✔
183
        reg, err := regexp.Compile(regString)
4✔
184
        if err != nil {
4✔
185
                return "", err
×
186
        }
×
187
        netval = reg.ReplaceAllString(netval, "")
4✔
188
        return netval, nil
4✔
189
}
190

191
// ReadFile read data from file or stdin
192
func ReadFile(fileName string) ([]byte, error) {
×
193
        if fileName == "-" {
×
194
                if StdinData == nil {
×
195
                        data, err := io.ReadAll(os.Stdin)
×
196
                        StdinData = data
×
197
                        return data, err
×
198
                }
×
199
                return StdinData, nil
×
200
        }
201
        return os.ReadFile(fileName)
×
202
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc