Skip to content

Commit

Permalink
feat: initial pass at service cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Sep 23, 2024
1 parent 7ad87f8 commit 119f2bb
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 214 deletions.
4 changes: 4 additions & 0 deletions cmd/identify_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ func TestIdentifyRoute(t *testing.T) {
if string(retJSON) != tt.wantJSON {
t.Errorf("returned autogen %v doesn't match want %v", string(retJSON), tt.wantJSON)
}
t.Cleanup(func() {
helpers.UnsetEnvVars(nil)
helpers.UnsetEnvVars(tt.args.BuildPodVariables)
})
})
}
}
Expand Down
70 changes: 27 additions & 43 deletions cmd/identify_lagoonservices.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"encoding/base64"
"encoding/json"
"fmt"

Expand All @@ -11,49 +10,35 @@ import (
)

type identifyServices struct {
Name string `json:"name"`
Type string `json:"type"`
Containers []containers `json:"containers,omitempty"`
}

type containers struct {
Name string `json:"name"`
Ports []ports `json:"ports,omitempty"`
}

type ports struct {
Port int32 `json:"port"`
Deployments []string `json:"deployments,omitempty"`
Volumes []string `json:"volumes,omitempty"`
Services []string `json:"services,omitempty"`
}

var lagoonServiceIdentify = &cobra.Command{
Use: "lagoon-services",
Aliases: []string{"ls"},
Short: "Identify the lagoon services for a Lagoon build",
RunE: func(cmd *cobra.Command, args []string) error {
gen, err := generator.GenerateInput(*rootCmd, true)
gen, err := generator.GenerateInput(*rootCmd, false)
if err != nil {
return err
}
images, err := rootCmd.PersistentFlags().GetString("images")
if err != nil {
return fmt.Errorf("error reading images flag: %v", err)
}
var imageRefs struct {
Images map[string]string `json:"images"`
}
imagesStr, err := base64.StdEncoding.DecodeString(images)
imageRefs, err := loadImagesFromFile(images)
if err != nil {
return fmt.Errorf("error decoding images payload: %v", err)
}
if err := json.Unmarshal(imagesStr, &imageRefs); err != nil {
return fmt.Errorf("error unmarshalling images payload: %v", err)
return err
}
gen.ImageReferences = imageRefs.Images
out, err := LagoonServiceTemplateIdentification(gen)
if err != nil {
return err
}
fmt.Println(out)
b, _ := json.Marshal(out)
fmt.Println(string(b))
return nil
},
}
Expand All @@ -62,9 +47,9 @@ var lagoonServiceIdentify = &cobra.Command{
// about the services that lagoon will be deploying (this will be kubernetes `kind: deployment`, but lagoon calls them services ¯\_(ツ)_/¯)
// 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
// and eventually removed
func LagoonServiceTemplateIdentification(g generator.GeneratorInput) ([]identifyServices, error) {
func LagoonServiceTemplateIdentification(g generator.GeneratorInput) (*identifyServices, error) {

lServices := []identifyServices{}
servicesData := identifyServices{}
lagoonBuild, err := generator.NewGenerator(
g,
)
Expand All @@ -74,27 +59,26 @@ func LagoonServiceTemplateIdentification(g generator.GeneratorInput) ([]identify

deployments, err := servicestemplates.GenerateDeploymentTemplate(*lagoonBuild.BuildValues)
if err != nil {
return nil, fmt.Errorf("couldn't generate template: %v", err)
return nil, fmt.Errorf("couldn't identify deployments: %v", err)
}
for _, d := range deployments {
dcs := []containers{}
for _, dc := range d.Spec.Template.Spec.Containers {
dcp := []ports{}
for _, p := range dc.Ports {
dcp = append(dcp, ports{Port: p.ContainerPort})
}
dcs = append(dcs, containers{
Name: dc.Name,
Ports: dcp,
})
}
lServices = append(lServices, identifyServices{
Name: d.Name,
Type: d.ObjectMeta.Labels["lagoon.sh/service-type"],
Containers: dcs,
})
servicesData.Deployments = append(servicesData.Deployments, d.ObjectMeta.Name)
}
pvcs, err := servicestemplates.GeneratePVCTemplate(*lagoonBuild.BuildValues)
if err != nil {
return nil, fmt.Errorf("couldn't identify volumes: %v", err)
}
for _, pvc := range pvcs {
servicesData.Volumes = append(servicesData.Volumes, pvc.ObjectMeta.Name)
}
services, err := servicestemplates.GenerateServiceTemplate(*lagoonBuild.BuildValues)
if err != nil {
return nil, fmt.Errorf("couldn't identify services: %v", err)
}
for _, service := range services {
servicesData.Services = append(servicesData.Services, service.ObjectMeta.Name)
}
return lServices, nil
return &servicesData, nil
}

func init() {
Expand Down
Loading

0 comments on commit 119f2bb

Please sign in to comment.