Skip to content

Instantly share code, notes, and snippets.

@welshstew
Last active March 5, 2020 12:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save welshstew/4f9532fd8737de1a43277c9154caa95e to your computer and use it in GitHub Desktop.
Save welshstew/4f9532fd8737de1a43277c9154caa95e to your computer and use it in GitHub Desktop.
Use oc run and mount volumes using overrides
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: bash-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 512Mi
oc run some-pod --overrides='
{
"spec": {
"containers": [
{
"command": [
"/bin/bash",
"-c",
"for i in {1..5}; do echo hi stuff; sleep 5; done"
],
"image": "registry.access.redhat.com/rhel7/rhel:latest",
"name": "some-pod",
"volumeMounts": [{
"mountPath": "/opt/scripts",
"name": "some-data"
}]
}
],
"volumes": [
{
"name": "some-data",
"persistentVolumeClaim": {
"claimName": "bash-claim"
}
}
]
}
}
' --image=dummy --restart=Never
@aceeric
Copy link

aceeric commented Mar 3, 2020

Hello - I found your post and I'm trying it with OpenShift 3.11.146, oc v3.11.59. The result I get is image pull error: docker.io/dummy not found. Realize your post is a couple years old - do you use this technique presently?

@welshstew
Copy link
Author

welshstew commented Mar 3, 2020

I just tried it and it still works..!
$ oc run some-pod --overrides='

{
"spec": {
"containers": [
{
"command": [
"/bin/bash",
"-c",
"for i in {1..5}; do echo hi stuff; sleep 5; done"
],
"image": "registry.access.redhat.com/rhel7/rhel:latest",
"name": "some-pod",
"volumeMounts": [{
"mountPath": "/opt/scripts",
"name": "some-data"
}]
}
],
"volumes": [
{
"name": "some-data",
"persistentVolumeClaim": {
"claimName": "bash-claim"
}
}
]
}
}
' --image=dummy --restart=Never
pod/some-pod created

with a later version of oc though...
$ oc version
Client Version: 4.3.2
Server Version: 4.3.3
Kubernetes Version: v1.16.2

Seems odd "--image" should really be ignored because the overrides would override it..!

@aceeric
Copy link

aceeric commented Mar 5, 2020

And working now. I went back through my notes but couldn't find what I had been doing wrong. I extended your example based on info found elsewhere to support using YAML as the override instead of JSON:

JSON=$(python -c 'import json,sys,yaml; y=yaml.safe_load(sys.stdin.read()); print(json.dumps(y))' <<EOF
spec:
  containers:
  - name: testcontainer
    image: ubuntu
  dnsPolicy: ClusterFirst
  volumes:
  - name: test
    persistentVolumeClaim:
      claimName: foo
  ...
EOF
)
oc -n namespace run test-pod --image=dummy --overrides=<<<"$JSON" --restart=Never

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment