Skip to content

Commit dea58dd

Browse files
Merge pull request #321 from uselagoon/services-from-configmap
feat: consume environment services from confimap
2 parents ce1eb1c + 5a0d44e commit dea58dd

File tree

5 files changed

+114
-75
lines changed

5 files changed

+114
-75
lines changed

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/onsi/ginkgo/v2 v2.27.3
1717
github.com/onsi/gomega v1.38.3
1818
github.com/prometheus/client_golang v1.23.2
19-
github.com/uselagoon/machinery v0.0.35-0.20251124010253-71bebf6d6966
19+
github.com/uselagoon/machinery v0.0.35
2020
github.com/vshn/k8up v1.99.99
2121
github.com/xhit/go-str2duration/v2 v2.1.0
2222
golang.org/x/text v0.32.0
@@ -111,10 +111,10 @@ require (
111111
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 // indirect
112112
golang.org/x/mod v0.30.0 // indirect
113113
golang.org/x/net v0.47.0 // indirect
114-
golang.org/x/oauth2 v0.30.0 // indirect
114+
golang.org/x/oauth2 v0.34.0 // indirect
115115
golang.org/x/sync v0.19.0 // indirect
116-
golang.org/x/sys v0.38.0 // indirect
117-
golang.org/x/term v0.37.0 // indirect
116+
golang.org/x/sys v0.40.0 // indirect
117+
golang.org/x/term v0.39.0 // indirect
118118
golang.org/x/time v0.9.0 // indirect
119119
golang.org/x/tools v0.39.0 // indirect
120120
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect

go.sum

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,10 +1241,8 @@ github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijb
12411241
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
12421242
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
12431243
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
1244-
github.com/uselagoon/machinery v0.0.34 h1:5DsvXEyMeXmzQhjt11YH7+kZJueabovrwKTv0x7jQV8=
1245-
github.com/uselagoon/machinery v0.0.34/go.mod h1:G0ujppuNR0BrtAnlmH8xDb9TDfayb4A36aeo0DYg7fQ=
1246-
github.com/uselagoon/machinery v0.0.35-0.20251124010253-71bebf6d6966 h1:zfOFGy2aaAn5SnGoOBn/GqmiPzQKY4Crd5A0Y+DXI90=
1247-
github.com/uselagoon/machinery v0.0.35-0.20251124010253-71bebf6d6966/go.mod h1:G0ujppuNR0BrtAnlmH8xDb9TDfayb4A36aeo0DYg7fQ=
1244+
github.com/uselagoon/machinery v0.0.35 h1:j4prhAVEh/xssvhzYv9MIoxsDZcJfSY3APt2fmeaE4o=
1245+
github.com/uselagoon/machinery v0.0.35/go.mod h1:UVqIxwF/Q9xO3LQMkQhWeuegpuKcsrxmBa4LE52SiWQ=
12481246
github.com/uudashr/gocognit v0.0.0-20190926065955-1655d0de0517/go.mod h1:j44Ayx2KW4+oB6SWMv8KsmHzZrOInQav7D3cQMJ5JUM=
12491247
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
12501248
github.com/valyala/fasthttp v1.2.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=
@@ -1533,8 +1531,8 @@ golang.org/x/oauth2 v0.0.0-20210220000619-9bb904979d93/go.mod h1:KelEdhl1UZF7XfJ
15331531
golang.org/x/oauth2 v0.0.0-20210313182246-cd4f82c27b84/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
15341532
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
15351533
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
1536-
golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI=
1537-
golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU=
1534+
golang.org/x/oauth2 v0.34.0 h1:hqK/t4AKgbqWkdkcAeI8XLmbK+4m4G5YeQRrmiotGlw=
1535+
golang.org/x/oauth2 v0.34.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
15381536
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
15391537
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
15401538
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -1662,16 +1660,16 @@ golang.org/x/sys v0.0.0-20220405210540-1e041c57c461/go.mod h1:oPkhp1MJrh7nUepCBc
16621660
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
16631661
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
16641662
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1665-
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
1666-
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
1663+
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
1664+
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
16671665
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
16681666
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
16691667
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
16701668
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
16711669
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
16721670
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
1673-
golang.org/x/term v0.37.0 h1:8EGAD0qCmHYZg6J17DvsMy9/wJ7/D/4pV/wfnld5lTU=
1674-
golang.org/x/term v0.37.0/go.mod h1:5pB4lxRNYYVZuTLmy8oR2BH8dflOR+IbTYFD8fi3254=
1671+
golang.org/x/term v0.39.0 h1:RclSuaJf32jOqZz74CkPA9qFuVTX7vhLlpfj/IGWlqY=
1672+
golang.org/x/term v0.39.0/go.mod h1:yxzUCTP/U+FzoxfdKmLaA0RV1WgE0VY7hXBwKtY/4ww=
16751673
golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
16761674
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
16771675
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

internal/controllers/v1beta2/buildpodmonitor_handlers.go

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -285,41 +285,57 @@ func (r *BuildMonitorReconciler) updateDeploymentAndEnvironmentTask(
285285
Cluster: r.LagoonTargetName,
286286
},
287287
}
288-
labelRequirements1, _ := labels.NewRequirement("lagoon.sh/service", selection.NotIn, []string{"faketest"})
289-
listOption := (&client.ListOptions{}).ApplyOptions([]client.ListOption{
290-
client.InNamespace(jobPod.Namespace),
291-
client.MatchingLabelsSelector{
292-
Selector: labels.NewSelector().Add(*labelRequirements1),
293-
},
294-
})
295-
depList := &appsv1.DeploymentList{}
296-
serviceNames := []string{}
297-
services := []schema.EnvironmentService{}
298-
if err := r.APIReader.List(context.TODO(), depList, listOption); err == nil {
299-
// generate the list of services to add or update to the environment
300-
for _, deployment := range depList.Items {
301-
var serviceName, serviceType string
302-
containers := []schema.ServiceContainer{}
303-
if name, ok := deployment.Labels["lagoon.sh/service"]; ok {
304-
serviceName = name
305-
serviceNames = append(serviceNames, serviceName)
306-
for _, container := range deployment.Spec.Template.Spec.Containers {
307-
containers = append(containers, schema.ServiceContainer{Name: container.Name})
288+
lagoonServices := &corev1.ConfigMap{}
289+
if err := r.APIReader.Get(ctx, types.NamespacedName{Namespace: jobPod.Namespace, Name: "lagoon-services"}, lagoonServices); err != nil {
290+
if helpers.IgnoreNotFound(err) != nil {
291+
return err
292+
}
293+
// if configmap doesn't exist, fall back to previous service check behaviour
294+
labelRequirements1, _ := labels.NewRequirement("lagoon.sh/service", selection.NotIn, []string{"faketest"})
295+
listOption := (&client.ListOptions{}).ApplyOptions([]client.ListOption{
296+
client.InNamespace(jobPod.Namespace),
297+
client.MatchingLabelsSelector{
298+
Selector: labels.NewSelector().Add(*labelRequirements1),
299+
},
300+
})
301+
depList := &appsv1.DeploymentList{}
302+
serviceNames := []string{}
303+
services := []schema.EnvironmentService{}
304+
if err := r.APIReader.List(ctx, depList, listOption); err == nil {
305+
// generate the list of services to add or update to the environment
306+
for _, deployment := range depList.Items {
307+
var serviceName, serviceType string
308+
containers := []schema.ServiceContainer{}
309+
if name, ok := deployment.Labels["lagoon.sh/service"]; ok {
310+
serviceName = name
311+
serviceNames = append(serviceNames, serviceName)
312+
for _, container := range deployment.Spec.Template.Spec.Containers {
313+
containers = append(containers, schema.ServiceContainer{Name: container.Name})
314+
}
308315
}
316+
if sType, ok := deployment.Labels["lagoon.sh/service-type"]; ok {
317+
serviceType = sType
318+
}
319+
// probably need to collect dbaas consumers too at some stage
320+
services = append(services, schema.EnvironmentService{
321+
Name: serviceName,
322+
Type: serviceType,
323+
Containers: containers,
324+
Replicas: *deployment.Spec.Replicas,
325+
})
309326
}
310-
if sType, ok := deployment.Labels["lagoon.sh/service-type"]; ok {
311-
serviceType = sType
327+
msg.Meta.Services = serviceNames
328+
msg.Meta.EnvironmentServices = services
329+
}
330+
} else {
331+
// otherwise get the values from configmap
332+
if val, ok := lagoonServices.Data["post-deploy"]; ok {
333+
serviceConfig := helpers.LagoonServices{}
334+
err := json.Unmarshal([]byte(val), &serviceConfig)
335+
if err == nil {
336+
msg.Meta.EnvironmentServices = serviceConfig.Services
312337
}
313-
// probably need to collect dbaas consumers too at some stage
314-
services = append(services, schema.EnvironmentService{
315-
Name: serviceName,
316-
Type: serviceType,
317-
Containers: containers,
318-
Replicas: *deployment.Spec.Replicas,
319-
})
320338
}
321-
msg.Meta.Services = serviceNames
322-
msg.Meta.EnvironmentServices = services
323339
}
324340
route, routes, err := helpers.GetLagoonEnvRoutes(ctx, opLog, r.Client, namespace.Name)
325341
// if we aren't being provided the lagoon config, we can skip adding the routes etc

internal/helpers/helpers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"github.com/go-logr/logr"
17+
"github.com/uselagoon/machinery/api/schema"
1718
"golang.org/x/text/cases"
1819
"golang.org/x/text/language"
1920
corev1 "k8s.io/api/core/v1"
@@ -363,3 +364,8 @@ func GetLagoonEnvRoutes(ctx context.Context, opLog logr.Logger, c client.Client,
363364
}
364365
return route, routes, nil
365366
}
367+
368+
type LagoonServices struct {
369+
Services []schema.EnvironmentService `json:"services"`
370+
Volumes []schema.EnvironmentVolume `json:"volumes"`
371+
}

internal/messenger/build_messenger.go

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/uselagoon/machinery/api/schema"
1212
lagooncrd "github.com/uselagoon/remote-controller/api/lagoon/v1beta2"
1313
"github.com/uselagoon/remote-controller/internal/helpers"
14+
appsv1 "k8s.io/api/apps/v1"
1415
corev1 "k8s.io/api/core/v1"
1516
"k8s.io/apimachinery/pkg/labels"
1617
"k8s.io/apimachinery/pkg/selection"
@@ -117,40 +118,58 @@ func (m *Messenger) UpdateDeploymentAndEnvironmentTask(
117118
Cluster: targetName,
118119
},
119120
}
120-
labelRequirements1, _ := labels.NewRequirement("lagoon.sh/service", selection.NotIn, []string{"faketest"})
121-
listOption := (&client.ListOptions{}).ApplyOptions([]client.ListOption{
122-
client.InNamespace(lagoonBuild.Namespace),
123-
client.MatchingLabelsSelector{
124-
Selector: labels.NewSelector().Add(*labelRequirements1),
125-
},
126-
})
127-
podList := &corev1.PodList{}
128-
serviceNames := []string{}
129-
services := []schema.EnvironmentService{}
130-
if err := m.Client.List(context.TODO(), podList, listOption); err == nil {
131-
// generate the list of services to add to the environment
132-
for _, pod := range podList.Items {
133-
var serviceName, serviceType string
134-
containers := []schema.ServiceContainer{}
135-
if name, ok := pod.Labels["lagoon.sh/service"]; ok {
136-
serviceName = name
137-
serviceNames = append(serviceNames, serviceName)
138-
for _, container := range pod.Spec.Containers {
139-
containers = append(containers, schema.ServiceContainer{Name: container.Name})
121+
122+
lagoonServices := &corev1.ConfigMap{}
123+
if err := m.Client.Get(ctx, types.NamespacedName{Namespace: lagoonBuild.Namespace, Name: "lagoon-services"}, lagoonServices); err != nil {
124+
if helpers.IgnoreNotFound(err) != nil {
125+
opLog.Error(err, "configmap lagoon-services not found")
126+
return
127+
}
128+
// if configmap doesn't exist, fall back to previous service check behaviour
129+
labelRequirements1, _ := labels.NewRequirement("lagoon.sh/service", selection.NotIn, []string{"faketest"})
130+
listOption := (&client.ListOptions{}).ApplyOptions([]client.ListOption{
131+
client.InNamespace(lagoonBuild.Namespace),
132+
client.MatchingLabelsSelector{
133+
Selector: labels.NewSelector().Add(*labelRequirements1),
134+
},
135+
})
136+
depList := &appsv1.DeploymentList{}
137+
serviceNames := []string{}
138+
services := []schema.EnvironmentService{}
139+
if err := m.Client.List(context.TODO(), depList, listOption); err == nil {
140+
// generate the list of services to add or update to the environment
141+
for _, deployment := range depList.Items {
142+
var serviceName, serviceType string
143+
containers := []schema.ServiceContainer{}
144+
if name, ok := deployment.Labels["lagoon.sh/service"]; ok {
145+
serviceName = name
146+
serviceNames = append(serviceNames, serviceName)
147+
for _, container := range deployment.Spec.Template.Spec.Containers {
148+
containers = append(containers, schema.ServiceContainer{Name: container.Name})
149+
}
150+
}
151+
if sType, ok := deployment.Labels["lagoon.sh/service-type"]; ok {
152+
serviceType = sType
140153
}
154+
// probably need to collect dbaas consumers too at some stage
155+
services = append(services, schema.EnvironmentService{
156+
Name: serviceName,
157+
Type: serviceType,
158+
Containers: containers,
159+
})
141160
}
142-
if sType, ok := pod.Labels["lagoon.sh/service-type"]; ok {
143-
serviceType = sType
161+
msg.Meta.Services = serviceNames
162+
msg.Meta.EnvironmentServices = services
163+
}
164+
} else {
165+
// otherwise get the values from configmap
166+
if val, ok := lagoonServices.Data["post-deploy"]; ok {
167+
serviceConfig := helpers.LagoonServices{}
168+
err := json.Unmarshal([]byte(val), &serviceConfig)
169+
if err == nil {
170+
msg.Meta.EnvironmentServices = serviceConfig.Services
144171
}
145-
// probably need to collect dbaas consumers too at some stage
146-
services = append(services, schema.EnvironmentService{
147-
Name: serviceName,
148-
Type: serviceType,
149-
Containers: containers,
150-
})
151172
}
152-
msg.Meta.Services = serviceNames
153-
msg.Meta.EnvironmentServices = services
154173
}
155174
if checkLagoonEnv {
156175
route, routes, err := helpers.GetLagoonEnvRoutes(ctx, opLog, m.Client, lagoonBuild.Namespace)

0 commit comments

Comments
 (0)