github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/operatorclient/role.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 "k8s.io/klog" 12 ) 13 14 // CreateRole creates the role or Updates if it already exists. 15 func (c *Client) CreateRole(r *rbacv1.Role) (*rbacv1.Role, error) { 16 createdRole, err := c.RbacV1().Roles(r.GetNamespace()).Create(context.TODO(), r, metav1.CreateOptions{}) 17 if apierrors.IsAlreadyExists(err) { 18 return c.UpdateRole(r) 19 } 20 return createdRole, err 21 } 22 23 // GetRole returns the existing role. 24 func (c *Client) GetRole(namespace, name string) (*rbacv1.Role, error) { 25 return c.RbacV1().Roles(namespace).Get(context.TODO(), name, metav1.GetOptions{}) 26 } 27 28 // DeleteRole deletes the role. 29 func (c *Client) DeleteRole(namespace, name string, options *metav1.DeleteOptions) error { 30 return c.RbacV1().Roles(namespace).Delete(context.TODO(), name, *options) 31 } 32 33 // UpdateRole will update the given Role resource. 34 func (c *Client) UpdateRole(crb *rbacv1.Role) (*rbacv1.Role, error) { 35 klog.V(4).Infof("[UPDATE Role]: %s", crb.GetName()) 36 oldCrb, err := c.GetRole(crb.GetNamespace(), crb.GetName()) 37 if err != nil { 38 return nil, err 39 } 40 patchBytes, err := createPatch(oldCrb, crb) 41 if err != nil { 42 return nil, fmt.Errorf("error creating patch for Role: %v", err) 43 } 44 return c.RbacV1().Roles(crb.GetNamespace()).Patch(context.TODO(), crb.GetName(), types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}) 45 }