Skip to content

Commit

Permalink
skip namespace in resourceMeta for cluster scoped resource (#324)
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Qiu <[email protected]>
  • Loading branch information
qiujian16 authored Nov 29, 2023
1 parent 4a1a890 commit f89d535
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 14 deletions.
13 changes: 0 additions & 13 deletions pkg/registration/helpers/testing/testinghelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,6 @@ func NewManifestWork(namespace, name string, finalizers []string,
return work
}

func NewRole(namespace, name string, finalizers []string, labels map[string]string, terminated bool) *rbacv1.Role {
role := &rbacv1.Role{}
role.Namespace = namespace
role.Name = name
role.Finalizers = finalizers
role.Labels = labels
if terminated {
now := metav1.Now()
role.DeletionTimestamp = &now
}
return role
}

func NewRoleBinding(namespace, name string, finalizers []string, labels map[string]string, terminated bool) *rbacv1.RoleBinding {
rolebinding := &rbacv1.RoleBinding{}
rolebinding.Namespace = namespace
Expand Down
5 changes: 5 additions & 0 deletions pkg/work/helper/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,11 @@ func BuildResourceMeta(
return resourceMeta, schema.GroupVersionResource{}, fmt.Errorf("the server doesn't have a resource type %q", gvk.Kind)
}

// if resource scope is non-namespaces, set namespace to empty in resourceMeta
if mapping.Scope.Name() != meta.RESTScopeNameNamespace {
resourceMeta.Namespace = ""
}

resourceMeta.Resource = mapping.Resource.Resource
return resourceMeta, mapping.Resource, err
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/util/assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func AssertWorkCondition(namespace, name string, workClient workclientset.Interf

// check manifest status conditions
if ok := HaveManifestCondition(work.Status.ResourceStatus.Manifests, expectedType, expectedManifestStatuses); !ok {
return fmt.Errorf("condition %s does not exist", expectedType)
return fmt.Errorf("condition %s does not exist,got %v ", expectedType, work.Status.ResourceStatus.Manifests)
}

// check work status condition
Expand Down
30 changes: 30 additions & 0 deletions test/integration/util/unstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ var (
Resource: "roles",
}

clusterRoleGVK = schema.GroupVersionKind{
Group: "rbac.authorization.k8s.io",
Version: "v1",
Kind: "ClusterRole",
}

clusterRoleGVR = schema.GroupVersionResource{
Group: "rbac.authorization.k8s.io",
Version: "v1",
Resource: "clusterroles",
}

roleBindingGVK = schema.GroupVersionKind{
Group: "rbac.authorization.k8s.io",
Version: "v1",
Expand Down Expand Up @@ -241,6 +253,24 @@ func NewRole(namespace, name string) (*unstructured.Unstructured, schema.GroupVe
return toUnstructured(obj, roleGVK, scheme), roleGVR
}

func NewClusterRole(namespace, name string) (*unstructured.Unstructured, schema.GroupVersionResource) {
obj := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
},
Rules: []rbacv1.PolicyRule{
{
Verbs: []string{"create", "get", "list", "watch"},
APIGroups: []string{""},
Resources: []string{"configmaps"},
},
},
}

return toUnstructured(obj, clusterRoleGVK, scheme), clusterRoleGVR
}

func NewRoleBinding(namespace, name, sa, role string) (*unstructured.Unstructured, schema.GroupVersionResource) {
obj := &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Expand Down
41 changes: 41 additions & 0 deletions test/integration/work/work_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,47 @@ var _ = ginkgo.Describe("ManifestWork", func() {
})
})

ginkgo.Context("With cluster scoped resource in manifests", func() {
var spokeDynamicClient dynamic.Interface
var gvrs []schema.GroupVersionResource
var objects []*unstructured.Unstructured

ginkgo.BeforeEach(func() {
spokeDynamicClient, err = dynamic.NewForConfig(spokeRestConfig)
gvrs = nil
objects = nil

// Create a clusterrole with namespace in metadata
u, gvr := util.NewClusterRole(commOptions.SpokeClusterName, "work-clusterrole")
gvrs = append(gvrs, gvr)
objects = append(objects, u)

for _, obj := range objects {
manifests = append(manifests, util.ToManifest(obj))
}
})

ginkgo.It("should create Clusterrole successfully", func() {
util.AssertWorkCondition(work.Namespace, work.Name, hubWorkClient, workapiv1.WorkApplied, metav1.ConditionTrue,
[]metav1.ConditionStatus{metav1.ConditionTrue},
eventuallyTimeout, eventuallyInterval)
util.AssertWorkCondition(work.Namespace, work.Name, hubWorkClient, workapiv1.WorkAvailable, metav1.ConditionTrue,
[]metav1.ConditionStatus{metav1.ConditionTrue},
eventuallyTimeout, eventuallyInterval)

var namespaces, names []string
for _, obj := range objects {
// the namespace should be empty for cluster scoped resource
namespaces = append(namespaces, "")
names = append(names, obj.GetName())
}

util.AssertExistenceOfResources(gvrs, namespaces, names, spokeDynamicClient, eventuallyTimeout, eventuallyInterval)
util.AssertAppliedResources(hubHash, work.Name, gvrs, namespaces, names, hubWorkClient, eventuallyTimeout, eventuallyInterval)
})

})

ginkgo.Context("With Service Account, Role, RoleBinding and Deployment in manifests", func() {
var spokeDynamicClient dynamic.Interface
var gvrs []schema.GroupVersionResource
Expand Down

0 comments on commit f89d535

Please sign in to comment.