Skip to content

Commit

Permalink
feat: Knative deployment support
Browse files Browse the repository at this point in the history
Signed-off-by: Abhishek Kumar <[email protected]>
  • Loading branch information
octonawish-akcodes committed Jun 20, 2024
1 parent 0b8d777 commit 496b8c1
Show file tree
Hide file tree
Showing 9 changed files with 857 additions and 1 deletion.
39 changes: 39 additions & 0 deletions config/systems.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,44 @@
}
}
}
},
"knative": {
"languages": {
"python": {
"base_images": {
"3.9": "python:3.9-slim",
"3.10": "python:3.10-slim"
},
"images": [
"build",
"run"
],
"username": "docker_user",
"deployment": {
"files": [
"handler.py",
"storage.py"
],
"packages": []
}
},
"nodejs": {
"base_images": {
"latest": "node:latest"
},
"images": [
"build",
"run"
],
"username": "docker_user",
"deployment": {
"files": [
"handler.js",
"storage.js"
],
"packages": []
}
}
}
}
}
2 changes: 1 addition & 1 deletion sebs.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def common_params(func):
@click.option(
"--deployment",
default=None,
type=click.Choice(["azure", "aws", "gcp", "local", "openwhisk"]),
type=click.Choice(["azure", "aws", "gcp", "local", "openwhisk", "knative"]),
help="Cloud deployment to use.",
)
@click.option(
Expand Down
4 changes: 4 additions & 0 deletions sebs/faas/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ def deserialize(config: dict, cache: Cache, handlers: LoggingHandlers) -> Config
from sebs.openwhisk.config import OpenWhiskConfig

implementations["openwhisk"] = OpenWhiskConfig.deserialize
if has_platform("knative"):
from sebs.knative.config import KnativeConfig

implementations["knative"] = KnativeConfig.deserialize
func = implementations.get(name)
assert func, "Unknown config type!"
return func(config[name] if name in config else config, cache, handlers)
Expand Down
226 changes: 226 additions & 0 deletions sebs/knative/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
from sebs.cache import Cache
from sebs.faas.config import Credentials, Resources, Config
from sebs.utils import LoggingHandlers
from sebs.storage.config import MinioConfig

from typing import cast, Optional


class KnativeCredentials(Credentials):
@staticmethod
def deserialize(config: dict, cache: Cache, handlers: LoggingHandlers) -> Credentials:
return KnativeCredentials()

def serialize(self) -> dict:
return {}


class KnativeResources(Resources):
def __init__(
self,
registry: Optional[str] = None,
username: Optional[str] = None,
password: Optional[str] = None,
registry_updated: bool = False,
):
super().__init__(name="knative")
self._docker_registry = registry if registry != "" else None
self._docker_username = username if username != "" else None
self._docker_password = password if password != "" else None
self._registry_updated = registry_updated
self._storage: Optional[MinioConfig] = None
self._storage_updated = False

@staticmethod
def typename() -> str:
return "Knative.Resources"

@property
def docker_registry(self) -> Optional[str]:
return self._docker_registry

@property
def docker_username(self) -> Optional[str]:
return self._docker_username

@property
def docker_password(self) -> Optional[str]:
return self._docker_password

@property
def storage_config(self) -> Optional[MinioConfig]:
return self._storage

@property
def storage_updated(self) -> bool:
return self._storage_updated

@property
def registry_updated(self) -> bool:
return self._registry_updated

@staticmethod
def initialize(res: Resources, dct: dict):
ret = cast(KnativeResources, res)
ret._docker_registry = dct["registry"]
ret._docker_username = dct["username"]
ret._docker_password = dct["password"]

@staticmethod
def deserialize(config: dict, cache: Cache, handlers: LoggingHandlers) -> Resources:

cached_config = cache.get_config("knative")
ret = KnativeResources()
if cached_config:
super(KnativeResources, KnativeResources).initialize(
ret, cached_config["resources"]
)

# Check for new config - overrides but check if it's different
if "docker_registry" in config:

KnativeResources.initialize(ret, config["docker_registry"])
ret.logging.info("Using user-provided Docker registry for Knative.")
ret.logging_handlers = handlers

# check if there has been an update
if not (
cached_config
and "resources" in cached_config
and "docker" in cached_config["resources"]
and cached_config["resources"]["docker"] == config["docker_registry"]
):
ret._registry_updated = True

# Load cached values
elif (
cached_config
and "resources" in cached_config
and "docker" in cached_config["resources"]
):
KnativeResources.initialize(ret, cached_config["resources"]["docker"])
ret.logging_handlers = handlers
ret.logging.info("Using cached Docker registry for Knative")
else:
ret = KnativeResources()
ret.logging.info("Using default Docker registry for Knative.")
ret.logging_handlers = handlers
ret._registry_updated = True

# Check for new config
if "storage" in config:
ret._storage = MinioConfig.deserialize(config["storage"])
ret.logging.info("Using user-provided configuration of storage for Knative.")

# check if there has been an update
if not (
cached_config
and "resources" in cached_config
and "storage" in cached_config["resources"]
and cached_config["resources"]["storage"] == config["storage"]
):
ret.logging.info(
"User-provided configuration is different from cached storage, "
"we will update existing Knative actions."
)
ret._storage_updated = True

# Load cached values
elif (
cached_config
and "resources" in cached_config
and "storage" in cached_config["resources"]
):
ret._storage = MinioConfig.deserialize(cached_config["resources"]["storage"])
ret.logging.info("Using cached configuration of storage for Knative.")

return ret

def update_cache(self, cache: Cache):
super().update_cache(cache)
cache.update_config(
val=self.docker_registry, keys=["knative", "resources", "docker", "registry"]
)
cache.update_config(
val=self.docker_username, keys=["knative", "resources", "docker", "username"]
)
cache.update_config(
val=self.docker_password, keys=["knative", "resources", "docker", "password"]
)
if self._storage:
self._storage.update_cache(["knative", "resources", "storage"], cache)

def serialize(self) -> dict:
out: dict = {
**super().serialize(),
"docker_registry": self.docker_registry,
"docker_username": self.docker_username,
"docker_password": self.docker_password,
}
if self._storage:
out = {**out, "storage": self._storage.serialize()}
return out


class KnativeConfig(Config):
name: str
shutdownStorage: bool
cache: Cache

def __init__(self, config: dict, cache: Cache):
super().__init__(name="knative")
self._credentials = KnativeCredentials()
self._resources = KnativeResources()
self.shutdownStorage = config["shutdownStorage"]
self.removeCluster = config["removeCluster"]
self.knative_exec = config["knativeExec"]
self.knative_bypass_security = config["knativeBypassSecurity"]
self.experimentalManifest = config["experimentalManifest"]
self.cache = cache

@property
def credentials(self) -> KnativeCredentials:
return self._credentials

@property
def resources(self) -> KnativeResources:
return self._resources

@staticmethod
def initialize(cfg: Config, dct: dict):
cfg._region = dct["region"]

def serialize(self) -> dict:
return {
"name": self._name,
"region": self._region,
"shutdownStorage": self.shutdownStorage,
"removeCluster": self.removeCluster,
"knativeExec": self.knative_exec,
"knativeBypassSecurity": self.knative_bypass_security,
"experimentalManifest": self.experimentalManifest,
"credentials": self._credentials.serialize(),
"resources": self._resources.serialize(),
}

@staticmethod
def deserialize(config: dict, cache: Cache, handlers: LoggingHandlers) -> Config:
cached_config = cache.get_config("knative")
resources = cast(
KnativeResources, KnativeResources.deserialize(config, cache, handlers)
)

res = KnativeConfig(config, cached_config)
res.logging_handlers = handlers
res._resources = resources
return res

def update_cache(self, cache: Cache):
cache.update_config(val=self.shutdownStorage, keys=["knative", "shutdownStorage"])
cache.update_config(val=self.removeCluster, keys=["knative", "removeCluster"])
cache.update_config(val=self.knative_exec, keys=["knative", "knativeExec"])
cache.update_config(val=self.knative_bypass_security, keys=["knative", "knativeBypassSecurity"])
cache.update_config(
val=self.experimentalManifest, keys=["knative", "experimentalManifest"]
)
self.resources.update_cache(cache)
Loading

0 comments on commit 496b8c1

Please sign in to comment.