Skip to content

Commit

Permalink
revert commits from explicit schema branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam-D-Lewis committed May 17, 2024
1 parent 240b6dc commit f172e8b
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 79 deletions.
3 changes: 1 addition & 2 deletions src/_nebari/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ def write_configuration(
"""Write the nebari configuration file to disk"""
with config_filename.open(mode) as f:
if isinstance(config, pydantic.BaseModel):
config_dict = config.write_config()
yaml.dump(config_dict, f)
yaml.dump(config.model_dump(), f)
else:
config = dump_nested_model(config)
yaml.dump(config, f)
Expand Down
5 changes: 3 additions & 2 deletions src/_nebari/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
DEFAULT_GCP_NODE_GROUPS,
node_groups_to_dict,
)
from _nebari.stages.kubernetes_ingress import LetsEncryptCertificate
from _nebari.stages.kubernetes_ingress import CertificateEnum
from _nebari.stages.kubernetes_keycloak import AuthenticationEnum
from _nebari.stages.terraform_state import TerraformStateEnum
from _nebari.utils import get_latest_kubernetes_version, random_secure_string
Expand Down Expand Up @@ -194,7 +194,8 @@ def render_config(
config["theme"]["jupyterhub"]["hub_subtitle"] = WELCOME_HEADER_TEXT

if ssl_cert_email:
config["certificate"] = LetsEncryptCertificate(acme_email=ssl_cert_email)
config["certificate"] = {"type": CertificateEnum.letsencrypt.value}
config["certificate"]["acme_email"] = ssl_cert_email

# validate configuration and convert to model
from nebari.plugins import nebari_plugin_manager
Expand Down
4 changes: 2 additions & 2 deletions src/_nebari/keycloak.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import requests
import rich

from _nebari.stages.kubernetes_ingress import SelfSignedCertificate
from _nebari.stages.kubernetes_ingress import CertificateEnum
from nebari import schema

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -91,7 +91,7 @@ def get_keycloak_admin_from_config(config: schema.Main):
"KEYCLOAK_ADMIN_PASSWORD", config.security.keycloak.initial_root_password
)

should_verify_tls = not isinstance(config.certificate, SelfSignedCertificate)
should_verify_tls = config.certificate.type != CertificateEnum.selfsigned

