Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantxu committed May 21, 2024
1 parent 218525d commit 68e4b33
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 115 deletions.
40 changes: 20 additions & 20 deletions backend/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ type ConvertObjectFunc func(context.Context, *ConversionRequest) (*AdmissionResp
type AdmissionRequestOperation int32

const (
AdmissionRequestCREATE AdmissionRequestOperation = 0
AdmissionRequestUPDATE AdmissionRequestOperation = 1
AdmissionRequestDELETE AdmissionRequestOperation = 2
AdmissionRequestCreate AdmissionRequestOperation = 0
AdmissionRequestUpdate AdmissionRequestOperation = 1
AdmissionRequestDelete AdmissionRequestOperation = 2
)

// String textual representation of the operation.
Expand All @@ -34,9 +34,9 @@ func (o AdmissionRequestOperation) String() string {

// Identify the Object properties
type GroupVersionKind struct {
Group string `protobuf:"bytes,1,opt,name=group,proto3" json:"group,omitempty"`
Version string `protobuf:"bytes,2,opt,name=version,proto3" json:"version,omitempty"`
Kind string `protobuf:"bytes,3,opt,name=kind,proto3" json:"kind,omitempty"`
Group string `json:"group,omitempty"`
Version string `json:"version,omitempty"`
Kind string `json:"kind,omitempty"`
}

// AdmissionRequest contains information from a kubernetes Admission request and decoded object(s).
Expand All @@ -45,52 +45,52 @@ type GroupVersionKind struct {
// NOTE: this does not include a plugin context
type AdmissionRequest struct {
// NOTE: this may not include app or datasource instance settings depending on the request
PluginContext PluginContext `protobuf:"bytes,1,opt,name=pluginContext,proto3" json:"pluginContext,omitempty"`
PluginContext PluginContext `json:"pluginContext,omitempty"`
// The requested operation
Operation AdmissionRequestOperation `protobuf:"varint,2,opt,name=operation,proto3,enum=pluginv2.AdmissionRequestOperation" json:"operation,omitempty"`
Operation AdmissionRequestOperation `json:"operation,omitempty"`
// The object kind
Kind GroupVersionKind `protobuf:"bytes,3,opt,name=kind,proto3" json:"kind,omitempty"`
Kind GroupVersionKind `json:"kind,omitempty"`
// Object is the object in the request. This includes the full metadata envelope.
ObjectBytes []byte `protobuf:"bytes,4,opt,name=object_bytes,json=objectBytes,proto3" json:"object_bytes,omitempty"`
ObjectBytes []byte `json:"object_bytes,omitempty"`
// OldObject is the object as it currently exists in storage. This includes the full metadata envelope.
OldObjectBytes []byte `protobuf:"bytes,5,opt,name=old_object_bytes,json=oldObjectBytes,proto3" json:"old_object_bytes,omitempty"`
OldObjectBytes []byte `json:"old_object_bytes,omitempty"`
}

// ConversionRequest supports converting an object from on version to another
type ConversionRequest struct {
// NOTE: this may not include app or datasource instance settings depending on the request
PluginContext PluginContext `protobuf:"bytes,1,opt,name=pluginContext,proto3" json:"pluginContext,omitempty"`
PluginContext PluginContext `json:"pluginContext,omitempty"`
// The object kind
Kind GroupVersionKind `protobuf:"bytes,2,opt,name=kind,proto3" json:"kind,omitempty"`
Kind GroupVersionKind `json:"kind,omitempty"`
// Object is the object in the request. This includes the full metadata envelope.
ObjectBytes []byte `protobuf:"bytes,3,opt,name=object_bytes,json=objectBytes,proto3" json:"object_bytes,omitempty"`
ObjectBytes []byte `json:"object_bytes,omitempty"`
// Target converted version
TargetVersion string `protobuf:"bytes,4,opt,name=target_version,json=targetVersion,proto3" json:"target_version,omitempty"`
TargetVersion string `json:"target_version,omitempty"`
}

// See https://github.com/kubernetes/kubernetes/blob/v1.30.0/pkg/apis/admission/types.go#L118
type AdmissionResponse struct {
// Allowed indicates whether or not the admission request was permitted.
Allowed bool `protobuf:"varint,1,opt,name=allowed,proto3" json:"allowed,omitempty"`
Allowed bool `json:"allowed,omitempty"`
// Result contains extra details into why an admission request was denied.
// This field IS NOT consulted in any way if "Allowed" is "true".
// +optional
Result *StatusResult `protobuf:"bytes,2,opt,name=result,proto3" json:"result,omitempty"`
Result *StatusResult `json:"result,omitempty"`
// AuditAnnotations is an unstructured key value map set by remote admission controller (e.g. error=image-blacklisted).
// MutatingAdmissionWebhook and ValidatingAdmissionWebhook admission controller will prefix the keys with
// admission webhook name (e.g. imagepolicy.example.com/error=image-blacklisted). AuditAnnotations will be provided by
// the admission webhook to add additional context to the audit log for this request.
// +optional
AuditAnnotations map[string]string `protobuf:"bytes,3,rep,name=auditAnnotations,proto3" json:"auditAnnotations,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
AuditAnnotations map[string]string `json:"auditAnnotations,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
// warnings is a list of warning messages to return to the requesting API client.
// Warning messages describe a problem the client making the API request should correct or be aware of.
// Limit warnings to 120 characters if possible.
// Warnings over 256 characters and large numbers of warnings may be truncated.
// +optional
Warnings []string `protobuf:"bytes,4,rep,name=warnings,proto3" json:"warnings,omitempty"`
Warnings []string `json:"warnings,omitempty"`
// Mutated object bytes (when requested)
// +optional
ObjectBytes []byte `protobuf:"bytes,5,opt,name=object_bytes,json=objectBytes,proto3" json:"object_bytes,omitempty"`
ObjectBytes []byte `json:"object_bytes,omitempty"`
}

type StatusResult struct {
Expand Down
43 changes: 0 additions & 43 deletions backend/admission_test.go

This file was deleted.

12 changes: 5 additions & 7 deletions backend/app/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/internal/buildinfo"
)

// ManageOpts can modify Manage behaviour.
// ManageOpts can modify Manage behavior.
type ManageOpts struct {
// GRPCSettings settings for gPRC.
GRPCSettings backend.GRPCSettings

// TracingOpts contains settings for tracing setup.
TracingOpts tracing.Opts

// Use the stateless version for some handlers
// Only supported for Admission controllers currently
Stateless backend.ServeOpts
// Stateless admission handler
AdmissionHandler backend.AdmissionHandler
}

// Manage starts serving the app over gPRC with automatic instance management.
Expand All @@ -47,8 +46,7 @@ func Manage(pluginID string, instanceFactory InstanceFactoryFunc, opts ManageOpt
CallResourceHandler: handler,
QueryDataHandler: handler,
StreamHandler: handler,
AdmissionHandler: opts.Stateless.AdmissionHandler,
// CollectMetricsHandler: opts.Stateless. ?? is this managed elsewhere?
GRPCSettings: opts.GRPCSettings,
AdmissionHandler: opts.AdmissionHandler,
GRPCSettings: opts.GRPCSettings,
})
}
38 changes: 0 additions & 38 deletions backend/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana-plugin-sdk-go/backend/proxy"
"github.com/grafana/grafana-plugin-sdk-go/backend/useragent"
"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2"
"github.com/grafana/grafana-plugin-sdk-go/internal/tenant"
"google.golang.org/protobuf/proto"
)

