Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/gitOpStyle'
Browse files Browse the repository at this point in the history
# Conflicts:
#	.vscode/launch.json
#	Makefile
#	cmd/apply.go
#	cmd/applyNamespace.go
#	cmd/delete.go
#	cmd/root.go
#	realm/namespaces/_defaults.yaml
#	realm/values/env/dev/default/values.yaml
  • Loading branch information
llazigusho committed Jul 14, 2023
2 parents 63a59b3 + 2c2982a commit 2558fa2
Show file tree
Hide file tree
Showing 24 changed files with 580 additions and 228 deletions.
16 changes: 15 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,21 @@
"host": "127.0.0.1",
"program": "${workspaceRoot}/main.go",
// "envFile": "${workspaceRoot}/.dev.env",
"args": ["apply","--skip-deps","--diff-run", "-e","dev", "-n", "default","--app","dummy","monitoring"],
"args": ["apply","--skip-deps","--diff-run", "-e","dev", "-n", "default","monitoring"],
"showLog": true
},
{
"name": "Launch Gitops",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"cwd": "${workspaceRoot}",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceRoot}/main.go",
// "envFile": "${workspaceRoot}/.dev.env",
"args": ["apply","-e","dev", "-n", "default","gitops-defs","--app","dummy","monitoring"],
"showLog": true
},
{
Expand Down
19 changes: 10 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
PROJECT_NAME := korgi
SHELL := /usr/bin/env bash

PROJECT_NAME:=korgi
GOFILES:=$(shell find . -name '*.go' | grep -v -E '(./vendor)')
OS := $(shell uname | tr '[:upper:]' '[:lower:]')
ARCH := amd64
LDFLAGS := -w -s
SRC := $(shell find . -type f -name '*.go' -print)
REPO := github.com/DataReply/korgi

bin: $(SRC)

run:
go run main.go

all: clean check bin

bin: $(GOFILES)
mkdir -p bin/${OS}/
GOOS=${OS} GOARCH=${ARCH} go build -ldflags "${LDFLAGS}" -o bin/${OS}/korgi main.go
GOOS=${OS} GOARCH=${ARCH} go build -a -tags musl -ldflags "$(LDFLAGS)" -o bin/${OS}/korgi main.go

gofmt:
gofmt -w -s pkg/
Expand All @@ -32,7 +36,4 @@ clean:
run:
go run main.go

all: clean check bin

.PHONY: all

120 changes: 10 additions & 110 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -16,122 +16,24 @@ limitations under the License.
package cmd

import (
"fmt"
"os"
"path/filepath"
"strconv"

"github.com/DataReply/korgi/pkg/utils"
"github.com/spf13/cobra"
"os"
)

func templateApp(app string, namespace string, inputFilePath string, appGroupDir string, lint bool) error {

targeAppDir := utils.ConcatDirs(appGroupDir, app)

err := os.MkdirAll(targeAppDir, os.ModePerm)
if err != nil {
return fmt.Errorf("creating app dir: %w", err)
}
if lint {
err = helmfileEngine.Lint(app, inputFilePath)
if err != nil {
return err
}
}
err = helmfileEngine.Template(app, namespace, inputFilePath, targeAppDir)
if err != nil {
return err
}

return nil
}

func getFinalOutputDir(outputDir string, isolated bool) string {
if isolated {
return utils.ConcatDirs(outputDir, strconv.FormatInt(execTime.UTC().Unix(), 10))
}
return outputDir
}

func applyAppGroup(group string, namespace string, outputDir string, appFilter string, lint bool, dryRun bool, askUser bool) error {

log.V(0).Info("applying", "group", group, "namespace", namespace, "app", appFilter, "lint", lint, "dry", dryRun, "ask", askUser)
namespaceDir := utils.GetNamespaceDir(namespace)
if _, err := os.Stat(namespaceDir); os.IsNotExist(err) {
return fmt.Errorf("%s directory does not exist", namespaceDir)
}

appGroupDir := utils.ConcatDirs(namespaceDir, group)

targetAppGroupDir := utils.ConcatDirs(outputDir, namespace, group)

err := os.MkdirAll(targetAppGroupDir, os.ModePerm)
if err != nil {
return fmt.Errorf("creating group directory: %w", err)
}

matches, err := filepath.Glob(appGroupDir + "/*")
if err != nil {
return fmt.Errorf("listing group directory: %w", err)
}

for _, matchedAppFile := range matches {
appFile := filepath.Base(matchedAppFile)
if appFile != "_app_group.yaml" {
app := utils.SanitzeAppName(appFile)
if appFilter != "" {

if app != appFilter {
continue
}
}

err = templateApp(app, namespace, matchedAppFile, targetAppGroupDir, lint)
if err != nil {
return fmt.Errorf("templating app: %w", err)
}

}

}
if !dryRun {
if appFilter != "" {
err = kappEngine.DeployApp(group+"-"+appFilter, utils.ConcatDirs(targetAppGroupDir, appFilter), namespace)
if err != nil {
return fmt.Errorf("running kapp deploy with appFilter: %w", err)
}
return nil
}

err = kappEngine.DeployGroup(group, targetAppGroupDir, namespace)
if err != nil {
return fmt.Errorf("running kapp deploy: %w", err)
}
}

return nil
}

// applyCmd represents the apply command
var applyCmd = &cobra.Command{
Use: "apply",
Short: "Apply resources to k8s",
Args: cobra.ExactArgs(1),
Use: "apply",
Short: "Apply resources to k8s",
Args: cobra.ExactArgs(1),
TraverseChildren: true,
RunE: func(cmd *cobra.Command, args []string) error {

namespace, _ := cmd.Flags().GetString("namespace")

lint, _ := cmd.Flags().GetBool("lint")

dryRun, _ := cmd.Flags().GetBool("dry-run")

appFilter, _ := cmd.Flags().GetString("app")

outputDir, _ := cmd.Flags().GetString("output-dir")

isolated, _ := cmd.Flags().GetBool("isolate")

askForConfirmation, _ := cmd.Flags().GetBool("ask-for-confirmation")

if !askForConfirmation {
Expand All @@ -144,21 +46,19 @@ var applyCmd = &cobra.Command{
}
}

err := applyAppGroup(args[0], namespace, getFinalOutputDir(outputDir, isolated), appFilter, lint, dryRun, askForConfirmation)
err := applyAppGroup(args[0], namespace, getFinalOutputDir(outputDir, isolated), appFilter, lint, dryRun, defaultMatcher, askForConfirmation)
if err != nil {
return err
}

return nil
},
}

func init() {
rootCmd.AddCommand(applyCmd)

applyCmd.Flags().BoolP("lint", "l", false, "Lint temlate")
applyCmd.Flags().BoolP("dry-run", "d", false, "Dry Run")
applyCmd.Flags().StringP("namespace", "n", "", "Target namespace")
applyCmd.PersistentFlags().BoolP("lint", "l", false, "Lint temlate")
applyCmd.PersistentFlags().BoolP("dry-run", "d", false, "Dry Run")
applyCmd.PersistentFlags().StringP("namespace", "n", "", "Target namespace")
applyCmd.MarkFlagRequired("namespace")

}
39 changes: 39 additions & 0 deletions cmd/applyGitOps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright © 2020 Artyom Topchyan a.topchyan@reply.de
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"github.com/spf13/cobra"
)

// applyAppDefs represents the apply command
var applyAppDefs = &cobra.Command{
Use: "gitops-manifests",
Short: "Apply custom definition for generated app resources to k8s",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
err := runApplyWithMatch(cmd, args, gitOpsMatcher)
if err != nil {
return err
}
return nil

},
}

func init() {
applyCmd.AddCommand(applyAppDefs)
}
Loading

0 comments on commit 2558fa2

Please sign in to comment.