try:
keycloak_admin = keycloak.KeycloakAdmin(
Expand Down
47 changes: 19 additions & 28 deletions src/_nebari/stages/kubernetes_ingress/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from __future__ import annotations

import enum
import logging
import socket
import sys
import time
from typing import Any, Dict, List, Literal, Optional, Type, Union

from pydantic import Field
from typing import Any, Dict, List, Optional, Type

from _nebari import constants
from _nebari.provider.dns.cloudflare import update_record
Expand Down Expand Up @@ -115,31 +112,25 @@ def _attempt_dns_lookup(
sys.exit(1)


class SelfSignedCertificate(schema.Base):
type: Literal["self-signed"] = Field("self-signed", validate_default=True)


class LetsEncryptCertificate(schema.Base):
type: Literal["lets-encrypt"] = Field("lets-encrypt", validate_default=True)
acme_email: str
acme_server: str = "https://acme-v02.api.letsencrypt.org/directory"
@schema.yaml_object(schema.yaml)
class CertificateEnum(str, enum.Enum):
letsencrypt = "lets-encrypt"
selfsigned = "self-signed"
existing = "existing"
disabled = "disabled"

@classmethod
def to_yaml(cls, representer, node):
return representer.represent_str(node.value)

class ExistingCertificate(schema.Base):
type: Literal["existing"] = Field("existing", validate_default=True)
secret_name: str


class DisabledCertificate(schema.Base):
type: Literal["disabled"] = Field("disabled", validate_default=True)


Certificate = Union[
SelfSignedCertificate,
LetsEncryptCertificate,
ExistingCertificate,
DisabledCertificate,
]
class Certificate(schema.Base):
type: CertificateEnum = CertificateEnum.selfsigned
# existing
secret_name: Optional[str] = None
# lets-encrypt
acme_email: Optional[str] = None
acme_server: str = "https://acme-v02.api.letsencrypt.org/directory"


class DnsProvider(schema.Base):
Expand All @@ -153,7 +144,7 @@ class Ingress(schema.Base):

class InputSchema(schema.Base):
domain: Optional[str] = None
certificate: Certificate = SelfSignedCertificate()
certificate: Certificate = Certificate()
ingress: Ingress = Ingress()
dns: DnsProvider = DnsProvider()

Expand Down
18 changes: 1 addition & 17 deletions src/_nebari/subcommands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ class InitInputs(schema.Base):
ssl_cert_email: Optional[schema.email_pydantic] = None
disable_prompt: bool = False
output: pathlib.Path = pathlib.Path("nebari-config.yaml")
verbose: bool = False


def enum_to_list(enum_cls):
Expand Down Expand Up @@ -153,7 +152,7 @@ def handle_init(inputs: InitInputs, config_schema: BaseModel):
try:
write_configuration(
inputs.output,
config if not inputs.verbose else config_schema(**config),
config,
mode="x",
)
except FileExistsError:
Expand Down Expand Up @@ -566,12 +565,6 @@ def init(
"-o",
help="Output file path for the rendered config file.",
),
verbose: bool = typer.Option(
False,
"--verbose",
"-v",
help="Write verbose nebari config file.",
),
):
"""
Create and initialize your [purple]nebari-config.yaml[/purple] file.
Expand Down Expand Up @@ -611,7 +604,6 @@ def init(
inputs.ssl_cert_email = ssl_cert_email
inputs.disable_prompt = disable_prompt
inputs.output = output
inputs.verbose = verbose

from nebari.plugins import nebari_plugin_manager

Expand Down Expand Up @@ -902,14 +894,6 @@ def guided_init_wizard(ctx: typer.Context, guided_init: str):
)
inputs.kubernetes_version = kubernetes_version

# VERBOSE
inputs.verbose = questionary.confirm(
"Would you like the nebari config to show all available options? (recommended for advanced users only)",
default=False,
qmark=qmark,
auto_enter=False,
).unsafe_ask()

from nebari.plugins import nebari_plugin_manager

config_schema = nebari_plugin_manager.config_schema
Expand Down
27 changes: 3 additions & 24 deletions src/nebari/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,32 +124,11 @@ def ordered_stages(self):
return self.get_available_stages()

@property
def ordered_schemas(self):
return [schema.Main] + [
def config_schema(self):
classes = [schema.Main] + [
_.input_schema for _ in self.ordered_stages if _.input_schema is not None
]

@property
def config_schema(self):
ordered_schemas = self.ordered_schemas

def write_config(self):
config_exclude = set()
for cls in self._ordered_schemas:
if hasattr(cls, "exclude_from_config"):
new_exclude = cls.exclude_from_config(self)
config_exclude = config_exclude.union(new_exclude)
return self.model_dump(exclude=config_exclude)

ConfigSchema = type(
"ConfigSchema",
tuple(ordered_schemas[::-1]),
{
"_ordered_schemas": ordered_schemas,
"write_config": write_config,
},
)
return ConfigSchema
return type("ConfigSchema", tuple(classes), {})


nebari_plugin_manager = NebariPluginManager()
4 changes: 1 addition & 3 deletions src/nebari/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@

class Base(pydantic.BaseModel):
model_config = ConfigDict(
extra="forbid",
validate_assignment=True,
populate_by_name=True,
extra="forbid", validate_assignment=True, populate_by_name=True
)


Expand Down
2 changes: 1 addition & 1 deletion tests/tests_unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_nebari_init(tmp_path, namespace, auth_provider, ci_provider, ssl_cert_e
assert config.namespace == namespace
assert config.security.authentication.type.lower() == auth_provider
assert config.ci_cd.type == ci_provider
assert getattr(config.certificate, "acme_email", None) == ssl_cert_email
assert config.certificate.acme_email == ssl_cert_email


@pytest.mark.parametrize(
Expand Down

0 comments on commit f172e8b

Please sign in to comment.