How to have multiple object types in a single openshift yaml template?
Asked Answered
B

3

6

Here is the sample template for single PersistentVolumeClaim with name : claim1

apiVersion: "v1"
kind: "PersistentVolumeClaim"
metadata:
  name: "claim1"
spec:
  accessModes:
    - "ReadWriteOnce"
  resources:
    requests:
      storage: "5Gi"
  volumeName: "pv0001"

How can i add multiple PersistentVolumeClaim in the same template file. For example adding claim2

I tried copy paste the same template from below it and change claim1 to claim2 but in openshift UI while importing the template it gives a warning : Duplicated Mapping key kind: PersistentVolumeClaim

Update: I have tried using --- as mentioned in the comments. But that throws a error Expected a single document in the stream but found more in the openshift UI while importing the yaml

Bereniceberenson answered 11/12, 2019 at 14:20 Comment(2)
Regarding your error message "Expected a single document in the stream but found more", I suspect that the OpenShift UI is looking for an actual template. Using the --- in a yaml file likely would work just fine with oc create -f file.yml from the CLIAbstergent
Yes you are right. I used the template as mentioned in your answer, it worked fine. Thank youBereniceberenson
A
3

I would recommend writing an actual template: https://docs.okd.io/latest/dev_guide/templates.html#writing-templates

Specifically it would look something like:

apiVersion: v1
kind: Template
metadata:
  name: multiple-pvcs
objects:
- apiVersion: "v1"
  kind: "PersistentVolumeClaim"
  metadata:
    name: "claim1"
  spec:
    accessModes:
      - "ReadWriteOnce"
    resources:
      requests:
        storage: "5Gi"
    volumeName: "pv0001"
- apiVersion: "v1"
  kind: "PersistentVolumeClaim"
  metadata:
    name: "claim2"
  spec:
    accessModes:
      - "ReadWriteOnce"
    resources:
      requests:
        storage: "5Gi"
    volumeName: "pv0002"
parameters: []

Then you can just create the objects with oc process template.yml | oc create -f -

Abstergent answered 11/12, 2019 at 15:45 Comment(0)
L
15

To separate the objects all you need to add is ---

Yeap exactly this:

---

for example

apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  name: replicaset-example-webapp-0-5
spec:
  selector:
    matchLabels:
      mylabel: mywebapp
  replicas: 6
  template:
    metadata:
      labels:
        mylabel: mywebapp
    spec:
      containers:
        - name: pod-example-0-5
          image: richardchesterwood/k8s-fleetman-webapp-angular:release0-5
---
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  name: replicaset-example-webapp-0
spec:
  selector:
    matchLabels:
      mylabel: mywebapp
  replicas: 2
  template:
    metadata:
      labels:
        mylabel: mywebapp
    spec:
      containers:
        - name: pod-example-0
          image: richardchesterwood/k8s-fleetman-webapp-angular:release0
---
apiVersion: v1
kind: Pod
metadata:
  name: just-a-queue
  labels:
    mylabelname: myqueueapp
spec:
  containers:
  - name: queue-app-container
    image: richardchesterwood/k8s-fleetman-queue:release1

Also you can have multiple Kubernetes objects under the same yaml file, all you need to do is to separate it with ---

But in OpenShift it's like this

Actually, this works if one creates an array of items:

apiVersion: v1beta3                                                                              
kind: List                                                                                       
items:                                                                                           
  - #list of API objects
Lohrman answered 11/12, 2019 at 14:30 Comment(2)
It throws an error in the openshift UI while importing: "Expected a single document in the stream but found more"Bereniceberenson
Thanks that helped.Bereniceberenson
A
3

I would recommend writing an actual template: https://docs.okd.io/latest/dev_guide/templates.html#writing-templates

Specifically it would look something like:

apiVersion: v1
kind: Template
metadata:
  name: multiple-pvcs
objects:
- apiVersion: "v1"
  kind: "PersistentVolumeClaim"
  metadata:
    name: "claim1"
  spec:
    accessModes:
      - "ReadWriteOnce"
    resources:
      requests:
        storage: "5Gi"
    volumeName: "pv0001"
- apiVersion: "v1"
  kind: "PersistentVolumeClaim"
  metadata:
    name: "claim2"
  spec:
    accessModes:
      - "ReadWriteOnce"
    resources:
      requests:
        storage: "5Gi"
    volumeName: "pv0002"
parameters: []

Then you can just create the objects with oc process template.yml | oc create -f -

Abstergent answered 11/12, 2019 at 15:45 Comment(0)
E
0

you can split yaml documents by adding on a new line

---

see https://yaml.org/spec/1.2/spec.html#id2760395

Elfie answered 11/12, 2019 at 14:24 Comment(1)
It throws an error in the openshift UI while importing: "Expected a single document in the stream but found more"Bereniceberenson

© 2022 - 2024 — McMap. All rights reserved.