Skip to content

Commit

Permalink
Add repo sync and some small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
arttii committed Aug 4, 2020
1 parent a8cda03 commit 295b24d
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func applyAppGroup(group string, namespace string, outputDir string, appFilter s
var applyCmd = &cobra.Command{
Use: "apply",
Short: "Apply resources to k8s",
Args: cobra.MinimumNArgs(1),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

namespace, _ := cmd.Flags().GetString("namespace")
Expand Down
2 changes: 1 addition & 1 deletion cmd/applyNamespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
var namespaceCmd = &cobra.Command{
Use: "apply-namespace",
Short: "Namespace apply",
Args: cobra.MinimumNArgs(1),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
lint, _ := cmd.Flags().GetBool("lint")

Expand Down
2 changes: 1 addition & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func deleteAppGroup(group string, namespace string, appFilter string) error {
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete app group or app",
Args: cobra.MinimumNArgs(1),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
group := args[0]

Expand Down
2 changes: 1 addition & 1 deletion cmd/deleteNamespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
var deleteNamespaceCmd = &cobra.Command{
Use: "delete-namespace",
Short: "Namespace apply",
Args: cobra.MinimumNArgs(1),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
appFilter, _ := cmd.Flags().GetString("app")

Expand Down
41 changes: 41 additions & 0 deletions cmd/syncRepos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright © 2020 Artyom Topchyan [email protected]
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"
)

// syncReposCmd represents the delete command
var syncReposCmd = &cobra.Command{
Use: "sync-repos",
Short: "Sync repositories listed in a state file, primarily useful for Helmfile",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

err := templateEngine.SyncRepos(args[0])
if err != nil {
return err
}

return nil
},
}

func init() {
rootCmd.AddCommand(syncReposCmd)

}
19 changes: 19 additions & 0 deletions pkg/template/helmfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewHelmFileEngine(Opts Opts, log logr.Logger) *HelmFileEngine {
}

func (e *HelmFileEngine) Template(name string, inputFilePath string, outputFilePath string) error {

inputArgs := append([]string{"--environment", e.Opts.Environment,
"--file", inputFilePath, "--state-values-set", fmt.Sprintf("app=%s", name),
"template", "--output-dir", outputFilePath}, e.Opts.ExtraArgs...)
Expand Down Expand Up @@ -69,3 +70,21 @@ func (e *HelmFileEngine) Lint(name string, inputFilePath string) error {

return nil
}

func (e *HelmFileEngine) SyncRepos(file string) error {

inputArgs := append([]string{"-f", file, "repos"}, e.Opts.ExtraArgs...)

cmd := exec.Command("helmfile", inputArgs...)

print := func(in string) {
e.log.Info(in)
}

err := utils.ExecWithOutput(cmd, print, print)

if err != nil {
return err
}
return nil
}
1 change: 1 addition & 0 deletions pkg/template/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
type TemplateEngine interface {
Template(name string, inputFilePath string, outputFilePath string) error
Lint(name string, inputFilePath string) error
SyncRepos(file string) error
}

type Opts struct {
Expand Down
4 changes: 4 additions & 0 deletions pkg/template/kontemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ func (e *KontemplateEngine) Lint(name string, inputFilePath string) error {

return nil
}

func (e *KontemplateEngine) SyncRepos(file string) error {
return nil
}
8 changes: 6 additions & 2 deletions pkg/utils/stringUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ func ConcatDirs(strs ...string) string {
return filepath.Clean(sb.String())

}
func GetNamespaceDir(namespace string) string {

func GetRealmeDir() string {
dir, _ := os.Getwd()
return ConcatDirs(dir, "realm", "namespaces", namespace)
return ConcatDirs(dir, "realm")
}
func GetNamespaceDir(namespace string) string {
return ConcatDirs(GetRealmeDir(), "namespaces", namespace)
}
8 changes: 8 additions & 0 deletions realm/namespaces/_defaults.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
repositories:
# To use official "stable" charts a.k.a https://github.com/helm/charts/tree/master/stable
- name: stable
url: https://kubernetes-charts.storage.googleapis.com

releases:
- name: dummy
chart: stable/raw

0 comments on commit 295b24d

Please sign in to comment.