Skip to content

Commit

Permalink
Merge pull request #4 from WanderaOrg/secrets-folders
Browse files Browse the repository at this point in the history
#3 - Loading data into secrets and folders
  • Loading branch information
coufalja authored May 10, 2018
2 parents 175a1fb + e0e9ca8 commit 2bcbfb4
Show file tree
Hide file tree
Showing 15 changed files with 565 additions and 133 deletions.
150 changes: 110 additions & 40 deletions cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,51 @@ var lp = struct {
git string
branch string
folder string
mapname string
target string
namespace string
mergetype string
verbose bool
includes []string
excludes []string
labels []string
annotations []string
}{}

var loadCmd = &cobra.Command{
Use: "load",
Short: "Loads files from git repository into ConfigMap",
PreRunE: cmd.ExpandArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return executeLoad()
Use: "load",
Short: "Loads files from git repository into target",
DisableFlagParsing: true,
PersistentPreRunE: cmd.ExpandArgs,
}

var loadConfigmapCmd = &cobra.Command{
Use: "configmap",
Short: "Loads files from git repository into ConfigMap",
DisableFlagParsing: true,
RunE: func(c *cobra.Command, args []string) error {
return executeLoad(cmd.ConfigMap)
},
}

var loadSecretCmd = &cobra.Command{
Use: "secret",
Short: "Loads files from git repository into Secret",
DisableFlagParsing: true,
RunE: func(c *cobra.Command, args []string) error {
return executeLoad(cmd.Secret)
},
}

