Skip to content

Commit

Permalink
Add unlink/rmdir. Fixes opencredo#7
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebryant committed Jul 26, 2016
1 parent d0e8124 commit a8ea7ff
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
7 changes: 5 additions & 2 deletions kubefuse/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ def get(self, key):
return None

def delete(self, key):
del(self._timestamps[key])
del(self._cache[key])
try:
del(self._timestamps[key])
del(self._cache[key])
except KeyError:
pass
19 changes: 18 additions & 1 deletion kubefuse/client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import logging
import subprocess
import yaml
import tempfile

from cache import ExpiringCache

LOGGER = logging.getLogger(__name__)

class KubernetesClient(object):
def __init__(self):
self._cache = ExpiringCache(30)
Expand All @@ -24,17 +27,31 @@ def _load_from_cache_or_do(self, key, func):
result = func()
self._cache.set(key, result)
return result

def get_namespaces(self):
key = "namespaces"
cb = self._get_namespaces
return self._load_from_cache_or_do(key, cb)

def delete_namespace(self, namespace):
LOGGER.info(self._run_command(('delete namespace ' + namespace).split()))
self._cache.delete('namespaces')

def get_entities(self, ns, entity):
key = "%s.%s" % (ns, entity)
cb = lambda: self._get_entities(ns,entity)
return self._load_from_cache_or_do(key, cb)

def delete_entities(self, ns, entity_type):
key = "%s.%s" % (ns, entity_type)
LOGGER.info(self._run_command(("delete %s --all " % entity_type).split() + self._namespace(ns)))
self._cache.delete(key)

def delete_entity(self, ns, entity_type, object_name):
key = "%s.%s" % (ns, entity_type)
LOGGER.info(self._run_command(("delete %s %s " % (entity_type, object_name)).split() + self._namespace(ns)))
self._cache.delete(key)

def get_object_in_format(self, ns, entity_type, object, format):
key = "%s.%s.%s.%s" % (ns, entity_type, object, format)
cb = lambda: self._get_object_in_format(ns, entity_type, object, format)
Expand Down
31 changes: 31 additions & 0 deletions kubefuse/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,37 @@ def list_files(self, path):
return p.SUPPORTED_RESOURCE_TYPES
return self.client.get_namespaces() # + ['all']

def rmdir(self, path):
p = KubePath().parse_path(path)
if not p.exists(self.client):
logging.info("path doesn't exist")
raise FuseOSError(errno.ENOENT)
if not p.is_dir():
logging.info("not a directory")
raise FuseOSError(errno.ENOTDIR)

if p.action:
raise FuseOSError(errno.EROFS)
if p.object_id:
return self.client.delete_entity(p.namespace, p.resource_type, p.object_id)
if p.resource_type:
return self.client.delete_entities(p.namespace, p.resource_type)
if p.namespace:
return self.client.delete_namespace(p.namespace)
raise FuseOSError(errno.EROFS)

def unlink(self, path):
p = KubePath().parse_path(path)
if not p.exists(self.client):
logging.info("path doesn't exist")
raise FuseOSError(errno.ENOENT)
if p.is_dir():
logging.info("is a directory")
raise FuseOSError(errno.EISDIR)
# We just let unlinks succeed, though the files will remain
# This is for compatibility with `rm -rf` - though just using rmdir
# will have the same effect in this implementation

def getattr(self, path):
p = KubePath().parse_path(path)
if path in self.open_files:
Expand Down
6 changes: 6 additions & 0 deletions kubefuse/kubefuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def readdir(self, path, fh):
def getattr(self, path, fh=None):
return self.fs.getattr(path)

def rmdir(self, path):
return self.fs.rmdir(path)

def unlink(self, path):
return self.fs.unlink(path)

def open(self, path, fh):
self.fd += 1
self.fs.open(path, fh)
Expand Down

0 comments on commit a8ea7ff

Please sign in to comment.