@@ -21,6 +21,7 @@ import (
2121
2222 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2323 "k8s.io/apimachinery/pkg/runtime/schema"
24+ "k8s.io/apimachinery/pkg/util/validation"
2425 "k8s.io/cli-runtime/pkg/resource"
2526)
2627
@@ -45,10 +46,15 @@ func CreateObjMetadata(namespace string, name string, gk schema.GroupKind) (*Obj
4546 // Namespace can be empty, but name cannot.
4647 name = strings .TrimSpace (name )
4748 if name == "" {
48- return nil , fmt .Errorf ("empty name for inventory object" )
49+ return nil , fmt .Errorf ("empty name for object" )
50+ }
51+ // Manually validate name, since by the time k8s reports the error
52+ // the invalid name has already been encoded into the inventory object.
53+ if ! validateNameChars (name ) {
54+ return nil , fmt .Errorf ("invalid characters in object name: %s" , name )
4955 }
5056 if gk .Empty () {
51- return nil , fmt .Errorf ("empty GroupKind for inventory object" )
57+ return nil , fmt .Errorf ("empty GroupKind for object" )
5258 }
5359
5460 return & ObjMetadata {
@@ -58,6 +64,23 @@ func CreateObjMetadata(namespace string, name string, gk schema.GroupKind) (*Obj
5864 }, nil
5965}
6066
67+ // validateNameChars returns false if the passed name string contains
68+ // any invalid characters; true otherwise. The allowed characters for
69+ // a Kubernetes resource name are:
70+ //
71+ // Most resource types require a name that can be used as a DNS label name
72+ // as defined in RFC 1123. This means the name must:
73+ //
74+ // * contain no more than 253 characters
75+ // * contain only lowercase alphanumeric characters, '-'
76+ // * start with an alphanumeric character
77+ // * end with an alphanumeric character
78+ //
79+ func validateNameChars (name string ) bool {
80+ errs := validation .IsDNS1123Subdomain (name )
81+ return len (errs ) == 0
82+ }
83+
6184// ParseObjMetadata takes a string, splits it into its five fields,
6285// and returns a pointer to an ObjMetadata struct storing the
6386// five fields. Example inventory string:
0 commit comments