Skip to content

Commit

Permalink
Use config file instead of passing all parameteres in the pipelines
Browse files Browse the repository at this point in the history
This reduces the number of parameters passed in the pipelines
  • Loading branch information
LucaLanziani committed Jul 14, 2023
1 parent c70fced commit 8fdb79a
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 73 deletions.
6 changes: 0 additions & 6 deletions assets/github/onbranch.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ jobs:
with:
args: onbranch --clean
env:
KKA_APP_NAME: {{ .AppName }}
KKA_PROJECT_DIRECTORY: {{ .ProjectDirectory }}
KKA_REPO_NAME: {{ .Repository }}
KKA_REGISTRY_USER: {{ `${{ github.actor }}` }}
KKA_REGISTRY_PASSWORD: {{ `${{ secrets.GITHUB_TOKEN }}` }}
KKA_CLUSTER_ENDPOINT: {{ `${{ secrets.CLUSTER_ENDPOINT }}` }}
Expand All @@ -35,9 +32,6 @@ jobs:
with:
args: onbranch
env:
KKA_APP_NAME: {{ .AppName }}
KKA_PROJECT_DIRECTORY: {{ .ProjectDirectory }}
KKA_REPO_NAME: {{ .Repository }}
KKA_REGISTRY_USER: {{ `${{ github.actor }}` }}
KKA_REGISTRY_PASSWORD: {{ `${{ secrets.GITHUB_TOKEN }}` }}
KKA_CLUSTER_ENDPOINT: {{ `${{ secrets.CLUSTER_ENDPOINT }}` }}
Expand Down
3 changes: 0 additions & 3 deletions assets/github/onmain.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ jobs:
with:
args: onmain
env:
KKA_APP_NAME: {{ .AppName }}
KKA_PROJECT_DIRECTORY: {{ .ProjectDirectory }}
KKA_REPO_NAME: {{ .Repository }}
KKA_REGISTRY_USER: {{ `${{ github.actor }}` }}
KKA_REGISTRY_PASSWORD: {{ `${{ secrets.GITHUB_TOKEN }}` }}
KKA_CLUSTER_ENDPOINT: {{ `${{ secrets.CLUSTER_ENDPOINT }}` }}
Expand Down
19 changes: 6 additions & 13 deletions src/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"embed"
"io"
"os"
"path"
"path/filepath"
"sort"

Expand Down Expand Up @@ -74,7 +73,12 @@ func (c *CLI) init(cCtx *cli.Context) error {
Tag: version,
}

dockerService, err := docker.New(project, dockerImage, cCtx.String(dockerFileNameFlag))
dockerFileName := cCtx.String(dockerFileNameFlag)
if dockerFileName == "" {
dockerFileName = defaults.GeneratedDockerFile
}

dockerService, err := docker.New(project, dockerImage, dockerFileName)
if err != nil {
logger.PrintError("Error creating docker service", err)
}
Expand Down Expand Up @@ -115,17 +119,6 @@ func (c CLI) Run(args []string) error {
return err
}

projectDirectory := ctx.String(projectDirectoryFlag)
absProjectDirectory, err := filepath.Abs(projectDirectory)

if err != nil {
return err
}

if ctx.String(appNameFlag) == "" {
ctx.Set(appNameFlag, path.Base(absProjectDirectory))
}

if err := c.checkRequiredFlags(ctx, []string{}); err != nil {
return err
}
Expand Down
25 changes: 17 additions & 8 deletions src/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strings"

