Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/k8s-grafana-apiserver' into sdbo…
Browse files Browse the repository at this point in the history
…yer/kindsys-admission-controller
  • Loading branch information
ryantxu committed Jul 10, 2023
2 parents d6a0b40 + e7e0483 commit f8ffd29
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/services/k8s/apiserver/entitystorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ func (s *entityStorage) Watch(ctx context.Context, key string, opts storage.List

switch s.gr.Group {
// NOTE: this first case is currently not active as we are delegating GRD storage to filepath implementation
// this is copied from filestorage, currently not active since we are letting file storage handle GRDs
case grafanaApiServerKinds.GroupName:
listObj = &grafanaApiServerKinds.GrafanaResourceDefinitionList{}
break
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/k8s/apiserver/restoptionsgetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func ProvideRESTOptionsGetter(cfg *setting.Cfg, features featuremgmt.FeatureTogg
}

func (f *RESTOptionsGetter) GetRESTOptions(resource schema.GroupResource) (generic.RESTOptions, error) {
if resource.Resource == "grafanakinds" {
if resource.Resource == "grafanaresourcedefinitions" {
return f.fallback.GetRESTOptions(resource)
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/services/k8s/apiserver/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func contextWithFakeGrafanaUser(ctx context.Context) (context.Context, error) {
user.OrgID = 1
user.UserID = 1
}

v, ok := info.GetExtra()["user-id"]
if ok && len(v) > 0 {
user.UserID, err = strconv.ParseInt(v[0], 10, 64)
Expand All @@ -139,16 +140,17 @@ func contextWithFakeGrafanaUser(ctx context.Context) (context.Context, error) {
// return nil, fmt.Errorf("insufficient information on user context, couldn't determine UserID and OrgID")
}

// HACK alert... change to the reqested org
// HACK alert... change to the requested org
// TODO: should validate that user has access to that org/tenant
ns, ok := request.NamespaceFrom(ctx)
if ok {
if ok && ns != "" {
nsorg, err := util.NamespaceToOrgID(ns)
if err != nil {
return nil, err
}
user.OrgID = nsorg
}

return appcontext.WithUser(ctx, user), nil
}

Expand Down
7 changes: 5 additions & 2 deletions pkg/util/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ func NamespaceToOrgID(ns string) (int64, error) {
if parts[0] == "default" {
return 1, nil
}
return 0, fmt.Errorf("invalid namespace")
if parts[0] == "" {
return 0, nil // no orgId, cluster scope
}
return 0, fmt.Errorf("invalid namespace (expected default)")
case 2:
if !(parts[0] == "org" || parts[0] == "tenant") {
return 0, fmt.Errorf("invalid namespace (org|tenant)")
Expand All @@ -31,5 +34,5 @@ func NamespaceToOrgID(ns string) (int64, error) {
}
return n, nil
}
return 0, fmt.Errorf("invalid namespace")
return 0, fmt.Errorf("invalid namespace (%d parts)", len(parts))
}

0 comments on commit f8ffd29

Please sign in to comment.