OpenShift oc create fails with "already exists"

11,361

Solution 1

The documentation is perhaps a bit badly worded. What it is saying is that if you try and create an object of a specific type where an object of that type with that name already exists, your attempt to create the new one will be ignored.

The situation you have will occur if you tried to create multiple instances of an application from the same raw resource definitions. There is no way using oc create -f from a set of raw resource definitions to override names from the command line so a second deployment is distinct.

What you would need to do if you want to create multiple instances from the same resource definitions, is to convert the definitions into a template and parameterise on the name so that you can pass in a different name for different instances. That way there will not be a clash.

Also, when you do create a set of resources, it is usually better to use the one name across all the resource types and not use a different name for each, eg., use just 'my-app-name' for all of them, and not separately, 'my-buildconfig', 'my-imagestream'.

More importantly, you need to ensure you add a label with same key/value on them all so it is easy then to work with them together, including deleting them all in one go.

What is the type of application you are trying to deploy? I can possibly point you at example templates you can use as a guide.


UPDATE 1

If you want to be able run oc create -f and have it not complain if the resources already exist, but create them if they don't, use oc apply -f instead.

Solution 2

How about using oc replace:

oc replace --filename file.yml --force
Share:
11,361

Related videos on Youtube

Tom Manterfield
Author by

Tom Manterfield

Tech advisor and platform engineer. When nobody is looking I sneak off and write code.

Updated on September 14, 2022

Comments

  • Tom Manterfield
    Tom Manterfield over 1 year

    Attempting to create a set of resources based on a file via oc create fails if they already exist.

    According to the docs here oc create should:

    Parse a configuration file and create one or more OpenShift Enterprise objects ... Any existing resources are ignored.

    (emphasis mine).

    I can't see any config options for this command or globally that would alter this behaviour and it seems to me to be counter to the docs.

    The command I ran is oc create -f some.file

    The output is:

    Error from server: services 'my-app' already exists
    Error from server: buildconfigs 'my-app' already exists
    Error from server: imagestreams 'my-app' already exists
    Error from server: deploymentconfigs 'my-app' already exists
    Error from server: routes 'my-app' already exists
    Error from server: secrets 'my-app' already exists
    

    It also exits with a non-zero exit code, so it's not just a warning. Am I missing something obvious here or misunderstanding what the documentation is saying?

    I just want to be able to apply this file and ensure the state of the OpenShift project afterwards.

  • Tom Manterfield
    Tom Manterfield over 6 years
    Sorry, what I'm actually trying to do is to 'create' the same resources (if they don't exist). Essentially trying to use it as an idempotent call, to ensure the set of resources exist once, rather than re-create them. Instead of ignoring that attempt, it throws the error. It's worth noting I do actually use the same name for each resource, I just used the above as (poor) examples. The same goes for them each having a label.
  • Graham Dumpleton
    Graham Dumpleton over 6 years
    Try using 'oc apply' instead of 'oc create'. If it doesn't exist it will be created. If it exists, it will be updated to be what you are trying to set it to. If don't want current one made to match what trying to set it to, look at seeing if --overwrite=false option to oc apply achieves what you want.
  • Tom Manterfield
    Tom Manterfield over 6 years
    Oh perfect, that seems to be what I really wanted! Would you mind adding it to your answer so I can accept please?