github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/operatorclient/clusterrolebinding.go (about)

     1  package operatorclient
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	rbacv1 "k8s.io/api/rbac/v1"
     8  	apierrors "k8s.io/apimachinery/pkg/api/errors"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/apimachinery/pkg/types"
    11  	acv1 "k8s.io/client-go/applyconfigurations/rbac/v1"
    12  	"k8s.io/klog"
    13  )
    14  
    15  // ApplyClusterRoleBinding applies the roleBinding.
    16  func (c *Client) ApplyClusterRoleBinding(applyConfig *acv1.ClusterRoleBindingApplyConfiguration, applyOptions metav1.ApplyOptions) (*rbacv1.ClusterRoleBinding, error) {
    17  	return c.RbacV1().ClusterRoleBindings().Apply(context.TODO(), applyConfig, applyOptions)
    18  }
    19  
    20  // CreateRoleBinding creates the roleBinding or Updates if it already exists.
    21  func (c *Client) CreateClusterRoleBinding(ig *rbacv1.ClusterRoleBinding) (*rbacv1.ClusterRoleBinding, error) {
    22  	createdCRB, err := c.RbacV1().ClusterRoleBindings().Create(context.TODO(), ig, metav1.CreateOptions{})
    23  	if apierrors.IsAlreadyExists(err) {
    24  		return c.UpdateClusterRoleBinding(ig)
    25  	}
    26  	return createdCRB, err
    27  }
    28  
    29  // GetRoleBinding returns the existing roleBinding.
    30  func (c *Client) GetClusterRoleBinding(name string) (*rbacv1.ClusterRoleBinding, error) {
    31  	return c.RbacV1().ClusterRoleBindings().Get(context.TODO(), name, metav1.GetOptions{})
    32  }
    33  
    34  // DeleteRoleBinding deletes the roleBinding.
    35  func (c *Client) DeleteClusterRoleBinding(name string, options *metav1.DeleteOptions) error {
    36  	return c.RbacV1().ClusterRoleBindings().Delete(context.TODO(), name, *options)
    37  }
    38  
    39  // UpdateRoleBinding will update the given RoleBinding resource.
    40  func (c *Client) UpdateClusterRoleBinding(crb *rbacv1.ClusterRoleBinding) (*rbacv1.ClusterRoleBinding, error) {
    41  	klog.V(4).Infof("[UPDATE RoleBinding]: %s", crb.GetName())
    42  	oldCrb, err := c.GetClusterRoleBinding(crb.GetName())
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	patchBytes, err := createPatch(oldCrb, crb)
    47  	if err != nil {
    48  		return nil, fmt.Errorf("error creating patch for RoleBinding: %v", err)
    49  	}
    50  	return c.RbacV1().ClusterRoleBindings().Patch(context.TODO(), crb.GetName(), types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{})
    51  }