github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/operatorclient/clusterrole.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 // CreateClusterRole creates the ClusterRole or Updates if it already exists. 15 func (c *Client) CreateClusterRole(r *rbacv1.ClusterRole) (*rbacv1.ClusterRole, error) { 16 createdClusterRole, err := c.RbacV1().ClusterRoles().Create(context.TODO(), r, metav1.CreateOptions{}) 17 if apierrors.IsAlreadyExists(err) { 18 return c.UpdateClusterRole(r) 19 } 20 return createdClusterRole, err 21 } 22 23 // GetClusterRole returns the existing ClusterRole. 24 func (c *Client) GetClusterRole(name string) (*rbacv1.ClusterRole, error) { 25 return c.RbacV1().ClusterRoles().Get(context.TODO(), name, metav1.GetOptions{}) 26 } 27 28 // DeleteClusterRole deletes the ClusterRole 29 func (c *Client) DeleteClusterRole(name string, options *metav1.DeleteOptions) error { 30 return c.RbacV1().ClusterRoles().Delete(context.TODO(), name, *options) 31 } 32 33 // UpdateClusterRole will update the given ClusterRole. 34 func (c *Client) UpdateClusterRole(crb *rbacv1.ClusterRole) (*rbacv1.ClusterRole, error) { 35 klog.V(4).Infof("[UPDATE Role]: %s", crb.GetName()) 36 oldCrb, err := c.GetClusterRole(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().ClusterRoles().Patch(context.TODO(), crb.GetName(), types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}) 45 }