Skip to content

Commit 119f2bb

Browse files
committed
feat: initial pass at service cleanup
1 parent 7ad87f8 commit 119f2bb

File tree

4 files changed

+238
-214
lines changed

4 files changed

+238
-214
lines changed

cmd/identify_ingress_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ func TestIdentifyRoute(t *testing.T) {
383383
if string(retJSON) != tt.wantJSON {
384384
t.Errorf("returned autogen %v doesn't match want %v", string(retJSON), tt.wantJSON)
385385
}
386+
t.Cleanup(func() {
387+
helpers.UnsetEnvVars(nil)
388+
helpers.UnsetEnvVars(tt.args.BuildPodVariables)
389+
})
386390
})
387391
}
388392
}

cmd/identify_lagoonservices.go

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"encoding/base64"
54
"encoding/json"
65
"fmt"
76

@@ -11,49 +10,35 @@ import (
1110
)
1211

1312
type identifyServices struct {
14-
Name string `json:"name"`
15-
Type string `json:"type"`
16-
Containers []containers `json:"containers,omitempty"`
17-
}
18-
19-
type containers struct {
20-
Name string `json:"name"`
21-
Ports []ports `json:"ports,omitempty"`
22-
}
23-
24-
type ports struct {
25-
Port int32 `json:"port"`
13+
Deployments []string `json:"deployments,omitempty"`
14+
Volumes []string `json:"volumes,omitempty"`
15+
Services []string `json:"services,omitempty"`
2616
}
2717

2818
var lagoonServiceIdentify = &cobra.Command{
2919
Use: "lagoon-services",
3020
Aliases: []string{"ls"},
3121
Short: "Identify the lagoon services for a Lagoon build",
3222
RunE: func(cmd *cobra.Command, args []string) error {
33-
gen, err := generator.GenerateInput(*rootCmd, true)
23+
gen, err := generator.GenerateInput(*rootCmd, false)
3424
if err != nil {
3525
return err
3626
}
3727
images, err := rootCmd.PersistentFlags().GetString("images")
3828
if err != nil {
3929
return fmt.Errorf("error reading images flag: %v", err)
4030
}
41-
var imageRefs struct {
42-
Images map[string]string `json:"images"`
43-
}
44-
imagesStr, err := base64.StdEncoding.DecodeString(images)
31+
imageRefs, err := loadImagesFromFile(images)
4532
if err != nil {
46-
return fmt.Errorf("error decoding images payload: %v", err)
47-
}
48-
if err := json.Unmarshal(imagesStr, &imageRefs); err != nil {
49-
return fmt.Errorf("error unmarshalling images payload: %v", err)
33+
return err
5034
}
5135
gen.ImageReferences = imageRefs.Images
5236
out, err := LagoonServiceTemplateIdentification(gen)
5337
if err != nil {
5438
return err
5539
}
56-
fmt.Println(out)
40+
b, _ := json.Marshal(out)
41+
fmt.Println(string(b))
5742
return nil
5843
},
5944
}
@@ -62,9 +47,9 @@ var lagoonServiceIdentify = &cobra.Command{
6247
// about the services that lagoon will be deploying (this will be kubernetes `kind: deployment`, but lagoon calls them services ¯\_(ツ)_/¯)
6348
// this command can be used to identify services that are deployed by the build, so that services that may remain in the environment can be identified
6449
// and eventually removed
65-
func LagoonServiceTemplateIdentification(g generator.GeneratorInput) ([]identifyServices, error) {
50+
func LagoonServiceTemplateIdentification(g generator.GeneratorInput) (*identifyServices, error) {
6651

67-
lServices := []identifyServices{}
52+
servicesData := identifyServices{}
6853
lagoonBuild, err := generator.NewGenerator(
6954
g,
7055
)
@@ -74,27 +59,26 @@ func LagoonServiceTemplateIdentification(g generator.GeneratorInput) ([]identify
7459

7560
deployments, err := servicestemplates.GenerateDeploymentTemplate(*lagoonBuild.BuildValues)
7661
if err != nil {
77-
return nil, fmt.Errorf("couldn't generate template: %v", err)
62+
return nil, fmt.Errorf("couldn't identify deployments: %v", err)
7863
}
7964
for _, d := range deployments {
80-
dcs := []containers{}
81-
for _, dc := range d.Spec.Template.Spec.Containers {
82-
dcp := []ports{}
83-
for _, p := range dc.Ports {
84-
dcp = append(dcp, ports{Port: p.ContainerPort})
85-
}
86-
dcs = append(dcs, containers{
87-
Name: dc.Name,
88-
Ports: dcp,
89-
})
90-
}
91-
lServices = append(lServices, identifyServices{
92-
Name: d.Name,
93-
Type: d.ObjectMeta.Labels["lagoon.sh/service-type"],
94-
Containers: dcs,
95-
})
65+
servicesData.Deployments = append(servicesData.Deployments, d.ObjectMeta.Name)
66+
}
67+
pvcs, err := servicestemplates.GeneratePVCTemplate(*lagoonBuild.BuildValues)
68+
if err != nil {
69+
return nil, fmt.Errorf("couldn't identify volumes: %v", err)
70+
}
71+
for _, pvc := range pvcs {
72+
servicesData.Volumes = append(servicesData.Volumes, pvc.ObjectMeta.Name)
73+
}
74+
services, err := servicestemplates.GenerateServiceTemplate(*lagoonBuild.BuildValues)
75+
if err != nil {
76+
return nil, fmt.Errorf("couldn't identify services: %v", err)
77+
}
78+
for _, service := range services {
79+
servicesData.Services = append(servicesData.Services, service.ObjectMeta.Name)
9680
}
97-
return lServices, nil
81+
return &servicesData, nil
9882
}
9983

10084
func init() {

0 commit comments

Comments
 (0)