Home > Mobile >  Kubernetes Java client: cannot mount an existing configMap to a Pod
Kubernetes Java client: cannot mount an existing configMap to a Pod

Time:12-17

I'm developing a Java application that creates some Kubernetes Jobs using the official Kubernetes Java client. Each Job uses its configuration directory, which already exists as a configMap in the cluster. (This configMap was created using kubectl create configmap {name} --from-file=/... )

Using Java client, I successfully created a V1ConfigMap object that refers to a specific configMap, and I also found that V1Volume.setConfigMap() will convert V1ConfigMapVolumeSource to V1Volume that can be mounted by a container.

However, I couldn't find the way to map V1ConfigMap and V1ConfigMapVolumeSource.

Here is my code:

public void setConfigMap(V1ConfigMap cm, String mountPath){
       
    V1ConfigMapVolumeSource volSource = new V1ConfigMapVolumeSource();
        
    //Some additional mappings are needed here.
    //volSource = ...(cm)

    //create V1Volume from V1ConfigMapVolumeSource
    String volName = "appSetting";
    V1Volume settingVol = new V1Volume().name(volName);
    settingVol.setConfigMap(volSource);

    //create V1VolumeMount
    V1VolumeMount volumeMount = new V1VolumeMount();
    volumeMount.setMountPath(mountPath);
    volumeMount.setName(volName);

    //set created objects to the Job
    job.getSpec().getTemplate().getSpec().addVolumesItem(settingVol);
    job.getSpec().getTemplate().getSpec().getContainers().get(0).addVolumeMountsItem(volumeMount);
}

Does anyone know the way to solve this, or is my way to approach completely wrong?

CodePudding user response:

As stated into the documentation for V1ConfigMapVolumeSource, there is a name parameter (type String) which is the name of the referent.

To link a ConfigMap inside a ConfigMapVolumeSource, just put the name of the ConfigMap in the name parameter, that should be enough.

  • Related