Skip to content

Commit 5af6137

Browse files
committed
Remove special-case object name validation
1 parent 152b41c commit 5af6137

File tree

2 files changed

+4
-67
lines changed

2 files changed

+4
-67
lines changed

pkg/object/objmetadata.go

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2929
"k8s.io/apimachinery/pkg/runtime"
3030
"k8s.io/apimachinery/pkg/runtime/schema"
31-
"k8s.io/apimachinery/pkg/util/validation"
3231
"k8s.io/cli-runtime/pkg/resource"
3332
)
3433

@@ -72,11 +71,6 @@ func CreateObjMetadata(namespace string, name string, gk schema.GroupKind) (ObjM
7271
if name == "" {
7372
return ObjMetadata{}, fmt.Errorf("empty name for object")
7473
}
75-
// Manually validate name, since by the time k8s reports the error
76-
// the invalid name has already been encoded into the inventory object.
77-
if !validateNameChars(name, gk) {
78-
return ObjMetadata{}, fmt.Errorf("invalid characters in object name: %s", name)
79-
}
8074
if gk.Empty() {
8175
return ObjMetadata{}, fmt.Errorf("empty GroupKind for object")
8276
}
@@ -87,27 +81,6 @@ func CreateObjMetadata(namespace string, name string, gk schema.GroupKind) (ObjM
8781
}, nil
8882
}
8983

90-
// validateNameChars returns false if the passed name is not a valid
91-
// resource name; true otherwise. For almost all resources, the following
92-
// characters are allowed:
93-
//
94-
// Most resource types require a name that can be used as a DNS label name
95-
// as defined in RFC 1123. This means the name must:
96-
//
97-
// * contain no more than 253 characters
98-
// * contain only lowercase alphanumeric characters, '-'
99-
// * start with an alphanumeric character
100-
// * end with an alphanumeric character
101-
//
102-
// For RBAC resources we also allow the colon character.
103-
func validateNameChars(name string, gk schema.GroupKind) bool {
104-
if _, exists := RBACGroupKind[gk]; exists {
105-
name = strings.ReplaceAll(name, ":", "")
106-
}
107-
errs := validation.IsDNS1123Subdomain(name)
108-
return len(errs) == 0
109-
}
110-
11184
// ParseObjMetadata takes a string, splits it into its four fields,
11285
// and returns an ObjMetadata struct storing the four fields.
11386
// Example inventory string:
@@ -143,6 +116,10 @@ func ParseObjMetadata(s string) (ObjMetadata, error) {
143116
// Finally, second field name. Name may contain colon transcoded as double underscore.
144117
name := s[:index]
145118
name = strings.ReplaceAll(name, colonTranscoded, ":")
119+
// Check that there are no extra fields by search for fieldSeparator.
120+
if strings.Contains(name, fieldSeparator) {
121+
return ObjMetadata{}, fmt.Errorf("too many fields within: %s", s)
122+
}
146123
// Create the ObjMetadata object from the four parsed fields.
147124
gk := schema.GroupKind{
148125
Group: strings.TrimSpace(group),

pkg/object/objmetadata_test.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,6 @@ func TestCreateObjMetadata(t *testing.T) {
5656
expected: "",
5757
isError: true,
5858
},
59-
"Underscore is invalid name character": {
60-
namespace: "test-namespace",
61-
name: "test_name", // Invalid "_" character
62-
gk: schema.GroupKind{
63-
Group: "apps",
64-
Kind: "ReplicaSet",
65-
},
66-
expected: "",
67-
isError: true,
68-
},
69-
"Name not starting with alphanumeric character is error": {
70-
namespace: "test-namespace",
71-
name: "-test",
72-
gk: schema.GroupKind{
73-
Group: "apps",
74-
Kind: "ReplicaSet",
75-
},
76-
expected: "",
77-
isError: true,
78-
},
79-
"Name not ending with alphanumeric character is error": {
80-
namespace: "test-namespace",
81-
name: "test-",
82-
gk: schema.GroupKind{
83-
Group: "apps",
84-
Kind: "ReplicaSet",
85-
},
86-
expected: "",
87-
isError: true,
88-
},
8959
"Colon is allowed in the name for RBAC resources": {
9060
namespace: "test-namespace",
9161
name: "system::kube-scheduler",
@@ -96,16 +66,6 @@ func TestCreateObjMetadata(t *testing.T) {
9666
expected: "test-namespace_system____kube-scheduler_rbac.authorization.k8s.io_Role",
9767
isError: false,
9868
},
99-
"Colon is not allowed in the name for non-RBAC resources": {
100-
namespace: "test-namespace",
101-
name: "system::kube-scheduler",
102-
gk: schema.GroupKind{
103-
Group: "",
104-
Kind: "Pod",
105-
},
106-
expected: "",
107-
isError: true,
108-
},
10969
}
11070

11171
for name, tc := range tests {

0 commit comments

Comments
 (0)