const dataCustomOptionsKey = "grafanaData"
Expand Down Expand Up @@ -66,24 +64,6 @@ func (s *AppInstanceSettings) GVK() GroupVersionKind {
}
}

// ProtoBytes returns the settings the the byte structure expected by admission hooks
func (s *AppInstanceSettings) ProtoBytes() []byte {
v, _ := proto.Marshal(ConvertToProtobuf{}.AppInstanceSettings(s))
return v
}

func AppInstanceSettingsFromProto(body []byte) (*AppInstanceSettings, error) {
if len(body) == 0 {
return nil, nil
}
tmp := &pluginv2.AppInstanceSettings{}
err := proto.Unmarshal(body, tmp)
if err != nil {
return nil, err
}
return ConvertFromProtobuf{}.AppInstanceSettings(tmp), nil
}

// DataSourceInstanceSettings represents settings for a data source instance.
//
// In Grafana a data source instance is a data source plugin of certain
Expand Down Expand Up @@ -181,24 +161,6 @@ func (s *DataSourceInstanceSettings) GVK() GroupVersionKind {
}
}

// ProtoBytes returns the settings the the byte structure expected by admission hooks
func (s *DataSourceInstanceSettings) ProtoBytes() []byte {
v, _ := proto.Marshal(ConvertToProtobuf{}.DataSourceInstanceSettings(s))
return v
}

func DataSourceInstanceSettingsFromProto(body []byte, pluginID string) (*DataSourceInstanceSettings, error) {
if len(body) == 0 {
return nil, nil
}
tmp := &pluginv2.DataSourceInstanceSettings{}
err := proto.Unmarshal(body, tmp)
if err != nil {
return nil, err
}
return ConvertFromProtobuf{}.DataSourceInstanceSettings(tmp, pluginID), nil
}

// PluginContext holds contextual information about a plugin request, such as
// Grafana organization, user and plugin instance settings.
type PluginContext struct {
Expand Down
40 changes: 40 additions & 0 deletions backend/convert_to_protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/grafana/grafana-plugin-sdk-go/backend/useragent"
"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2"
"google.golang.org/protobuf/proto"
)

