parent
ed4437ad22
commit
6307a43004
|
@ -63,22 +63,25 @@ func GetResourceModifiersFromConfig(cm *v1.ConfigMap) (*ResourceModifiers, error
|
|||
return resModifiers, nil
|
||||
}
|
||||
|
||||
func (p *ResourceModifiers) ApplyResourceModifierRules(obj *unstructured.Unstructured, log logrus.FieldLogger) []error {
|
||||
func (p *ResourceModifiers) ApplyResourceModifierRules(obj *unstructured.Unstructured, groupResource string, log logrus.FieldLogger) []error {
|
||||
var errs []error
|
||||
for _, rule := range p.ResourceModifierRules {
|
||||
errs = append(errs, rule.Apply(obj, log))
|
||||
err := rule.Apply(obj, groupResource, log)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
func (r *ResourceModifierRule) Apply(obj *unstructured.Unstructured, log logrus.FieldLogger) error {
|
||||
func (r *ResourceModifierRule) Apply(obj *unstructured.Unstructured, groupResource string, log logrus.FieldLogger) error {
|
||||
namespaceInclusion := collections.NewIncludesExcludes().Includes(r.Conditions.Namespaces...)
|
||||
if !namespaceInclusion.ShouldInclude(obj.GetNamespace()) {
|
||||
return nil
|
||||
}
|
||||
if !strings.EqualFold(obj.GroupVersionKind().GroupKind().String(), r.Conditions.GroupKind) {
|
||||
return nil
|
||||
if !strings.EqualFold(groupResource, r.Conditions.GroupKind) {
|
||||
return nil
|
||||
}
|
||||
if r.Conditions.ResourceNameRegex != "" {
|
||||
match, _ := regexp.MatchString(r.Conditions.ResourceNameRegex, obj.GetName())
|
||||
|
|
|
@ -60,24 +60,41 @@ func TestGetResourceModifiersFromConfig(t *testing.T) {
|
|||
assert.Equal(t, original, expected)
|
||||
}
|
||||
|
||||
// pvc1 := &unstructured.Unstructured{
|
||||
// Object: map[string]interface{}{
|
||||
// "kind": "PersistentVolumeClaim",
|
||||
// "metadata": map[string]interface{}{
|
||||
// "name": "test-pvc",
|
||||
// "namespace": "foo",
|
||||
// },
|
||||
// },
|
||||
// }
|
||||
|
||||
func TestResourceModifiers_ApplyResourceModifierRules(t *testing.T) {
|
||||
|
||||
pvcStandardSc := &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"kind": "PersistentVolumeClaim",
|
||||
"metadata": map[string]interface{}{
|
||||
"name": "test-pvc",
|
||||
"namespace": "foo",
|
||||
},
|
||||
"spec": map[string]interface{}{
|
||||
"storageClassName": "standard",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
pvcPremiumSc := &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"kind": "PersistentVolumeClaim",
|
||||
"metadata": map[string]interface{}{
|
||||
"name": "test-pvc",
|
||||
"namespace": "foo",
|
||||
},
|
||||
"spec": map[string]interface{}{
|
||||
"storageClassName": "premium",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
type fields struct {
|
||||
Version string
|
||||
ResourceModifierRules []ResourceModifierRule
|
||||
}
|
||||
type args struct {
|
||||
obj *unstructured.Unstructured
|
||||
log logrus.FieldLogger
|
||||
obj *unstructured.Unstructured
|
||||
groupResource string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
|
@ -86,7 +103,34 @@ func TestResourceModifiers_ApplyResourceModifierRules(t *testing.T) {
|
|||
want []error
|
||||
wantObj *unstructured.Unstructured
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
{
|
||||
name: "test1",
|
||||
fields: fields{
|
||||
Version: "v1",
|
||||
ResourceModifierRules: []ResourceModifierRule{
|
||||
{
|
||||
Conditions: Conditions{
|
||||
GroupKind: "persistentvolumeclaims",
|
||||
ResourceNameRegex: ".*",
|
||||
Namespaces: []string{"foo"},
|
||||
},
|
||||
Patches: []JsonPatch{
|
||||
{
|
||||
Operation: "replace",
|
||||
Path: "/spec/storageClassName",
|
||||
NewValue: "premium",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
obj: pvcStandardSc.DeepCopy(),
|
||||
groupResource: "persistentvolumeclaims",
|
||||
},
|
||||
want: nil,
|
||||
wantObj: pvcPremiumSc.DeepCopy(),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
@ -94,10 +138,10 @@ func TestResourceModifiers_ApplyResourceModifierRules(t *testing.T) {
|
|||
Version: tt.fields.Version,
|
||||
ResourceModifierRules: tt.fields.ResourceModifierRules,
|
||||
}
|
||||
// comapre lenght of got and want {
|
||||
got := p.ApplyResourceModifierRules(tt.args.obj, tt.args.log)
|
||||
assert.Equal(t, len(got), len(tt.want))
|
||||
assert.Equal(t, *tt.args.obj, *tt.wantObj)
|
||||
got := p.ApplyResourceModifierRules(tt.args.obj, tt.args.groupResource, logrus.New())
|
||||
|
||||
assert.Equal(t, len(tt.want), len(got))
|
||||
assert.Equal(t, *tt.wantObj, *tt.args.obj)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1344,7 +1344,7 @@ func (ctx *restoreContext) restoreItem(obj *unstructured.Unstructured, groupReso
|
|||
addRestoreLabels(obj, ctx.restore.Name, ctx.restore.Spec.BackupName)
|
||||
|
||||
if ctx.resourceModifiers != nil {
|
||||
if errList := ctx.resourceModifiers.ApplyResourceModifierRules(obj, ctx.log); errList != nil {
|
||||
if errList := ctx.resourceModifiers.ApplyResourceModifierRules(obj, groupResource.String(), ctx.log); errList != nil {
|
||||
for _, err := range errList {
|
||||
errs.Add(namespace, err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue