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

kubernetes / kompose / 8161564993

05 Mar 2024 06:45PM UTC coverage: 54.686% (+0.1%) from 54.539%
8161564993

Pull #1833

github

sosan
Merge branch 'main' kubernetes/kompose last changes: unify e2e tests to compose yaml
Pull Request #1833: Feature 1635 - customizing service name with prefix, flag --prefix (-x)

17 of 19 new or added lines in 2 files covered. (89.47%)

157 existing lines in 3 files now uncovered.

2293 of 4193 relevant lines covered (54.69%)

7.68 hits per line

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

65.06
/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
        "regexp"
23
        "strings"
24

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

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

31
const (
32
        // LabelServiceType defines the type of service to be created
33
        LabelServiceType = "kompose.service.type"
34
        // LabelServiceExternalTrafficPolicy defines the external policy traffic of service to be created
35
        LabelServiceExternalTrafficPolicy = "kompose.service.external-traffic-policy"
36
        // LabelServiceGroup defines the group of services in a single pod
37
        LabelServiceGroup = "kompose.service.group"
38
        // LabelNodePortPort defines the port value for NodePort service
39
        LabelNodePortPort = "kompose.service.nodeport.port"
40
        // LabelServiceExpose defines if the service needs to be made accessible from outside the cluster or not
41
        LabelServiceExpose = "kompose.service.expose"
42
        // LabelServiceExposeTLSSecret provides the name of the TLS secret to use with the Kubernetes ingress controller
43
        LabelServiceExposeTLSSecret = "kompose.service.expose.tls-secret"
44
        // LabelServiceExposeIngressClassName provides the name of ingress class to use with the Kubernetes ingress controller
45
        LabelServiceExposeIngressClassName = "kompose.service.expose.ingress-class-name"
46
        // LabelServiceAccountName defines the service account name to provide the credential info of the pod.
47
        LabelServiceAccountName = "kompose.serviceaccount-name"
48
        // LabelControllerType defines the type of controller to be created
49
        LabelControllerType = "kompose.controller.type"
50
        // LabelImagePullSecret defines a secret name for kubernetes ImagePullSecrets
51
        LabelImagePullSecret = "kompose.image-pull-secret"
52
        // LabelImagePullPolicy defines Kubernetes PodSpec imagePullPolicy.
53
        LabelImagePullPolicy = "kompose.image-pull-policy"
54
        // HealthCheckReadinessDisable defines readiness health check disable
55
        HealthCheckReadinessDisable = "kompose.service.healthcheck.readiness.disable"
56
        // HealthCheckReadinessTest defines readiness health check test
57
        HealthCheckReadinessTest = "kompose.service.healthcheck.readiness.test"
58
        // HealthCheckReadinessInterval defines readiness health check interval
59
        HealthCheckReadinessInterval = "kompose.service.healthcheck.readiness.interval"
60
        // HealthCheckReadinessTimeout defines readiness health check timeout
61
        HealthCheckReadinessTimeout = "kompose.service.healthcheck.readiness.timeout"
62
        // HealthCheckReadinessRetries defines readiness health check retries
63
        HealthCheckReadinessRetries = "kompose.service.healthcheck.readiness.retries"
64
        // HealthCheckReadinessStartPeriod defines readiness health check start period
65
        HealthCheckReadinessStartPeriod = "kompose.service.healthcheck.readiness.start_period"
66
        // HealthCheckReadinessHTTPGetPath defines readiness health check HttpGet path
67
        HealthCheckReadinessHTTPGetPath = "kompose.service.healthcheck.readiness.http_get_path"
68
        // HealthCheckReadinessHTTPGetPort defines readiness health check HttpGet port
69
        HealthCheckReadinessHTTPGetPort = "kompose.service.healthcheck.readiness.http_get_port"
70
        // HealthCheckReadinessTCPPort defines readiness health check tcp port
71
        HealthCheckReadinessTCPPort = "kompose.service.healthcheck.readiness.tcp_port"
72
        // HealthCheckLivenessHTTPGetPath defines liveness health check HttpGet path
73
        HealthCheckLivenessHTTPGetPath = "kompose.service.healthcheck.liveness.http_get_path"
74
        // HealthCheckLivenessHTTPGetPort defines liveness health check HttpGet port
75
        HealthCheckLivenessHTTPGetPort = "kompose.service.healthcheck.liveness.http_get_port"
76
        // HealthCheckLivenessTCPPort defines liveness health check tcp port
77
        HealthCheckLivenessTCPPort = "kompose.service.healthcheck.liveness.tcp_port"
78
        // ServiceTypeHeadless ...
79
        ServiceTypeHeadless = "Headless"
80
        // LabelSecurityContextFsGroup defines the pod FsGroup
81
        LabelSecurityContextFsGroup = "kompose.security-context.fsgroup"
82
        // LabelContainerVolumeSubpath defines the volume mount subpath inside container
83
        LabelContainerVolumeSubpath = "kompose.volume.subpath"
84
        // LabelCronJobSchedule defines the cron job schedule
85
        LabelCronJobSchedule = "kompose.cronjob.schedule"
86
        // LabelCronJobConcurrencyPolicy defines the cron job concurrency policy
87
        LabelCronJobConcurrencyPolicy = "kompose.cronjob.concurrency_policy"
88
        // LabelCronJobBackoffLimit defines the job backoff limit
89
        LabelCronJobBackoffLimit = "kompose.cronjob.backoff_limit"
90
)
91

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

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

132
        return envs
8✔
133
}
134

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

UNCOV
150
func handleServiceExternalTrafficPolicy(ServiceExternalTrafficPolicyType string) (string, error) {
×
151
        switch strings.ToLower(ServiceExternalTrafficPolicyType) {
×
152
        case "", "cluster":
×
153
                return string(api.ServiceExternalTrafficPolicyTypeCluster), nil
×
154
        case "local":
×
155
                return string(api.ServiceExternalTrafficPolicyTypeLocal), nil
×
156
        default:
×
157
                return "", errors.New("Unknown value " + ServiceExternalTrafficPolicyType + " , supported values are 'local, cluster'")
×
158
        }
159
}
160

UNCOV
161
func normalizeContainerNames(svcName string) string {
×
162
        return strings.ToLower(svcName)
×
163
}
×
164

165
func normalizeServiceNames(svcName string) string {
3✔
166
        re := regexp.MustCompile("[._]")
3✔
167
        return strings.ToLower(re.ReplaceAllString(svcName, "-"))
3✔
168
}
3✔
169

UNCOV
170
func normalizeVolumes(svcName string) string {
×
171
        return strings.Replace(svcName, "_", "-", -1)
×
172
}
×
173

174
func normalizeNetworkNames(netName string) (string, error) {
4✔
175
        netval := strings.ToLower(strings.Replace(netName, "_", "-", -1))
4✔
176
        regString := "[^A-Za-z0-9.-]+"
4✔
177
        reg, err := regexp.Compile(regString)
4✔
178
        if err != nil {
4✔
UNCOV
179
                return "", err
×
180
        }
×
181
        netval = reg.ReplaceAllString(netval, "")
4✔
182
        return netval, nil
4✔
183
}
184

185
// ReadFile read data from file or stdin
UNCOV
186
func ReadFile(fileName string) ([]byte, error) {
×
187
        if fileName == "-" {
×
188
                if StdinData == nil {
×
189
                        data, err := io.ReadAll(os.Stdin)
×
190
                        StdinData = data
×
191
                        return data, err
×
192
                }
×
193
                return StdinData, nil
×
194
        }
UNCOV
195
        return os.ReadFile(fileName)
×
196
}
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