Search code examples
kubernetesopenshiftopenshift-originopenshift-3okd

How to order the creation of objects in an Openshift template


I'm trying to create a template that allows an application to be promoted across several environments. Currently I create a project and then import the template into that, the template creates all the objects needed in the default namespace(the project I manually created), the other projects I need and a cluster role allowing me to promote pods through the environments. The problem is that I also want the template to create objects in the projects that were newly created but this fails with the error message:

the namespace of the provided object does not match the namespace sent on the request

I think this may be an issue with Openshift trying to create the objects at the same time as the projects and it doesn't find the project at the time it tries to create the objects. To fix this I tried using the service.alpha.openshift.io/dependencies annotation but got the same error

Any assistance would be greatly appreciated


Solution

  • According to documentation:

    If an object definition’s metadata includes a fixed namespace field value, the field will be stripped out of the definition during template instantiation. If the namespace field contains a parameter reference, normal parameter substitution will be performed and the object will be created in whatever namespace the parameter substitution resolved the value to, assuming the user has permission to create objects in that namespace.

    So just specify required namespaces as template's parameters. Usable example:

    apiVersion: template.openshift.io/v1
    kind: Template
    metadata:
      name: yyy
    parameters:
    - name: ns3
      value: test3
    objects:
    - apiVersion: v1
      kind: Namespace
      metadata:
        name: ${ns3}
      spec:
        finalizers:
        - kubernetes
    - apiVersion: v1
      data:
        key3: value3
      kind: ConfigMap
      metadata:
        namespace: ${ns3}
        name: cm3