The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/services/apiserver/endpoints/request/namespace.go

51 lines
1.3 KiB

package request
import (
"context"
"fmt"
"strconv"
"k8s.io/apiserver/pkg/endpoints/request"
"github.com/grafana/authlib/claims"
"github.com/grafana/grafana/pkg/apimachinery/identity"
"github.com/grafana/grafana/pkg/setting"
)
// NamespaceMapper converts an orgID into a namespace
type NamespaceMapper = claims.NamespaceFormatter
// GetNamespaceMapper returns a function that will convert orgIds into a consistent namespace
func GetNamespaceMapper(cfg *setting.Cfg) NamespaceMapper {
if cfg != nil && cfg.StackID != "" {
stackId, err := strconv.ParseInt(cfg.StackID, 10, 64)
if err != nil {
stackId = 0
}
cloudNamespace := claims.CloudNamespaceFormatter(stackId)
return func(_ int64) string { return cloudNamespace }
}
return claims.OrgNamespaceFormatter
}
func NamespaceInfoFrom(ctx context.Context, requireOrgID bool) (claims.NamespaceInfo, error) {
info, err := claims.ParseNamespace(request.NamespaceValue(ctx))
if err == nil && requireOrgID && info.OrgID < 1 {
return info, fmt.Errorf("expected valid orgId in namespace")
}
return info, err
}
func OrgIDForList(ctx context.Context) (int64, error) {
ns := request.NamespaceValue(ctx)
if ns == "" {
user, err := identity.GetRequester(ctx)
if user != nil {
return user.GetOrgID(), err
}
return -1, err
}
info, err := claims.ParseNamespace(ns)
return info.OrgID, err
}