Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support cleaning up removed lagoonyml routes #238

Merged
merged 10 commits into from
Dec 4, 2023
25 changes: 17 additions & 8 deletions cmd/identify_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,35 @@ func IdentifyPrimaryIngress(g generator.GeneratorInput) (string, []string, []str
}

var autogenIngressIdentify = &cobra.Command{
Use: "autogenerated-ingress",
Aliases: []string{"ai"},
Short: "Identify all autogenerated ingress object names for a specific environment",
Use: "created-ingress",
Aliases: []string{"ci"},
Short: "Identify all created ingress object names for a specific environment",
RunE: func(cmd *cobra.Command, args []string) error {
generator, err := generatorInput(false)
if err != nil {
return err
}
autogen, err := AutogeneratedIngressIdentification(generator)
autogen, secondary, err := CreatedIngressIdentification(generator)
if err != nil {
return err
}
ret := ingressIdentifyJSON{
Autogenerated: autogen,
Secondary: secondary,
}
retJSON, _ := json.Marshal(ret)
fmt.Println(string(retJSON))
return nil
},
}

// AutogeneratedIngressIdentification handles identifying autogenerated ingress
func AutogeneratedIngressIdentification(g generator.GeneratorInput) ([]string, error) {
// CreatedIngressIdentification handles identifying autogenerated ingress
func CreatedIngressIdentification(g generator.GeneratorInput) ([]string, []string, error) {
lagoonBuild, err := generator.NewGenerator(
g,
)
if err != nil {
return nil, err
return nil, nil, err
}

autogenIngress := []string{}
Expand All @@ -105,7 +106,15 @@ func AutogeneratedIngressIdentification(g generator.GeneratorInput) ([]string, e
autogenIngress = append(autogenIngress, route.LagoonService)
}

return autogenIngress, nil
secondary := []string{}
// generate the templates
for _, route := range lagoonBuild.MainRoutes.Routes {
secondary = append(secondary, route.IngressName)
}
for _, route := range lagoonBuild.ActiveStandbyRoutes.Routes {
secondary = append(secondary, route.IngressName)
}
return autogenIngress, secondary, nil
}

func init() {
Expand Down
65 changes: 58 additions & 7 deletions cmd/identify_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,27 @@ func TestIdentifyRoute(t *testing.T) {
wantautoGen: []string{"https://nginx-example-project-main.example.com", "https://varnish-example-project-main.example.com"},
wantJSON: `{"primary":"https://nginx-example-project-main.example.com","secondary":["https://nginx-example-project-main.example.com","https://varnish-example-project-main.example.com"],"autogenerated":["https://nginx-example-project-main.example.com","https://varnish-example-project-main.example.com"]}`,
},
{
name: "test18 autogenerated routes with wildcard and altnames",
args: args{
alertContact: "alertcontact",
statusPageID: "statuspageid",
projectName: "example-project",
environmentName: "main",
environmentType: "production",
buildType: "branch",
lagoonVersion: "v2.7.x",
branch: "main",
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]`,
envVars: `[]`,
lagoonYAML: "../test-resources/identify-ingress/test18/lagoon.yml",
templatePath: "../test-resources/output",
},
want: "https://wild.example.com",
wantRemain: []string{"https://nginx-example-project-main.example.com", "https://wild.example.com", "https://alt.example.com", "https://www.example.com", "https://en.example.com"},
wantautoGen: []string{"https://nginx-example-project-main.example.com"},
wantJSON: `{"primary":"https://wild.example.com","secondary":["https://nginx-example-project-main.example.com","https://wild.example.com","https://alt.example.com","https://www.example.com","https://en.example.com"],"autogenerated":["https://nginx-example-project-main.example.com"]}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -534,7 +555,7 @@ func TestIdentifyRoute(t *testing.T) {
}
}

func TestAutogeneratedIngressIdentification(t *testing.T) {
func TestCreatedIngressIdentification(t *testing.T) {
type args struct {
alertContact string
statusPageID string
Expand Down Expand Up @@ -582,8 +603,9 @@ func TestAutogeneratedIngressIdentification(t *testing.T) {
lagoonYAML: "../test-resources/identify-ingress/test13/lagoon.yml",
templatePath: "../test-resources/output",
},
wantRemain: []string{"example.com"},
wantautoGen: []string{},
wantJSON: `{"primary":"","secondary":null,"autogenerated":[]}`,
wantJSON: `{"primary":"","secondary":["example.com"],"autogenerated":[]}`,
},
{
name: "test14 only autogenerated route",
Expand All @@ -601,8 +623,9 @@ func TestAutogeneratedIngressIdentification(t *testing.T) {
lagoonYAML: "../test-resources/identify-ingress/test14/lagoon.yml",
templatePath: "../test-resources/output",
},
wantRemain: []string{},
wantautoGen: []string{"node"},
wantJSON: `{"primary":"","secondary":null,"autogenerated":["node"]}`,
wantJSON: `{"primary":"","secondary":[],"autogenerated":["node"]}`,
},
{
name: "test15 only autogenerated route complex",
Expand All @@ -620,8 +643,9 @@ func TestAutogeneratedIngressIdentification(t *testing.T) {
lagoonYAML: "../test-resources/identify-ingress/test15/lagoon.yml",
templatePath: "../test-resources/output",
},
wantRemain: []string{},
wantautoGen: []string{"nginx"},
wantJSON: `{"primary":"","secondary":null,"autogenerated":["nginx"]}`,
wantJSON: `{"primary":"","secondary":[],"autogenerated":["nginx"]}`,
},
{
name: "test16 autogenerated routes where lagoon.name of service does not match service names",
Expand All @@ -639,8 +663,9 @@ func TestAutogeneratedIngressIdentification(t *testing.T) {
lagoonYAML: "../test-resources/identify-ingress/test16/lagoon.yml",
templatePath: "../test-resources/output",
},
wantRemain: []string{},
wantautoGen: []string{"nginx-php"},
wantJSON: `{"primary":"","secondary":null,"autogenerated":["nginx-php"]}`,
wantJSON: `{"primary":"","secondary":[],"autogenerated":["nginx-php"]}`,
},
{
name: "test17 autogenerated routes with mulitple routeable services",
Expand All @@ -658,8 +683,29 @@ func TestAutogeneratedIngressIdentification(t *testing.T) {
lagoonYAML: "../test-resources/identify-ingress/test17/lagoon.yml",
templatePath: "../test-resources/output",
},
wantRemain: []string{},
wantautoGen: []string{"nginx", "varnish"},
wantJSON: `{"primary":"","secondary":null,"autogenerated":["nginx","varnish"]}`,
wantJSON: `{"primary":"","secondary":[],"autogenerated":["nginx","varnish"]}`,
},
{
name: "test18 autogenerated routes with wildcard and altnames",
args: args{
alertContact: "alertcontact",
statusPageID: "statuspageid",
projectName: "example-project",
environmentName: "main",
environmentType: "production",
buildType: "branch",
lagoonVersion: "v2.7.x",
branch: "main",
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]`,
envVars: `[]`,
lagoonYAML: "../test-resources/identify-ingress/test18/lagoon.yml",
templatePath: "../test-resources/output",
},
wantRemain: []string{"wildcard-wild.example.com", "alt.example.com"},
wantautoGen: []string{"nginx"},
wantJSON: `{"primary":"","secondary":["wildcard-wild.example.com","alt.example.com"],"autogenerated":["nginx"]}`,
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -745,7 +791,7 @@ func TestAutogeneratedIngressIdentification(t *testing.T) {
RetryWaitMax: time.Duration(50) * time.Millisecond,
})

autogen, err := AutogeneratedIngressIdentification(generator)
autogen, remainders, err := CreatedIngressIdentification(generator)
if err != nil {
t.Errorf("%v", err)
}
Expand All @@ -754,8 +800,13 @@ func TestAutogeneratedIngressIdentification(t *testing.T) {
t.Errorf("returned autogen %v doesn't match want %v", autogen, tt.wantautoGen)
}

if !reflect.DeepEqual(remainders, tt.wantRemain) {
t.Errorf("returned remainders %v doesn't match want %v", remainders, tt.wantRemain)
}

ret := ingressIdentifyJSON{
Autogenerated: autogen,
Secondary: remainders,
}
retJSON, _ := json.Marshal(ret)

Expand Down
1 change: 1 addition & 0 deletions internal/generator/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func generateAutogenRoutes(
IngressClass: ingressClass,
Autogenerated: true,
LagoonService: serviceOverrideName,
IngressName: serviceOverrideName,
ComposeService: service.Name,
Insecure: &insecure,
AlternativeNames: alternativeNames,
Expand Down
19 changes: 19 additions & 0 deletions internal/generator/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func Test_generateAndMerge(t *testing.T) {
Insecure: helpers.StrPtr("Redirect"),
MonitoringPath: "/bypass-cache",
AlternativeNames: []string{},
IngressName: "a.example.com",
},
{
Domain: "b.example.com",
Expand All @@ -178,6 +179,7 @@ func Test_generateAndMerge(t *testing.T) {
Insecure: helpers.StrPtr("Redirect"),
MonitoringPath: "/",
AlternativeNames: []string{},
IngressName: "b.example.com",
},
{
Domain: "c.example.com",
Expand All @@ -187,6 +189,7 @@ func Test_generateAndMerge(t *testing.T) {
Insecure: helpers.StrPtr("Redirect"),
MonitoringPath: "/",
AlternativeNames: []string{},
IngressName: "c.example.com",
},
{
Domain: "test1.example.com",
Expand All @@ -196,6 +199,7 @@ func Test_generateAndMerge(t *testing.T) {
Insecure: helpers.StrPtr("Redirect"),
Annotations: map[string]string{},
AlternativeNames: []string{},
IngressName: "test1.example.com",
},
},
},
Expand Down Expand Up @@ -234,6 +238,7 @@ func Test_generateAndMerge(t *testing.T) {
Insecure: helpers.StrPtr("Redirect"),
Annotations: map[string]string{},
AlternativeNames: []string{},
IngressName: "test1.example.com",
},
{
Domain: "a.example.com",
Expand All @@ -243,6 +248,7 @@ func Test_generateAndMerge(t *testing.T) {
Insecure: helpers.StrPtr("Redirect"),
MonitoringPath: "/bypass-cache",
AlternativeNames: []string{},
IngressName: "a.example.com",
},
},
},
Expand Down Expand Up @@ -307,6 +313,7 @@ func Test_generateAndMerge(t *testing.T) {
MonitoringPath: "/bypass-cache",
IngressClass: "nginx",
AlternativeNames: []string{},
IngressName: "a.example.com",
},
{
Domain: "b.example.com",
Expand All @@ -317,6 +324,7 @@ func Test_generateAndMerge(t *testing.T) {
MonitoringPath: "/",
IngressClass: "nginx",
AlternativeNames: []string{},
IngressName: "b.example.com",
},
{
Domain: "c.example.com",
Expand All @@ -327,6 +335,7 @@ func Test_generateAndMerge(t *testing.T) {
MonitoringPath: "/",
IngressClass: "nginx",
AlternativeNames: []string{},
IngressName: "c.example.com",
},
{
Domain: "test1.example.com",
Expand All @@ -337,6 +346,7 @@ func Test_generateAndMerge(t *testing.T) {
Annotations: map[string]string{},
IngressClass: "nginx",
AlternativeNames: []string{},
IngressName: "test1.example.com",
},
},
},
Expand Down Expand Up @@ -393,6 +403,7 @@ func Test_generateAndMerge(t *testing.T) {
HSTSEnabled: helpers.BoolPtr(true),
HSTSMaxAge: 36000,
AlternativeNames: []string{},
IngressName: "a.example.com",
},
},
},
Expand Down Expand Up @@ -437,6 +448,7 @@ func Test_generateAndMerge(t *testing.T) {
IngressClass: "nginx",
AlternativeNames: []string{},
Wildcard: helpers.BoolPtr(true),
IngressName: "wildcard-a.example.com",
},
},
},
Expand Down Expand Up @@ -537,6 +549,7 @@ func Test_generateActiveStandbyRoutes(t *testing.T) {
Insecure: helpers.StrPtr("Redirect"),
MonitoringPath: "/",
AlternativeNames: []string{},
IngressName: "active.example.com",
},
},
},
Expand Down Expand Up @@ -581,6 +594,7 @@ func Test_generateActiveStandbyRoutes(t *testing.T) {
MonitoringPath: "/",
IngressClass: "nginx",
AlternativeNames: []string{},
IngressName: "active.example.com",
},
},
},
Expand Down Expand Up @@ -626,6 +640,7 @@ func Test_generateActiveStandbyRoutes(t *testing.T) {
MonitoringPath: "/",
IngressClass: "custom-nginx",
AlternativeNames: []string{},
IngressName: "active.example.com",
},
},
},
Expand Down Expand Up @@ -706,6 +721,7 @@ func Test_generateActiveStandbyRoutes(t *testing.T) {
IngressClass: "nginx",
AlternativeNames: []string{},
Wildcard: helpers.BoolPtr(true),
IngressName: "wildcard-active.example.com",
},
},
},
Expand Down Expand Up @@ -835,6 +851,7 @@ func Test_generateAutogenRoutes(t *testing.T) {
"lagoon.sh/service": "nginx",
"lagoon.sh/service-type": "nginx",
},
IngressName: "nginx",
},
},
},
Expand Down Expand Up @@ -887,6 +904,7 @@ func Test_generateAutogenRoutes(t *testing.T) {
"lagoon.sh/service": "nginx",
"lagoon.sh/service-type": "nginx",
},
IngressName: "nginx",
},
},
},
Expand Down Expand Up @@ -945,6 +963,7 @@ func Test_generateAutogenRoutes(t *testing.T) {
"lagoon.sh/service": "nginx",
"lagoon.sh/service-type": "nginx",
},
IngressName: "nginx",
},
},
},
Expand Down
Loading
Loading