-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate checks for check-gke-ingress
- Loading branch information
1 parent
7d9c4fc
commit 7202a5e
Showing
4 changed files
with
403 additions
and
103 deletions.
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
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,167 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package ingress | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"reflect" | ||
"runtime" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/ingress-gce/cmd/check-gke-ingress/app/kube" | ||
"k8s.io/ingress-gce/cmd/check-gke-ingress/app/report" | ||
) | ||
|
||
func CheckAllIngresses(kubeconfig, kubecontext, namespace string) string { | ||
output := report.Report{ | ||
Resources: []*report.Resource{}, | ||
} | ||
client, err := kube.NewClientSet(kubecontext, kubeconfig) | ||
beconfigClient, err := kube.NewBackendConfigClientSet(kubecontext, kubeconfig) | ||
feConfigClient, err := kube.NewFrontendConfigClientSet(kubecontext, kubeconfig) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error connecting to Kubernetes: %v", err) | ||
os.Exit(1) | ||
} | ||
|
||
ingressList, err := client.NetworkingV1().Ingresses(namespace).List(context.TODO(), metav1.ListOptions{}) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error listing ingresses: %v", err) | ||
os.Exit(1) | ||
} | ||
for _, ingress := range ingressList.Items { | ||
|
||
// Ingress related checks | ||
ingressRes := &report.Resource{ | ||
Kind: "Ingress", | ||
Namespace: ingress.Namespace, | ||
Name: ingress.Name, | ||
Checks: []*report.Check{}, | ||
} | ||
ingressChecker := &IngressChecker{ | ||
client: client, | ||
ingress: &ingress, | ||
} | ||
ingressChecks := []ingressCheckFunc{ | ||
CheckIngressRule, | ||
CheckL7ILBFrontendConfig, | ||
CheckRuleHostOverwrite, | ||
} | ||
for _, check := range ingressChecks { | ||
checkName, res, msg := check(ingressChecker) | ||
addCheckResult(ingressRes, checkName, msg, res) | ||
} | ||
|
||
// FrontendConfig related checks | ||
feconfigChecker := &FrontendConfigChecker{ | ||
client: feConfigClient, | ||
namespace: ingress.Namespace, | ||
name: ingress.Name, | ||
} | ||
feconfigChecks := []frontendConfigCheckFunc{CheckFrontendConfigExistence} | ||
for _, check := range feconfigChecks { | ||
checkName, res, msg := check(feconfigChecker) | ||
addCheckResult(ingressRes, checkName, msg, res) | ||
} | ||
|
||
// Get the names of the services referenced by the ingress. | ||
svcNames := []string{} | ||
if ingress.Spec.DefaultBackend != nil { | ||
svcNames = append(svcNames, ingress.Spec.DefaultBackend.Service.Name) | ||
} | ||
if ingress.Spec.Rules != nil { | ||
for _, rule := range ingress.Spec.Rules { | ||
if rule.HTTP != nil { | ||
for _, path := range rule.HTTP.Paths { | ||
if path.Backend.Service != nil { | ||
svcNames = append(svcNames, path.Backend.Service.Name) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Service related checks | ||
for _, svcName := range svcNames { | ||
serviceChecker := &ServiceChecker{ | ||
namespace: ingress.Namespace, | ||
name: svcName, | ||
isL7ILB: isL7ILB(&ingress), | ||
client: client, | ||
} | ||
serviceChecks := []serviceCheckFunc{ | ||
CheckServiceExistence, | ||
CheckBackendConfigAnnotation, | ||
CheckAppProtocolAnnotation, | ||
} | ||
for _, check := range serviceChecks { | ||
checkName, res, msg := check(serviceChecker) | ||
addCheckResult(ingressRes, checkName, msg, res) | ||
} | ||
|
||
// Get all the BackendConfigs referenced by the service. | ||
beconfigNames := []string{} | ||
if serviceChecker.beConfigs != nil { | ||
if serviceChecker.beConfigs.Default != "" { | ||
beconfigNames = append(beconfigNames, serviceChecker.beConfigs.Default) | ||
} | ||
for _, beconfig := range serviceChecker.beConfigs.Ports { | ||
beconfigNames = append(beconfigNames, beconfig) | ||
} | ||
} | ||
// BackendConfig related rules | ||
for _, beconfigName := range beconfigNames { | ||
beconfigChecker := &BackendConfigChecker{ | ||
namespace: ingress.Namespace, | ||
name: beconfigName, | ||
client: beconfigClient, | ||
serviceName: svcName, | ||
} | ||
beconfigChecks := []backendConfigCheckFunc{ | ||
CheckBackendConfigExistence, | ||
CheckHealthCheckTimeout, | ||
} | ||
for _, check := range beconfigChecks { | ||
checkName, res, msg := check(beconfigChecker) | ||
addCheckResult(ingressRes, checkName, msg, res) | ||
} | ||
} | ||
} | ||
output.Resources = append(output.Resources, ingressRes) | ||
} | ||
|
||
res, err := report.JsonReport(&output) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error processing results: %v", err) | ||
os.Exit(1) | ||
} | ||
return res | ||
} | ||
|
||
func addCheckResult(ingressRes *report.Resource, checkName, msg, res string) { | ||
ingressRes.Checks = append(ingressRes.Checks, &report.Check{ | ||
Name: checkName, | ||
Message: msg, | ||
Result: res, | ||
}) | ||
} | ||
|
||
func getCheckName(check func()) string { | ||
return runtime.FuncForPC(reflect.ValueOf(check).Pointer()).Name() | ||
} |
Oops, something went wrong.