Skip to content

Commit

Permalink
chore(refacto): fix secret and use katenary schema
Browse files Browse the repository at this point in the history
- add possibility to use a katenary.yaml file to setup values
- fix secret generation
  • Loading branch information
metal3d committed Nov 18, 2024
1 parent 14877fb commit cc1019b
Show file tree
Hide file tree
Showing 39 changed files with 375 additions and 155 deletions.
27 changes: 11 additions & 16 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
.venv
dist/*
.cache/*
chart/*
*.yaml
*.yml
!.markdownlint.yaml
!generator/*.yaml
doc/venv/*
!doc/mkdocs.yaml
!.readthedocs.yaml
./katenary
*.env
docker-compose*
!examples/**/docker-compose*
.credentials
release.id
configs/
cover*
.sq
./katenary
.aider*
.python_history
.bash_history
katenary

.cache/
.aider/
.config/
*/venv

# local binary
./katenary

# will be treated later
/examples/*
5 changes: 4 additions & 1 deletion .markdownlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
default: true

MD013: # Line length
line_length: 240
line_length: 120

MD010: # Hard tabs
code_blocks: false
Expand All @@ -16,3 +16,6 @@ MD041: false
# list indentation
MD007:
indent: 4

# no problem using several code blocks styles
MD046: false
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ tests: test
test:
@echo -e "\033[1;33mTesting katenary $(VERSION)...\033[0m"
go test -coverprofile=cover.out ./...
$(MAKE) cover

cover:
go tool cover -func=cover.out | grep "total:"
go tool cover -html=cover.out -o cover.html
if [ "$(BROWSER)" = "xdg-open" ]; then
Expand Down
29 changes: 22 additions & 7 deletions cmd/katenary/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package main
import (
"fmt"
"katenary/generator"
"katenary/generator/katenaryfile"
"katenary/generator/labels"
"katenary/utils"
"os"
"strings"
Expand Down Expand Up @@ -43,6 +45,7 @@ func buildRootCmd() *cobra.Command {
generateConvertCommand(),
generateHashComposefilesCommand(),
generateLabelHelpCommand(),
generateSchemaCommand(),
)

return rootCmd
Expand Down Expand Up @@ -245,31 +248,31 @@ func generateLabelHelpCommand() *cobra.Command {
If no label is specified, the help for all labels is printed.
If a label is specified, the help for this label is printed.
The name of the label must be specified without the prefix ` + generator.Prefix() + `.
The name of the label must be specified without the prefix ` + labels.Prefix() + `.
e.g.
kanetary help-labels
katenary help-labels ingress
katenary help-labels map-env
`,
ValidArgs: generator.GetLabelNames(),
ValidArgs: labels.GetLabelNames(),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
fmt.Println(generator.GetLabelHelpFor(args[0], markdown))
fmt.Println(labels.GetLabelHelpFor(args[0], markdown))
return
}
if all {
// show the help for all labels
l := len(generator.GetLabelNames())
for i, label := range generator.GetLabelNames() {
fmt.Println(generator.GetLabelHelpFor(label, markdown))
l := len(labels.GetLabelNames())
for i, label := range labels.GetLabelNames() {
fmt.Println(labels.GetLabelHelpFor(label, markdown))
if !markdown && i < l-1 {
fmt.Println(strings.Repeat("-", 80))
}
}
return
}
fmt.Println(generator.GetLabelHelp(markdown))
fmt.Println(labels.GetLabelHelp(markdown))
},
}

Expand Down Expand Up @@ -298,3 +301,15 @@ If no composefile is specified, the hash of all composefiles is printed.`,
}
return cmd
}

func generateSchemaCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "schema",
Short: "Print the schema of the katenary file",
Long: "Generate a schama for katenary.yaml file that can be used to validate the file or to use with yaml LSP to complete and check your configuration.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(katenaryfile.GenerateSchema())
},
}
return cmd
}
2 changes: 1 addition & 1 deletion cmd/katenary/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestBuildCommand(t *testing.T) {
if rootCmd.Use != "katenary" {
t.Errorf("Expected rootCmd.Use to be katenary, got %s", rootCmd.Use)
}
numCommands := 5
numCommands := 6
if len(rootCmd.Commands()) != numCommands {
t.Errorf("Expected %d command, got %d", numCommands, len(rootCmd.Commands()))
}
Expand Down
16 changes: 9 additions & 7 deletions generator/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package generator

import (
"fmt"
"katenary/generator/labelStructs"
"katenary/generator/labels"
"katenary/generator/labels/labelStructs"
"katenary/utils"
"log"
"os"
Expand Down Expand Up @@ -136,7 +137,7 @@ func (chart *HelmChart) generateConfigMapsAndSecrets(project *types.Project) err
originalEnv[k] = v
}

if v, ok := s.Labels[LabelSecrets]; ok {
if v, ok := s.Labels[labels.LabelSecrets]; ok {
list, err := labelStructs.SecretsFrom(v)
if err != nil {
log.Fatal("error unmarshaling secrets label:", err)
Expand Down Expand Up @@ -210,7 +211,8 @@ func (chart *HelmChart) generateDeployment(service types.ServiceConfig, deployme

// get the same-pod label if exists, add it to the list.
// We later will copy some parts to the target deployment and remove this one.
if samePod, ok := service.Labels[LabelSamePod]; ok && samePod != "" {
if samePod, ok := service.Labels[labels.LabelSamePod]; ok && samePod != "" {
log.Printf("Found same-pod label for %s", service.Name)
podToMerge[samePod] = &service
}

Expand Down Expand Up @@ -247,7 +249,7 @@ func (chart *HelmChart) setChartVersion(service types.ServiceConfig) {

// setCronJob creates a cronjob from the service labels.
func (chart *HelmChart) setCronJob(service types.ServiceConfig, appName string) *CronJob {
if _, ok := service.Labels[LabelCronJob]; !ok {
if _, ok := service.Labels[labels.LabelCronJob]; !ok {
return nil
}
cronjob, rbac := NewCronJob(service, chart, appName)
Expand Down Expand Up @@ -281,7 +283,7 @@ func (chart *HelmChart) setCronJob(service types.ServiceConfig, appName string)
// setDependencies sets the dependencies from the service labels.
func (chart *HelmChart) setDependencies(service types.ServiceConfig) (bool, error) {
// helm dependency
if v, ok := service.Labels[LabelDependencies]; ok {
if v, ok := service.Labels[labels.LabelDependencies]; ok {
d, err := labelStructs.DependenciesFrom(v)
if err != nil {
return false, err
Expand All @@ -307,10 +309,10 @@ func (chart *HelmChart) setDependencies(service types.ServiceConfig) (bool, erro
func (chart *HelmChart) setSharedConf(service types.ServiceConfig, deployments map[string]*Deployment) {
// if the service has the "shared-conf" label, we need to add the configmap
// to the chart and add the env vars to the service
if _, ok := service.Labels[LabelEnvFrom]; !ok {
if _, ok := service.Labels[labels.LabelEnvFrom]; !ok {
return
}
fromservices, err := labelStructs.EnvFromFrom(service.Labels[LabelEnvFrom])
fromservices, err := labelStructs.EnvFromFrom(service.Labels[labels.LabelEnvFrom])
if err != nil {
log.Fatal("error unmarshaling env-from label:", err)
}
Expand Down
9 changes: 5 additions & 4 deletions generator/configMap.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package generator

import (
"katenary/generator/labelStructs"
"katenary/generator/labels"
"katenary/generator/labels/labelStructs"
"katenary/utils"
"log"
"os"
Expand Down Expand Up @@ -73,7 +74,7 @@ func NewConfigMap(service types.ServiceConfig, appName string, forFile bool) *Co
}

// get the secrets from the labels
secrets, err := labelStructs.SecretsFrom(service.Labels[LabelSecrets])
secrets, err := labelStructs.SecretsFrom(service.Labels[labels.LabelSecrets])
if err != nil {
log.Fatal(err)
}
Expand All @@ -82,7 +83,7 @@ func NewConfigMap(service types.ServiceConfig, appName string, forFile bool) *Co
drop[secret] = true
}
// get the label values from the labels
varDescriptons := utils.GetValuesFromLabel(service, LabelValues)
varDescriptons := utils.GetValuesFromLabel(service, labels.LabelValues)
for value := range varDescriptons {
labelValues = append(labelValues, value)
}
Expand All @@ -98,7 +99,7 @@ func NewConfigMap(service types.ServiceConfig, appName string, forFile bool) *Co
if !forFile {
// do not bind env variables to the configmap
// remove the variables that are already defined in the environment
if l, ok := service.Labels[LabelMapEnv]; ok {
if l, ok := service.Labels[labels.LabelMapEnv]; ok {
envmap, err := labelStructs.MapEnvFrom(l)
if err != nil {
log.Fatal("Error parsing map-env", err)
Expand Down
17 changes: 11 additions & 6 deletions generator/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"errors"
"fmt"
"katenary/generator/extrafiles"
"katenary/generator/labelStructs"
"katenary/generator/katenaryfile"
"katenary/generator/labels"
"katenary/generator/labels/labelStructs"
"katenary/parser"
"katenary/utils"
"log"
Expand Down Expand Up @@ -125,6 +127,9 @@ func Convert(config ConvertOptions, dockerComposeFile ...string) {
os.Exit(1)
}

// TODO: use katenary.yaml file here to set the labels
katenaryfile.OverrideWithConfig(project)

if !config.Force {
// check if the chart directory exists
// if yes, prevent the user from overwriting it and ask for confirmation
Expand Down Expand Up @@ -264,7 +269,7 @@ func addDependencyDescription(values []byte, dependencies []labelStructs.Depende
// of the service definition.
func addDescriptions(values []byte, project types.Project) []byte {
for _, service := range project.Services {
if description, ok := service.Labels[LabelDescription]; ok {
if description, ok := service.Labels[labels.LabelDescription]; ok {
// set it as comment
description = "\n# " + strings.ReplaceAll(description, "\n", "\n# ")

Expand All @@ -288,7 +293,7 @@ func addDescriptions(values []byte, project types.Project) []byte {

func addDocToVariable(service types.ServiceConfig, lines []string) []string {
currentService := ""
variables := utils.GetValuesFromLabel(service, LabelValues)
variables := utils.GetValuesFromLabel(service, labels.LabelValues)
for i, line := range lines {
// if the line is a service, it is a name followed by a colon
if regexp.MustCompile(`(?m)^` + service.Name + `:`).MatchString(line) {
Expand Down Expand Up @@ -378,7 +383,7 @@ func addMainTagAppDoc(values []byte, project *types.Project) []byte {

for _, service := range project.Services {
// read the label LabelMainApp
if v, ok := service.Labels[LabelMainApp]; !ok {
if v, ok := service.Labels[labels.LabelMainApp]; !ok {
continue
} else if v == "false" || v == "no" || v == "0" {
continue
Expand Down Expand Up @@ -651,7 +656,7 @@ func checkOldLabels(project *types.Project) error {
badServices := make([]string, 0)
for _, service := range project.Services {
for label := range service.Labels {
if strings.Contains(label, "katenary.") && !strings.Contains(label, katenaryLabelPrefix) {
if strings.Contains(label, "katenary.") && !strings.Contains(label, labels.KatenaryLabelPrefix) {
badServices = append(badServices, fmt.Sprintf("- %s: %s", service.Name, label))
}
}
Expand All @@ -667,7 +672,7 @@ func checkOldLabels(project *types.Project) error {
Services to upgrade:
%s`,
project.Name,
katenaryLabelPrefix[0:len(katenaryLabelPrefix)-1],
labels.KatenaryLabelPrefix[0:len(labels.KatenaryLabelPrefix)-1],
strings.Join(badServices, "\n"),
)

Expand Down
5 changes: 3 additions & 2 deletions generator/cronJob.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package generator

import (
"katenary/generator/labelStructs"
"katenary/generator/labels"
"katenary/generator/labels/labelStructs"
"katenary/utils"
"log"
"strings"
Expand All @@ -25,7 +26,7 @@ type CronJob struct {

// NewCronJob creates a new CronJob from a compose service. The appName is the name of the application taken from the project name.
func NewCronJob(service types.ServiceConfig, chart *HelmChart, appName string) (*CronJob, *RBAC) {
labels, ok := service.Labels[LabelCronJob]
labels, ok := service.Labels[labels.LabelCronJob]
if !ok {
return nil, nil
}
Expand Down
Loading

0 comments on commit cc1019b

Please sign in to comment.