Skip to content

Commit

Permalink
draft for service name and pod matching check
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulina Grochal authored and cloudlena committed Feb 10, 2024
1 parent aa4a573 commit e1f4a8d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
51 changes: 51 additions & 0 deletions internal/livelint/check_are_service_name_and_port_matching.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package livelint

import (
"context"
"fmt"
"log"

v1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (n *Livelint) checkServiceNameAndPortAreMatching(ingress netv1.Ingress, services []v1.Service, namespace string) CheckResult {

for _, rule := range ingress.Spec.Rules {
if rule.HTTP == nil {
continue
}

for _, path := range rule.HTTP.Paths {
switch {
case path.Backend.Service != nil:
//TODO compare here if service name matches any serviceName forwarded to the function as list parameter
service, err := n.k8s.CoreV1().Services(namespace).Get(context.Background(), path.Backend.Service.Name, metav1.GetOptions{})

Check failure on line 24 in internal/livelint/check_are_service_name_and_port_matching.go

View workflow job for this annotation

GitHub Actions / Verify

n.k8s undefined (type *Livelint has no field or method k8s, but does have K8s)) (typecheck)

Check failure on line 24 in internal/livelint/check_are_service_name_and_port_matching.go

View workflow job for this annotation

GitHub Actions / Verify

n.k8s undefined (type *Livelint has no field or method k8s, but does have K8s) (typecheck)

Check failure on line 24 in internal/livelint/check_are_service_name_and_port_matching.go

View workflow job for this annotation

GitHub Actions / Verify

n.k8s undefined (type *Livelint has no field or method k8s, but does have K8s)) (typecheck)
if err != nil {
log.Fatal(fmt.Errorf("error getting backends for Ingress %s in Namespace %s: %w", ingress.Name, namespace, err))
}

//TODO compare port from service forwarded to function and the one from ingress spec
hasPortMatch := false
for _, servicePort := range service.Spec.Ports {
if servicePort.Port == path.Backend.Service.Port.Number {
hasPortMatch = true
break
}
}

if !hasPortMatch {
return CheckResult{
HasFailed: true,
Message: fmt.Sprintf("Service name and port mismatch for %s", ingress.Name),
}
}
}
}
}

return CheckResult{
Message: fmt.Sprintf("Service name and port mismatch for %s", ingress.Name),
}
}
13 changes: 13 additions & 0 deletions internal/livelint/run_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,19 @@ func (n *Livelint) RunChecks(namespace, deploymentName string, verbose bool) err
return nil
}

// allServices, partlyMatchingServices, err := n.getServices(namespace, deploymentName)
// if err != nil {
// log.Fatal(fmt.Errorf("error getting services in namespace %s: %w", namespace, err))
// }
// TODO fetch services from previous step
// allServices = append(allServices, partlyMatchingServices...)

// result = n.checkServiceNameAndPortAreMatching(ingress, allServices, namespace)
// n.ui.DisplayCheckResult(result)
// if result.HasFailed {
// return nil
// }

result = checkHasValidIngressClass(ingress, ingressClasses)
n.ui.DisplayCheckResult(result)
if result.HasFailed {
Expand Down

0 comments on commit e1f4a8d

Please sign in to comment.