Skip to content

Commit

Permalink
Add IsClientOrgMember utility (#616)
Browse files Browse the repository at this point in the history
Signed-off-by: Arnob Kumar Saha <[email protected]>
  • Loading branch information
ArnobKumarSaha authored Feb 7, 2025
1 parent 7f3619f commit 9610d76
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cluster/ocm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ package cluster

import (
"context"
"fmt"

kmapi "kmodules.xyz/client-go/api/v1"

core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/authentication/user"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -53,3 +58,27 @@ func IsOpenClusterMulticlusterControlplane(mapper meta.RESTMapper) bool {
}
return IsOpenClusterHub(mapper) && missingDeployment
}

func IsClientOrgMember(kc client.Client, user user.Info) (bool, string, error) {
orgs, exists := user.GetExtra()[kmapi.AceOrgIDKey]
if !exists || len(orgs) == 0 {
return false, "", nil
}
if len(orgs) > 1 {
return false, "", fmt.Errorf("user %s associated with multiple orgs %v", user.GetName(), orgs)
}

var list core.NamespaceList
if err := kc.List(context.TODO(), &list, client.MatchingLabels{
kmapi.ClientOrgKey: "true",
}); err != nil {
return false, "", err
}

for _, ns := range list.Items {
if ns.Annotations[kmapi.AceOrgIDKey] == orgs[0] {
return true, orgs[0], nil
}
}
return false, "", nil
}

0 comments on commit 9610d76

Please sign in to comment.