feat(refacto): move everything in internal package
This allows to install katenary with `go install` and to clean up the project folder.
This commit is contained in:
13
internal/generator/labels/labelstructs/configMap.go
Normal file
13
internal/generator/labels/labelstructs/configMap.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package labelstructs
|
||||
|
||||
import "gopkg.in/yaml.v3"
|
||||
|
||||
type ConfigMapFiles []string
|
||||
|
||||
func ConfigMapFileFrom(data string) (ConfigMapFiles, error) {
|
||||
var mapping ConfigMapFiles
|
||||
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mapping, nil
|
||||
}
|
17
internal/generator/labels/labelstructs/configMap_test.go
Normal file
17
internal/generator/labels/labelstructs/configMap_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package labelstructs_test
|
||||
|
||||
import (
|
||||
"github.com/katenary/katenary/internal/generator/labels/labelstructs"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfigMapFileFrom(t *testing.T) {
|
||||
ts := "- foo/bar"
|
||||
tc2, _ := labelstructs.ConfigMapFileFrom(ts)
|
||||
if len(tc2) != 1 {
|
||||
t.Errorf("Expected ConfigMapFile to have 1 item, got %d", len(tc2))
|
||||
}
|
||||
if tc2[0] != "foo/bar" {
|
||||
t.Errorf("Expected ConfigMapFile to contain 'foo/bar', got %s", tc2[0])
|
||||
}
|
||||
}
|
18
internal/generator/labels/labelstructs/cronJob.go
Normal file
18
internal/generator/labels/labelstructs/cronJob.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package labelstructs
|
||||
|
||||
import "gopkg.in/yaml.v3"
|
||||
|
||||
type CronJob struct {
|
||||
Image string `yaml:"image,omitempty" json:"image,omitempty"`
|
||||
Command string `yaml:"command" json:"command,omitempty"`
|
||||
Schedule string `yaml:"schedule" json:"schedule,omitempty"`
|
||||
Rbac bool `yaml:"rbac" json:"rbac,omitempty"`
|
||||
}
|
||||
|
||||
func CronJobFrom(data string) (*CronJob, error) {
|
||||
var mapping CronJob
|
||||
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &mapping, nil
|
||||
}
|
25
internal/generator/labels/labelstructs/cronJob_test.go
Normal file
25
internal/generator/labels/labelstructs/cronJob_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package labelstructs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCronJobFrom(t *testing.T) {
|
||||
ts := `
|
||||
image: fooimage
|
||||
command: thecommand
|
||||
schedule: "0/3 0 * * *"
|
||||
Rbac: false
|
||||
`
|
||||
tc, _ := CronJobFrom(ts)
|
||||
if tc.Image != "fooimage" {
|
||||
t.Errorf("Expected CronJob image to be 'fooimage', got %s", tc.Image)
|
||||
}
|
||||
if tc.Command != "thecommand" {
|
||||
t.Errorf("Expected CronJob command to be 'thecommand', got %s", tc.Command)
|
||||
}
|
||||
if tc.Schedule != "0/3 0 * * *" {
|
||||
t.Errorf("Expected CronJob schedule to be '0/3 0 * * *', got %s", tc.Schedule)
|
||||
}
|
||||
if tc.Rbac != false {
|
||||
t.Errorf("Expected CronJob rbac to be false, got %t", tc.Rbac)
|
||||
}
|
||||
}
|
21
internal/generator/labels/labelstructs/dependencies.go
Normal file
21
internal/generator/labels/labelstructs/dependencies.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package labelstructs
|
||||
|
||||
import "gopkg.in/yaml.v3"
|
||||
|
||||
// Dependency is a dependency of a chart to other charts.
|
||||
type Dependency struct {
|
||||
Values map[string]any `yaml:"-" json:"values,omitempty"`
|
||||
Name string `yaml:"name" json:"name"`
|
||||
Version string `yaml:"version" json:"version"`
|
||||
Repository string `yaml:"repository" json:"repository"`
|
||||
Alias string `yaml:"alias,omitempty" json:"alias,omitempty"`
|
||||
}
|
||||
|
||||
// DependenciesFrom returns a slice of dependencies from the given string.
|
||||
func DependenciesFrom(data string) ([]Dependency, error) {
|
||||
var mapping []Dependency
|
||||
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mapping, nil
|
||||
}
|
14
internal/generator/labels/labelstructs/dependencies_test.go
Normal file
14
internal/generator/labels/labelstructs/dependencies_test.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package labelstructs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDependenciesLabel(t *testing.T) {
|
||||
ts := "- name: mongodb"
|
||||
tc, _ := DependenciesFrom(ts)
|
||||
if len(tc) != 1 {
|
||||
t.Errorf("Expected DependenciesLabel to have 1 item, got %d", len(tc))
|
||||
}
|
||||
if tc[0].Name != "mongodb" {
|
||||
t.Errorf("Expected DependenciesLabel to contain 'mongodb', got %s", tc[0].Name)
|
||||
}
|
||||
}
|
2
internal/generator/labels/labelstructs/doc.go
Normal file
2
internal/generator/labels/labelstructs/doc.go
Normal file
@@ -0,0 +1,2 @@
|
||||
// Package labelstructs is a package that contains the structs used to represent the labels in the yaml files.
|
||||
package labelstructs
|
14
internal/generator/labels/labelstructs/envFrom.go
Normal file
14
internal/generator/labels/labelstructs/envFrom.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package labelstructs
|
||||
|
||||
import "gopkg.in/yaml.v3"
|
||||
|
||||
type EnvFrom []string
|
||||
|
||||
// EnvFromFrom returns a EnvFrom from the given string.
|
||||
func EnvFromFrom(data string) (EnvFrom, error) {
|
||||
var mapping EnvFrom
|
||||
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mapping, nil
|
||||
}
|
17
internal/generator/labels/labelstructs/envFrom_test.go
Normal file
17
internal/generator/labels/labelstructs/envFrom_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package labelstructs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestEnvFromLabel(t *testing.T) {
|
||||
ts := "- foo\n- bar"
|
||||
tc, _ := EnvFromFrom(ts)
|
||||
if len(tc) != 2 {
|
||||
t.Errorf("Expected EnvFrom to have 2 items, got %d", len(tc))
|
||||
}
|
||||
if tc[0] != "foo" {
|
||||
t.Errorf("Expected EnvFrom to contain 'foo', got %s", tc[0])
|
||||
}
|
||||
if tc[1] != "bar" {
|
||||
t.Errorf("Expected EnvFrom to contain 'bar', got %s", tc[1])
|
||||
}
|
||||
}
|
20
internal/generator/labels/labelstructs/exchangeVolume.go
Normal file
20
internal/generator/labels/labelstructs/exchangeVolume.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package labelstructs
|
||||
|
||||
import "gopkg.in/yaml.v3"
|
||||
|
||||
type ExchangeVolume struct {
|
||||
Name string `yaml:"name" json:"name"`
|
||||
MountPath string `yaml:"mountPath" json:"mountPath"`
|
||||
Type string `yaml:"type,omitempty" json:"type,omitempty"`
|
||||
Init string `yaml:"init,omitempty" json:"init,omitempty"`
|
||||
}
|
||||
|
||||
func NewExchangeVolumes(data string) ([]*ExchangeVolume, error) {
|
||||
mapping := []*ExchangeVolume{}
|
||||
|
||||
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mapping, nil
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package labelstructs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestExchangeVolumeLabel(t *testing.T) {
|
||||
ts := "- name: exchange-volume\n mountPath: /exchange\n readOnly: true"
|
||||
tc, _ := NewExchangeVolumes(ts)
|
||||
if len(tc) != 1 {
|
||||
t.Errorf("Expected ExchangeVolumeLabel to have 1 item, got %d", len(tc))
|
||||
}
|
||||
if tc[0].Name != "exchange-volume" {
|
||||
t.Errorf("Expected ExchangeVolumeLabel to contain 'exchange-volume', got %s", tc[0].Name)
|
||||
}
|
||||
if tc[0].MountPath != "/exchange" {
|
||||
t.Errorf("Expected MountPath to be '/exchange', got %s", tc[0].MountPath)
|
||||
}
|
||||
}
|
41
internal/generator/labels/labelstructs/ingress.go
Normal file
41
internal/generator/labels/labelstructs/ingress.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package labelstructs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/katenary/katenary/internal/utils"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type TLS struct {
|
||||
Enabled bool `yaml:"enabled" json:"enabled,omitempty"`
|
||||
}
|
||||
|
||||
type Ingress struct {
|
||||
Port *int32 `yaml:"port,omitempty" json:"port,omitempty"`
|
||||
Annotations map[string]string `yaml:"annotations,omitempty" jsonschema:"nullable" json:"annotations,omitempty"`
|
||||
Hostname string `yaml:"hostname,omitempty" json:"hostname,omitempty"`
|
||||
Path *string `yaml:"path,omitempty" json:"path,omitempty"`
|
||||
Class *string `yaml:"class,omitempty" json:"class,omitempty" jsonschema:"default:-"`
|
||||
Enabled bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`
|
||||
TLS *TLS `yaml:"tls,omitempty" json:"tls,omitempty"`
|
||||
}
|
||||
|
||||
// IngressFrom creates a new Ingress from a compose service.
|
||||
func IngressFrom(data string) (*Ingress, error) {
|
||||
mapping := Ingress{
|
||||
Hostname: "",
|
||||
Path: utils.StrPtr("/"),
|
||||
Enabled: false,
|
||||
Class: utils.StrPtr("-"),
|
||||
Port: nil,
|
||||
TLS: &TLS{Enabled: true},
|
||||
}
|
||||
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if mapping.Port == nil {
|
||||
return nil, fmt.Errorf("port is required in ingress definition")
|
||||
}
|
||||
return &mapping, nil
|
||||
}
|
31
internal/generator/labels/labelstructs/ingress_test.go
Normal file
31
internal/generator/labels/labelstructs/ingress_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package labelstructs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestIngressLabel(t *testing.T) {
|
||||
ts := "\nhostname: example.com\npath: /\nenabled: true\nport: 8888"
|
||||
tc, err := IngressFrom(ts)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing IngressLabel: %v", err)
|
||||
}
|
||||
if tc.Hostname != "example.com" {
|
||||
t.Errorf("Expected IngressLabel to contain 'example.com', got %s", tc.Hostname)
|
||||
}
|
||||
if tc.Path == nil || *tc.Path != "/" {
|
||||
t.Errorf("Expected IngressLabel to contain '/', got %v", tc.Path)
|
||||
}
|
||||
if tc.Enabled != true {
|
||||
t.Errorf("Expected IngressLabel to be enabled, got %v", tc.Enabled)
|
||||
}
|
||||
if tc.Port == nil || *tc.Port != 8888 {
|
||||
t.Errorf("Expected IngressLabel to have port 8888, got %d", tc.Port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIngressLabelNoPort(t *testing.T) {
|
||||
ts := "\nhostname: example.com\npath: /\nenabled: true"
|
||||
_, err := IngressFrom(ts)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error when parsing IngressLabel without port, got nil")
|
||||
}
|
||||
}
|
14
internal/generator/labels/labelstructs/mapenv.go
Normal file
14
internal/generator/labels/labelstructs/mapenv.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package labelstructs
|
||||
|
||||
import "gopkg.in/yaml.v3"
|
||||
|
||||
type MapEnv map[string]string
|
||||
|
||||
// MapEnvFrom returns a MapEnv from the given string.
|
||||
func MapEnvFrom(data string) (MapEnv, error) {
|
||||
var mapping MapEnv
|
||||
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mapping, nil
|
||||
}
|
11
internal/generator/labels/labelstructs/mapenv_test.go
Normal file
11
internal/generator/labels/labelstructs/mapenv_test.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package labelstructs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestConfigMapLabel(t *testing.T) {
|
||||
ts := "foo: bar"
|
||||
tc, _ := MapEnvFrom(ts)
|
||||
if len(tc) != 1 {
|
||||
t.Errorf("Expected ConfigMapFile to have 1 item, got %d", len(tc))
|
||||
}
|
||||
}
|
14
internal/generator/labels/labelstructs/ports.go
Normal file
14
internal/generator/labels/labelstructs/ports.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package labelstructs
|
||||
|
||||
import "gopkg.in/yaml.v3"
|
||||
|
||||
type Ports []uint32
|
||||
|
||||
// PortsFrom returns a Ports from the given string.
|
||||
func PortsFrom(data string) (Ports, error) {
|
||||
var mapping Ports
|
||||
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mapping, nil
|
||||
}
|
23
internal/generator/labels/labelstructs/ports_test.go
Normal file
23
internal/generator/labels/labelstructs/ports_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package labelstructs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPortsFromLabel(t *testing.T) {
|
||||
data := "- 8080\n- 9090\n"
|
||||
expected := Ports{8080, 9090}
|
||||
|
||||
ports, err := PortsFrom(data)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if len(ports) != len(expected) {
|
||||
t.Fatalf("expected length %d, got %d", len(expected), len(ports))
|
||||
}
|
||||
|
||||
for i, port := range ports {
|
||||
if port != expected[i] {
|
||||
t.Errorf("expected port %d at index %d, got %d", expected[i], i, port)
|
||||
}
|
||||
}
|
||||
}
|
56
internal/generator/labels/labelstructs/probes.go
Normal file
56
internal/generator/labels/labelstructs/probes.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package labelstructs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
)
|
||||
|
||||
type HealthCheck struct {
|
||||
LivenessProbe *corev1.Probe `yaml:"livenessProbe,omitempty" json:"livenessProbe,omitempty"`
|
||||
ReadinessProbe *corev1.Probe `yaml:"readinessProbe,omitempty" json:"readinessProbe,omitempty"`
|
||||
}
|
||||
|
||||
func ProbeFrom(data string) (*HealthCheck, error) {
|
||||
mapping := HealthCheck{}
|
||||
tmp := map[string]any{}
|
||||
err := yaml.Unmarshal([]byte(data), &tmp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if livenessProbe, ok := tmp["livenessProbe"]; ok {
|
||||
livenessProbeBytes, err := json.Marshal(livenessProbe)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling livenessProbe: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
livenessProbe := &corev1.Probe{}
|
||||
err = json.Unmarshal(livenessProbeBytes, livenessProbe)
|
||||
if err != nil {
|
||||
log.Printf("Error unmarshalling livenessProbe: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
mapping.LivenessProbe = livenessProbe
|
||||
}
|
||||
|
||||
if readinessProbe, ok := tmp["readinessProbe"]; ok {
|
||||
readinessProbeBytes, err := json.Marshal(readinessProbe)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling readinessProbe: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
readinessProbe := &corev1.Probe{}
|
||||
err = json.Unmarshal(readinessProbeBytes, readinessProbe)
|
||||
if err != nil {
|
||||
log.Printf("Error unmarshalling readinessProbe: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
mapping.ReadinessProbe = readinessProbe
|
||||
|
||||
}
|
||||
|
||||
return &mapping, err
|
||||
}
|
16
internal/generator/labels/labelstructs/probes_test.go
Normal file
16
internal/generator/labels/labelstructs/probes_test.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package labelstructs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestProbesLabel(t *testing.T) {
|
||||
readiness := "readinessProbe:\n httpGet:\n path: /healthz\n port: 8080\n initialDelaySeconds: 5\n periodSeconds: 10"
|
||||
tc, err := ProbeFrom(readiness)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing ProbesLabel: %v %v", err, tc)
|
||||
}
|
||||
liveness := "livenessProbe:\n httpGet:\n path: /healthz\n port: 8080\n initialDelaySeconds: 5\n periodSeconds: 10"
|
||||
tc2, err := ProbeFrom(liveness)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing ProbesLabel: %v %v", err, tc2)
|
||||
}
|
||||
}
|
13
internal/generator/labels/labelstructs/secrets.go
Normal file
13
internal/generator/labels/labelstructs/secrets.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package labelstructs
|
||||
|
||||
import "gopkg.in/yaml.v3"
|
||||
|
||||
type Secrets []string
|
||||
|
||||
func SecretsFrom(data string) (Secrets, error) {
|
||||
var mapping Secrets
|
||||
if err := yaml.Unmarshal([]byte(data), &mapping); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mapping, nil
|
||||
}
|
17
internal/generator/labels/labelstructs/secrets_test.go
Normal file
17
internal/generator/labels/labelstructs/secrets_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package labelstructs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSecretLabel(t *testing.T) {
|
||||
data := "- foo\n- bar"
|
||||
tc, err := SecretsFrom(data)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing SecretLabel: %v %v", err, tc)
|
||||
}
|
||||
items := []string{"foo", "bar"}
|
||||
for i, item := range tc {
|
||||
if item != items[i] {
|
||||
t.Errorf("Expected SecretLabel to contain '%s', got '%s'", items[i], item)
|
||||
}
|
||||
}
|
||||
}
|
13
internal/generator/labels/labelstructs/valueFrom.go
Normal file
13
internal/generator/labels/labelstructs/valueFrom.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package labelstructs
|
||||
|
||||
import "gopkg.in/yaml.v3"
|
||||
|
||||
type ValueFrom map[string]string
|
||||
|
||||
func GetValueFrom(data string) (*ValueFrom, error) {
|
||||
vf := ValueFrom{}
|
||||
if err := yaml.Unmarshal([]byte(data), &vf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &vf, nil
|
||||
}
|
25
internal/generator/labels/labelstructs/valueFrom_test.go
Normal file
25
internal/generator/labels/labelstructs/valueFrom_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package labelstructs
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValueFromLabel(t *testing.T) {
|
||||
data := "data: foo\ndata2: bar"
|
||||
tc, err := GetValueFrom(data)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if tc == nil {
|
||||
t.Fatalf("expected non-nil map, got nil")
|
||||
}
|
||||
if len(*tc) != 2 {
|
||||
t.Errorf("expected 2 items, got %d", len(*tc))
|
||||
}
|
||||
if (*tc)["data"] != "foo" {
|
||||
t.Errorf("expected 'data' to be 'foo', got %s", (*tc)["data"])
|
||||
}
|
||||
if (*tc)["data2"] != "bar" {
|
||||
t.Errorf("expected 'data2' to be 'bar', got %s", (*tc)["data2"])
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user