@@ -2,7 +2,6 @@ package utils
22
33import (
44 "context"
5-
65 "github.com/beclab/oachecker"
76 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
87 "k8s.io/apimachinery/pkg/runtime/schema"
@@ -37,7 +36,8 @@ func GetAdminUsername(ctx context.Context) (string, error) {
3736 continue
3837 }
3938 annotations := u .GetAnnotations ()
40- if annotations ["bytetrade.io/owner-role" ] == "platform-admin" {
39+ ownerRole := annotations ["bytetrade.io/owner-role" ]
40+ if ownerRole == "owner" || ownerRole == "admin" {
4141 admin = u .GetName ()
4242 break
4343 }
@@ -51,10 +51,72 @@ func GetAppConfig(owner string, data []byte) (*oachecker.AppConfiguration, error
5151 if err != nil {
5252 return nil , err
5353 }
54+ isAdmin , err := IsAdmin (context .TODO (), owner )
55+ if err != nil {
56+ return nil , err
57+ }
58+
5459 opts := []func (map [string ]interface {}){
5560 oachecker .WithAdmin (admin ),
5661 oachecker .WithOwner (owner ),
62+ WithIsAdmin (isAdmin ),
5763 }
5864 appcfg , err := oachecker .GetAppConfigurationFromContent (data , opts ... )
5965 return appcfg , nil
6066}
67+ func WithIsAdmin (isAdmin bool ) func (map [string ]interface {}) {
68+ return func (values map [string ]interface {}) {
69+ values ["isAdmin" ] = isAdmin
70+
71+ }
72+ }
73+
74+ // GetAdminUserList returns admin list, an error if there is any.
75+ func GetAdminUserList (ctx context.Context ) ([]string , error ) {
76+ adminUserList := make ([]string , 0 )
77+
78+ gvr := schema.GroupVersionResource {
79+ Group : "iam.kubesphere.io" ,
80+ Version : "v1alpha2" ,
81+ Resource : "users" ,
82+ }
83+ kubeConfig , err := ctrl .GetConfig ()
84+ if err != nil {
85+ return adminUserList , err
86+ }
87+ client , err := dynamic .NewForConfig (kubeConfig )
88+ if err != nil {
89+ return adminUserList , err
90+ }
91+ data , err := client .Resource (gvr ).List (ctx , metav1.ListOptions {})
92+ if err != nil {
93+ klog .Errorf ("Failed to get user list err=%v" , err )
94+ return adminUserList , err
95+ }
96+
97+ for _ , u := range data .Items {
98+ if u .Object == nil {
99+ continue
100+ }
101+ annotations := u .GetAnnotations ()
102+ role := annotations ["bytetrade.io/owner-role" ]
103+ if role == "owner" || role == "admin" {
104+ adminUserList = append (adminUserList , u .GetName ())
105+ }
106+ }
107+
108+ return adminUserList , nil
109+ }
110+
111+ func IsAdmin (ctx context.Context , owner string ) (bool , error ) {
112+ adminList , err := GetAdminUserList (ctx )
113+ if err != nil {
114+ return false , err
115+ }
116+ for _ , user := range adminList {
117+ if user == owner {
118+ }
119+ return true , nil
120+ }
121+ return false , nil
122+ }
0 commit comments