Skip to content

Commit

Permalink
Add CheckServiceExistence Rule for gke-ingress-checker
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixiansong committed May 2, 2023
1 parent 6676215 commit efcf0fb
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
39 changes: 39 additions & 0 deletions cmd/gke-diagnoser/app/ingress/rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
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"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/ingress-gce/cmd/gke-diagnoser/app/report"
)

func CheckServiceExistence(namespace, name string, client clientset.Interface) (*corev1.Service, string, string) {
svc, err := client.CoreV1().Services(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return nil, report.Failed, fmt.Sprintf("Service %s/%s does not exist", namespace, name)
}
return nil, report.Failed, fmt.Sprintf("Failed to get service %s/%s: %v", namespace, name, err)
}
return svc, report.Passed, fmt.Sprintf("Service %s/%s found", namespace, name)
}
73 changes: 73 additions & 0 deletions cmd/gke-diagnoser/app/ingress/rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
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"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/ingress-gce/cmd/gke-diagnoser/app/report"
)

func TestCheckServiceExistence(t *testing.T) {
ns := "namespace1"
client := fake.NewSimpleClientset()
client.CoreV1().Services(ns).Create(context.TODO(), &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: "service-1",
},
}, metav1.CreateOptions{})

for _, tc := range []struct {
desc string
namespace string
name string
expect string
}{
{
desc: "empty input",
expect: report.Failed,
},
{
desc: "correct namespace and correct name",
namespace: "namespace1",
name: "service-1",
expect: report.Passed,
},
{
desc: "correct namespace and wrong name",
namespace: "namespace1",
name: "service-2",
expect: report.Failed,
},
{
desc: "wrong namespace and correct name",
namespace: "namespace2",
name: "service-1",
expect: report.Failed,
},
} {
_, res, _ := CheckServiceExistence(tc.namespace, tc.name, client)
if res != tc.expect {
t.Errorf("For test case %q, expect check result = %s, but got %s", tc.desc, tc.expect, res)
}
}
}

0 comments on commit efcf0fb

Please sign in to comment.