Dashboards: Add apiVersion to dashboard table (#100845)
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
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:
|
||||
if id, ok := v.Spec.Object["id"].(float64); ok {
|
||||
delete(v.Spec.Object, "id")
|
||||
internalID = int64(id)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("mutation error: expected to dashboard, got %T", obj)
|
||||
}
|
||||
|
||||
if internalID != 0 {
|
||||
meta.SetDeprecatedInternalID(internalID) // nolint:staticcheck
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user