URIResolver verifies that services actually exist (#2149)

This commit is contained in:
Ben Moss 2021-06-11 04:23:43 -04:00 committed by GitHub
parent 9593069dae
commit dd15eb0998
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 13 deletions

View File

@ -158,18 +158,6 @@ func (r *URIResolver) URIFromObjectReference(ctx context.Context, ref *corev1.Ob
return nil, apierrs.NewNotFound(gvr.GroupResource(), ref.Name)
}
// K8s Services are special cased. They can be called, even though they do not satisfy the
// Callable interface.
// TODO(spencer-p,n3wscott) Verify that the service actually exists in K8s.
if ref.APIVersion == "v1" && ref.Kind == "Service" {
url := &apis.URL{
Scheme: "http",
Host: network.GetServiceHostname(ref.Name, ref.Namespace),
Path: "/",
}
return url, nil
}
lister, err := r.listerFactory(gvr)
if err != nil {
return nil, apierrs.NewNotFound(gvr.GroupResource(), "Lister")
@ -180,6 +168,17 @@ func (r *URIResolver) URIFromObjectReference(ctx context.Context, ref *corev1.Ob
return nil, apierrs.NewNotFound(gvr.GroupResource(), ref.Name)
}
// K8s Services are special cased. They can be called, even though they do not satisfy the
// Callable interface.
if ref.APIVersion == "v1" && ref.Kind == "Service" {
url := &apis.URL{
Scheme: "http",
Host: network.GetServiceHostname(ref.Name, ref.Namespace),
Path: "/",
}
return url, nil
}
addressable, ok := obj.(*duckv1.AddressableType)
if !ok {
return nil, apierrs.NewBadRequest(fmt.Sprintf("%+v (%T) is not an AddressableType", ref, ref))

View File

@ -417,7 +417,7 @@ func TestGetURIDestinationV1(t *testing.T) {
wantURI: addressableDNS,
}, "happy ref to k8s service": {
objects: []runtime.Object{
getAddressable(),
getAddressableFromKRef(k8sServiceRef()),
},
dest: duckv1.Destination{Ref: k8sServiceRef()},
wantURI: "http://testsink.testnamespace.svc.cluster.local/",
@ -529,6 +529,9 @@ func TestGetURIDestinationV1(t *testing.T) {
}, "notFound": {
dest: duckv1.Destination{Ref: unaddressableKnativeRef()},
wantErr: fmt.Sprintf("%s %q not found", unaddressableResource, unaddressableName),
}, "notFound k8s service": {
dest: duckv1.Destination{Ref: k8sServiceRef()},
wantErr: fmt.Sprintf("services %q not found", addressableName),
}}
for n, tc := range tests {
@ -771,3 +774,21 @@ func unaddressableRef() *corev1.ObjectReference {
Namespace: testNS,
}
}
func getAddressableFromKRef(ref *duckv1.KReference) *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": ref.APIVersion,
"kind": ref.Kind,
"metadata": map[string]interface{}{
"namespace": ref.Namespace,
"name": ref.Name,
},
"status": map[string]interface{}{
"address": map[string]interface{}{
"url": addressableDNS,
},
},
},
}
}