Skip to content
This repository was archived by the owner on Nov 1, 2022. It is now read-only.

Break generational cycle #2525

Merged
merged 2 commits into from
Oct 21, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add option for fluxctl install to write to files
It's pretty useful to be able to get a flux config as individual
files, so you can e.g., check them into git. This also means we can
use `fluxctl install` to generate the files under deploy/, so they
will be reliably close to what you'd get from running the tool.
  • Loading branch information
squaremo committed Oct 21, 2019
commit 851e50a3d9ff1d1913884b2def4ca3338a8f89c2
50 changes: 38 additions & 12 deletions cmd/fluxctl/install_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/spf13/cobra"

"github.com/fluxcd/flux/pkg/install"
)

type installOpts install.TemplateParameters
type installOpts struct {
install.TemplateParameters
outputDir string
}

func newInstall() *installOpts {
return &installOpts{}
Expand All @@ -23,22 +28,23 @@ func (opts *installOpts) Command() *cobra.Command {
fluxctl install --git-url '[email protected]:<your username>/flux-get-started' | kubectl -f -`,
RunE: opts.RunE,
}
cmd.Flags().StringVarP(&opts.GitURL, "git-url", "", "",
cmd.Flags().StringVar(&opts.GitURL, "git-url", "",
"URL of the Git repository to be used by Flux, e.g. [email protected]:<your username>/flux-get-started")
cmd.Flags().StringVarP(&opts.GitBranch, "git-branch", "", "master",
cmd.Flags().StringVar(&opts.GitBranch, "git-branch", "master",
"Git branch to be used by Flux")
cmd.Flags().StringSliceVarP(&opts.GitPaths, "git-paths", "", []string{},
cmd.Flags().StringSliceVar(&opts.GitPaths, "git-paths", []string{},
"Relative paths within the Git repo for Flux to locate Kubernetes manifests")
cmd.Flags().StringSliceVarP(&opts.GitPaths, "git-path", "", []string{},
cmd.Flags().StringSliceVar(&opts.GitPaths, "git-path", []string{},
"Relative paths within the Git repo for Flux to locate Kubernetes manifests")
cmd.Flags().StringVarP(&opts.GitLabel, "git-label", "", "flux",
cmd.Flags().StringVar(&opts.GitLabel, "git-label", "flux",
"Git label to keep track of Flux's sync progress; overrides both --git-sync-tag and --git-notes-ref")
cmd.Flags().StringVarP(&opts.GitUser, "git-user", "", "Flux",
cmd.Flags().StringVar(&opts.GitUser, "git-user", "Flux",
"Username to use as git committer")
cmd.Flags().StringVarP(&opts.GitEmail, "git-email", "", "",
cmd.Flags().StringVar(&opts.GitEmail, "git-email", "",
"Email to use as git committer")
cmd.Flags().StringVarP(&opts.Namespace, "namespace", "", getKubeConfigContextNamespace("default"),
cmd.Flags().StringVar(&opts.Namespace, "namespace", getKubeConfigContextNamespace("default"),
"Cluster namespace where to install flux")
cmd.Flags().StringVarP(&opts.outputDir, "output-dir", "o", "", "a directory in which to write individual manifests, rather than printing to stdout")

// Hide and deprecate "git-paths", which was wrongly introduced since its inconsistent with fluxd's git-path flag
cmd.Flags().MarkHidden("git-paths")
Expand All @@ -54,15 +60,35 @@ func (opts *installOpts) RunE(cmd *cobra.Command, args []string) error {
if opts.GitEmail == "" {
return fmt.Errorf("please supply a valid --git-email argument")
}
manifests, err := install.FillInTemplates(install.TemplateParameters(*opts))
manifests, err := install.FillInTemplates(opts.TemplateParameters)
if err != nil {
return err
}

writeManifest := func(fileName string, content []byte) error {
_, err := os.Stdout.Write(content)
return err
}

if opts.outputDir != "" {
info, err := os.Stat(opts.outputDir)
if err != nil {
return err
}
if !info.IsDir() {
return fmt.Errorf("%s is not a directory", opts.outputDir)
}
writeManifest = func(fileName string, content []byte) error {
path := filepath.Join(opts.outputDir, fileName)
fmt.Fprintf(os.Stderr, "writing %s\n", path)
return ioutil.WriteFile(path, content, os.FileMode(0666))
}
}

for fileName, content := range manifests {
if _, err := os.Stdout.Write(content); err != nil {
if err := writeManifest(fileName, content); err != nil {
return fmt.Errorf("cannot output manifest file %s: %s", fileName, err)
}

}

return nil
Expand Down