Merge "RT: add error handling for missing target docs"

This commit is contained in:
Zuul 2020-05-18 15:54:18 +00:00 committed by Gerrit Code Review
commit 7276dd68d8
3 changed files with 56 additions and 6 deletions

View File

@ -56,12 +56,12 @@ func (e ErrMultipleResources) Error() string {
return fmt.Sprintf("found more than one resources matching from %v", e.ResList)
}
// ErrResourceNotFound returned if resource does not exist in resource map
type ErrResourceNotFound struct {
// ErrResourceNotFound returned if a replacement source resource does not exist in resource map
type ErrSourceNotFound struct {
ObjRef *types.Target
}
func (e ErrResourceNotFound) Error() string {
func (e ErrSourceNotFound) Error() string {
keys := [5]string{"Group:", "Version:", "Kind:", "Name:", "Namespace:"}
values := [5]string{e.ObjRef.Group, e.ObjRef.Version, e.ObjRef.Kind, e.ObjRef.Name, e.ObjRef.Namespace}
@ -71,7 +71,27 @@ func (e ErrResourceNotFound) Error() string {
resFilter += key + values[i] + " "
}
}
return fmt.Sprintf("failed to find any resources identified by %s", strings.TrimSpace(resFilter))
return fmt.Sprintf("failed to find any source resources identified by %s", strings.TrimSpace(resFilter))
}
// ErrSelectorNotFound returned if a replacement target resource does not exist in the resource map
type ErrTargetNotFound struct {
ObjRef *types.Selector
}
func (e ErrTargetNotFound) Error() string {
keys := [7]string{"Group:", "Version:", "Kind:", "Name:", "Namespace:",
"AnnotationSelector:", "LabelSelector:"}
values := [7]string{e.ObjRef.Group, e.ObjRef.Version, e.ObjRef.Kind, e.ObjRef.Name,
e.ObjRef.Namespace, e.ObjRef.AnnotationSelector, e.ObjRef.LabelSelector}
var resFilter string
for i, key := range keys {
if values[i] != "" {
resFilter += key + values[i] + " "
}
}
return fmt.Sprintf("failed to find any target resources identified by %s", strings.TrimSpace(resFilter))
}
// ErrPatternSubstring returned in case of issues with sub-string pattern substitution

View File

@ -140,7 +140,7 @@ func getReplacement(m resmap.ResMap, objRef *types.Target, fieldRef string) (int
return nil, ErrMultipleResources{ResList: resources}
}
if len(resources) == 0 {
return nil, ErrResourceNotFound{ObjRef: objRef}
return nil, ErrSourceNotFound{ObjRef: objRef}
}
if fieldRef == "" {
fieldRef = "metadata.name"
@ -153,6 +153,9 @@ func substitute(m resmap.ResMap, to *types.ReplTarget, replacement interface{})
if err != nil {
return err
}
if len(resources) == 0 {
return ErrTargetNotFound{ObjRef: to.ObjRef}
}
for _, r := range resources {
for _, p := range to.FieldRefs {
// TODO (dukov) rework this using k8s.io/client-go/util/jsonpath

View File

@ -564,7 +564,34 @@ replacements:
kind: Deployment
fieldrefs:
- spec.template.spec.containers[name=nginx-latest].image`,
expectedErr: "failed to find any resources identified by Kind:Pod Name:pod1 Namespace:default",
expectedErr: "failed to find any source resources identified by Kind:Pod Name:pod1 Namespace:default",
},
{
cfg: `
apiVersion: airshipit.org/v1alpha1
kind: ReplacementTransformer
metadata:
name: notImportantHere
replacements:
- source:
objref:
kind: Pod
name: pod1
target:
objref:
kind: Deployment
fieldrefs:
- spec.template.spec.containers[name=nginx-latest].image`,
in: `
apiVersion: v1
kind: Pod
metadata:
name: pod1
spec:
containers:
- name: myapp-container
image: busybox`,
expectedErr: "failed to find any target resources identified by Kind:Deployment",
},
{
cfg: `