Best way to check if a bucket is ready in node.js

2/15/2018

I'm using gcsfuse to mount a volume in a container, and I need it to start my node.js application.

To mount the volume I'm using the lifecycle hooks of kubernetes, but it doesn't ensure that it will be executed before the entrypoint of my container.

I've been thinking about how should I check when the volume is mounted, and if it goes down.

To check when it is mounted and unmounted I read and search the existence of the volume in /proc/mounts, and adding a watcher to it for changes.

Is there a simplier way to ensure that the volume is mounted in node.js, docker, or kubernetes?

-- Ivan Beldad
docker
gcsfuse
google-cloud-storage
kubernetes
node.js

1 Answer

2/23/2018

You can run this dockerfile in privileged mode:

FROM ubuntu 
RUN echo "deb http://packages.cloud.google.com/apt gcsfuse-stretch main" | tee /etc/apt/sources.list.d/gcsfuse.list 
RUN apt-get update && apt install curl -y 
RUN curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - 
RUN apt-get update 
RUN apt-get install gcsfuse fuse -y 
RUN mkdir -p /mnt/tmp 
CMD gcsfuse [BUCKET NAME] /mnt/tmp && /bin/bash

This way you are sure that the bucket is mounted when the pod initializes.

On the other hand I do not recommend this approach as there is a Node.sj library for Google Cloud Storage [1].

Here is an example of bucket listing:

// Imports the Google Cloud client library
const Storage = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

// Lists all buckets in the current project
storage
  .getBuckets()
  .then(results => {
    const buckets = results[0];

    console.log('Buckets:');
    buckets.forEach(bucket => {
      console.log(bucket.name);
    });
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

[1] https://github.com/googleapis/nodejs-storage/tree/master/samples

-- Pol Arroyo
Source: StackOverflow