Search code examples
javakubernetesspark-java

FileNotFoundException (operation not permitted) error but signs point to file present?


I am trying to deploy a SparkJava REST app in a Kubernetes container on my Windows machine.

  • Windows 10
  • Kubernetes v1.22.5
  • (edit) base image: openjdk:8-alpine

I am trying to read in a properties file when the app starts. I have created a volume mount in my YAML that points to where the file is. However, the container always crashes when I start it. I have taken screenshots of the YAML and the logs from the container. I have tried logging some test results to make sure the system can find the mounted drive and the file, and I also logged the canRead property to debug whether it is a permissions problem. Tests seem to indicate the file is visible and readable; but the error that is thrown would indicate otherwise.

Some research I did points to a possible bug or hack required to get the volume mount working correctly, but I haven't read anything that seems to mirror my issue closely.

Does anybody see what I am doing wrong?

enter image description here

enter image description here

Here is my java:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import static spark.Spark.*;

public class RestClient {

    private static final int sparkPort = 4567;

    public static void main(String[] args) {
        port(sparkPort);

        String hostPath = "/root";
        String propsFilePath = hostPath + "/resttest.properties";

        File host = new File(hostPath);

        if(!host.exists()){
            System.out.println("Could not find host path");
            return;
        }

        System.out.println("Found host path");

        File propsFile = new File(propsFilePath);

        if(!propsFile.exists()){
            System.out.println("Could not find host path");
            return;
        }

        System.out.println("Found propsFile path");
        System.out.println(">> isDirectory: " + propsFile.isDirectory());
        System.out.println(">> isFile: " + propsFile.isFile());
        System.out.println(">> canRead: " + propsFile.canRead());


        Properties properties = new Properties();
        FileInputStream fileInputStream = null;

        try {
            fileInputStream = new FileInputStream(propsFile);
        } catch (SecurityException fnf) {
            System.out.println("Security issue");
            fnf.printStackTrace();
            return;
        } catch (FileNotFoundException fnf) {
            System.out.println("Could not open file");
            fnf.printStackTrace();
            return;
        }

        try {
            properties.load(fileInputStream);
        } catch (IOException fe1) {
            fe1.printStackTrace();
        }

        get("/hello", (req,res) -> {
            return "Hello World!  My properties file is "+ propsFile +" and from it I learned I was "+ properties.getProperty("age") +" years old";
        });
    }
}

Solution

  • Posted community wiki answer based on the topic with similar issue on the GitHub. Feel free to expand it.


    The solution is to add /run/desktop/mnt/host before the /c/users/<some-folder>/<some-folder>/gits/resttest:

    - name: approot
      hostPath:
        path: /run/desktop/mnt/host/c/users/<some-folder>/<some-folder>/gits/resttest
        type: Directory