MatiWall / CatCode.Service.CoreAPI

0 stars 0 forks source link

CoreAPI Resource Cache #4

Open MatiWall opened 2 months ago

MatiWall commented 2 months ago

In memory cache used to efficiently map between the singular and plural resource names to api group (and versions)

Should map between

MatiWall commented 2 months ago

class KubernetesResourceCache: def init(self):

Initialize all the mappings

    self.singular_to_kind = {}
    self.singular_to_group = {}
    self.plural_to_kind = {}
    self.plural_to_group = {}
    self.singular_to_plural = {}
    self.plural_to_singular = {}
    self.kind_to_plural = {}
    self.kind_to_singular = {}
    self.kind_to_group_version = {}
    self.group_version_to_plural = {}

# Method to add a resource definition to the cache
def add_resource(self, singular_name, plural_name, kind, group, version):
    # Fill the basic mappings
    self.singular_to_kind[singular_name] = kind
    self.singular_to_group[singular_name] = group
    self.plural_to_kind[plural_name] = kind
    self.plural_to_group[plural_name] = group

    # Map singular to plural and vice versa
    self.singular_to_plural[singular_name] = plural_name
    self.plural_to_singular[plural_name] = singular_name

    # Kind mappings
    self.kind_to_plural[kind] = plural_name
    self.kind_to_singular[kind] = singular_name

    # Group/Version mappings
    group_version = f"{group}/{version}"
    self.kind_to_group_version[kind] = group_version
    self.group_version_to_plural[group_version] = plural_name

# Example method for retrieving plural from singular
def get_plural_from_singular(self, singular_name):
    return self.singular_to_plural.get(singular_name)

# Example method for retrieving singular from plural
def get_singular_from_plural(self, plural_name):
    return self.plural_to_singular.get(plural_name)

# Example method for retrieving kind from singular
def get_kind_from_singular(self, singular_name):
    return self.singular_to_kind.get(singular_name)

# Example method for retrieving group from singular
def get_group_from_singular(self, singular_name):
    return self.singular_to_group.get(singular_name)

# Example method for retrieving plural from kind
def get_plural_from_kind(self, kind):
    return self.kind_to_plural.get(kind)

# Example method for retrieving group/version from kind
def get_group_version_from_kind(self, kind):
    return self.kind_to_group_version.get(kind)

# Example method for retrieving plural from group/version
def get_plural_from_group_version(self, group_version):
    return `self.group_version_to_plural.get(group_version)