// ConvertToProtobuf has a collection of methods for converting the autogenerated
Expand Down Expand Up @@ -49,6 +50,45 @@ func (t ConvertToProtobuf) AppInstanceSettings(s *AppInstanceSettings) *pluginv2
}
}

func AppInstanceSettingsToProtoBytes(s *AppInstanceSettings) ([]byte, error) {
if s == nil {
return nil, nil
}
return proto.Marshal(ConvertToProtobuf{}.AppInstanceSettings(s))
}

func AppInstanceSettingsFromProto(body []byte) (*AppInstanceSettings, error) {
if len(body) == 0 {
return nil, nil
}
tmp := &pluginv2.AppInstanceSettings{}
err := proto.Unmarshal(body, tmp)
if err != nil {
return nil, err
}
return ConvertFromProtobuf{}.AppInstanceSettings(tmp), nil
}

func DataSourceInstanceSettingsToProtoBytes(s *DataSourceInstanceSettings) ([]byte, error) {
if s == nil {
return nil, nil
}
return proto.Marshal(ConvertToProtobuf{}.DataSourceInstanceSettings(s))
}

func DataSourceInstanceSettingsFromProto(body []byte, pluginID string) (*DataSourceInstanceSettings, error) {
if len(body) == 0 {
return nil, nil
}
tmp := &pluginv2.DataSourceInstanceSettings{}
err := proto.Unmarshal(body, tmp)
if err != nil {
return nil, err
}
return ConvertFromProtobuf{}.DataSourceInstanceSettings(tmp, pluginID), nil
}


// DataSourceInstanceSettings converts the SDK version of a DataSourceInstanceSettings to the protobuf version.
func (t ConvertToProtobuf) DataSourceInstanceSettings(s *DataSourceInstanceSettings) *pluginv2.DataSourceInstanceSettings {
if s == nil {
Expand Down
41 changes: 39 additions & 2 deletions backend/convert_to_protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"io/fs"
"net/http"
"os"
"time"
"syscall"
"testing"

"github.com/stretchr/testify/require"

"github.com/grafana/grafana-plugin-sdk-go/data"
)

Expand Down Expand Up @@ -105,3 +104,41 @@ func TestConvertToProtobufStatus(t *testing.T) {
require.Equal(t, "c", ar.Reason)
require.Equal(t, int32(234), ar.Code)
}

func TestInstanceSettingsAdmissionConversions(t *testing.T) {
t.Run("DataSource", func(t *testing.T) {
before := &DataSourceInstanceSettings{
URL: "http://something",
Updated: time.Now(),
User: "u",
JSONData: []byte(`{"hello": "world"}`),
DecryptedSecureJSONData: map[string]string{
"A": "B",
},
}
wire, err := DataSourceInstanceSettingsToProtoBytes(before)
require.NoError(t, err)
after, err := DataSourceInstanceSettingsFromProto(wire, "")
require.NoError(t, err)
require.Equal(t, before.URL, after.URL)
require.Equal(t, before.User, after.User)
require.Equal(t, before.JSONData, after.JSONData)
require.Equal(t, before.DecryptedSecureJSONData, after.DecryptedSecureJSONData)
})

t.Run("App", func(t *testing.T) {
before := &AppInstanceSettings{
Updated: time.Now(),
JSONData: []byte(`{"hello": "world"}`),
DecryptedSecureJSONData: map[string]string{
"A": "B",
},
}
wire, err := AppInstanceSettingsToProtoBytes(before)
require.NoError(t, err)
after, err := AppInstanceSettingsFromProto(wire)
require.NoError(t, err)
require.Equal(t, before.JSONData, after.JSONData)
require.Equal(t, before.DecryptedSecureJSONData, after.DecryptedSecureJSONData)
})
}
9 changes: 4 additions & 5 deletions backend/datasource/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/internal/buildinfo"
)

// ManageOpts can modify Manage behaviour.
// ManageOpts can modify Manage behavior.
type ManageOpts struct {
// GRPCSettings settings for gPRC.
GRPCSettings backend.GRPCSettings

// TracingOpts contains settings for tracing setup.
TracingOpts tracing.Opts

// Use the stateless version for some handlers
// Only supported for Admission controllers currently
Stateless backend.ServeOpts
// Stateless admission handler
AdmissionHandler backend.AdmissionHandler
}

// Manage starts serving the data source over gPRC with automatic instance management.
Expand All @@ -47,7 +46,7 @@ func Manage(pluginID string, instanceFactory InstanceFactoryFunc, opts ManageOpt
CallResourceHandler: handler,
QueryDataHandler: handler,
StreamHandler: handler,
AdmissionHandler: opts.Stateless.AdmissionHandler,
AdmissionHandler: opts.AdmissionHandler,
GRPCSettings: opts.GRPCSettings,
})
}

0 comments on commit 68e4b33

Please sign in to comment.