Skip to content

Commit

Permalink
Merge pull request #1 from dortlii/features/add-helm-generator-funcs
Browse files Browse the repository at this point in the history
Features/add helm generator funcs
  • Loading branch information
dortlii authored Sep 22, 2024
2 parents 6091631 + 4a9312e commit cd5af61
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 14 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/go_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
pull_request:
branches: ["main"]
paths-ignore:
- "docs/**"
- "examples/**"
- "deployment/**"
- ".devcontainer/**"
- ".vscode/**"
- "config/**"

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.23.x"

- name: Install dependencies
run: go get github.com/dortlii/helm-chart-generator/cmd/...

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
43 changes: 43 additions & 0 deletions .github/workflows/image_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This is a basic workflow to help you get started with Actions
name: Build Container Image

# Controls when the workflow will run
on:
push:
tags:
- "*"

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}/odsg
TAG: ${{ github.ref_name }}

jobs:
docker:
permissions:
contents: read
packages: write
id-token: write

runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push Docker image
id: push
uses: docker/build-push-action@v6
with:
file: ./deployment/Containerfile
platforms: linux/amd64
push: true
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.TAG }}
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ go.work.sum

# env file
.env

# idea folder
.idea
27 changes: 27 additions & 0 deletions cmd/hcg/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"fmt"
"log"

"github.com/dortlii/helm-chart-generator/generator"
)

func main() {
helmChart, err := generator.NewHelmChart("2", "myChart", "1.0.0", "application")
if err != nil {
log.Fatalf("Failed to create helm chart: %s", err)
}

emptyHelmChart, err := generator.NewHelmChart("", "", "", "")
if err != nil {
log.Fatalf("Failed to create helm chart: %s", err)
}

fmt.Printf("This is my helm chart: %s\n", helmChart)
fmt.Printf("This is my empty helm chart: %s\n", emptyHelmChart)

// Save to disk
helm := generator.NewHelmFileService("/tmp/", helmChart)
helm.Save()
}
29 changes: 29 additions & 0 deletions deployment/Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Stage 1: Build the Go application
FROM golang:1.23-alpine AS builder

# Set the working directory inside the container
WORKDIR /app

# Copy go.mod and go.sum files
COPY ../go.mod ../go.sum ./

# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download

# Copy the source code into the container
COPY ../ .

# Build the Go application
RUN go build -o main cmd/hcg/main.go

# Stage 2: Create a minimal image with the built binary
FROM alpine:latest as run

# Set the working directory inside the container
WORKDIR /app

# Copy the binary from the builder stage
COPY --from=builder /app/main .

# Command to run the binary
CMD ["./main"]
58 changes: 58 additions & 0 deletions generator/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package generator

import (
"log"
"os"
"path"

"github.com/dortlii/helm-chart-generator/helm"
)

var helmFolders = []string{
"templates",
"crds",
"charts",
}

// FileService defines the interface for file services
type FileService interface {
Save() error
}

// NewHelmFileService is a factory function that returns the interface
func NewHelmFileService(path string, helm helm.Helm) FileService {
return &HelmFiles{
Path: path,
Helm: helm,
}
}

// HelmFiles is the implementation of the helm file structure
type HelmFiles struct {
// Path where the folder should be placed
Path string
// Helm parts to create
Helm helm.Helm
}

// Save writes the helm chart to the filesystem
func (hf HelmFiles) Save() error {
// Create the root folder of the helm chart
fullRootHelmPath := path.Join(hf.Path, hf.Helm.Chart.Name)
createFolder(fullRootHelmPath)

// Create all sub folders
for _, helmFolder := range helmFolders {
helmDestFolder := path.Join(fullRootHelmPath, helmFolder)
createFolder(helmDestFolder)
}

return nil
}

// createFolder helper function
func createFolder(path string) {
if err := os.Mkdir(path, os.ModePerm); err != nil {
log.Fatal(err)
}
}
50 changes: 50 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package generator

import "github.com/dortlii/helm-chart-generator/helm"

// NewHelmChart takes parameters to create a helm.Helm object and returns it
func NewHelmChart(apiVersion, name, version, helmType string) (helm.Helm, error) {
chart := fillChart(apiVersion, name, version, helmType)
values := helm.NewValues()
template := helm.NewTemplate()

helmChart := helm.Helm{
Chart: chart,
Values: values,
Template: template,
}

return helmChart, nil
}

// fillChart creates a new, empty helm.Chart object, fills it with default
// values and overrides them with given values
func fillChart(apiVersion, name, version, helmType string) helm.Chart {
chart := helm.NewChart()

chart = *helm.SetDefaults(chart)

if checkEmptyField(apiVersion) {
chart.ApiVersion = apiVersion
}

if checkEmptyField(name) {
chart.Name = name
}

if checkEmptyField(version) {
chart.Version = version
}

if checkEmptyField(helmType) {
chart.Type = helmType
}

return chart
}

// checkEmptyField checks the given field, if it's empty
// and returns a bool
func checkEmptyField(field string) bool {
return field != ""
}
34 changes: 29 additions & 5 deletions helm/chart.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
package helm

// Chart item of the helm chart, equals to Chart.yaml
type Chart struct {
ApiVersion string
Name string
// ApiVersion represents the version of the helm chart schema
ApiVersion string
// Name of the helm chart
Name string
// Description describes what the helm chart is for
Description string
Type string
Version string
AppVersion string
// Type of the helm chart, application or library
Type string
// Version of the helm chart
Version string
// AppVersion is the version of the app to be installed
AppVersion string
}

// NewChart creates an empty helm chart
func NewChart() Chart {
return Chart{}
}

// SetDefaults adds default values to a Chart struct
func SetDefaults(chart Chart) *Chart {
chart.ApiVersion = "2"
chart.AppVersion = "0.0.0"
chart.Version = "0.0.0"
chart.Name = "default"
chart.Description = "description"
chart.Type = "application"

return &chart
}
8 changes: 6 additions & 2 deletions helm/helm.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package helm

// Helm chart components
type Helm struct {
Chart Chart
Values Values
// Chart of the helm chart
Chart Chart
// Values of the helm chart
Values Values
// Template of the helm chart
Template Template
}
9 changes: 9 additions & 0 deletions helm/template.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package helm

// Template elements of the helm chart
type Template struct {
// Notes content for the Template
Notes Notes
}

// Notes for the helm chart functionality
type Notes struct {
// Content describes the text which gets written to Notes
Content string
}

// NewTemplate creates an empty template
func NewTemplate() Template {
return Template{}
}
6 changes: 6 additions & 0 deletions helm/values.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package helm

// Values of the helm chart, equals to a values.yaml file
type Values struct {
Key string
}

// NewValues returns a new, empty Values object
func NewValues() Values {
return Values{}
}
7 changes: 0 additions & 7 deletions main.go

This file was deleted.

0 comments on commit cd5af61

Please sign in to comment.