Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CheckServiceExistence Rule for gke-ingress-checker #2025

Merged
merged 2 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check that the error is actually a not found error.
If there was a different kind of error, you need to surface that to the user and also print a different message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, thanks!

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,
},
{
ruixiansong marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package util
package report

import (
"encoding/json"
Expand Down