Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Including namespace whiteliste resources support #3292

Merged
merged 4 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/apis/application/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,8 @@ type AppProjectSpec struct {
OrphanedResources *OrphanedResourcesMonitorSettings `json:"orphanedResources,omitempty" protobuf:"bytes,7,opt,name=orphanedResources"`
// SyncWindows controls when syncs can be run for apps in this project
SyncWindows SyncWindows `json:"syncWindows,omitempty" protobuf:"bytes,8,opt,name=syncWindows"`
// NamespaceResourceWhitelist contains list of blacklisted namespace level resources
alexmt marked this conversation as resolved.
Show resolved Hide resolved
NamespaceResourceWhitelist []metav1.GroupKind `json:"namespaceResourceWhitelist,omitempty" protobuf:"bytes,9,opt,name=namespaceResourceWhitelist"`
}

// SyncWindows is a collection of sync windows in this project
Expand Down Expand Up @@ -2127,7 +2129,9 @@ func isResourceInList(res metav1.GroupKind, list []metav1.GroupKind) bool {
func (proj AppProject) IsGroupKindPermitted(gk schema.GroupKind, namespaced bool) bool {
res := metav1.GroupKind{Group: gk.Group, Kind: gk.Kind}
if namespaced {
return !isResourceInList(res, proj.Spec.NamespaceResourceBlacklist)
isWhiteListed := len(proj.Spec.NamespaceResourceWhitelist) == 0 || isResourceInList(res, proj.Spec.NamespaceResourceWhitelist)
isBlackListed := isResourceInList(res, proj.Spec.NamespaceResourceBlacklist)
return isWhiteListed && !isBlackListed
} else {
return isResourceInList(res, proj.Spec.ClusterResourceWhitelist)
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/apis/application/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -115,6 +116,25 @@ func TestAppProject_IsDestinationPermitted(t *testing.T) {
}
}

func TestAppProject_IsGroupKindPermitted(t *testing.T) {
proj := AppProject{
Spec: AppProjectSpec{
NamespaceResourceWhitelist: []metav1.GroupKind{},
NamespaceResourceBlacklist: []metav1.GroupKind{{Group: "apps", Kind: "Deployment"},},
},
}
assert.Equal(t, proj.IsGroupKindPermitted(schema.GroupKind{Group: "apps", Kind: "ReplicaSet"}, true), true)
alexmt marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, proj.IsGroupKindPermitted(schema.GroupKind{Group: "apps", Kind: "Deployment"}, true), false)
proj2 := AppProject{
Spec: AppProjectSpec{
NamespaceResourceWhitelist: []metav1.GroupKind{{Group: "apps", Kind: "ReplicaSet"}},
NamespaceResourceBlacklist: []metav1.GroupKind{{Group: "apps", Kind: "Deployment"},},
},
}
assert.Equal(t, proj2.IsGroupKindPermitted(schema.GroupKind{Group: "apps", Kind: "ReplicaSet"}, true), true)
assert.Equal(t, proj2.IsGroupKindPermitted(schema.GroupKind{Group: "apps", Kind: "Action"}, true), false)
}

func TestAppProject_GetRoleByName(t *testing.T) {
t.Run("NotExists", func(t *testing.T) {
p := &AppProject{}
Expand Down