|
| 1 | +package backend |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2" |
| 7 | +) |
| 8 | + |
| 9 | +// AdmissionHandler is an EXPERIMENTAL service that allows checking objects before they are saved |
| 10 | +// This is modeled after the kubernetes model for admission controllers |
| 11 | +// Since grafana 11.1, this feature is under active development and will continue to evolve in 2024 |
| 12 | +// This may also be replaced with a more native kubernetes solution that does not work with existing tooling |
| 13 | +type AdmissionHandler interface { |
| 14 | + // ValidateAdmission is a simple yes|no check if an object can be saved |
| 15 | + ValidateAdmission(context.Context, *AdmissionRequest) (*ValidationResponse, error) |
| 16 | + // MutateAdmission converts the input into an object that can be saved, or rejects the request |
| 17 | + MutateAdmission(context.Context, *AdmissionRequest) (*MutationResponse, error) |
| 18 | + // ConvertObject is called to covert objects between different versions |
| 19 | + ConvertObject(context.Context, *ConversionRequest) (*ConversionResponse, error) |
| 20 | +} |
| 21 | + |
| 22 | +type ValidateAdmissionFunc func(context.Context, *AdmissionRequest) (*ValidationResponse, error) |
| 23 | +type MutateAdmissionFunc func(context.Context, *AdmissionRequest) (*MutationResponse, error) |
| 24 | +type ConvertObjectFunc func(context.Context, *ConversionRequest) (*ConversionResponse, error) |
| 25 | + |
| 26 | +// Operation is the type of resource operation being checked for admission control |
| 27 | +// https://github.com/kubernetes/kubernetes/blob/v1.30.0/pkg/apis/admission/types.go#L158 |
| 28 | +type AdmissionRequestOperation int32 |
| 29 | + |
| 30 | +const ( |
| 31 | + AdmissionRequestCreate AdmissionRequestOperation = 0 |
| 32 | + AdmissionRequestUpdate AdmissionRequestOperation = 1 |
| 33 | + AdmissionRequestDelete AdmissionRequestOperation = 2 |
| 34 | +) |
| 35 | + |
| 36 | +// String textual representation of the operation. |
| 37 | +func (o AdmissionRequestOperation) String() string { |
| 38 | + return pluginv2.AdmissionRequest_Operation(o).String() |
| 39 | +} |
| 40 | + |
| 41 | +// Identify the Object properties |
| 42 | +type GroupVersionKind struct { |
| 43 | + Group string `json:"group,omitempty"` |
| 44 | + Version string `json:"version,omitempty"` |
| 45 | + Kind string `json:"kind,omitempty"` |
| 46 | +} |
| 47 | + |
| 48 | +type AdmissionRequest struct { |
| 49 | + // NOTE: this may not include populated instance settings depending on the request |
| 50 | + PluginContext PluginContext `json:"pluginContext,omitempty"` |
| 51 | + // The requested operation |
| 52 | + Operation AdmissionRequestOperation `json:"operation,omitempty"` |
| 53 | + // The object kind |
| 54 | + Kind GroupVersionKind `json:"kind,omitempty"` |
| 55 | + // Object is the object in the request. This includes the full metadata envelope. |
| 56 | + ObjectBytes []byte `json:"object_bytes,omitempty"` |
| 57 | + // OldObject is the object as it currently exists in storage. This includes the full metadata envelope. |
| 58 | + OldObjectBytes []byte `json:"old_object_bytes,omitempty"` |
| 59 | +} |
| 60 | + |
| 61 | +// ConversionRequest supports converting an object from on version to another |
| 62 | +type ConversionRequest struct { |
| 63 | + // NOTE: this may not include app or datasource instance settings depending on the request |
| 64 | + PluginContext PluginContext `json:"pluginContext,omitempty"` |
| 65 | + // The object kind |
| 66 | + Kind GroupVersionKind `json:"kind,omitempty"` |
| 67 | + // Object is the object in the request. This includes the full metadata envelope. |
| 68 | + ObjectBytes []byte `json:"object_bytes,omitempty"` |
| 69 | + // Target converted version |
| 70 | + TargetVersion string `json:"target_version,omitempty"` |
| 71 | +} |
| 72 | + |
| 73 | +// Basic request to say if the validation was successful or not |
| 74 | +type ValidationResponse struct { |
| 75 | + // Allowed indicates whether or not the admission request was permitted. |
| 76 | + Allowed bool `json:"allowed,omitempty"` |
| 77 | + // Result contains extra details into why an admission request was denied. |
| 78 | + // This field IS NOT consulted in any way if "Allowed" is "true". |
| 79 | + // +optional |
| 80 | + Result *StatusResult `json:"result,omitempty"` |
| 81 | + // warnings is a list of warning messages to return to the requesting API client. |
| 82 | + // Warning messages describe a problem the client making the API request should correct or be aware of. |
| 83 | + // Limit warnings to 120 characters if possible. |
| 84 | + // Warnings over 256 characters and large numbers of warnings may be truncated. |
| 85 | + // +optional |
| 86 | + Warnings []string `json:"warnings,omitempty"` |
| 87 | +} |
| 88 | + |
| 89 | +type MutationResponse struct { |
| 90 | + // Allowed indicates whether or not the admission request was permitted. |
| 91 | + Allowed bool `json:"allowed,omitempty"` |
| 92 | + // Result contains extra details into why an admission request was denied. |
| 93 | + // This field IS NOT consulted in any way if "Allowed" is "true". |
| 94 | + // +optional |
| 95 | + Result *StatusResult `json:"result,omitempty"` |
| 96 | + // warnings is a list of warning messages to return to the requesting API client. |
| 97 | + // Warning messages describe a problem the client making the API request should correct or be aware of. |
| 98 | + // Limit warnings to 120 characters if possible. |
| 99 | + // Warnings over 256 characters and large numbers of warnings may be truncated. |
| 100 | + // +optional |
| 101 | + Warnings []string `json:"warnings,omitempty"` |
| 102 | + // Mutated object bytes (when requested) |
| 103 | + // +optional |
| 104 | + ObjectBytes []byte `json:"object_bytes,omitempty"` |
| 105 | +} |
| 106 | + |
| 107 | +type ConversionResponse struct { |
| 108 | + // Allowed indicates whether or not the admission request was permitted. |
| 109 | + Allowed bool `json:"allowed,omitempty"` |
| 110 | + // Result contains extra details into why an admission request was denied. |
| 111 | + // This field IS NOT consulted in any way if "Allowed" is "true". |
| 112 | + // +optional |
| 113 | + Result *StatusResult `json:"result,omitempty"` |
| 114 | + // Converted object bytes |
| 115 | + ObjectBytes []byte `json:"object_bytes,omitempty"` |
| 116 | +} |
| 117 | + |
| 118 | +type StatusResult struct { |
| 119 | + // Status of the operation. |
| 120 | + // One of: "Success" or "Failure". |
| 121 | + // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status |
| 122 | + // +optional |
| 123 | + Status string `json:"status,omitempty"` |
| 124 | + // A human-readable description of the status of this operation. |
| 125 | + // +optional |
| 126 | + Message string `json:"message,omitempty"` |
| 127 | + // A machine-readable description of why this operation is in the |
| 128 | + // "Failure" status. If this value is empty there |
| 129 | + // is no information available. A Reason clarifies an HTTP status |
| 130 | + // code but does not override it. |
| 131 | + // +optional |
| 132 | + Reason string `json:"reason,omitempty"` |
| 133 | + // Suggested HTTP return code for this status, 0 if not set. |
| 134 | + // +optional |
| 135 | + Code int32 `json:"code,omitempty"` |
| 136 | +} |
0 commit comments