@@ -18,10 +18,16 @@ package cluster
1818
1919import (
2020 "context"
21+ "fmt"
22+ "strings"
2123
24+ kmapi "kmodules.xyz/client-go/api/v1"
25+
26+ core "k8s.io/api/core/v1"
2227 "k8s.io/apimachinery/pkg/api/meta"
2328 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2429 "k8s.io/apimachinery/pkg/runtime/schema"
30+ "k8s.io/apiserver/pkg/authentication/user"
2531 "sigs.k8s.io/controller-runtime/pkg/client"
2632)
2733
@@ -53,3 +59,42 @@ func IsOpenClusterMulticlusterControlplane(mapper meta.RESTMapper) bool {
5359 }
5460 return IsOpenClusterHub (mapper ) && missingDeployment
5561}
62+
63+ type ClientOrgResult struct {
64+ IsClientOrg bool
65+ OrgID string
66+ Namespace core.Namespace
67+ }
68+
69+ func IsClientOrgMember (kc client.Client , user user.Info ) (* ClientOrgResult , error ) {
70+ orgs , exists := user .GetExtra ()[kmapi .AceOrgIDKey ]
71+ if ! exists || len (orgs ) == 0 {
72+ return & ClientOrgResult {}, nil
73+ }
74+ if len (orgs ) > 1 {
75+ return nil , fmt .Errorf ("user %s associated with multiple orgs %v" , user .GetName (), orgs )
76+ }
77+
78+ var list core.NamespaceList
79+ if err := kc .List (context .TODO (), & list , client.MatchingLabels {
80+ kmapi .ClientOrgKey : "true" ,
81+ }); err != nil {
82+ return nil , err
83+ }
84+
85+ for _ , ns := range list .Items {
86+ if ns .Annotations [kmapi .AceOrgIDKey ] == orgs [0 ] {
87+ return & ClientOrgResult {
88+ IsClientOrg : true ,
89+ OrgID : orgs [0 ],
90+ Namespace : ns ,
91+ }, nil
92+ }
93+ }
94+ return & ClientOrgResult {}, nil
95+ }
96+
97+ func ClientDashboardTitle (title string ) string {
98+ title = strings .TrimPrefix (title , "KubeDB /" )
99+ return strings .TrimSpace (title )
100+ }
0 commit comments