-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ingress: make sure that all ingresses targets a pod
- Loading branch information
Showing
4 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ingress | ||
|
||
import ( | ||
"fmt" | ||
"github.com/zegl/kube-score/scorecard" | ||
corev1 "k8s.io/api/core/v1" | ||
extensionsv1beta1 "k8s.io/api/extensions/v1beta1" | ||
) | ||
|
||
func ScoreIngressTargetsService(allServices []corev1.Service) func(extensionsv1beta1.Ingress) scorecard.TestScore { | ||
return func(ingress extensionsv1beta1.Ingress) (score scorecard.TestScore) { | ||
score.Name = "Ingress targets Service" | ||
score.ID = "ingress-targets-service" | ||
|
||
allRulesHaveMatches := true | ||
|
||
for _, rule := range ingress.Spec.Rules { | ||
for _, path := range rule.IngressRuleValue.HTTP.Paths { | ||
|
||
pathHasMatch := false | ||
|
||
for _, service := range allServices { | ||
if service.Namespace != ingress.Namespace { | ||
continue | ||
} | ||
|
||
if service.Name == path.Backend.ServiceName { | ||
for _, servicePort := range service.Spec.Ports { | ||
if servicePort.Port == path.Backend.ServicePort.IntVal { | ||
pathHasMatch = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
if !pathHasMatch { | ||
allRulesHaveMatches = false | ||
score.AddComment(path.Path, "No service match was found", fmt.Sprintf("No service with name %s and port %d was found", path.Backend.ServiceName, path.Backend.ServicePort.IntVal)) | ||
} | ||
} | ||
|
||
} | ||
|
||
if allRulesHaveMatches { | ||
score.Grade = scorecard.GradeAllOK | ||
} else { | ||
score.Grade = scorecard.GradeCritical | ||
} | ||
|
||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
apiVersion: extensions/v1beta1 | ||
kind: Ingress | ||
metadata: | ||
name: app-ingress | ||
namespace: testspace | ||
spec: | ||
rules: | ||
- http: | ||
paths: | ||
- path: /app | ||
backend: | ||
serviceName: app-service | ||
servicePort: 5601 | ||
|
||
- path: /no-match | ||
backend: | ||
serviceName: app-service-123123 | ||
servicePort: 5601 | ||
|
||
--- | ||
kind: Service | ||
apiVersion: v1 | ||
metadata: | ||
name: app-service | ||
namespace: testspace | ||
spec: | ||
selector: | ||
app: kibana | ||
ports: | ||
- name: http | ||
protocol: TCP | ||
port: 5601 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
apiVersion: extensions/v1beta1 | ||
kind: Ingress | ||
metadata: | ||
name: app-ingress | ||
namespace: testspace | ||
spec: | ||
rules: | ||
- http: | ||
paths: | ||
- path: /app | ||
backend: | ||
serviceName: app-service | ||
servicePort: 5601 | ||
--- | ||
kind: Service | ||
apiVersion: v1 | ||
metadata: | ||
name: app-service | ||
namespace: testspace | ||
spec: | ||
selector: | ||
app: kibana | ||
ports: | ||
- name: http | ||
protocol: TCP | ||
port: 5601 |