tinkerbell / tink

Workflow Engine for provisioning Bare Metal
https://tinkerbell.org
Apache License 2.0
914 stars 134 forks source link

Creating a template sharing the same name as a deleted template causes an error #363

Closed kqdeng closed 3 years ago

kqdeng commented 3 years ago

Expected Behaviour

Templates can be successfully created even when it shares the same name as a deleted template.

Current Behaviour

Due to the fact that templates are soft deleted and the recent change of name also being a unique field, creating a template sharing the same name as a deleted template returns this error:

rpc error: code = Unknown desc = INSERT: pq: duplicate key value violates unique constraint "template_name_key"

Possible Solution

Steps to Reproduce (for bugs)

  1. create a unique constraint on name (if db isn't using the latest tink migrations)
    alter table template add constraint template_name_key unique (name);
  2. create a workflow with a name
  3. delete the workflow
  4. create another workflow with the same name as in step 1

Context

At first, I thought the soft delete was to be able to get workflow data even after the associated template has been (soft) deleted, but this is not the case. Therefore, what exactly is the purpose of soft deleting?

Granted, there is a workaround for this (e.g. use a UUID as part of the name), but it seems unintuitive that new templates have to take deleted templates into consideration. Also, the only way to see what names have already been used is going into the db.

Your Environment

Getting a workflow with a deleted associated template

/ # tink template list
+--------------------------------------+-----------------+-------------------------------+-------------------------------+
| TEMPLATE ID                          | TEMPLATE NAME   | CREATED AT                    | UPDATED AT                    |
+--------------------------------------+-----------------+-------------------------------+-------------------------------+
| f1e780de-2460-11eb-ad81-0242ac130004 | hello-new-world | 2020-11-11 21:00:35 +0000 UTC | 2020-11-11 21:00:35 +0000 UTC |
+--------------------------------------+-----------------+-------------------------------+-------------------------------+
/ # tink workflow list
+--------------------------------------+--------------------------------------+-----------------------------------+-------------------------------+-------------------------------+
| WORKFLOW ID                          | TEMPLATE ID                          | HARDWARE DEVICE                   | CREATED AT                    | UPDATED AT                    |
+--------------------------------------+--------------------------------------+-----------------------------------+-------------------------------+-------------------------------+
| d3de4f6a-246c-11eb-933f-0242ac130004 | f1e780de-2460-11eb-ad81-0242ac130004 | {"device_1": "b4:96:91:5f:a9:50"} | 2020-11-11 22:25:39 +0000 UTC | 2020-11-11 22:25:39 +0000 UTC |
+--------------------------------------+--------------------------------------+-----------------------------------+-------------------------------+-------------------------------+
/ # tink workflow get d3de4f6a-246c-11eb-933f-0242ac130004
version: 0.1
name: hello_world
global_timeout: 6000
tasks:
- name: "hello-world"
  worker: "b4:96:91:5f:a9:50"
  actions:
  - name: "hello-world"
    image: hello-world
    timeout: 90

/ # tink template delete f1e780de-2460-11eb-ad81-0242ac130004
/ # tink workflow get d3de4f6a-246c-11eb-933f-0242ac130004
2020/11/11 23:01:12 rpc error: code = Unknown desc = failed to get template with ID: f1e780de-2460-11eb-ad81-0242ac130004: sql: no rows in result set

Original reporter: Matt Burns

parauliya commented 3 years ago

@kdeng3849 @gianarb , The reason behind doing a soft delete for template was to keep the workflow and whenever user wants to do a workflow get <workflow_id> it should get the workflow. But this is not happening right now as we are getting error while getting the workflow after deletion of it's template. And this is what we need to fix first.

Also regarding issue where user is not able to create a new template with the same name even after deleting the existing one, this is also an issue and needed a fix.

As per the above comment, I would like to do following fixes for this issue:

  1. Let the user create the template with the same name if the existing one is deleted (soft).
  2. Workflow get should not give the error even if the template is deleted.

Please let me know if you feel any concern with the approach or fixes asap.

mattcburns commented 3 years ago

So is there no way to permanently delete a workflow template? It seems weird to me as a user that if I delete something it still hangs around.