mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 02:10:45 -06:00
Chore: Add basic admission handlers for registered API services (#89824)
This commit is contained in:
parent
f18da6f4dc
commit
f41ee615ba
77
pkg/promlib/admission_handler.go
Normal file
77
pkg/promlib/admission_handler.go
Normal file
@ -0,0 +1,77 @@
|
||||
package promlib
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
var (
|
||||
_ backend.AdmissionHandler = (*Service)(nil)
|
||||
)
|
||||
|
||||
// ValidateAdmission implements backend.AdmissionHandler.
|
||||
func (s *Service) ValidateAdmission(ctx context.Context, req *backend.AdmissionRequest) (*backend.ValidationResponse, error) {
|
||||
rsp, err := s.MutateAdmission(ctx, req)
|
||||
if rsp != nil {
|
||||
return &backend.ValidationResponse{
|
||||
Allowed: rsp.Allowed,
|
||||
Result: rsp.Result,
|
||||
Warnings: rsp.Warnings,
|
||||
}, err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// MutateAdmission implements backend.AdmissionHandler.
|
||||
func (s *Service) MutateAdmission(ctx context.Context, req *backend.AdmissionRequest) (*backend.MutationResponse, error) {
|
||||
expected := (&backend.DataSourceInstanceSettings{}).GVK()
|
||||
if req.Kind.Kind != expected.Kind && req.Kind.Group != expected.Group {
|
||||
return getBadRequest("expected DataSourceInstanceSettings protobuf payload"), nil
|
||||
}
|
||||
|
||||
// Convert the payload from protobuf to an SDK struct
|
||||
settings, err := backend.DataSourceInstanceSettingsFromProto(req.ObjectBytes, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if settings == nil {
|
||||
return getBadRequest("missing datasource settings"), nil
|
||||
}
|
||||
|
||||
switch settings.APIVersion {
|
||||
case "", "v0alpha1":
|
||||
// OK!
|
||||
default:
|
||||
return getBadRequest(fmt.Sprintf("expected apiVersion: v0alpha1, found: %s", settings.APIVersion)), nil
|
||||
}
|
||||
if settings.URL != "" {
|
||||
return getBadRequest("unsupported URL value"), nil
|
||||
}
|
||||
|
||||
pb, err := backend.DataSourceInstanceSettingsToProtoBytes(settings)
|
||||
return &backend.MutationResponse{
|
||||
Allowed: true,
|
||||
ObjectBytes: pb,
|
||||
}, err
|
||||
}
|
||||
|
||||
// ConvertObject implements backend.AdmissionHandler.
|
||||
func (s *Service) ConvertObject(ctx context.Context, req *backend.ConversionRequest) (*backend.ConversionResponse, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func getBadRequest(msg string) *backend.MutationResponse {
|
||||
return &backend.MutationResponse{
|
||||
Allowed: false,
|
||||
Result: &backend.StatusResult{
|
||||
Status: "Failure",
|
||||
Message: msg,
|
||||
Reason: string(metav1.StatusReasonBadRequest),
|
||||
Code: http.StatusBadRequest,
|
||||
},
|
||||
}
|
||||
}
|
@ -67,6 +67,8 @@ func RegisterAPIService(
|
||||
var err error
|
||||
var builder *DataSourceAPIBuilder
|
||||
all := pluginStore.Plugins(context.Background(), plugins.TypeDataSource)
|
||||
// ATTENTION: Adding a datasource here requires the plugin to implement
|
||||
// an AdmissionHandler to validate the datasource settings.
|
||||
ids := []string{
|
||||
"grafana-testdata-datasource",
|
||||
"prometheus",
|
||||
|
77
pkg/tsdb/graphite/admission_handler.go
Normal file
77
pkg/tsdb/graphite/admission_handler.go
Normal file
@ -0,0 +1,77 @@
|
||||
package graphite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
var (
|
||||
_ backend.AdmissionHandler = (*Service)(nil)
|
||||
)
|
||||
|
||||
// ValidateAdmission implements backend.AdmissionHandler.
|
||||
func (s *Service) ValidateAdmission(ctx context.Context, req *backend.AdmissionRequest) (*backend.ValidationResponse, error) {
|
||||
rsp, err := s.MutateAdmission(ctx, req)
|
||||
if rsp != nil {
|
||||
return &backend.ValidationResponse{
|
||||
Allowed: rsp.Allowed,
|
||||
Result: rsp.Result,
|
||||
Warnings: rsp.Warnings,
|
||||
}, err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// MutateAdmission implements backend.AdmissionHandler.
|
||||
func (s *Service) MutateAdmission(ctx context.Context, req *backend.AdmissionRequest) (*backend.MutationResponse, error) {
|
||||
expected := (&backend.DataSourceInstanceSettings{}).GVK()
|
||||
if req.Kind.Kind != expected.Kind && req.Kind.Group != expected.Group {
|
||||
return getBadRequest("expected DataSourceInstanceSettings protobuf payload"), nil
|
||||
}
|
||||
|
||||
// Convert the payload from protobuf to an SDK struct
|
||||
settings, err := backend.DataSourceInstanceSettingsFromProto(req.ObjectBytes, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if settings == nil {
|
||||
return getBadRequest("missing datasource settings"), nil
|
||||
}
|
||||
|
||||
switch settings.APIVersion {
|
||||
case "", "v0alpha1":
|
||||
// OK!
|
||||
default:
|
||||
return getBadRequest(fmt.Sprintf("expected apiVersion: v0alpha1, found: %s", settings.APIVersion)), nil
|
||||
}
|
||||
if settings.URL != "" {
|
||||
return getBadRequest("unsupported URL value"), nil
|
||||
}
|
||||
|
||||
pb, err := backend.DataSourceInstanceSettingsToProtoBytes(settings)
|
||||
return &backend.MutationResponse{
|
||||
Allowed: true,
|
||||
ObjectBytes: pb,
|
||||
}, err
|
||||
}
|
||||
|
||||
// ConvertObject implements backend.AdmissionHandler.
|
||||
func (s *Service) ConvertObject(ctx context.Context, req *backend.ConversionRequest) (*backend.ConversionResponse, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func getBadRequest(msg string) *backend.MutationResponse {
|
||||
return &backend.MutationResponse{
|
||||
Allowed: false,
|
||||
Result: &backend.StatusResult{
|
||||
Status: "Failure",
|
||||
Message: msg,
|
||||
Reason: string(metav1.StatusReasonBadRequest),
|
||||
Code: http.StatusBadRequest,
|
||||
},
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user