forked from open-cluster-management-io/ocm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
the manifest size limit can be configured (open-cluster-management-io…
…#186) Signed-off-by: Zhiwei Yin <zyin@redhat.com>
- Loading branch information
1 parent
ed501f7
commit 9a872eb
Showing
10 changed files
with
149 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
workv1 "open-cluster-management.io/api/work/v1" | ||
) | ||
|
||
type Validator struct { | ||
limit int | ||
} | ||
|
||
var ManifestValidator = &Validator{limit: 500 * 1024} // the default manifest limit is 500k. | ||
|
||
func (m *Validator) WithLimit(limit int) { | ||
m.limit = limit | ||
} | ||
|
||
func (m *Validator) ValidateManifests(manifests []workv1.Manifest) error { | ||
if len(manifests) == 0 { | ||
return apierrors.NewBadRequest("Workload manifests should not be empty") | ||
} | ||
|
||
totalSize := 0 | ||
for _, manifest := range manifests { | ||
totalSize = totalSize + manifest.Size() | ||
} | ||
|
||
if totalSize > m.limit { | ||
return fmt.Errorf("the size of manifests is %v bytes which exceeds the %v limit", totalSize, m.limit) | ||
} | ||
|
||
for _, manifest := range manifests { | ||
err := validateManifest(manifest.Raw) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func validateManifest(manifest []byte) error { | ||
// If the manifest cannot be decoded, return err | ||
unstructuredObj := &unstructured.Unstructured{} | ||
err := unstructuredObj.UnmarshalJSON(manifest) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// The object must have name specified, generateName is not allowed in manifestwork | ||
if unstructuredObj.GetName() == "" { | ||
return fmt.Errorf("name must be set in manifest") | ||
} | ||
|
||
if unstructuredObj.GetGenerateName() != "" { | ||
return fmt.Errorf("generateName must not be set in manifest") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
workv1 "open-cluster-management.io/api/work/v1" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func newManifest(size int) workv1.Manifest { | ||
data := "" | ||
for i := 0; i < size; i++ { | ||
data += "a" | ||
} | ||
|
||
obj := &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"apiVersion": "v1", | ||
"kind": "Secret", | ||
"metadata": map[string]interface{}{ | ||
"namespace": "test", | ||
"name": "test", | ||
}, | ||
"data": data, | ||
}, | ||
} | ||
objectStr, _ := obj.MarshalJSON() | ||
manifest := workv1.Manifest{} | ||
manifest.Raw = objectStr | ||
return manifest | ||
} | ||
func Test_Validator(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
manifests []workv1.Manifest | ||
expectedError error | ||
}{ | ||
{ | ||
name: "not exceed the limit", | ||
manifests: []workv1.Manifest{newManifest(100 * 1024), newManifest(100 * 1024)}, | ||
expectedError: nil, | ||
}, | ||
{ | ||
name: "exceed the limit", | ||
manifests: []workv1.Manifest{newManifest(300 * 1024), newManifest(200 * 1024)}, | ||
expectedError: fmt.Errorf("the size of manifests is 512192 bytes which exceeds the 512000 limit"), | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
err := ManifestValidator.ValidateManifests(c.manifests) | ||
if !reflect.DeepEqual(err, c.expectedError) { | ||
t.Errorf("expected %#v but got: %#v", c.expectedError, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.