Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion getdeck/api/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@stopwatch
def get_available_decks(deckfile_location: str) -> List:
data_aux = fetch_data(deckfile_location, fetch_sources=False)
data_aux = fetch_data(deckfile_location, fetch_sources_flag=False)
available_decks = data_aux.deckfile.get_decks()
del data_aux

Expand Down
2 changes: 1 addition & 1 deletion getdeck/api/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def remove_cluster(
) -> bool:
from getdeck.utils import ensure_cluster

data_aux = fetch_data(deckfile_location, fetch_sources=False)
data_aux = fetch_data(deckfile_location, fetch_sources_flag=False)
k8s_provider = ensure_cluster(
data_aux.deckfile, config, ignore_cluster, do_install=False
)
Expand Down
50 changes: 25 additions & 25 deletions getdeck/deckfile/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ def get_provider(self, config) -> AbstractProvider:
raise e


class DeckfileHelmSource(BaseModel):
class InlineSource(BaseModel):
type: str = "inline"
content: Dict = None


class FileSource(BaseModel):
type: str = "file"
ref: str = None
targetRevision: Optional[str] = None
path: str = ""


class HelmSource(BaseModel):
type: str = "helm"
ref: str
targetRevision: Optional[str] = None
Expand All @@ -48,26 +60,14 @@ class DeckfileHelmSource(BaseModel):
helmPlugins: List[str] = None


class DeckfileInlineSource(BaseModel):
type: str = "inline"
content: Dict = None


class DeckfileFileSource(BaseModel):
type: str = "file"
ref: str = None
targetRevision: Optional[str] = None
path: str = ""


class DeckfileKustomizeSource(BaseModel):
class KustomizeSource(BaseModel):
type: str = "kustomize"
ref: str
targetRevision: Optional[str] = None
path: str = ""


class DeckfileDirectorySource(BaseModel):
class DirectorySource(BaseModel):
type: str = "directory"
ref: str
targetRevision: Optional[str] = None
Expand All @@ -82,11 +82,11 @@ class DeckfileDeck(BaseModel):
hosts: List[str] = []
sources: List[
Union[
DeckfileInlineSource,
DeckfileFileSource,
DeckfileDirectorySource,
DeckfileHelmSource,
DeckfileKustomizeSource,
InlineSource,
FileSource,
DirectorySource,
HelmSource,
KustomizeSource,
]
]

Expand All @@ -107,11 +107,11 @@ def __init__(self, *args, **data):
source_type = "inline"

source_class = {
"inline": DeckfileInlineSource,
"file": DeckfileFileSource,
"directory": DeckfileDirectorySource,
"kustomize": DeckfileKustomizeSource,
"helm": DeckfileHelmSource,
"inline": InlineSource,
"file": FileSource,
"directory": DirectorySource,
"kustomize": KustomizeSource,
"helm": HelmSource,
}.get(source_type)
self.sources.append(source_class(**source))
except KeyError:
Expand Down
89 changes: 56 additions & 33 deletions getdeck/fetch/fetch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import os
from typing import List
from typing import List, Union


from getdeck.fetch.deck_fetcher import (
Expand All @@ -10,7 +10,14 @@
)
from getdeck.fetch.types import DataAux
from getdeck.fetch.utils import detect_deckfile, get_path_and_name
from getdeck.deckfile.file import DeckfileDeck
from getdeck.deckfile.file import (
DeckfileDeck,
DirectorySource,
FileSource,
HelmSource,
InlineSource,
KustomizeSource,
)

