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

kubernetes / kompose / 3911886254

13 Jan 2023 01:59PM UTC coverage: 52.223%. Remained the same
3911886254

push

github

GitHub
chore: require go >=1.18, bump deps and get rid of replace directives (#1552)

8 of 8 new or added lines in 5 files covered. (100.0%)

1903 of 3644 relevant lines covered (52.22%)

7.69 hits per line

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

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

78
        // ServiceTypeHeadless ...
79
        ServiceTypeHeadless = "Headless"
80
)
81

82
// load environment variables from compose file
83
func loadEnvVars(envars []string) []kobject.EnvVar {
8✔
84
        envs := []kobject.EnvVar{}
8✔
85
        for _, e := range envars {
16✔
86
                character := ""
8✔
87
                equalPos := strings.Index(e, "=")
8✔
88
                colonPos := strings.Index(e, ":")
8✔
89
                switch {
8✔
90
                case equalPos == -1 && colonPos == -1:
2✔
91
                        character = ""
2✔
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
                        if equalPos > colonPos {
3✔
98
                                character = ":"
1✔
99
                        } else {
2✔
100
                                character = "="
1✔
101
                        }
1✔
102
                }
103

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

122
        return envs
8✔
123
}
124

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

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

154
func normalizeContainerNames(svcName string) string {
×
155
        return strings.ToLower(svcName)
×
156
}
×
157

158
func normalizeServiceNames(svcName string) string {
3✔
159
        re := regexp.MustCompile("[._]")
3✔
160
        return strings.ToLower(re.ReplaceAllString(svcName, "-"))
3✔
161
}
3✔
162

163
func normalizeVolumes(svcName string) string {
×
164
        return strings.Replace(svcName, "_", "-", -1)
×
165
}
×
166

167
func normalizeNetworkNames(netName string) (string, error) {
4✔
168
        netval := strings.ToLower(strings.Replace(netName, "_", "-", -1))
4✔
169
        regString := "[^A-Za-z0-9.-]+"
4✔
170
        reg, err := regexp.Compile(regString)
4✔
171
        if err != nil {
4✔
172
                return "", err
×
173
        }
×
174
        netval = reg.ReplaceAllString(netval, "")
4✔
175
        return netval, nil
4✔
176
}
177

178
// ReadFile read data from file or stdin
179
func ReadFile(fileName string) ([]byte, error) {
×
180
        if fileName == "-" {
×
181
                if StdinData == nil {
×
182
                        data, err := io.ReadAll(os.Stdin)
×
183
                        StdinData = data
×
184
                        return data, err
×
185
                }
×
186
                return StdinData, nil
×
187
        }
188
        return os.ReadFile(fileName)
×
189
}
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

© 2026 Coveralls, Inc