mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-04 23:17:50 +00:00
Kubectl support for validating nested objects with different ApiGroups (e.g. Lists containing objects in different api groups). Closes #24089
This commit is contained in:
@@ -815,7 +815,7 @@ func writeSchemaFile(schemaData []byte, cacheDir, cacheFile, prefix, groupVersio
|
||||
return nil
|
||||
}
|
||||
|
||||
func getSchemaAndValidate(c schemaClient, data []byte, prefix, groupVersion, cacheDir string) (err error) {
|
||||
func getSchemaAndValidate(c schemaClient, data []byte, prefix, groupVersion, cacheDir string, delegate validation.Schema) (err error) {
|
||||
var schemaData []byte
|
||||
var firstSeen bool
|
||||
fullDir, err := substituteUserHome(cacheDir)
|
||||
@@ -836,7 +836,7 @@ func getSchemaAndValidate(c schemaClient, data []byte, prefix, groupVersion, cac
|
||||
return err
|
||||
}
|
||||
}
|
||||
schema, err := validation.NewSwaggerSchemaFromBytes(schemaData)
|
||||
schema, err := validation.NewSwaggerSchemaFromBytes(schemaData, delegate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -849,7 +849,7 @@ func getSchemaAndValidate(c schemaClient, data []byte, prefix, groupVersion, cac
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
schema, err := validation.NewSwaggerSchemaFromBytes(schemaData)
|
||||
schema, err := validation.NewSwaggerSchemaFromBytes(schemaData, delegate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -888,20 +888,20 @@ func (c *clientSwaggerSchema) ValidateBytes(data []byte) error {
|
||||
if c.c.AutoscalingClient == nil {
|
||||
return errors.New("unable to validate: no autoscaling client")
|
||||
}
|
||||
return getSchemaAndValidate(c.c.AutoscalingClient.RESTClient, data, "apis/", gvk.GroupVersion().String(), c.cacheDir)
|
||||
return getSchemaAndValidate(c.c.AutoscalingClient.RESTClient, data, "apis/", gvk.GroupVersion().String(), c.cacheDir, c)
|
||||
}
|
||||
if gvk.Group == apps.GroupName {
|
||||
if c.c.AppsClient == nil {
|
||||
return errors.New("unable to validate: no autoscaling client")
|
||||
}
|
||||
return getSchemaAndValidate(c.c.AppsClient.RESTClient, data, "apis/", gvk.GroupVersion().String(), c.cacheDir)
|
||||
return getSchemaAndValidate(c.c.AppsClient.RESTClient, data, "apis/", gvk.GroupVersion().String(), c.cacheDir, c)
|
||||
}
|
||||
|
||||
if gvk.Group == batch.GroupName {
|
||||
if c.c.BatchClient == nil {
|
||||
return errors.New("unable to validate: no batch client")
|
||||
}
|
||||
return getSchemaAndValidate(c.c.BatchClient.RESTClient, data, "apis/", gvk.GroupVersion().String(), c.cacheDir)
|
||||
return getSchemaAndValidate(c.c.BatchClient.RESTClient, data, "apis/", gvk.GroupVersion().String(), c.cacheDir, c)
|
||||
}
|
||||
if registered.IsThirdPartyAPIGroupVersion(gvk.GroupVersion()) {
|
||||
// Don't attempt to validate third party objects
|
||||
@@ -911,9 +911,9 @@ func (c *clientSwaggerSchema) ValidateBytes(data []byte) error {
|
||||
if c.c.ExtensionsClient == nil {
|
||||
return errors.New("unable to validate: no experimental client")
|
||||
}
|
||||
return getSchemaAndValidate(c.c.ExtensionsClient.RESTClient, data, "apis/", gvk.GroupVersion().String(), c.cacheDir)
|
||||
return getSchemaAndValidate(c.c.ExtensionsClient.RESTClient, data, "apis/", gvk.GroupVersion().String(), c.cacheDir, c)
|
||||
}
|
||||
return getSchemaAndValidate(c.c.RESTClient, data, "api", gvk.GroupVersion().String(), c.cacheDir)
|
||||
return getSchemaAndValidate(c.c.RESTClient, data, "api", gvk.GroupVersion().String(), c.cacheDir, c)
|
||||
}
|
||||
|
||||
// DefaultClientConfig creates a clientcmd.ClientConfig with the following hierarchy:
|
||||
|
||||
@@ -198,7 +198,7 @@ func loadSchemaForTest() (validation.Schema, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return validation.NewSwaggerSchemaFromBytes(data)
|
||||
return validation.NewSwaggerSchemaFromBytes(data, nil)
|
||||
}
|
||||
|
||||
func TestRefetchSchemaWhenValidationFails(t *testing.T) {
|
||||
@@ -250,7 +250,7 @@ func TestRefetchSchemaWhenValidationFails(t *testing.T) {
|
||||
}
|
||||
|
||||
// Re-get request, should use HTTP and write
|
||||
if getSchemaAndValidate(c, data, "foo", "bar", dir); err != nil {
|
||||
if getSchemaAndValidate(c, data, "foo", "bar", dir, nil); err != nil {
|
||||
t.Errorf("unexpected error validating: %v", err)
|
||||
}
|
||||
if requests["/swaggerapi/foo/bar"] != 1 {
|
||||
@@ -295,7 +295,7 @@ func TestValidateCachesSchema(t *testing.T) {
|
||||
}
|
||||
|
||||
// Initial request, should use HTTP and write
|
||||
if getSchemaAndValidate(c, data, "foo", "bar", dir); err != nil {
|
||||
if getSchemaAndValidate(c, data, "foo", "bar", dir, nil); err != nil {
|
||||
t.Errorf("unexpected error validating: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(path.Join(dir, "foo", "bar", schemaFileName)); err != nil {
|
||||
@@ -306,7 +306,7 @@ func TestValidateCachesSchema(t *testing.T) {
|
||||
}
|
||||
|
||||
// Same version and group, should skip HTTP
|
||||
if getSchemaAndValidate(c, data, "foo", "bar", dir); err != nil {
|
||||
if getSchemaAndValidate(c, data, "foo", "bar", dir, nil); err != nil {
|
||||
t.Errorf("unexpected error validating: %v", err)
|
||||
}
|
||||
if requests["/swaggerapi/foo/bar"] != 2 {
|
||||
@@ -314,7 +314,7 @@ func TestValidateCachesSchema(t *testing.T) {
|
||||
}
|
||||
|
||||
// Different API group, should go to HTTP and write
|
||||
if getSchemaAndValidate(c, data, "foo", "baz", dir); err != nil {
|
||||
if getSchemaAndValidate(c, data, "foo", "baz", dir, nil); err != nil {
|
||||
t.Errorf("unexpected error validating: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(path.Join(dir, "foo", "baz", schemaFileName)); err != nil {
|
||||
@@ -325,7 +325,7 @@ func TestValidateCachesSchema(t *testing.T) {
|
||||
}
|
||||
|
||||
// Different version, should go to HTTP and write
|
||||
if getSchemaAndValidate(c, data, "foo2", "bar", dir); err != nil {
|
||||
if getSchemaAndValidate(c, data, "foo2", "bar", dir, nil); err != nil {
|
||||
t.Errorf("unexpected error validating: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(path.Join(dir, "foo2", "bar", schemaFileName)); err != nil {
|
||||
@@ -336,7 +336,7 @@ func TestValidateCachesSchema(t *testing.T) {
|
||||
}
|
||||
|
||||
// No cache dir, should go straight to HTTP and not write
|
||||
if getSchemaAndValidate(c, data, "foo", "blah", ""); err != nil {
|
||||
if getSchemaAndValidate(c, data, "foo", "blah", "", nil); err != nil {
|
||||
t.Errorf("unexpected error validating: %v", err)
|
||||
}
|
||||
if requests["/swaggerapi/foo/blah"] != 1 {
|
||||
|
||||
Reference in New Issue
Block a user