Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
Co-authored-by: ybelmekk <youssef.bel.mekki@nav.no>
Co-authored-by: Carl Hedgren<carl.hedgren@nav.no>
  • Loading branch information
sindrerh2 and ybelMekk committed Nov 8, 2024
1 parent be18542 commit 8c84c5a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
21 changes: 14 additions & 7 deletions pkg/resourcecreator/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type Config interface {

func ingressRule(appName string, u *url.URL) networkingv1.IngressRule {
pathType := networkingv1.PathTypeImplementationSpecific

return networkingv1.IngressRule{
Host: u.Host,
IngressRuleValue: networkingv1.IngressRuleValue{
Expand Down Expand Up @@ -67,6 +66,7 @@ func ingressRules(source Source) ([]networkingv1.IngressRule, error) {

for _, ingress := range ingresses {
parsedUrl, err := parseIngress(string(ingress))
fmt.Println("parsedUrl", parsedUrl)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -104,7 +104,7 @@ func copyNginxAnnotations(dst, src map[string]string) {
}
}

func createIngressBase(source Source, rules []networkingv1.IngressRule) *networkingv1.Ingress {
func createIngressBase(source Source) *networkingv1.Ingress {
objectMeta := resource.CreateObjectMeta(source)
objectMeta.Annotations["prometheus.io/scrape"] = "true"
objectMeta.Annotations["prometheus.io/path"] = source.GetLiveness().Path
Expand All @@ -116,14 +116,14 @@ func createIngressBase(source Source, rules []networkingv1.IngressRule) *network
},
ObjectMeta: objectMeta,
Spec: networkingv1.IngressSpec{
Rules: rules,
Rules: []networkingv1.IngressRule{},
},
}
}

func createIngressBaseNginx(source Source, ingressClass string, redirect string) (*networkingv1.Ingress, error) {
var err error
ingress := createIngressBase(source, []networkingv1.IngressRule{})
ingress := createIngressBase(source)
baseName := fmt.Sprintf("%s-%s", source.GetName(), ingressClass)
ingress.Name, err = namegen.ShortName(baseName, validation.DNS1035LabelMaxLength)
if err != nil {
Expand Down Expand Up @@ -205,21 +205,28 @@ func createRedirectIngresses(source Source, cfg Config, redirects []nais_io_v1.R
for _, ing := range ingresses {
for _, redirect := range redirects {
for _, rule := range ing.Spec.Rules {
parsedFromRedirectUrl, err := parseIngress(string(redirect.From))
if err != nil {
return err
}
parsedToRedirectUrl, err := parseIngress(string(redirect.To))
if err != nil {
return err
}

// found the ingress that matches the redirect
if rule.Host == parsedToRedirectUrl.Host {
r := ingressRule(source.GetName(), parsedToRedirectUrl)
urlPath, err := url.Parse(strings.TrimRight(parsedFromRedirectUrl.String(), "/"))
if err != nil {
return err
}
urlPath.Path = urlPath.Path + regexSuffix
r := ingressRule(source.GetName(), urlPath)
parsedFromUrl, err := parseIngress(string(redirect.From))
if err != nil {
return err
}
ingressClass := util.ResolveIngressClass(parsedFromUrl.Host, cfg.GetGatewayMappings())
fmt.Println("ingressClass", *ingressClass)
fmt.Println("redirectTo", string(redirect.To))
rdIngress, err := getIngress(source, cfg, r, ingressClass, string(redirect.To))
if err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions pkg/resourcecreator/testdata/ingress_redirect.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ tests:
port:
number: 80
path: /
pathType: ImplementationSpecific

- operation: CreateOrUpdate
apiVersion: networking.k8s.io/v1
Expand All @@ -64,12 +65,13 @@ tests:
spec:
ingressClassName: "nais-ingress"
rules:
- host: "myapplication-other.domain"
- host: "myapplication.from.redirect.old.dev.nais.io"
http:
paths:
- backend:
service:
name: myapplicationredirect
port:
number: 80
path: /
path: (/.*)?
pathType: ImplementationSpecific
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ input:
spec:
image: navikt/myapplication:1.2.3
ingresses:
- "https://myapplication-other.domain"
- "https://notspecinredirects.to.domain"
redirects:
- from: "https://myapplication.from.redirect.old.dev.nais.io"
to: "https://myapplication.dev.nais.io"
Expand Down

0 comments on commit 8c84c5a

Please sign in to comment.