mirror of https://github.com/grafana/grafana.git
Provisioning: Skip validation when writing v1 dashbaords (#103893)
This commit is contained in:
parent
40127072e5
commit
ac7edd3032
|
|
@ -37,7 +37,7 @@ func ReadClassicResource(ctx context.Context, info *repository.FileInfo) (*unstr
|
|||
return nil, nil, "", err
|
||||
}
|
||||
} else {
|
||||
return nil, nil, "", fmt.Errorf("classic resource must be JSON")
|
||||
return nil, nil, "", fmt.Errorf("unable to read file")
|
||||
}
|
||||
|
||||
// regular version headers exist
|
||||
|
|
@ -64,7 +64,7 @@ func ReadClassicResource(ctx context.Context, info *repository.FileInfo) (*unstr
|
|||
value["tags"] != nil {
|
||||
gvk := &schema.GroupVersionKind{
|
||||
Group: dashboard.GROUP,
|
||||
Version: dashboard.VERSION, // v1
|
||||
Version: "v0alpha1", // no schema
|
||||
Kind: "Dashboard"}
|
||||
return &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
|
|
|
|||
|
|
@ -138,7 +138,7 @@ func (r *parser) Parse(ctx context.Context, info *repository.FileInfo) (parsed *
|
|||
logger.Debug("failed to find GVK of the input data, trying fallback loader", "error", err)
|
||||
parsed.Obj, gvk, parsed.Classic, err = ReadClassicResource(ctx, info)
|
||||
if err != nil || gvk == nil {
|
||||
return nil, err
|
||||
return nil, apierrors.NewBadRequest("unable to read file as a resource")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -227,18 +227,25 @@ func (f *ParsedResource) DryRun(ctx context.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
fieldValidation := "Strict"
|
||||
if f.GVR == DashboardResource {
|
||||
fieldValidation = "Ignore" // FIXME: temporary while we improve validation
|
||||
}
|
||||
|
||||
// FIXME: shouldn't we check for the specific error?
|
||||
// Dry run CREATE or UPDATE
|
||||
f.Existing, _ = f.Client.Get(ctx, f.Obj.GetName(), metav1.GetOptions{})
|
||||
if f.Existing == nil {
|
||||
f.Action = provisioning.ResourceActionCreate
|
||||
f.DryRunResponse, err = f.Client.Create(ctx, f.Obj, metav1.CreateOptions{
|
||||
DryRun: []string{"All"},
|
||||
DryRun: []string{"All"},
|
||||
FieldValidation: fieldValidation,
|
||||
})
|
||||
} else {
|
||||
f.Action = provisioning.ResourceActionUpdate
|
||||
f.DryRunResponse, err = f.Client.Update(ctx, f.Obj, metav1.UpdateOptions{
|
||||
DryRun: []string{"All"},
|
||||
DryRun: []string{"All"},
|
||||
FieldValidation: fieldValidation,
|
||||
})
|
||||
}
|
||||
return err
|
||||
|
|
@ -262,13 +269,22 @@ func (f *ParsedResource) Run(ctx context.Context) error {
|
|||
f.Existing, _ = f.Client.Get(ctx, f.Obj.GetName(), metav1.GetOptions{})
|
||||
}
|
||||
|
||||
fieldValidation := "Strict"
|
||||
if f.GVR == DashboardResource {
|
||||
fieldValidation = "Ignore" // FIXME: temporary while we improve validation
|
||||
}
|
||||
|
||||
// Run update or create
|
||||
if f.Existing == nil {
|
||||
f.Action = provisioning.ResourceActionCreate
|
||||
f.Upsert, err = f.Client.Create(ctx, f.Obj, metav1.CreateOptions{})
|
||||
f.Upsert, err = f.Client.Create(ctx, f.Obj, metav1.CreateOptions{
|
||||
FieldValidation: fieldValidation,
|
||||
})
|
||||
} else {
|
||||
f.Action = provisioning.ResourceActionUpdate
|
||||
f.Upsert, err = f.Client.Update(ctx, f.Obj, metav1.UpdateOptions{})
|
||||
f.Upsert, err = f.Client.Update(ctx, f.Obj, metav1.UpdateOptions{
|
||||
FieldValidation: fieldValidation,
|
||||
})
|
||||
}
|
||||
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ func TestParser(t *testing.T) {
|
|||
Data: []byte("hello"), // not a real resource
|
||||
})
|
||||
require.Error(t, err)
|
||||
require.Equal(t, "classic resource must be JSON", err.Error())
|
||||
require.Equal(t, "unable to read file as a resource", err.Error())
|
||||
})
|
||||
|
||||
t.Run("dashboard parsing (with and without name)", func(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -177,10 +177,20 @@ func (r *ResourcesManager) WriteResourceFromFile(ctx context.Context, path strin
|
|||
parsed.Meta.SetUID("")
|
||||
parsed.Meta.SetResourceVersion("")
|
||||
|
||||
// TODO: use parsed.Run() (but that has an extra GET now!!)
|
||||
fieldValidation := "Strict"
|
||||
if parsed.GVR == DashboardResource {
|
||||
fieldValidation = "Ignore" // FIXME: temporary while we improve validation
|
||||
}
|
||||
|
||||
// Update or Create resource
|
||||
parsed.Upsert, err = parsed.Client.Update(ctx, parsed.Obj, metav1.UpdateOptions{})
|
||||
parsed.Upsert, err = parsed.Client.Update(ctx, parsed.Obj, metav1.UpdateOptions{
|
||||
FieldValidation: fieldValidation,
|
||||
})
|
||||
if apierrors.IsNotFound(err) {
|
||||
parsed.Upsert, err = parsed.Client.Create(ctx, parsed.Obj, metav1.CreateOptions{})
|
||||
parsed.Upsert, err = parsed.Client.Create(ctx, parsed.Obj, metav1.CreateOptions{
|
||||
FieldValidation: fieldValidation,
|
||||
})
|
||||
}
|
||||
|
||||
return parsed.Obj.GetName(), parsed.GVK, err
|
||||
|
|
|
|||
Loading…
Reference in New Issue