Implement a GetRoleBindings() feature
Mo0rBy opened this issue · comments
Describe the solution you'd like
Very simple. Just to create GetRoleBindings()
function that can be used just like the GetRole()
function.
Describe alternatives you've considered
I haven't considered any alternatives as I think this is a really simple task, just need to plug things in really.
Additional context
Was writing some tests and realised that this function doesn't actually exist. I had a quick look at how GetRole()
is implemented and I think this GetRoleBinding()
function can be implemented in the exact same way. I had a look at the contribution docs and I believe this is something I can do quite easily. See below for a description of "plugging things in" as I said earlier.
Here's the GetRole()
functions:
package k8s
import (
"context"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GetRole returns a Kubernetes role resource in the provided namespace with the given name. The namespace used
// is the one provided in the KubectlOptions. This will fail the test if there is an error.
func GetRole(t testing.TestingT, options *KubectlOptions, roleName string) *rbacv1.Role {
role, err := GetRoleE(t, options, roleName)
require.NoError(t, err)
return role
}
// GetRoleE returns a Kubernetes role resource in the provided namespace with the given name. The namespace used
// is the one provided in the KubectlOptions.
func GetRoleE(t testing.TestingT, options *KubectlOptions, roleName string) (*rbacv1.Role, error) {
clientset, err := GetKubernetesClientFromOptionsE(t, options)
if err != nil {
return nil, err
}
return clientset.RbacV1().Roles(options.Namespace).Get(context.Background(), roleName, metav1.GetOptions{})
}
The RbacV1()
function (see final return in above code block) returns the RbacV1Interface
. This interface includes a return function for the RoleBindingInterface
:
type RbacV1Interface interface {
RESTClient() rest.Interface
ClusterRolesGetter
ClusterRoleBindingsGetter
RolesGetter
RoleBindingsGetter
}
// RbacV1Client is used to interact with features provided by the rbac.authorization.k8s.io group.
type RbacV1Client struct {
restClient rest.Interface
}
func (c *RbacV1Client) ClusterRoles() ClusterRoleInterface {
return newClusterRoles(c)
}
func (c *RbacV1Client) ClusterRoleBindings() ClusterRoleBindingInterface {
return newClusterRoleBindings(c)
}
func (c *RbacV1Client) Roles(namespace string) RoleInterface {
return newRoles(c, namespace)
}
func (c *RbacV1Client) RoleBindings(namespace string) RoleBindingInterface {
return newRoleBindings(c, namespace)
}
Therefore, I can just create 2 new functions for GetRoleBinding that look something like this (just writing this up quickly before I go to sleep, so I'm not confident this is correct, just giving any readers the main idea):
// GetRoleBinding returns a Kubernetes roleBinding resource in the provided namespace with the given name. The namespace used
// is the one provided in the KubectlOptions. This will fail the test if there is an error.
func GetRoleBinfing(t testing.TestingT, options *KubectlOptions, roleBindingName string) *rbacv1.RoleBinding {
role, err := GetRoleBindingE(t, options, roleName)
require.NoError(t, err)
return role
}
// GetRoleBindingE returns a Kubernetes roleBinding resource in the provided namespace with the given name. The namespace used
// is the one provided in the KubectlOptions.
func GetRoleBindingE(t testing.TestingT, options *KubectlOptions, roleBindingName string) (*rbacv1.RoleBinding, error) {
clientset, err := GetKubernetesClientFromOptionsE(t, options)
if err != nil {
return nil, err
}
return clientset.RbacV1().RoleBindings(options.Namespace).Get(context.Background(), roleName, metav1.GetOptions{})
}
As I've already said, I'd be happy to do all the work for this improvement, I've a brief glance at the contribution docs already.