Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
This was generated with help from gpt3-python-helper
  • Loading branch information
allenporter committed Feb 5, 2023
1 parent 08dbd03 commit 79e3997
Show file tree
Hide file tree
Showing 14 changed files with 313 additions and 1 deletion.
49 changes: 49 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/pycqa/isort
rev: 5.9.3
hooks:
- id: isort
args: ["--profile", "black"]
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
- repo: local
hooks:
- id: mypy
name: mypy
entry: "./run-mypy"
language: python
additional_dependencies: ["mypy==0.930"]
types: [python]
# use require_serial so that script
# is only called once per commit
require_serial: true
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
args:
[
"-rn", # Only display messages
"-sn", # Don't display the score
"--disable=too-few-public-methods",
]
- repo: https://github.com/codespell-project/codespell
rev: v2.1.0
hooks:
- id: codespell
36 changes: 36 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## Python virtual environment

Python virtual environments are used to isolate python projects from each other.
Virtual environments are typically stored in a directory called 'venv'. The
virtual environment is created using the 'venv' command line tool.

Example:
```bash
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt


## Testing

Testing is important for any project, and this project is no different. The project
uses the pytest testing framework, and the tests are located in the tests/ directory.
The tests are run automatically on every commit and pull request.

To run the tests locally, you can use the following command:

```bash
$ pytest
```

## Documentation

Documentation is important for any project. This project uses the pydoc documentation
tool to generate documentation from the source code. The documentation is located in
the docs/ directory.

To generate the documentation locally, you can use the following command:

```bash
$ pydoc -w ./
```
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# flux-local
flux-local is a set of tools and libraries for managing a local flux gitops repository focused on validation steps to help improve quality of commits, PRs, and general local testing.

flux-local is a set of tools and libraries for managing a local flux gitops repository focused on validation steps to help improve quality of commits, PRs, and general local testing.

Flux local uses local command line tools like kustomize and helm to replicate the behavior of
flux and understand the desired end state of the cluster. It only looks at the local git repo,
and not a live cluster. However, this is fine since the local repository has enough information
and the definition is simple. Secrets are ignored as the content is not needed to validate the
cluster is creating valid objects.
35 changes: 35 additions & 0 deletions flux_local/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
flux-local is a command line program that calls the flux-local library.
The main things that happen in the program are:
- Reads the training data markdown file
- Creates prompts to include the necessary training data
- Parses the response output filenames and files
- Writes the files to disk or prints them on the screen
The prompts must be created so they result in structured output that can be parsed by the
program. The program handles creating the prompts as well as parsing the response to pull
out the structured response with filenames and file contents.
Since the input/output files may be large, the program breaks down the requests into
smaller pieces to avoid any API limits.
Example usage:
./flux-local.py --api-key=<KEY> --project-file ./project.yaml --training-data ./project.md --output-dir=/tmp/project
"""

import argparse

import api


def main():
"""flux-local command line tool."""
parser = argparse.ArgumentParser(description="flux-local")
args = parser.parse_args()

print(api.call(args[0]))


if __name__ == "__main__":
main()
23 changes: 23 additions & 0 deletions flux_local/cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import subprocess


def run(command, **kwargs):
"""Runs a command in the shell and returns the results.
This function runs a command in the shell and returns the results. The results
include the stdout, stderr, and return code from the command.
Keyword Arguments:
command {str} -- The command to run in the shell
Returns:
dict -- Dictionary with the fields stdout, stderr, and returncode
"""
result = subprocess.run(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs
)
return {
"stdout": result.stdout.decode("utf-8"),
"stderr": result.stderr.decode("utf-8"),
"returncode": result.returncode,
}
9 changes: 9 additions & 0 deletions flux_local/helm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Library for running `helm template` to produce local items in the cluster."""

import subprocess


def render(chart, values):
"""Runs `helm template` to produce local items in the cluster."""
cmd = ["helm", "template", chart, "--values", values]
return subprocess.check_output(cmd)
21 changes: 21 additions & 0 deletions flux_local/kustomize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
kustomize.py is a library that can run kustomize build on the local cluster to build a manifest.
Example usage:
```
k = Kustomize()
k.build("./manifests")
```
"""

import subprocess


class Kustomize:
"""Kustomize class that can run kustomize build on the local cluster to build a manifest."""

def build(self, path):
"""Runs kustomize build on the local cluster to build a manifest."""
subprocess.run(["kustomize", "build", path])
46 changes: 46 additions & 0 deletions flux_local/manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
This library represents dataclasses for objects in the cluster.
The dataclasses are used to represent objects in the cluster such as flux Kustomizations, HelmRepositories and HelmReleases.
"""

from dataclasses import dataclass


@dataclass
class FluxKustomization:
"""
This class represents a flux kustomization.
A flux kustomization is a kustomization that is used by flux to manage a cluster.
"""

name: str
namespace: str
path: str


@dataclass
class HelmRepository:
"""
This class represents a helm repository.
A helm repository is a repository of helm charts.
"""

name: str
url: str


@dataclass
class HelmRelease:
"""
This class represents a helm release.
A helm release is a release of a helm chart.
"""

name: str
chart: str
namespace: str
values: dict
20 changes: 20 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-e .
aiofiles==22.1.0
black==22.10.0
coverage==6.4.2
flake8==5.0.4
flake8-black==0.3.5
GitPython==3.1.30
pdoc==12.1.0
pip==22.1.2
pre-commit==3.0.4
pydantic==1.10.4
pylint==2.14.4
pytest==7.2.1
pytest-asyncio==0.20.3
python-slugify==8.0.0
PyYAML==6.0
typing-extensions==4.4.0
wheel==0.37.1
yamllint==1.29.0
yq==3.1.0
10 changes: 10 additions & 0 deletions run-mypy
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

set -o errexit

# Change directory to the project root directory.
cd "$(dirname "$0")"

pip3 install -r requirements.txt --no-input --quiet

mypy .
14 changes: 14 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[metadata]
name = flux-local
version = 0.0.1
description = flux-local is a python library and set of tools for managing a flux gitops repository, with validation steps to help improve quality of commits, PRs, and general local testing.

[options]
packages = find:
install_requires =
flux_local

[options.packages.find]
exclude =
tests
venv
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""Minimal setup for the project, see setup.cfg"""
from setuptools import setup

setup()
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for flux-local."""
37 changes: 37 additions & 0 deletions tests/test_manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import json
import os
import tempfile

import flux_local.manifest


def test_generate_manifest():
"""Test generating a manifest file."""
# Setup
project_name = "my-project"
project_version = "0.0.1"
description = "My project is a command line tool that does x, y, and z."
files = [
{"name": "main.py", "description": "The main program file."},
{"name": "README.md", "description": "The project README file."},
]

# Run
with tempfile.TemporaryDirectory() as temp_dir:
output_path = os.path.join(temp_dir, "manifest.json")
manifest.generate_manifest(
project_name, project_version, description, files, output_path
)

# Verify
with open(output_path) as f:
data = json.load(f)

assert data["name"] == project_name
assert data["version"] == project_version
assert data["description"] == description
assert len(data["files"]) == 2
assert data["files"][0]["name"] == "main.py"
assert data["files"][0]["description"] == "The main program file."
assert data["files"][1]["name"] == "README.md"
assert data["files"][1]["description"] == "The project README file."

0 comments on commit 79e3997

Please sign in to comment.