Skip to content

Commit

Permalink
Merge pull request #252 from jumpstarter-dev/metadata
Browse files Browse the repository at this point in the history
Add ObjectMeta to config
  • Loading branch information
mangelajo authored Feb 6, 2025
2 parents 4049bf8 + e5e41e7 commit 8d64e20
Show file tree
Hide file tree
Showing 18 changed files with 226 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
ClientConfigV1Alpha1,
ClientConfigV1Alpha1Drivers,
ExporterConfigV1Alpha1,
ObjectMeta,
)

# Generate a random client name
Expand All @@ -32,19 +33,21 @@
CLIENT_OBJECT = V1Alpha1Client(
api_version="jumpstarter.dev/v1alpha1",
kind="Client",
metadata=V1ObjectMeta(name=CLIENT_NAME, namespace="default", creation_timestamp="2024-01-01T21:00:00Z"),
metadata=V1ObjectMeta(namespace="default", name=CLIENT_NAME, creation_timestamp="2024-01-01T21:00:00Z"),
status=V1Alpha1ClientStatus(endpoint=CLIENT_ENDPOINT, credential=None),
)

UNSAFE_CLIENT_CONFIG = ClientConfigV1Alpha1(
name=CLIENT_NAME,
metadata=ObjectMeta(namespace="default", name=CLIENT_NAME),
endpoint=CLIENT_ENDPOINT,
token=CLIENT_TOKEN,
drivers=ClientConfigV1Alpha1Drivers(allow=[], unsafe=True),
)

