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

kubernetes / kompose / 6696194161

30 Oct 2023 05:02PM UTC coverage: 54.96%. Remained the same
6696194161

push

github

web-flow
chore(deps)(deps): bump golang.org/x/tools from 0.13.0 to 0.14.0 (#1723)

Bumps [golang.org/x/tools](https://github.com/golang/tools) from 0.13.0 to 0.14.0.
- [Release notes](https://github.com/golang/tools/releases)
- [Commits](https://github.com/golang/tools/compare/v0.13.0...v0.14.0)

---
updated-dependencies:
- dependency-name: golang.org/x/tools
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

2205 of 4012 relevant lines covered (54.96%)

7.91 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
        // LabelSecurityContextFsGroup defines the pod FsGroup
83
        LabelSecurityContextFsGroup = "kompose.security-context.fsgroup"
84

85
        // LabelContainerVolumeSubpath defines the volume mount subpath inside container
86
        LabelContainerVolumeSubpath = "kompose.volume.subpath"
87
)
88

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

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

129
        return envs
8✔
130
}
131

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

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

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

172
func normalizeContainerNames(svcName string) string {
×
173
        return strings.ToLower(svcName)
×
174
}
×
175

176
func normalizeServiceNames(svcName string) string {
3✔
177
        re := regexp.MustCompile("[._]")
3✔
178
        return strings.ToLower(re.ReplaceAllString(svcName, "-"))
3✔
179
}
3✔
180

181
func normalizeVolumes(svcName string) string {
×
182
        return strings.Replace(svcName, "_", "-", -1)
×
183
}
×
184

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

196
// ReadFile read data from file or stdin
197
func ReadFile(fileName string) ([]byte, error) {
×
198
        if fileName == "-" {
×
199
                if StdinData == nil {
×
200
                        data, err := io.ReadAll(os.Stdin)
×
201
                        StdinData = data
×
202
                        return data, err
×
203
                }
×
204
                return StdinData, nil
×
205
        }
206
        return os.ReadFile(fileName)
×
207
}
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