Debian Codegraph @

pkg/admission/plugin/webhook/testing/service_resolver.go

raw
1 /*
2 Copyright 2018 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package testing
18
19 import (
20 "fmt"
21 "net/url"
22
23 "k8s.io/apiserver/pkg/util/webhook"
24 )
25
26 type serviceResolver struct {
27 base url.URL
28 }
29
30 // NewServiceResolver returns a static service resolve that return the given URL or
31 // an error for the failResolve namespace.
32 func NewServiceResolver(base url.URL) webhook.ServiceResolver {
33 return &serviceResolver{base}
34 }
35
36 func (f serviceResolver) ResolveEndpoint(namespace, name string, port int32) (*url.URL, error) {
37 if namespace == "failResolve" {
38 return nil, fmt.Errorf("couldn't resolve service location")
39 }
40 u := f.base
41 return &u, nil
42 }
43
44 type panickingResolver struct {
45 panicMessage string
46 }
47
48 // NewPanickingServiceResolver returns a static service resolver that panics.
49 func NewPanickingServiceResolver(panicMessage string) webhook.ServiceResolver {
50 return &panickingResolver{panicMessage}
51 }
52
53 func (f panickingResolver) ResolveEndpoint(namespace, name string, port int32) (*url.URL, error) {
54 panic(f.panicMessage)
55 }