mirror of https://github.com/grafana/grafana
K8s/Playlist: Isolate apiGroup from server (#75321)
parent
042833376a
commit
e72b5c54f8
@ -0,0 +1,63 @@ |
||||
package apis |
||||
|
||||
import ( |
||||
"fmt" |
||||
"strconv" |
||||
"strings" |
||||
|
||||
"k8s.io/apimachinery/pkg/runtime" |
||||
"k8s.io/apimachinery/pkg/runtime/serializer" |
||||
genericapiserver "k8s.io/apiserver/pkg/server" |
||||
"k8s.io/kube-openapi/pkg/common" |
||||
"k8s.io/kube-openapi/pkg/spec3" |
||||
) |
||||
|
||||
// TODO: this (or something like it) belongs in grafana-app-sdk,
|
||||
// but lets keep it here while we iterate on a few simple examples
|
||||
type APIGroupBuilder interface { |
||||
// Add the kinds to the server scheme
|
||||
InstallSchema(scheme *runtime.Scheme) error |
||||
|
||||
// Build the group+version behavior
|
||||
GetAPIGroupInfo( |
||||
scheme *runtime.Scheme, |
||||
codecs serializer.CodecFactory, // pointer?
|
||||
) *genericapiserver.APIGroupInfo |
||||
|
||||
// Get OpenAPI definitions
|
||||
GetOpenAPIDefinitions() common.GetOpenAPIDefinitions |
||||
|
||||
// Register additional routes with the server
|
||||
GetOpenAPIPostProcessor() func(*spec3.OpenAPI) (*spec3.OpenAPI, error) |
||||
} |
||||
|
||||
func OrgIdToNamespace(orgId int64) string { |
||||
if orgId > 1 { |
||||
return fmt.Sprintf("org-%d", orgId) |
||||
} |
||||
return "default" |
||||
} |
||||
|
||||
func NamespaceToOrgID(ns string) (int64, error) { |
||||
parts := strings.Split(ns, "-") |
||||
switch len(parts) { |
||||
case 1: |
||||
if parts[0] == "default" { |
||||
return 1, nil |
||||
} |
||||
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)") |
||||
} |
||||
n, err := strconv.ParseInt(parts[1], 10, 64) |
||||
if err != nil { |
||||
return 0, fmt.Errorf("invalid namepscae (%w)", err) |
||||
} |
||||
return n, nil |
||||
} |
||||
return 0, fmt.Errorf("invalid namespace (%d parts)", len(parts)) |
||||
} |
@ -1,14 +0,0 @@ |
||||
package install |
||||
|
||||
import ( |
||||
"k8s.io/apimachinery/pkg/runtime" |
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
||||
|
||||
playlistv1 "github.com/grafana/grafana/pkg/apis/playlist/v1" |
||||
) |
||||
|
||||
// Install registers the API group and adds types to a scheme
|
||||
func Install(scheme *runtime.Scheme) { |
||||
utilruntime.Must(playlistv1.AddToScheme(scheme)) |
||||
utilruntime.Must(scheme.SetVersionPriority(playlistv1.SchemeGroupVersion)) |
||||
} |
@ -0,0 +1,115 @@ |
||||
package v1 |
||||
|
||||
import ( |
||||
common "k8s.io/kube-openapi/pkg/common" |
||||
spec "k8s.io/kube-openapi/pkg/validation/spec" |
||||
) |
||||
|
||||
// NOTE: this must match the golang fully qualifid name!
|
||||
const kindKey = "github.com/grafana/grafana/pkg/apis/playlist/v1.Playlist" |
||||
|
||||
func getOpenAPIDefinitions(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition { |
||||
return map[string]common.OpenAPIDefinition{ |
||||
kindKey: schema_pkg_Playlist(ref), |
||||
kindKey + "List": schema_pkg_PlaylistList(ref), |
||||
} |
||||
} |
||||
|
||||
func schema_pkg_Playlist(ref common.ReferenceCallback) common.OpenAPIDefinition { |
||||
return common.OpenAPIDefinition{ |
||||
Schema: spec.Schema{ |
||||
SchemaProps: spec.SchemaProps{ |
||||
Description: "Playlist", |
||||
Type: []string{"object"}, |
||||
Properties: map[string]spec.Schema{ |
||||
"kind": { |
||||
SchemaProps: spec.SchemaProps{ |
||||
Description: "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds", |
||||
Type: []string{"string"}, |
||||
Format: "", |
||||
}, |
||||
}, |
||||
"apiVersion": { |
||||
SchemaProps: spec.SchemaProps{ |
||||
Description: "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources", |
||||
Type: []string{"string"}, |
||||
Format: "", |
||||
}, |
||||
}, |
||||
"metadata": { |
||||
SchemaProps: spec.SchemaProps{ |
||||
Default: map[string]interface{}{}, |
||||
Ref: ref("k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"), |
||||
}, |
||||
}, |
||||
// TODO! add a real spec here
|
||||
// "spec": {
|
||||
// SchemaProps: spec.SchemaProps{
|
||||
// Default: map[string]interface{}{},
|
||||
// Ref: ref("github.com/grafana/google-sheets-datasource/pkg/apis/googlesheets/v1.DatasourceSpec"),
|
||||
// },
|
||||
// },
|
||||
// "status": {
|
||||
// SchemaProps: spec.SchemaProps{
|
||||
// Default: map[string]interface{}{},
|
||||
// Ref: ref("github.com/grafana/google-sheets-datasource/pkg/apis/googlesheets/v1.DatasourceStatus"),
|
||||
// },
|
||||
// },
|
||||
}, |
||||
}, |
||||
}, |
||||
Dependencies: []string{ |
||||
"k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta", |
||||
}, |
||||
} |
||||
} |
||||
|
||||
func schema_pkg_PlaylistList(ref common.ReferenceCallback) common.OpenAPIDefinition { |
||||
return common.OpenAPIDefinition{ |
||||
Schema: spec.Schema{ |
||||
SchemaProps: spec.SchemaProps{ |
||||
Description: "PlaylistList", |
||||
Type: []string{"object"}, |
||||
Properties: map[string]spec.Schema{ |
||||
"kind": { |
||||
SchemaProps: spec.SchemaProps{ |
||||
Description: "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds", |
||||
Type: []string{"string"}, |
||||
Format: "", |
||||
}, |
||||
}, |
||||
"apiVersion": { |
||||
SchemaProps: spec.SchemaProps{ |
||||
Description: "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources", |
||||
Type: []string{"string"}, |
||||
Format: "", |
||||
}, |
||||
}, |
||||
"metadata": { |
||||
SchemaProps: spec.SchemaProps{ |
||||
Default: map[string]interface{}{}, |
||||
Ref: ref("k8s.io/apimachinery/pkg/apis/meta/v1.ListMeta"), |
||||
}, |
||||
}, |
||||
"items": { |
||||
SchemaProps: spec.SchemaProps{ |
||||
Type: []string{"array"}, |
||||
Items: &spec.SchemaOrArray{ |
||||
Schema: &spec.Schema{ |
||||
SchemaProps: spec.SchemaProps{ |
||||
Default: map[string]interface{}{}, |
||||
Ref: ref(kindKey), |
||||
}, |
||||
}, |
||||
}, |
||||
}, |
||||
}, |
||||
}, |
||||
Required: []string{"items"}, |
||||
}, |
||||
}, |
||||
Dependencies: []string{ |
||||
kindKey, |
||||
"k8s.io/apimachinery/pkg/apis/meta/v1.ListMeta"}, |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue