mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 23:53:10 +08:00

* Generate Dashboard kinds with `grafana-app-sdk` Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com> * Hack together a fix for invalid TS codegen for v0 & v1 Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com> * Address Go linter issues Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com> * Address TS linter issues Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com> * Add new app to CODEOWNERS Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com> * Fix a couple of issues detected by tests Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com> * Update OpenAPI definitions and test files Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com> * Remove title from Dashboard v1alpha1 spec Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com> * Remove unused CUE schemas Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com> * remove unrelated files * allow any in the generated betterer * Add a comment explaining why we don't use deepcopy-gen Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com> * Default to v2alpha1 if dashboards v2 FF is enabled Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com> --------- Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com> Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"k8s.io/apiserver/pkg/admission"
|
|
|
|
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
|
dashboardV0 "github.com/grafana/grafana/pkg/apis/dashboard/v0alpha1"
|
|
dashboardV1 "github.com/grafana/grafana/pkg/apis/dashboard/v1alpha1"
|
|
dashboardV2 "github.com/grafana/grafana/pkg/apis/dashboard/v2alpha1"
|
|
)
|
|
|
|
func (b *DashboardsAPIBuilder) Mutate(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) (err error) {
|
|
op := a.GetOperation()
|
|
|
|
// Mutate removes any internal ID set in the spec & adds it as a label
|
|
if op != admission.Create && op != admission.Update {
|
|
return nil
|
|
}
|
|
var internalID int64
|
|
obj := a.GetObject()
|
|
meta, err := utils.MetaAccessor(obj)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
switch v := obj.(type) {
|
|
case *dashboardV0.Dashboard:
|
|
if id, ok := v.Spec.Object["id"].(float64); ok {
|
|
delete(v.Spec.Object, "id")
|
|
internalID = int64(id)
|
|
}
|
|
case *dashboardV1.Dashboard:
|
|
if id, ok := v.Spec.Object["id"].(float64); ok {
|
|
delete(v.Spec.Object, "id")
|
|
internalID = int64(id)
|
|
}
|
|
case *dashboardV2.Dashboard:
|
|
// Noop for V2
|
|
default:
|
|
return fmt.Errorf("mutation error: expected to dashboard, got %T", obj)
|
|
}
|
|
|
|
if internalID != 0 {
|
|
meta.SetDeprecatedInternalID(internalID) // nolint:staticcheck
|
|
}
|
|
|
|
return nil
|
|
}
|