from getdeck.deckfile.selector import deckfile_selector
from getdeck.fetch.source_fetcher import (
Expand All @@ -27,7 +34,7 @@ class FetchError(Exception):
pass


def _fetch_deck(data_aux: DataAux, location: str) -> DataAux:
def fetch_deck(data_aux: DataAux, location: str) -> DataAux:
deckfile_aux = DeckfileAux(location=location)
fetch_behavior = select_deck_fetch_behavior(location=location)
if fetch_behavior:
Expand All @@ -43,41 +50,55 @@ def _fetch_deck(data_aux: DataAux, location: str) -> DataAux:
return data_aux


def _fetch_sources(deck: DeckfileDeck) -> List[SourceAux]:
source_fetcher = SourceFetcher(fetch_behavior=None)
def fetch_source(
source: Union[
InlineSource,
FileSource,
DirectorySource,
HelmSource,
KustomizeSource,
],
source_fetcher: SourceFetcher = None,
) -> SourceAux:
ref = getattr(source, "ref", None)
source_aux = SourceAux(location=ref)
source_aux.source = source # does not work during SourceAux initialization

fetch_behavior = select_source_fetch_behavior(source=source)
if not fetch_behavior:
return source_aux

if not source_fetcher:
source_fetcher = SourceFetcher(fetch_behavior=fetch_behavior)
else:
source_fetcher.fetch_behavior = fetch_behavior

source_auxs = []
for source in deck.sources:
ref = getattr(source, "ref", None)
logger.info(
"Fetching source " f"{source.__class__.__name__}: {ref or 'no ref'}"
)
try:
source_dict = source.dict()
source_aux = source_fetcher.fetch(data=source_aux, **source_dict)
except Exception as e:
logger.debug(str(e))
del source_aux
raise FetchError(f"Source fetching error: {str(e)}")

source_aux = SourceAux(location=ref)
# assigning source during SourceAux initialization changes DeckfileHelmSource to DeckfileInlineSource.. ???
source_aux.source = source
return source_aux # noqa: F821

fetch_behavior = select_source_fetch_behavior(source=source)
source_fetcher.fetch_behavior = fetch_behavior
if not fetch_behavior:
source_auxs.append(source_aux)
continue

try:
source_dict = source.dict()
source_aux = source_fetcher.fetch(data=source_aux, **source_dict)
except Exception as e:
logger.debug(str(e))
del source_aux, source_auxs[:]
raise FetchError(f"Source fetching error: {str(e)}")
def fetch_all_sources(deck: DeckfileDeck) -> List[SourceAux]:
source_fetcher = SourceFetcher(fetch_behavior=None)

source_auxs.append(source_aux) # noqa: F821
source_auxs = []
for source in deck.sources:
ref = getattr(source, "ref", None)
logger.info(f"Fetching {source.__class__.__name__}: {ref or '-'}")
source_aux = fetch_source(source=source, source_fetcher=source_fetcher)
source_auxs.append(source_aux)

return source_auxs


def fetch_data(
location: str, deck_name: str = None, fetch_sources: bool = True
location: str, deck_name: str = None, fetch_sources_flag: bool = True
) -> DataAux:
"""
delete returned DataAux to clean up temporary resources
Expand All @@ -92,7 +113,7 @@ def fetch_data(

# fetch deck
data_aux = DataAux()
data_aux = _fetch_deck(data_aux=data_aux, location=location)
data_aux = fetch_deck(data_aux=data_aux, location=location)

# validate
file_detected = os.path.join(data_aux.deckfile_aux.path, data_aux.deckfile_aux.name)
Expand All @@ -105,9 +126,11 @@ def fetch_data(
data_aux.deckfile = deckfile

# fetch sources
if fetch_sources:
deck = deckfile.get_deck(deck_name)
source_auxs = _fetch_sources(deck=deck)
data_aux.source_auxs = source_auxs
if not fetch_sources_flag:
return data_aux

deck = deckfile.get_deck(deck_name)
source_auxs = fetch_all_sources(deck=deck)
data_aux.source_auxs = source_auxs

return data_aux
20 changes: 10 additions & 10 deletions getdeck/fetch/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

from getdeck import configuration
from getdeck.deckfile.file import (
DeckfileDirectorySource,
DeckfileFileSource,
DeckfileHelmSource,
DeckfileInlineSource,
DeckfileKustomizeSource,
DirectorySource,
FileSource,
HelmSource,
InlineSource,
KustomizeSource,
)


Expand All @@ -30,11 +30,11 @@ class SourceAux(BaseModel):

source: Optional[
Union[
DeckfileInlineSource,
DeckfileFileSource,
DeckfileDirectorySource,
DeckfileHelmSource,
DeckfileKustomizeSource,
InlineSource,
FileSource,
DirectorySource,
HelmSource,
KustomizeSource,
]
] = None

Expand Down
20 changes: 10 additions & 10 deletions getdeck/sources/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

from getdeck.configuration import ClientConfiguration
from getdeck.deckfile.file import (
DeckfileDirectorySource,
DeckfileFileSource,
DeckfileInlineSource,
DeckfileKustomizeSource,
DeckfileHelmSource,
DirectorySource,
FileSource,
InlineSource,
KustomizeSource,
HelmSource,
)
from getdeck.fetch.types import DeckfileAux, SourceAux
from getdeck.sources.types import K8sSourceFile
Expand All @@ -25,11 +25,11 @@ def __init__(
self,
path: str,
source: Union[
DeckfileInlineSource,
DeckfileFileSource,
DeckfileDirectorySource,
DeckfileHelmSource,
DeckfileKustomizeSource,
InlineSource,
FileSource,
DirectorySource,
HelmSource,
KustomizeSource,
],
config: ClientConfiguration,
namespace: str,
Expand Down
30 changes: 15 additions & 15 deletions getdeck/sources/selector.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Union, Optional
from getdeck.deckfile.file import (
DeckfileDirectorySource,
DeckfileFileSource,
DeckfileHelmSource,
DeckfileInlineSource,
DeckfileKustomizeSource,
DirectorySource,
FileSource,
HelmSource,
InlineSource,
KustomizeSource,
)
from getdeck.sources.generator import RenderBehavior
from getdeck.sources.file import File
Expand All @@ -15,18 +15,18 @@

def select_render_behavior(
source: Union[
DeckfileInlineSource,
DeckfileFileSource,
DeckfileDirectorySource,
DeckfileHelmSource,
DeckfileKustomizeSource,
InlineSource,
FileSource,
DirectorySource,
HelmSource,
KustomizeSource,
],
) -> Optional[RenderBehavior]:
render_behavior = {
DeckfileInlineSource: Inline,
DeckfileFileSource: File,
DeckfileDirectorySource: File,
DeckfileHelmSource: Helm,
DeckfileKustomizeSource: Kustomize,
InlineSource: Inline,
FileSource: File,
DirectorySource: File,
HelmSource: Helm,
KustomizeSource: Kustomize,
}.get(type(source), None)
return render_behavior
Empty file removed test/resources/helm/.gitkeep
Empty file.
23 changes: 23 additions & 0 deletions test/resources/helm/empty/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
Loading