func executeLoad() error {
if err := os.MkdirAll(lp.folder, 755); err != nil {
var loadFolderCmd = &cobra.Command{
Use: "folder",
Short: "Loads files from git repository into Folder",
DisableFlagParsing: true,
RunE: func(c *cobra.Command, args []string) error {
return executeLoad(cmd.Folder)
},
}

func executeLoad(lt cmd.LoadType) error {
if err := os.MkdirAll(lp.folder, os.ModePerm); err != nil {
return err
}

Expand All @@ -44,20 +68,6 @@ func executeLoad() error {

fetcher := fetch.NewFetcher(lp.git, lp.folder, lp.branch, auth)

uploader, err := upload.NewConfigMapUploader(&upload.UploaderOptions{
Kubeconfig: lp.kubeconfig,
ConfigMapName: lp.mapname,
Namespace: lp.namespace,
MergeType: upload.MergeType(lp.mergetype),
Includes: lp.includes,
Excludes: lp.excludes,
Annotations: lp.annotations,
Labels: lp.labels,
})
if err != nil {
return err
}

c, err := fetcher.Fetch()
if err != nil {
return err
Expand All @@ -68,7 +78,52 @@ func executeLoad() error {
return err
}

err = uploader.Upload(c.ID().String(), iter)
var up upload.Uploader
switch lt {
case cmd.ConfigMap:
uploader, err := upload.NewConfigMapUploader(&upload.UploaderOptions{
Kubeconfig: lp.kubeconfig,
Target: lp.target,
Namespace: lp.namespace,
MergeType: upload.MergeType(lp.mergetype),
Includes: lp.includes,
Excludes: lp.excludes,
Annotations: lp.annotations,
Labels: lp.labels,
})
if err != nil {
return err
}
up = uploader
case cmd.Secret:
uploader, err := upload.NewSecretUploader(&upload.UploaderOptions{
Kubeconfig: lp.kubeconfig,
Target: lp.target,
Namespace: lp.namespace,
MergeType: upload.MergeType(lp.mergetype),
Includes: lp.includes,
Excludes: lp.excludes,
Annotations: lp.annotations,
Labels: lp.labels,
})
if err != nil {
return err
}
up = uploader
case cmd.Folder:
uploader, err := upload.NewFolderUploader(&upload.UploaderOptions{
Source: lp.folder,
Target: lp.target,
Includes: lp.includes,
Excludes: lp.excludes,
})
if err != nil {
return err
}
up = uploader
}

err = up.Upload(c.ID().String(), iter)
if err != nil {
return err
}
Expand All @@ -77,20 +132,35 @@ func executeLoad() error {
}

func init() {
loadCmd.Flags().BoolVarP(&lp.verbose, "verbose", "v", false, "verbose output")
loadCmd.Flags().BoolVarP(&lp.kubeconfig, "kubeconfig", "k", false, "true if locally stored ~/.kube/config should be used, InCluster config will be used if false (options: true|false) (default: false)")
loadCmd.Flags().StringVarP(&lp.mergetype, "merge-type", "", "delete", "how to merge ConfigMap data whether to also delete missing values or just upsert new (options: delete|upsert)")
loadCmd.Flags().StringVarP(&lp.git, "git", "g", "", "git repository address, either http(s) or ssh protocol has to be specified")
loadCmd.Flags().StringVarP(&lp.branch, "branch", "b", "master", "branch name to pull")
loadCmd.Flags().StringVarP(&lp.folder, "cache-folder", "c", "/tmp/git2kube/data/", "destination on filesystem where cache of repository will be stored")
loadCmd.Flags().StringVarP(&lp.namespace, "namespace", "n", "default", "target namespace for the resulting ConfigMap")
loadCmd.Flags().StringVarP(&lp.mapname, "configmap", "m", "", "name for the resulting ConfigMap")
loadCmd.Flags().StringSliceVar(&lp.includes, "include", []string{".*"}, "regex that if is a match includes the file in the upload, example: '*.yaml' or 'folder/*' if you want to match a folder")
loadCmd.Flags().StringSliceVar(&lp.excludes, "exclude", []string{"^\\..*"}, "regex that if is a match excludes the file from the upload, example: '*.yaml' or 'folder/*' if you want to match a folder")
loadCmd.Flags().StringSliceVar(&lp.labels, "label", []string{}, "label to add to K8s ConfigMap (format NAME=VALUE)")
loadCmd.Flags().StringSliceVar(&lp.annotations, "annotation", []string{}, "annotation to add to K8s ConfigMap (format NAME=VALUE)")

loadCmd.MarkFlagFilename("kubeconfig")
loadCmd.MarkFlagRequired("git")
loadCmd.MarkFlagRequired("configmap")
loadCmd.PersistentFlags().StringVarP(&lp.git, "git", "g", "", "git repository address, either http(s) or ssh protocol has to be specified")
loadCmd.PersistentFlags().StringVarP(&lp.branch, "branch", "b", "master", "branch name to pull")
loadCmd.PersistentFlags().StringVarP(&lp.folder, "cache-folder", "c", "/tmp/git2kube/data/", "destination on filesystem where cache of repository will be stored")
loadCmd.PersistentFlags().StringSliceVar(&lp.includes, "include", []string{".*"}, "regex that if is a match includes the file in the upload, example: '*.yaml' or 'folder/*' if you want to match a folder")
loadCmd.PersistentFlags().StringSliceVar(&lp.excludes, "exclude", []string{"^\\..*"}, "regex that if is a match excludes the file from the upload, example: '*.yaml' or 'folder/*' if you want to match a folder")
loadCmd.MarkPersistentFlagRequired("git")

loadConfigmapCmd.Flags().BoolVarP(&lp.kubeconfig, "kubeconfig", "k", false, "true if locally stored ~/.kube/config should be used, InCluster config will be used if false (options: true|false) (default: false)")
loadConfigmapCmd.Flags().StringVarP(&lp.namespace, "namespace", "n", "default", "target namespace for the resulting ConfigMap")
loadConfigmapCmd.Flags().StringVarP(&lp.target, "configmap", "m", "", "name for the resulting ConfigMap")
loadConfigmapCmd.Flags().StringSliceVar(&lp.labels, "label", []string{}, "label to add to K8s ConfigMap (format NAME=VALUE)")
loadConfigmapCmd.Flags().StringSliceVar(&lp.annotations, "annotation", []string{}, "annotation to add to K8s ConfigMap (format NAME=VALUE)")
loadConfigmapCmd.Flags().StringVarP(&lp.mergetype, "merge-type", "", "delete", "how to merge ConfigMap data whether to also delete missing values or just upsert new (options: delete|upsert)")
loadConfigmapCmd.MarkFlagFilename("kubeconfig")
loadConfigmapCmd.MarkFlagRequired("configmap")

loadSecretCmd.Flags().BoolVarP(&lp.kubeconfig, "kubeconfig", "k", false, "true if locally stored ~/.kube/config should be used, InCluster config will be used if false (options: true|false) (default: false)")
loadSecretCmd.Flags().StringVarP(&lp.namespace, "namespace", "n", "default", "target namespace for the resulting ConfigMap")
loadSecretCmd.Flags().StringVarP(&lp.target, "secret", "s", "", "name for the resulting Secret")
loadSecretCmd.Flags().StringSliceVar(&lp.labels, "label", []string{}, "label to add to K8s Secret (format NAME=VALUE)")
loadSecretCmd.Flags().StringSliceVar(&lp.annotations, "annotation", []string{}, "annotation to add to K8s Secret (format NAME=VALUE)")
loadSecretCmd.Flags().StringVarP(&lp.mergetype, "merge-type", "", "delete", "how to merge Secret data whether to also delete missing values or just upsert new (options: delete|upsert)")
loadSecretCmd.MarkFlagFilename("kubeconfig")
loadSecretCmd.MarkFlagRequired("secret")

loadFolderCmd.Flags().StringVarP(&lp.target, "target-folder", "t", "", "path to target folder")
loadFolderCmd.MarkFlagRequired("target-folder")

loadCmd.AddCommand(loadConfigmapCmd)
loadCmd.AddCommand(loadSecretCmd)
loadCmd.AddCommand(loadFolderCmd)
}
148 changes: 112 additions & 36 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ var wp = struct {
git string
branch string
folder string
mapname string
target string
namespace string
mergetype string
verbose bool
interval int
includes []string
excludes []string
Expand All @@ -30,16 +29,40 @@ var wp = struct {

var watchCmd = &cobra.Command{
Use: "watch",
Short: "Runs watcher that periodically check the provided repository and updates K8s configmap accordingly",
Short: "Runs watcher that periodically check the provided repository",
DisableFlagParsing: true,
PreRunE: cmd.ExpandArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return executeWatch()
PersistentPreRunE: cmd.ExpandArgs,
}

var watchConfigmapCmd = &cobra.Command{
Use: "configmap",
Short: "Runs watcher that periodically check the provided repository and updates K8s ConfigMap accordingly",
DisableFlagParsing: true,
RunE: func(c *cobra.Command, args []string) error {
return executeWatch(cmd.ConfigMap)
},
}

var watchSecretCmd = &cobra.Command{
Use: "secret",
Short: "Runs watcher that periodically check the provided repository and updates K8s Secret accordingly",
DisableFlagParsing: true,
RunE: func(c *cobra.Command, args []string) error {
return executeWatch(cmd.Secret)
},
}

var watchFolderCmd = &cobra.Command{
Use: "folder",
Short: "Runs watcher that periodically check the provided repository and updates target folder accordingly",
DisableFlagParsing: true,
RunE: func(c *cobra.Command, args []string) error {
return executeWatch(cmd.Folder)
},
}

func executeWatch() error {
if err := os.MkdirAll(wp.folder, 755); err != nil {
func executeWatch(lt cmd.LoadType) error {
if err := os.MkdirAll(wp.folder, os.ModePerm); err != nil {
return err
}

Expand All @@ -50,19 +73,57 @@ func executeWatch() error {

fetcher := fetch.NewFetcher(wp.git, wp.folder, wp.branch, auth)

uploader, err := upload.NewConfigMapUploader(&upload.UploaderOptions{
Kubeconfig: wp.kubeconfig,
ConfigMapName: wp.mapname,
Namespace: wp.namespace,
MergeType: upload.MergeType(wp.mergetype),
Includes: wp.includes,
Excludes: wp.excludes,
Annotations: wp.annotations,
Labels: wp.labels,
})
var up upload.Uploader
switch lt {
case cmd.ConfigMap:
uploader, err := upload.NewConfigMapUploader(&upload.UploaderOptions{
Kubeconfig: wp.kubeconfig,
Target: wp.target,
Namespace: wp.namespace,
MergeType: upload.MergeType(wp.mergetype),
Includes: wp.includes,
Excludes: wp.excludes,
Annotations: wp.annotations,
Labels: wp.labels,
})
if err != nil {
return err
}
up = uploader
case cmd.Secret:
uploader, err := upload.NewSecretUploader(&upload.UploaderOptions{
Kubeconfig: wp.kubeconfig,
Target: wp.target,
Namespace: wp.namespace,
MergeType: upload.MergeType(wp.mergetype),
Includes: wp.includes,
Excludes: wp.excludes,
Annotations: wp.annotations,
Labels: wp.labels,
})
if err != nil {
return err
}
up = uploader
case cmd.Folder:
uploader, err := upload.NewFolderUploader(&upload.UploaderOptions{
Source: wp.folder,
Target: wp.target,
Includes: wp.includes,
Excludes: wp.excludes,
})
if err != nil {
return err
}
up = uploader
}

err = refresh(fetcher, up)
if err != nil {
log.Errorf("Initial sync failed: %v", err)
return err
}
log.Info("Initial sync succeeded")

ticker := time.NewTicker(time.Duration(wp.interval) * time.Second)
stop := make(chan struct{})
Expand All @@ -71,7 +132,7 @@ func executeWatch() error {
for {
select {
case <-ticker.C:
err := refresh(fetcher, uploader)
err := refresh(fetcher, up)
if err != nil {
log.Warn(err)
}
Expand Down Expand Up @@ -113,21 +174,36 @@ func refresh(fetcher fetch.Fetcher, uploader upload.Uploader) error {
}

func init() {
watchCmd.Flags().IntVarP(&wp.interval, "interval", "i", 10, "interval in seconds in which to try refreshing ConfigMap from git")
watchCmd.Flags().StringVarP(&wp.mergetype, "merge-type", "", "delete", "how to merge ConfigMap data whether to also delete missing values or just upsert new (options: delete|upsert)")
watchCmd.Flags().BoolVarP(&wp.verbose, "verbose", "v", false, "verbose output")
watchCmd.Flags().BoolVarP(&wp.kubeconfig, "kubeconfig", "k", false, "true if locally stored ~/.kube/config should be used, InCluster config will be used if false (options: true|false) (default: false)")
watchCmd.Flags().StringVarP(&wp.git, "git", "g", "", "git repository address, either http(s) or ssh protocol has to be specified")
watchCmd.Flags().StringVarP(&wp.branch, "branch", "b", "master", "branch name to pull")
watchCmd.Flags().StringVarP(&wp.folder, "cache-folder", "c", "/tmp/git2kube/data/", "destination on filesystem where cache of repository will be stored")
watchCmd.Flags().StringVarP(&wp.namespace, "namespace", "n", "default", "target namespace for the resulting ConfigMap")
watchCmd.Flags().StringVarP(&wp.mapname, "configmap", "m", "", "name for the resulting ConfigMap")
watchCmd.Flags().StringSliceVar(&wp.includes, "include", []string{".*"}, "regex that if is a match includes the file in the upload, example: '*.yaml' or 'folder/*' if you want to match a folder")
watchCmd.Flags().StringSliceVar(&wp.excludes, "exclude", []string{"^\\..*"}, "regex that if is a match excludes the file from the upload, example: '*.yaml' or 'folder/*' if you want to match a folder")
watchCmd.Flags().StringSliceVar(&wp.labels, "label", []string{}, "label to add to K8s ConfigMap (format NAME=VALUE)")
watchCmd.Flags().StringSliceVar(&wp.annotations, "annotation", []string{}, "annotation to add to K8s ConfigMap (format NAME=VALUE)")

watchCmd.MarkFlagFilename("kubeconfig")
watchCmd.MarkFlagRequired("git")
watchCmd.MarkFlagRequired("configmap")
watchCmd.PersistentFlags().IntVarP(&wp.interval, "interval", "i", 10, "interval in seconds in which to try refreshing ConfigMap from git")
watchCmd.PersistentFlags().StringVarP(&wp.git, "git", "g", "", "git repository address, either http(s) or ssh protocol has to be specified")
watchCmd.PersistentFlags().StringVarP(&wp.branch, "branch", "b", "master", "branch name to pull")
watchCmd.PersistentFlags().StringVarP(&wp.folder, "cache-folder", "c", "/tmp/git2kube/data/", "destination on filesystem where cache of repository will be stored")
watchCmd.PersistentFlags().StringSliceVar(&wp.includes, "include", []string{".*"}, "regex that if is a match includes the file in the upload, example: '*.yaml' or 'folder/*' if you want to match a folder")
watchCmd.PersistentFlags().StringSliceVar(&wp.excludes, "exclude", []string{"^\\..*"}, "regex that if is a match excludes the file from the upload, example: '*.yaml' or 'folder/*' if you want to match a folder")
watchCmd.MarkPersistentFlagRequired("git")

watchConfigmapCmd.Flags().BoolVarP(&wp.kubeconfig, "kubeconfig", "k", false, "true if locally stored ~/.kube/config should be used, InCluster config will be used if false (options: true|false) (default: false)")
watchConfigmapCmd.Flags().StringVarP(&wp.namespace, "namespace", "n", "default", "target namespace for the resulting ConfigMap")
watchConfigmapCmd.Flags().StringVarP(&wp.target, "configmap", "m", "", "name for the resulting ConfigMap")
watchConfigmapCmd.Flags().StringSliceVar(&wp.labels, "label", []string{}, "label to add to K8s ConfigMap (format NAME=VALUE)")
watchConfigmapCmd.Flags().StringSliceVar(&wp.annotations, "annotation", []string{}, "annotation to add to K8s ConfigMap (format NAME=VALUE)")
watchConfigmapCmd.Flags().StringVarP(&wp.mergetype, "merge-type", "", "delete", "how to merge ConfigMap data whether to also delete missing values or just upsert new (options: delete|upsert)")
watchConfigmapCmd.MarkFlagFilename("kubeconfig")
watchConfigmapCmd.MarkFlagRequired("configmap")

watchSecretCmd.Flags().BoolVarP(&wp.kubeconfig, "kubeconfig", "k", false, "true if locally stored ~/.kube/config should be used, InCluster config will be used if false (options: true|false) (default: false)")
watchSecretCmd.Flags().StringVarP(&wp.namespace, "namespace", "n", "default", "target namespace for the resulting ConfigMap")
watchSecretCmd.Flags().StringVarP(&wp.target, "secret", "s", "", "name for the resulting Secret")
watchSecretCmd.Flags().StringSliceVar(&wp.labels, "label", []string{}, "label to add to K8s Secret (format NAME=VALUE)")
watchSecretCmd.Flags().StringSliceVar(&wp.annotations, "annotation", []string{}, "annotation to add to K8s Secret (format NAME=VALUE)")
watchSecretCmd.Flags().StringVarP(&wp.mergetype, "merge-type", "", "delete", "how to merge Secret data whether to also delete missing values or just upsert new (options: delete|upsert)")
watchSecretCmd.MarkFlagFilename("kubeconfig")
watchSecretCmd.MarkFlagRequired("secret")

watchFolderCmd.Flags().StringVarP(&wp.target, "target-folder", "t", "", "path to target folder")
watchFolderCmd.MarkFlagRequired("target-folder")

watchCmd.AddCommand(watchConfigmapCmd)
watchCmd.AddCommand(watchSecretCmd)
watchCmd.AddCommand(watchFolderCmd)
}
7 changes: 4 additions & 3 deletions docs/git2kube.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ Commandline tool for loading files from git repository into K8s ConfigMap
### Options

```
-h, --help help for git2kube
-h, --help help for git2kube
-l, --log-level string command log level (options: [panic fatal error warning info debug]) (default "info")
```

### SEE ALSO

* [git2kube gendoc](git2kube_gendoc.md) - Generates documentation for this tool in Markdown format
* [git2kube load](git2kube_load.md) - Loads files from git repository into ConfigMap
* [git2kube watch](git2kube_watch.md) - Runs watcher that periodically check the provided repository and updates K8s configmap accordingly
* [git2kube load](git2kube_load.md) - Loads files from git repository into target
* [git2kube watch](git2kube_watch.md) - Runs watcher that periodically check the provided repository

Loading

0 comments on commit 2bcbfb4

Please sign in to comment.