mirror of https://github.com/grafana/grafana.git
				
				
				
			
		
			
				
	
	
		
			174 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			174 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Go
		
	
	
	
| package conversion
 | |
| 
 | |
| import (
 | |
| 	"k8s.io/apimachinery/pkg/conversion"
 | |
| 	"k8s.io/apimachinery/pkg/runtime"
 | |
| 
 | |
| 	dashv0 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1"
 | |
| 	dashv1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v1beta1"
 | |
| 	dashv2 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v2alpha1"
 | |
| 	"github.com/grafana/grafana/apps/dashboard/pkg/migration"
 | |
| 	"github.com/grafana/grafana/apps/dashboard/pkg/migration/schemaversion"
 | |
| )
 | |
| 
 | |
| func RegisterConversions(s *runtime.Scheme) error {
 | |
| 	if err := s.AddConversionFunc((*dashv0.Dashboard)(nil), (*dashv1.Dashboard)(nil), func(a, b interface{}, scope conversion.Scope) error {
 | |
| 		return Convert_V0_to_V1(a.(*dashv0.Dashboard), b.(*dashv1.Dashboard), scope)
 | |
| 	}); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	if err := s.AddConversionFunc((*dashv0.Dashboard)(nil), (*dashv2.Dashboard)(nil), func(a, b interface{}, scope conversion.Scope) error {
 | |
| 		return Convert_V0_to_V2(a.(*dashv0.Dashboard), b.(*dashv2.Dashboard), scope)
 | |
| 	}); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	if err := s.AddConversionFunc((*dashv1.Dashboard)(nil), (*dashv0.Dashboard)(nil), func(a, b interface{}, scope conversion.Scope) error {
 | |
| 		return Convert_V1_to_V0(a.(*dashv1.Dashboard), b.(*dashv0.Dashboard), scope)
 | |
| 	}); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	if err := s.AddConversionFunc((*dashv1.Dashboard)(nil), (*dashv2.Dashboard)(nil), func(a, b interface{}, scope conversion.Scope) error {
 | |
| 		return Convert_V1_to_V2(a.(*dashv1.Dashboard), b.(*dashv2.Dashboard), scope)
 | |
| 	}); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	if err := s.AddConversionFunc((*dashv2.Dashboard)(nil), (*dashv0.Dashboard)(nil), func(a, b interface{}, scope conversion.Scope) error {
 | |
| 		return Convert_V2_to_V0(a.(*dashv2.Dashboard), b.(*dashv0.Dashboard), scope)
 | |
| 	}); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	if err := s.AddConversionFunc((*dashv2.Dashboard)(nil), (*dashv1.Dashboard)(nil), func(a, b interface{}, scope conversion.Scope) error {
 | |
| 		return Convert_V2_to_V1(a.(*dashv2.Dashboard), b.(*dashv1.Dashboard), scope)
 | |
| 	}); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func Convert_V0_to_V1(in *dashv0.Dashboard, out *dashv1.Dashboard, scope conversion.Scope) error {
 | |
| 	out.ObjectMeta = in.ObjectMeta
 | |
| 
 | |
| 	out.Spec.Object = in.Spec.Object
 | |
| 
 | |
| 	out.Status = dashv1.DashboardStatus{
 | |
| 		Conversion: &dashv1.DashboardConversionStatus{
 | |
| 			StoredVersion: dashv0.VERSION,
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	if err := migration.Migrate(out.Spec.Object, schemaversion.LATEST_VERSION); err != nil {
 | |
| 		out.Status.Conversion.Failed = true
 | |
| 		out.Status.Conversion.Error = err.Error()
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func Convert_V0_to_V2(in *dashv0.Dashboard, out *dashv2.Dashboard, scope conversion.Scope) error {
 | |
| 	out.ObjectMeta = in.ObjectMeta
 | |
| 
 | |
| 	// TODO (@radiohead): implement V0 to V2 conversion
 | |
| 	// This is the bare minimum conversion that is needed to make the dashboard servable.
 | |
| 
 | |
| 	if v, ok := in.Spec.Object["title"]; ok {
 | |
| 		if title, ok := v.(string); ok {
 | |
| 			out.Spec.Title = title
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	// We need to make sure the layout is set to some value, otherwise the JSON marshaling will fail.
 | |
| 	out.Spec.Layout = dashv2.DashboardGridLayoutKindOrRowsLayoutKindOrAutoGridLayoutKindOrTabsLayoutKind{
 | |
| 		GridLayoutKind: &dashv2.DashboardGridLayoutKind{
 | |
| 			Kind: "GridLayout",
 | |
| 			Spec: dashv2.DashboardGridLayoutSpec{},
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	out.Status = dashv2.DashboardStatus{
 | |
| 		Conversion: &dashv2.DashboardConversionStatus{
 | |
| 			StoredVersion: dashv0.VERSION,
 | |
| 			Failed:        true,
 | |
| 			Error:         "backend conversion not yet implemented",
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func Convert_V1_to_V0(in *dashv1.Dashboard, out *dashv0.Dashboard, scope conversion.Scope) error {
 | |
| 	out.ObjectMeta = in.ObjectMeta
 | |
| 
 | |
| 	out.Spec.Object = in.Spec.Object
 | |
| 
 | |
| 	out.Status = dashv0.DashboardStatus{
 | |
| 		Conversion: &dashv0.DashboardConversionStatus{
 | |
| 			StoredVersion: dashv1.VERSION,
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func Convert_V1_to_V2(in *dashv1.Dashboard, out *dashv2.Dashboard, scope conversion.Scope) error {
 | |
| 	out.ObjectMeta = in.ObjectMeta
 | |
| 
 | |
| 	// TODO (@radiohead): implement V1 to V2 conversion
 | |
| 	// This is the bare minimum conversion that is needed to make the dashboard servable.
 | |
| 
 | |
| 	if v, ok := in.Spec.Object["title"]; ok {
 | |
| 		if title, ok := v.(string); ok {
 | |
| 			out.Spec.Title = title
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	// We need to make sure the layout is set to some value, otherwise the JSON marshaling will fail.
 | |
| 	out.Spec.Layout = dashv2.DashboardGridLayoutKindOrRowsLayoutKindOrAutoGridLayoutKindOrTabsLayoutKind{
 | |
| 		GridLayoutKind: &dashv2.DashboardGridLayoutKind{
 | |
| 			Kind: "GridLayout",
 | |
| 			Spec: dashv2.DashboardGridLayoutSpec{},
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	out.Status = dashv2.DashboardStatus{
 | |
| 		Conversion: &dashv2.DashboardConversionStatus{
 | |
| 			StoredVersion: dashv1.VERSION,
 | |
| 			Failed:        true,
 | |
| 			Error:         "backend conversion not yet implemented",
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func Convert_V2_to_V0(in *dashv2.Dashboard, out *dashv0.Dashboard, scope conversion.Scope) error {
 | |
| 	out.ObjectMeta = in.ObjectMeta
 | |
| 
 | |
| 	// TODO: implement V2 to V0 conversion
 | |
| 
 | |
| 	out.Status = dashv0.DashboardStatus{
 | |
| 		Conversion: &dashv0.DashboardConversionStatus{
 | |
| 			StoredVersion: dashv2.VERSION,
 | |
| 			Failed:        true,
 | |
| 			Error:         "backend conversion not yet implemented",
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func Convert_V2_to_V1(in *dashv2.Dashboard, out *dashv1.Dashboard, scope conversion.Scope) error {
 | |
| 	out.ObjectMeta = in.ObjectMeta
 | |
| 
 | |
| 	// TODO: implement V2 to V1 conversion
 | |
| 
 | |
| 	out.Status = dashv1.DashboardStatus{
 | |
| 		Conversion: &dashv1.DashboardConversionStatus{
 | |
| 			StoredVersion: dashv2.VERSION,
 | |
| 			Failed:        true,
 | |
| 			Error:         "backend conversion not yet implemented",
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 |