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
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 16 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ BUILD_DATE:=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')

DOCS_PORT:=8000

GENERATED_TEMPLATES_FILE=pkg/install/generated_templates.gogen.go

all: $(GOBIN)/fluxctl $(GOBIN)/fluxd build/.flux.done

release-bins:
release-bins: $(GENERATED_TEMPLATES_FILE)
for arch in amd64; do \
for os in linux darwin windows; do \
CGO_ENABLED=0 GOOS=$$os GOARCH=$$arch go build -o "build/fluxctl_"$$os"_$$arch" $(LDFLAGS) -ldflags "-X main.version=$(shell ./docker/image-tag)" ./cmd/fluxctl/; \
Expand All @@ -51,7 +53,7 @@ clean:
realclean: clean
rm -rf ./cache

test: test/bin/helm test/bin/kubectl test/bin/kustomize
test: test/bin/helm test/bin/kubectl test/bin/kustomize $(GENERATED_TEMPLATES_FILE)
PATH="${PWD}/bin:${PWD}/test/bin:${PATH}" go test ${TEST_FLAGS} $(shell go list ./... | grep -v "^github.com/fluxcd/flux/vendor" | sort -u)

e2e: test/bin/helm test/bin/kubectl build/.flux.done
Expand Down Expand Up @@ -104,7 +106,7 @@ cache/%/helm-$(HELM_VERSION): docker/helm.version
tar -m -C ./cache -xzf cache/$*/helm-$(HELM_VERSION).tar.gz $*/helm
mv cache/$*/helm $@

$(GOBIN)/fluxctl: $(FLUXCTL_DEPS)
$(GOBIN)/fluxctl: $(FLUXCTL_DEPS) $(GENERATED_TEMPLATES_FILE)
go install ./cmd/fluxctl

$(GOBIN)/fluxd: $(FLUXD_DEPS)
Expand All @@ -113,14 +115,19 @@ $(GOBIN)/fluxd: $(FLUXD_DEPS)
integration-test: all
test/bin/test-flux

generate-deploy: pkg/install/generated_templates.gogen.go
cd deploy && go run ../pkg/install/generate.go deploy
generate-deploy: $(GOBIN)/fluxctl
$(GOBIN)/fluxctl install -o ./deploy \
--git-url git@github.com:fluxcd/flux-get-started \
--git-email flux@example.com \
--git-user 'Flux automation' \
--git-label flux-sync \
--namespace flux

pkg/install/generated_templates.gogen.go: pkg/install/templates/*
cd pkg/install && go run generate.go embedded-templates
$(GENERATED_TEMPLATES_FILE): pkg/install/templates/*.tmpl pkg/install/generate.go
go generate ./pkg/install

check-generated: generate-deploy pkg/install/generated_templates.gogen.go
git diff --exit-code -- integrations/apis integrations/client pkg/install/generated_templates.gogen.go
check-generated: generate-deploy
git diff --exit-code -- deploy/

build-docs:
@cd docs && docker build -t flux-docs .
Expand Down
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 'git@github.com:<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. git@github.com:<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
3 changes: 3 additions & 0 deletions deploy/flux-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ spec:
# Include this if you want to restrict the manifests considered by flux
# to those under the following relative paths in the git repository
# - --git-path=subdir1,subdir2
- --git-label=flux-sync
- --git-user=Flux automation
- --git-email=flux@example.com

# Include these two to enable git commit signing
# - --git-gpg-key-import=/root/gpg-import
Expand Down
49 changes: 12 additions & 37 deletions pkg/install/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,33 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"

"github.com/shurcooL/vfsgen"

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

func main() {
usage := func() {
fmt.Fprintf(os.Stderr, "usage: %s {embedded-templates,deploy}\n", os.Args[0])
fmt.Fprintf(os.Stderr, "usage: %s\n", os.Args[0])
os.Exit(1)
}
if len(os.Args) != 2 {
if len(os.Args) != 1 {
usage()
}
switch os.Args[1] {
case "embedded-templates":
var fs http.FileSystem = modTimeFS{
fs: http.Dir("templates/"),
}
err := vfsgen.Generate(fs, vfsgen.Options{
Filename: "generated_templates.gogen.go",
PackageName: "install",
VariableName: "templates",
})
if err != nil {
log.Fatalln(err)
}
case "deploy":
params := install.TemplateParameters{
GitURL: "git@github.com:fluxcd/flux-get-started",
GitBranch: "master",
Namespace: "flux",
}
manifests, err := install.FillInTemplates(params)
if err != nil {
fmt.Fprintf(os.Stderr, "error: failed to fill in templates: %s\n", err)
os.Exit(1)
}
for fileName, contents := range manifests {
if err := ioutil.WriteFile(fileName, contents, 0600); err != nil {
fmt.Fprintf(os.Stderr, "error: failed to write deploy file %s: %s\n", fileName, err)
os.Exit(1)
}
}

default:
usage()
var fs http.FileSystem = modTimeFS{
fs: http.Dir("templates/"),
}
err := vfsgen.Generate(fs, vfsgen.Options{
Filename: "generated_templates.gogen.go",
PackageName: "install",
VariableName: "templates",
})
if err != nil {
log.Fatalln(err)
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/shurcooL/httpfs/vfsutil"
)

//go:generate go run generate.go

type TemplateParameters struct {
GitURL string
GitBranch string
Expand Down