I need to create kubernetes job that will run below script on mongo shell:
var operations = [];
db.product.find().forEach(function(doc) {
var documentLink = doc.documentLink;
var operation = { updateMany :{
"filter" : {"_id" : doc._id},
"update" : {$set:{"documentLinkMap.en":documentLink,"documentLinkMap.de":""},
$unset: {documentLink:"","descriptionMap.tr":"","news.tr":"","descriptionInternal.tr":"","salesDescription.tr":"","salesInternal.tr":"","deliveryDescription.tr":"","deliveryInternal.tr":"","productRoadMapDescription.tr":"","productRoadMapInternal.tr":"","technicalsAndIntegration.tr":"","technicalsAndIntegrationInternal.tr":"","versions.$[].descriptionMap.tr":"","versions.$[].releaseNoteMap.tr":"","versions.$[].artifacts.$[].descriptionMap.tr":"","versions.$[].artifacts.$[].artifactNotes.tr":""}}}};
operations.push(operation);
});
operations.push( {
ordered: true,
writeConcern: { w: "majority", wtimeout: 5000 }
});
db.product.bulkWrite(operations);
I will need a sample of how that job will look like. Should I create presistent volume and claim to it or is there possibility to run this job without persistent volume? I need to run this once and then remove it.
You can solve it much easier with configMap and then mount the configMap as a volume which will be resolved in a file.
Below is example how to proceed with it (Note! You will need to use a proper image for it as well as some other changes how mongo shell works):
Create a configMap from file. Can be done by running this command:
$ kubectl create cm mongoscript-cm --from-file=mongoscript.js
configmap/mongoscript-cm created
You can check that you file is stored inside by running:
$ kubectl describe cm mongoscript-cm
Create a job with volume mount from configmap (spec template is the same as it used in pods):
apiVersion: batch/v1
kind: Job
metadata:
name: mongojob
spec:
template:
spec:
containers:
- name: mongojob
image: ubuntu # for testing purposes, you need to use appropriate one
command: ['bin/bash', '-c', 'echo STARTED ; cat /opt/mongoscript.js ; sleep 120 ; echo FINISHED'] # same for command, that's for demo purposes
volumeMounts:
- name: mongoscript
mountPath: /opt # where to mount the file
volumes:
- name: mongoscript
configMap:
name: mongoscript-cm # reference to previously created configmap
restartPolicy: OnFailure # required for jobs
Checking how it looks inside the pod
Connect to the pod:
$ kubectl exec -it mongojob--1-8w4ml -- /bin/bash
Check file is presented:
# ls /opt
mongoscript.js
Check its content:
# cat /opt/mongoscript.js
var operations = [];
db.product.find().forEach(function(doc) {
var documentLink = doc.documentLink;
var operation = { updateMany :{
"filter" : {"_id" : doc._id},
"update" : {$set {"documentLinkMap.en":documentLink,"documentLinkMap.de":""},
$unset: {documentLink:"","descriptionMap.tr":"","news.tr":"","descriptionInternal.tr":"","salesDescription.tr":"","salesInternal.tr":"","deliveryDescription.tr":"","deliveryInternal.tr":"","productRoadMapDescription.tr":"","productRoadMapInternal.tr":"","technicalsAndIntegration.tr":"","technicalsAndIntegrationInternal.tr":"","versions.$[].descriptionMap.tr":"","versions.$[].releaseNoteMap.tr":"","versions.$[].artifacts.$[].descriptionMap.tr":"","versions.$[].artifacts.$[].artifactNotes.tr":""}}}};
operations.push(operation);
});
operations.push( {
ordered: true,
writeConcern: { w: "majority", wtimeout: 5000 }
});
db.product.bulkWrite(operations);