-
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.
Add CheckServiceExistence Rule for gke-ingress-checker
- Loading branch information
1 parent
6676215
commit efcf0fb
Showing
2 changed files
with
112 additions
and
0 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
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) | ||
} |
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,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) | ||
} | ||
} | ||
} |