@@ -4,28 +4,27 @@ import (
44 "fmt"
55 "sort"
66 "strconv"
7- "strings"
87 "time"
9-
8+
109 composetypes "github.com/compose-spec/compose-go/types"
1110 "github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
1211 v1 "k8s.io/api/core/v1"
1312)
1413
1514func (cb * configBuilder ) AddDeployment (dockerCompose * composetypes.Project , service composetypes.ServiceConfig ) error {
1615 values := map [string ]interface {}{}
17-
16+
1817 volumes , volumeMounts , _ := volumesConfig (service , dockerCompose .Volumes , cb .log )
1918 if len (volumes ) > 0 {
2019 values ["volumes" ] = volumes
2120 }
22-
21+
2322 container , err := containerConfig (service , volumeMounts )
2423 if err != nil {
2524 return err
2625 }
2726 values ["containers" ] = []interface {}{container }
28-
27+
2928 if service .Restart != "" {
3029 restartPolicy := string (v1 .RestartPolicyNever )
3130 switch service .Restart {
@@ -36,7 +35,7 @@ func (cb *configBuilder) AddDeployment(dockerCompose *composetypes.Project, serv
3635 }
3736 values ["restartPolicy" ] = restartPolicy
3837 }
39-
38+
4039 ports := []interface {}{}
4140 for _ , port := range service .Ports {
4241 var protocol string
@@ -48,24 +47,24 @@ func (cb *configBuilder) AddDeployment(dockerCompose *composetypes.Project, serv
4847 default :
4948 return fmt .Errorf ("invalid protocol %s" , port .Protocol )
5049 }
51-
50+
5251 if port .Published == "" {
5352 cb .log .Warnf ("Unassigned ports are not supported: %s" , port .Target )
5453 continue
5554 }
56-
55+
5756 portNumber , err := strconv .Atoi (port .Published )
5857 if err != nil {
5958 return err
6059 }
61-
60+
6261 ports = append (ports , map [string ]interface {}{
6362 "port" : portNumber ,
6463 "containerPort" : int (port .Target ),
6564 "protocol" : protocol ,
6665 })
6766 }
68-
67+
6968 for _ , port := range service .Expose {
7069 intPort , err := strconv .Atoi (port )
7170 if err != nil {
@@ -75,46 +74,43 @@ func (cb *configBuilder) AddDeployment(dockerCompose *composetypes.Project, serv
7574 "port" : intPort ,
7675 })
7776 }
78-
77+
7978 if len (ports ) > 0 {
8079 values ["service" ] = map [string ]interface {}{
8180 "ports" : ports ,
8281 }
8382 }
84-
83+
8584 if len (service .ExtraHosts ) > 0 {
8685 hostsMap := map [string ][]interface {}{}
87- for _ , host := range service .ExtraHosts {
88- hostTokens := strings .Split (host , ":" )
89- hostName := hostTokens [0 ]
90- hostIP := hostTokens [1 ]
86+ for hostName , hostIP := range service .ExtraHosts {
9187 hostsMap [hostIP ] = append (hostsMap [hostIP ], hostName )
9288 }
93-
89+
9490 hostAliases := []interface {}{}
9591 for ip , hosts := range hostsMap {
9692 hostAliases = append (hostAliases , map [string ]interface {}{
9793 "ip" : ip ,
9894 "hostnames" : hosts ,
9995 })
10096 }
101-
97+
10298 values ["hostAliases" ] = hostAliases
10399 }
104-
100+
105101 deployment := & latest.DeploymentConfig {
106102 Helm : & latest.HelmConfig {
107103 Values : values ,
108104 },
109105 }
110-
106+
111107 if cb .config .Deployments == nil {
112108 cb .config .Deployments = map [string ]* latest.DeploymentConfig {}
113109 }
114-
110+
115111 deploymentName := formatName (service .Name )
116112 cb .config .Deployments [deploymentName ] = deployment
117-
113+
118114 return nil
119115}
120116
@@ -123,22 +119,22 @@ func containerConfig(service composetypes.ServiceConfig, volumeMounts []interfac
123119 "name" : containerName (service ),
124120 "image" : resolveImage (service ),
125121 }
126-
122+
127123 if len (service .Command ) > 0 {
128124 container ["args" ] = shellCommandToSlice (service .Command )
129125 }
130-
126+
131127 if service .Build == nil && len (service .Entrypoint ) > 0 {
132128 container ["command" ] = shellCommandToSlice (service .Entrypoint )
133129 }
134-
130+
135131 if service .Environment != nil {
136132 env := containerEnv (service .Environment )
137133 if len (env ) > 0 {
138134 container ["env" ] = env
139135 }
140136 }
141-
137+
142138 if service .HealthCheck != nil {
143139 livenessProbe , err := containerLivenessProbe (service .HealthCheck )
144140 if err != nil {
@@ -148,11 +144,11 @@ func containerConfig(service composetypes.ServiceConfig, volumeMounts []interfac
148144 container ["livenessProbe" ] = livenessProbe
149145 }
150146 }
151-
147+
152148 if len (volumeMounts ) > 0 {
153149 container ["volumeMounts" ] = volumeMounts
154150 }
155-
151+
156152 return container , nil
157153}
158154
@@ -163,7 +159,7 @@ func containerEnv(env composetypes.MappingWithEquals) []interface{} {
163159 keys = append (keys , name )
164160 }
165161 sort .Strings (keys )
166-
162+
167163 for _ , name := range keys {
168164 value := env [name ]
169165 envs = append (envs , map [string ]interface {}{
@@ -185,7 +181,7 @@ func containerLivenessProbe(health *composetypes.HealthCheckConfig) (map[string]
185181 if len (health .Test ) == 0 {
186182 return nil , nil
187183 }
188-
184+
189185 var command []interface {}
190186 testKind := health .Test [0 ]
191187 switch testKind {
@@ -202,33 +198,33 @@ func containerLivenessProbe(health *composetypes.HealthCheckConfig) (map[string]
202198 default :
203199 command = append (command , health .Test [0 :])
204200 }
205-
201+
206202 livenessProbe := map [string ]interface {}{
207203 "exec" : map [string ]interface {}{
208204 "command" : command ,
209205 },
210206 }
211-
207+
212208 if health .Retries != nil {
213209 livenessProbe ["failureThreshold" ] = int (* health .Retries )
214210 }
215-
211+
216212 if health .Interval != nil {
217213 period , err := time .ParseDuration (health .Interval .String ())
218214 if err != nil {
219215 return nil , err
220216 }
221217 livenessProbe ["periodSeconds" ] = int (period .Seconds ())
222218 }
223-
219+
224220 if health .StartPeriod != nil {
225221 initialDelay , err := time .ParseDuration (health .Interval .String ())
226222 if err != nil {
227223 return nil , err
228224 }
229225 livenessProbe ["initialDelaySeconds" ] = int (initialDelay .Seconds ())
230226 }
231-
227+
232228 return livenessProbe , nil
233229}
234230
0 commit comments