CLIENT_CONFIG = ClientConfigV1Alpha1(
name=CLIENT_NAME,
metadata=ObjectMeta(namespace="default", name=CLIENT_NAME),
endpoint=CLIENT_ENDPOINT,
token=CLIENT_TOKEN,
drivers=ClientConfigV1Alpha1Drivers(allow=[], unsafe=True),
Expand Down Expand Up @@ -116,11 +119,12 @@ async def test_create_client(
EXPORTER_OBJECT = V1Alpha1Exporter(
api_version="jumpstarter.dev/v1alpha1",
kind="Exporter",
metadata=V1ObjectMeta(name=EXPORTER_NAME, namespace="default", creation_timestamp="2024-01-01T21:00:00Z"),
metadata=V1ObjectMeta(namespace="default", name=EXPORTER_NAME, creation_timestamp="2024-01-01T21:00:00Z"),
status=V1Alpha1ExporterStatus(endpoint=EXPORTER_ENDPOINT, credential=None, devices=[]),
)
EXPORTER_CONFIG = ExporterConfigV1Alpha1(
alias=EXPORTER_NAME,
metadata=ObjectMeta(namespace="default", name=EXPORTER_NAME),
endpoint=EXPORTER_ENDPOINT,
token=EXPORTER_TOKEN,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ClientConfigV1Alpha1,
ClientConfigV1Alpha1Drivers,
ExporterConfigV1Alpha1,
ObjectMeta,
UserConfigV1Alpha1,
UserConfigV1Alpha1Config,
)
Expand All @@ -29,6 +30,7 @@

CLIENT_CONFIG = ClientConfigV1Alpha1(
name=CLIENT_NAME,
metadata=ObjectMeta(namespace="default", name=CLIENT_NAME),
endpoint=CLIENT_ENDPOINT,
token=CLIENT_TOKEN,
drivers=ClientConfigV1Alpha1Drivers(allow=[], unsafe=True),
Expand Down Expand Up @@ -133,11 +135,12 @@ async def test_delete_client(
EXPORTER_OBJECT = V1Alpha1Exporter(
api_version="jumpstarter.dev/v1alpha1",
kind="Exporter",
metadata=V1ObjectMeta(name=EXPORTER_NAME, namespace="default", creation_timestamp="2024-01-01T21:00:00Z"),
metadata=V1ObjectMeta(namespace="default", name=EXPORTER_NAME, creation_timestamp="2024-01-01T21:00:00Z"),
status=V1Alpha1ExporterStatus(endpoint=EXPORTER_ENDPOINT, credential=None, devices=[]),
)
EXPORTER_CONFIG = ExporterConfigV1Alpha1(
alias=EXPORTER_NAME,
metadata=ObjectMeta(namespace="default", name=EXPORTER_NAME),
endpoint=EXPORTER_ENDPOINT,
token=EXPORTER_TOKEN,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ClientConfigV1Alpha1,
ClientConfigV1Alpha1Drivers,
ExporterConfigV1Alpha1,
ObjectMeta,
)

# Generate a random client name
Expand All @@ -23,12 +24,14 @@
# Create a test client config
UNSAFE_CLIENT_CONFIG = ClientConfigV1Alpha1(
name=CLIENT_NAME,
metadata=ObjectMeta(namespace="default", name=CLIENT_NAME),
endpoint=CLIENT_ENDPOINT,
token=CLIENT_TOKEN,
drivers=ClientConfigV1Alpha1Drivers(allow=[], unsafe=True),
)
CLIENT_CONFIG = ClientConfigV1Alpha1(
name=CLIENT_NAME,
metadata=ObjectMeta(namespace="default", name=CLIENT_NAME),
endpoint=CLIENT_ENDPOINT,
token=CLIENT_TOKEN,
drivers=ClientConfigV1Alpha1Drivers(allow=[DRIVER_NAME], unsafe=False),
Expand Down Expand Up @@ -85,6 +88,7 @@ async def test_import_client(_load_kube_config_mock, get_client_config_mock: Asy
# Create a test exporter config
EXPORTER_CONFIG = ExporterConfigV1Alpha1(
alias=EXPORTER_NAME,
metadata=ObjectMeta(namespace="default", name=EXPORTER_NAME),
endpoint=EXPORTER_ENDPOINT,
token=EXPORTER_TOKEN,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@
import asyncclick as click
from jumpstarter_cli_common import make_table

from jumpstarter.config import ClientConfigV1Alpha1, ClientConfigV1Alpha1Drivers, UserConfigV1Alpha1
from jumpstarter.config import ClientConfigV1Alpha1, ClientConfigV1Alpha1Drivers, ObjectMeta, UserConfigV1Alpha1


@click.command("create-config", short_help="Create a client config.")
@click.argument("name")
@click.argument("alias")
@click.option(
"-o",
"--out",
type=click.Path(dir_okay=False, resolve_path=True, writable=True),
help="Specify an output file for the client config.",
)
@click.option(
"--namespace",
type=str,
help="Enter the Jumpstarter client namespace.",
prompt="Enter a valid Jumpstarter client nespace",
)
@click.option(
"--name",
type=str,
help="Enter the Jumpstarter client name.",
prompt="Enter a valid Jumpstarter client name",
)
@click.option(
"-e",
"--endpoint",
Expand All @@ -39,6 +51,8 @@
)
@click.option("--unsafe", is_flag=True, help="Should all driver client packages be allowed to load (UNSAFE!).")
def create_client_config(
alias: str,
namespace: str,
name: str,
endpoint: str,
token: str,
Expand All @@ -47,11 +61,12 @@ def create_client_config(
out: Optional[str],
):
"""Create a Jumpstarter client configuration."""
if out is None and ClientConfigV1Alpha1.exists(name):
raise click.ClickException(f"A client with the name '{name}' already exists.")
if out is None and ClientConfigV1Alpha1.exists(alias):
raise click.ClickException(f"A client with the name '{alias}' already exists.")

config = ClientConfigV1Alpha1(
name=name,
name=alias,
metadata=ObjectMeta(namespace=namespace, name=name),
endpoint=endpoint,
token=token,
drivers=ClientConfigV1Alpha1Drivers(allow=allow.split(","), unsafe=unsafe),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,48 @@ async def test_client(tmp_config_path):
# create client non-interactively
result = await runner.invoke(
client,
["create-config", "test1", "--endpoint", "example.com:443", "--token", "dummy", "--allow", "jumpstarter.*"],
[
"create-config",
"test1",
"--namespace",
"default",
"--name",
"test1",
"--endpoint",
"example.com:443",
"--token",
"dummy",
"--allow",
"jumpstarter.*",
],
)
assert result.exit_code == 0

# create duplicate client
result = await runner.invoke(
client,
["create-config", "test1", "--endpoint", "example.com:443", "--token", "dummy", "--allow", "jumpstarter.*"],
[
"create-config",
"test1",
"--namespace",
"default",
"--name",
"test1",
"--endpoint",
"example.com:443",
"--token",
"dummy",
"--allow",
"jumpstarter.*",
],
)
assert result.exit_code != 0

# create client interactively
result = await runner.invoke(
client,
["create-config", "test2"],
input="example.org:443\ndummytoken\njumpstarter.*,com.example.*\n",
input="default\ntest2\nexample.org:443\ndummytoken\njumpstarter.*,com.example.*\n",
)
assert result.exit_code == 0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import asyncclick as click
from jumpstarter_cli_common import make_table

from jumpstarter.config.exporter import ExporterConfigV1Alpha1
from jumpstarter.config.exporter import ExporterConfigV1Alpha1, ObjectMeta

arg_alias = click.argument("alias", default="default")


@click.command("create-config")
@click.option("--namespace", prompt=True)
@click.option("--name", prompt=True)
@click.option("--endpoint", prompt=True)
@click.option("--token", prompt=True)
@arg_alias
def create_exporter_config(alias, endpoint, token):
def create_exporter_config(alias, namespace, name, endpoint, token):
"""Create an exporter config."""
try:
ExporterConfigV1Alpha1.load(alias)
Expand All @@ -21,6 +23,7 @@ def create_exporter_config(alias, endpoint, token):

config = ExporterConfigV1Alpha1(
alias=alias,
metadata=ObjectMeta(namespace=namespace, name=name),
endpoint=endpoint,
token=token,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,44 @@ async def test_exporter(tmp_config_path):

# create exporter non-interactively
result = await runner.invoke(
exporter, ["create-config", "test1", "--endpoint", "example.com:443", "--token", "dummy"]
exporter,
[
"create-config",
"test1",
"--namespace",
"default",
"--name",
"test1",
"--endpoint",
"example.com:443",
"--token",
"dummy",
],
)
assert result.exit_code == 0

# create duplicate exporter
result = await runner.invoke(
exporter, ["create-config", "test1", "--endpoint", "example.com:443", "--token", "dummy"]
exporter,
[
"create-config",
"test1",
"--namespace",
"default",
"--name",
"test1",
"--endpoint",
"example.com:443",
"--token",
"dummy",
],
)
assert result.exit_code != 0

# create exporter interactively
result = await runner.invoke(exporter, ["create-config", "test2"], input="example.org:443\ndummytoken\n")
result = await runner.invoke(
exporter, ["create-config", "test2"], input="default\ntest2\nexample.org:443\ndummytoken\n"
)
assert result.exit_code == 0

# list exporters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from kubernetes_asyncio.client.models import V1ObjectMeta, V1ObjectReference

from .util import AbstractAsyncCustomObjectApi
from jumpstarter.config import ClientConfigV1Alpha1, ClientConfigV1Alpha1Drivers
from jumpstarter.config import ClientConfigV1Alpha1, ClientConfigV1Alpha1Drivers, ObjectMeta

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -105,6 +105,10 @@ async def get_client_config(self, name: str, allow: list[str], unsafe=False) ->
token = base64.b64decode(secret.data["token"]).decode("utf8")
return ClientConfigV1Alpha1(
name=name,
metadata=ObjectMeta(
namespace=client.metadata.namespace,
name=client.metadata.name,
),
endpoint=endpoint,
token=token,
drivers=ClientConfigV1Alpha1Drivers(allow=allow, unsafe=unsafe),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from kubernetes_asyncio.client.models import V1ObjectMeta, V1ObjectReference

from .util import AbstractAsyncCustomObjectApi
from jumpstarter.config import ExporterConfigV1Alpha1
from jumpstarter.config import ExporterConfigV1Alpha1, ObjectMeta

CREATE_EXPORTER_DELAY = 1
CREATE_EXPORTER_COUNT = 10
Expand Down Expand Up @@ -110,7 +110,16 @@ async def get_exporter_config(self, name: str) -> ExporterConfigV1Alpha1:
secret = await self.core_api.read_namespaced_secret(exporter.status.credential.name, self.namespace)
endpoint = exporter.status.endpoint
token = base64.b64decode(secret.data["token"]).decode("utf8")
return ExporterConfigV1Alpha1(alias=name, endpoint=endpoint, token=token, export={})
return ExporterConfigV1Alpha1(
alias=name,
metadata=ObjectMeta(
namespace=exporter.metadata.namespace,
name=exporter.metadata.name,
),
endpoint=endpoint,
token=token,
export={},
)

async def delete_exporter(self, name: str):
"""Delete an exporter object"""
Expand Down
3 changes: 2 additions & 1 deletion packages/jumpstarter/jumpstarter/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
ClientConfigV1Alpha1,
ClientConfigV1Alpha1Drivers,
)
from .common import CONFIG_API_VERSION, CONFIG_PATH
from .common import CONFIG_API_VERSION, CONFIG_PATH, ObjectMeta
from .env import JMP_CLIENT_CONFIG, JMP_DRIVERS_ALLOW, JMP_ENDPOINT, JMP_TOKEN
from .exporter import ExporterConfigV1Alpha1, ExporterConfigV1Alpha1DriverInstance
from .user import UserConfigV1Alpha1, UserConfigV1Alpha1Config
Expand All @@ -15,6 +15,7 @@
"JMP_TOKEN",
"JMP_DRIVERS_ALLOW",
"JMP_DRIVERS_ALLOW_UNSAFE",
"ObjectMeta",
"UserConfigV1Alpha1",
"UserConfigV1Alpha1Config",
"ClientConfigV1Alpha1",
Expand Down
13 changes: 10 additions & 3 deletions packages/jumpstarter/jumpstarter/config/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from jumpstarter_protocol import jumpstarter_pb2, jumpstarter_pb2_grpc
from pydantic import BaseModel, Field, ValidationError

from .common import CONFIG_PATH
from .env import JMP_DRIVERS_ALLOW, JMP_ENDPOINT, JMP_LEASE, JMP_TOKEN
from .common import CONFIG_PATH, ObjectMeta
from .env import JMP_DRIVERS_ALLOW, JMP_ENDPOINT, JMP_LEASE, JMP_NAME, JMP_NAMESPACE, JMP_TOKEN
from .grpc import call_credentials
from .tls import TLSConfigV1Alpha1
from jumpstarter.common import MetadataFilter
from jumpstarter.common.grpc import aio_secure_channel, ssl_channel_credentials
Expand Down Expand Up @@ -41,6 +42,8 @@ class ClientConfigV1Alpha1(BaseModel):
apiVersion: Literal["jumpstarter.dev/v1alpha1"] = Field(default="jumpstarter.dev/v1alpha1")
kind: Literal["ClientConfig"] = Field(default="ClientConfig")

metadata: ObjectMeta

endpoint: str
tls: TLSConfigV1Alpha1 = Field(default_factory=TLSConfigV1Alpha1)
token: str
Expand All @@ -50,7 +53,7 @@ class ClientConfigV1Alpha1(BaseModel):
async def channel(self):
credentials = grpc.composite_channel_credentials(
ssl_channel_credentials(self.endpoint, self.tls),
grpc.access_token_call_credentials(self.token),
call_credentials("Client", self.metadata, self.token),
)

return aio_secure_channel(self.endpoint, credentials)
Expand Down Expand Up @@ -141,6 +144,10 @@ def try_from_env(cls):
def from_env(cls):
allow, unsafe = _allow_from_env()
return cls(
metadata=ObjectMeta(
namespace=os.environ.get(JMP_NAMESPACE),
name=os.environ.get(JMP_NAME),
),
endpoint=os.environ.get(JMP_ENDPOINT),
token=os.environ.get(JMP_TOKEN),
drivers=ClientConfigV1Alpha1Drivers(
Expand Down
Loading

0 comments on commit 8d64e20

Please sign in to comment.