"github.com/nearform/k8s-kurated-addons-cli/src/services/git"
"github.com/nearform/k8s-kurated-addons-cli/src/utils/defaults"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -46,7 +47,14 @@ const (
var requiredFlags []string
var flags map[FlagsType]([]cli.Flag)

// This function is executed when the module is loaded
func init() {
registry := ""
org, err := git.GetGithubOrg()
if err == nil {
registry = fmt.Sprintf("ghcr.io/%s", org)
}

defaultFlags := map[FlagsType]([]cli.Flag){
Build: []cli.Flag{
&cli.StringFlag{
Expand Down Expand Up @@ -111,9 +119,10 @@ func init() {
},
App: []cli.Flag{
&cli.StringFlag{
Name: appNameFlag,
Usage: "The name of the app",
EnvVars: []string{"KKA_APP_NAME"},
Name: appNameFlag,
Usage: "The name of the app",
Required: true,
EnvVars: []string{"KKA_APP_NAME"},
},
&cli.StringFlag{
Name: appVersionFlag,
Expand All @@ -128,15 +137,15 @@ func init() {
EnvVars: []string{"KKA_PROJECT_DIRECTORY"},
},
&cli.StringFlag{
Name: repoNameFlag,
Usage: "The base address of the container repository",
Value: defaults.RepoName,
EnvVars: []string{"KKA_REPO_NAME"},
Name: repoNameFlag,
Usage: "The base address of the container repository",
Value: registry,
Required: registry == "",
EnvVars: []string{"KKA_REPO_NAME"},
},
&cli.StringFlag{
Name: dockerFileNameFlag,
Usage: "The name of the Dockerfile",
Value: defaults.DockerfileName,
EnvVars: []string{"KKA_DOCKERFILE_NAME"},
},
&cli.StringFlag{
Expand Down
15 changes: 3 additions & 12 deletions src/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import (
"github.com/charmbracelet/log"
"k8s.io/utils/strings/slices"

"github.com/nearform/k8s-kurated-addons-cli/src/services/git"
"github.com/nearform/k8s-kurated-addons-cli/src/services/k8s"
"github.com/nearform/k8s-kurated-addons-cli/src/services/project"
"github.com/nearform/k8s-kurated-addons-cli/src/utils/defaults"
"github.com/urfave/cli/v2"
)

Expand All @@ -20,14 +18,6 @@ func (c CLI) InitGithubCMD(cCtx *cli.Context) error {
logger.SetLevel(log.DebugLevel)
registry := cCtx.String(repoNameFlag)

if registry == defaults.RepoName {
org, err := git.GetGithubOrg()
if err != nil {
return fmt.Errorf("cannot get github organization %v", err)
}
registry = fmt.Sprintf("ghcr.io/%s", org)
}

options := project.InitOptions{
PipelineType: cCtx.Command.Name,
DestinationFolder: cCtx.String(destinationFolderFlag),
Expand All @@ -52,6 +42,7 @@ func (c CLI) InitGithubCMD(cCtx *cli.Context) error {
func (c CLI) InitConfigCMD(ctx *cli.Context) error {
excludedFlags := []string{
"help",
appVersionFlag,
namespaceFlag,
configFileFlag,
projectDirectoryFlag,
Expand All @@ -74,13 +65,13 @@ func (c CLI) InitConfigCMD(ctx *cli.Context) error {

value := ctx.String(stringFlag.Name)
if value == "" {
stringFlag.GetValue()
value = stringFlag.Value
}

if value == "" {
fmt.Fprintf(c.Writer, "%s: null\n", stringFlag.Name)
} else {
fmt.Fprintf(c.Writer, "%s: %s\n", stringFlag.Name, ctx.String(stringFlag.Name))
fmt.Fprintf(c.Writer, "%s: %s\n", stringFlag.Name, value)
}
}

Expand Down
17 changes: 4 additions & 13 deletions src/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,18 @@ import (
"testing"

"github.com/charmbracelet/log"
"github.com/nearform/k8s-kurated-addons-cli/src/utils/defaults"
)

func compareConfig(t *testing.T, appName string, writer io.Writer) {
configTemplate := fmt.Sprintf(`app-name: %s
app-version: %s
cluster-endpoint: null
default-branch: null
dockerfile-name: %s
default-branch: main
dockerfile-name: null
registry-user: null
repo-name: %s
repo-name: ghcr.io/nearform
runtime-version: null
`,
appName,
defaults.AppVersion,
defaults.DockerfileName,
defaults.RepoName,
)

result := fmt.Sprint(writer.(*bytes.Buffer))
Expand All @@ -35,7 +30,6 @@ runtime-version: null
}

func TestInitConfig(t *testing.T) {

cli := CLI{
Writer: new(bytes.Buffer),
Logger: log.NewWithOptions(os.Stderr, log.Options{
Expand All @@ -45,11 +39,8 @@ func TestInitConfig(t *testing.T) {
}),
}

if err := cli.Run([]string{"kka", "init", "config"}); err != nil {
t.Error(err)
}
os.Setenv("KKA_REPO_NAME", "ghcr.io/nearform")

compareConfig(t, "cli", cli.Writer)
// Config file is read correctly

// Generate temporary file and add app-name parameter
Expand Down
46 changes: 29 additions & 17 deletions src/services/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/nearform/k8s-kurated-addons-cli/src/services/project"

"github.com/nearform/k8s-kurated-addons-cli/src/utils/defaults"
"github.com/nearform/k8s-kurated-addons-cli/src/utils/logger"
)

Expand Down Expand Up @@ -52,6 +53,29 @@ func getClient() (*client.Client, error) {
return cli, nil
}

func (ds DockerService) generateDockerfile(tarWriter *tar.Writer) error {
// Add another file to the build context from an array of bytes
fileBytes, err := ds.project.Dockerfile()
if err != nil {
return fmt.Errorf("Loading dockerfile %v", err)
}
hdr := &tar.Header{
Name: defaults.GeneratedDockerFile,
Mode: 0600,
Size: int64(len(fileBytes)),
ModTime: time.Now(),
}

if err := tarWriter.WriteHeader(hdr); err != nil {
return fmt.Errorf("Writing Dockerfile header %v", err)
}
if _, err := tarWriter.Write(fileBytes); err != nil {
return fmt.Errorf("Writing Dockerfile content %v", err)
}

return nil
}

func (ds DockerService) buildContext() (*bytes.Reader, error) {
// Get the context for the docker build
existingBuildContext, err := archive.TarWithOptions(ds.dockerImage.Directory, &archive.TarOptions{})
Expand Down Expand Up @@ -81,23 +105,11 @@ func (ds DockerService) buildContext() (*bytes.Reader, error) {
}
}

// Add another file to the build context from an array of bytes
fileBytes, err := ds.project.Dockerfile()
if err != nil {
return nil, fmt.Errorf("Loading dockerfile %v", err)
}
hdr := &tar.Header{
Name: "Dockerfile.kka",
Mode: 0600,
Size: int64(len(fileBytes)),
ModTime: time.Now(),
}

if err := tarWriter.WriteHeader(hdr); err != nil {
return nil, fmt.Errorf("Writing Dockerfile header %v", err)
}
if _, err := tarWriter.Write(fileBytes); err != nil {
return nil, fmt.Errorf("Writing Dockerfile content %v", err)
// if dockerfile-name is not specified generate a dockerfile
if ds.DockerFileName == defaults.GeneratedDockerFile {
if err = ds.generateDockerfile(tarWriter); err != nil {
return nil, err
}
}

// Close the tar archive
Expand Down
2 changes: 1 addition & 1 deletion src/utils/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package defaults
const (
ProjectDirectory string = "."
RepoName string = "ghcr.io/nearform"
DockerfileName string = "Dockerfile.kka"
GithubActionFolder string = ".github/workflows"
GithubDefaultBranch string = "main"
ConfigFile string = ".kka"
AppVersion string = "latest"
GeneratedDockerFile string = "Dockerfile.kka"
)

// renovate: datasource=docker depName=node
Expand Down

0 comments on commit 8fdb79a

Please sign in to comment.