Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Bucket sources to Kustomizations/HRs #270

Merged
merged 2 commits into from
Sep 24, 2020
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
20 changes: 12 additions & 8 deletions cmd/gotk/create_helmrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"io/ioutil"
"strings"

"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -55,6 +54,12 @@ var createHelmReleaseCmd = &cobra.Command{
--source=GitRepository/podinfo \
--chart=./charts/podinfo

# Create a HelmRelease with a chart from a Bucket source
gotk create hr podinfo \
--interval=10m \
--source=Bucket/podinfo \
--chart=./charts/podinfo

# Create a HelmRelease with values from a local YAML file
gotk create hr podinfo \
--source=HelmRepository/podinfo \
Expand Down Expand Up @@ -113,14 +118,13 @@ func createHelmReleaseCmdRun(cmd *cobra.Command, args []string) error {
if hrSource == "" {
return fmt.Errorf("source is required")
}
hrSourceElements := strings.Split(hrSource, "/")
if len(hrSourceElements) != 2 {
sourceKind, sourceName := utils.parseObjectKindName(hrSource)
if sourceKind == "" {
return fmt.Errorf("invalid source '%s', must be in format <kind>/<name>", hrSource)
}
hrSourceKind, hrSourceName := hrSourceElements[0], hrSourceElements[1]
if !utils.containsItemString(supportedHelmChartSourceKinds, hrSourceKind) {
if !utils.containsItemString(supportedHelmChartSourceKinds, sourceKind) {
return fmt.Errorf("source kind %s is not supported, can be %v",
hrSourceKind, supportedHelmChartSourceKinds)
sourceKind, supportedHelmChartSourceKinds)
}
if hrChart == "" {
return fmt.Errorf("chart name or path is required")
Expand Down Expand Up @@ -153,8 +157,8 @@ func createHelmReleaseCmdRun(cmd *cobra.Command, args []string) error {
Chart: hrChart,
Version: hrChartVersion,
SourceRef: helmv2.CrossNamespaceObjectReference{
Kind: hrSourceKind,
Name: hrSourceName,
Kind: sourceKind,
Name: sourceName,
},
},
},
Expand Down
31 changes: 19 additions & 12 deletions cmd/gotk/create_kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var createKsCmd = &cobra.Command{
Use: "kustomization [name]",
Aliases: []string{"ks"},
Short: "Create or update a Kustomization resource",
Long: "The kustomization source create command generates a Kustomize resource for a given GitRepository source.",
Long: "The kustomization source create command generates a Kustomize resource for a given source.",
Example: ` # Create a Kustomization resource from a source at a given path
gotk create kustomization contour \
--source=contour \
Expand All @@ -60,15 +60,11 @@ var createKsCmd = &cobra.Command{
--interval=5m \
--validation=client

# Create a Kustomization resource that runs under a service account
gotk create kustomization webapp \
--source=webapp \
--path="./deploy/overlays/staging" \
# Create a Kustomization resource that references a Bucket
gotk create kustomization secrets \
--source=Bucket/secrets \
--prune=true \
--interval=5m \
--validation=client \
--sa-name=reconclier \
--sa-namespace=staging
--interval=5m
`,
RunE: createKsCmdRun,
}
Expand All @@ -88,7 +84,8 @@ var (
)

func init() {
createKsCmd.Flags().StringVar(&ksSource, "source", "", "GitRepository name")
createKsCmd.Flags().StringVar(&ksSource, "source", "",
"source that contains the Kubernetes manifests in the format '[<kind>/]<name>', where kind can be GitRepository or Bucket, if kind is not specified it defaults to GitRepository")
createKsCmd.Flags().StringVar(&ksPath, "path", "./", "path to the directory containing the Kustomization file")
createKsCmd.Flags().BoolVar(&ksPrune, "prune", false, "enable garbage collection")
createKsCmd.Flags().StringArrayVar(&ksHealthCheck, "health-check", nil, "workload to be included in the health assessment, in the format '<kind>/<name>.<namespace>'")
Expand All @@ -111,6 +108,16 @@ func createKsCmdRun(cmd *cobra.Command, args []string) error {
if ksSource == "" {
return fmt.Errorf("source is required")
}

sourceKind, sourceName := utils.parseObjectKindName(ksSource)
if sourceKind == "" {
sourceKind = sourcev1.GitRepositoryKind
}
if !utils.containsItemString(supportedKustomizationSourceKinds, sourceKind) {
return fmt.Errorf("source kind %s is not supported, can be %v",
sourceKind, supportedKustomizationSourceKinds)
}

if ksPath == "" {
return fmt.Errorf("path is required")
}
Expand Down Expand Up @@ -141,8 +148,8 @@ func createKsCmdRun(cmd *cobra.Command, args []string) error {
Path: ksPath,
Prune: ksPrune,
SourceRef: kustomizev1.CrossNamespaceSourceReference{
Kind: sourcev1.GitRepositoryKind,
Name: ksSource,
Kind: sourceKind,
Name: sourceName,
},
Suspend: false,
Validation: ksValidation,
Expand Down
19 changes: 10 additions & 9 deletions cmd/gotk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,16 @@ var (
)

var (
defaultComponents = []string{"source-controller", "kustomize-controller", "helm-controller", "notification-controller"}
defaultVersion = "latest"
defaultNamespace = "gitops-system"
defaultNotification = "notification-controller"
supportedLogLevels = []string{"debug", "info", "error"}
supportedArch = []string{"amd64", "arm", "arm64"}
supportedDecryptionProviders = []string{"sops"}
supportedHelmChartSourceKinds = []string{sourcev1.HelmRepositoryKind, sourcev1.GitRepositoryKind}
supportedSourceBucketProviders = []string{sourcev1.GenericBucketProvider, sourcev1.AmazonBucketProvider}
defaultComponents = []string{"source-controller", "kustomize-controller", "helm-controller", "notification-controller"}
defaultVersion = "latest"
defaultNamespace = "gitops-system"
defaultNotification = "notification-controller"
supportedLogLevels = []string{"debug", "info", "error"}
supportedArch = []string{"amd64", "arm", "arm64"}
supportedDecryptionProviders = []string{"sops"}
supportedKustomizationSourceKinds = []string{sourcev1.GitRepositoryKind, sourcev1.BucketKind}
supportedHelmChartSourceKinds = []string{sourcev1.HelmRepositoryKind, sourcev1.GitRepositoryKind, sourcev1.BucketKind}
supportedSourceBucketProviders = []string{sourcev1.GenericBucketProvider, sourcev1.AmazonBucketProvider}
)

func init() {
Expand Down
6 changes: 4 additions & 2 deletions cmd/gotk/reconcile_helmrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ func reconcileHrCmdRun(cmd *cobra.Command, args []string) error {
if syncHrWithSource {
switch helmRelease.Spec.Chart.Spec.SourceRef.Kind {
case sourcev1.HelmRepositoryKind:
err = syncSourceHelmCmdRun(nil, []string{helmRelease.Spec.Chart.Spec.SourceRef.Name})
err = reconcileSourceHelmCmdRun(nil, []string{helmRelease.Spec.Chart.Spec.SourceRef.Name})
case sourcev1.GitRepositoryKind:
err = syncSourceGitCmdRun(nil, []string{helmRelease.Spec.Chart.Spec.SourceRef.Name})
err = reconcileSourceGitCmdRun(nil, []string{helmRelease.Spec.Chart.Spec.SourceRef.Name})
case sourcev1.BucketKind:
err = reconcileSourceBucketCmdRun(nil, []string{helmRelease.Spec.Chart.Spec.SourceRef.Name})
}
if err != nil {
return err
Expand Down
8 changes: 7 additions & 1 deletion cmd/gotk/reconcile_kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1alpha1"
consts "github.com/fluxcd/pkg/runtime"
sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1"
)

var reconcileKsCmd = &cobra.Command{
Expand Down Expand Up @@ -80,7 +81,12 @@ func reconcileKsCmdRun(cmd *cobra.Command, args []string) error {
}

if syncKsWithSource {
err := syncSourceGitCmdRun(nil, []string{kustomization.Spec.SourceRef.Name})
switch kustomization.Spec.SourceRef.Kind {
case sourcev1.GitRepositoryKind:
err = reconcileSourceGitCmdRun(nil, []string{kustomization.Spec.SourceRef.Name})
case sourcev1.BucketKind:
err = reconcileSourceBucketCmdRun(nil, []string{kustomization.Spec.SourceRef.Name})
}
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/gotk/reconcile_source_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ var reconcileSourceBucketCmd = &cobra.Command{
Example: ` # Trigger a reconciliation for an existing source
gotk reconcile source bucket podinfo
`,
RunE: syncSourceBucketCmdRun,
RunE: reconcileSourceBucketCmdRun,
}

func init() {
reconcileSourceCmd.AddCommand(reconcileSourceBucketCmd)
}

func syncSourceBucketCmdRun(cmd *cobra.Command, args []string) error {
func reconcileSourceBucketCmdRun(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("source name is required")
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/gotk/reconcile_source_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"context"
"fmt"

"time"

"github.com/spf13/cobra"
Expand All @@ -37,14 +36,14 @@ var reconcileSourceGitCmd = &cobra.Command{
Example: ` # Trigger a git pull for an existing source
gotk reconcile source git podinfo
`,
RunE: syncSourceGitCmdRun,
RunE: reconcileSourceGitCmdRun,
}

func init() {
reconcileSourceCmd.AddCommand(reconcileSourceGitCmd)
}

func syncSourceGitCmdRun(cmd *cobra.Command, args []string) error {
func reconcileSourceGitCmdRun(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("source name is required")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/gotk/reconcile_source_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ var reconcileSourceHelmCmd = &cobra.Command{
Example: ` # Trigger a reconciliation for an existing source
gotk reconcile source helm podinfo
`,
RunE: syncSourceHelmCmdRun,
RunE: reconcileSourceHelmCmdRun,
}

func init() {
reconcileSourceCmd.AddCommand(reconcileSourceHelmCmd)
}

func syncSourceHelmCmdRun(cmd *cobra.Command, args []string) error {
func reconcileSourceHelmCmdRun(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("source name is required")
}
Expand Down
11 changes: 11 additions & 0 deletions cmd/gotk/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ func (*Utils) containsItemString(s []string, e string) bool {
return false
}

func (*Utils) parseObjectKindName(input string) (string, string) {
kind := ""
name := input
parts := strings.Split(input, "/")
if len(parts) == 2 {
kind, name = parts[0], parts[1]
}

return kind, name
}

func (*Utils) makeDependsOn(deps []string) []dependency.CrossNamespaceDependencyReference {
refs := []dependency.CrossNamespaceDependencyReference{}
for _, dep := range deps {
Expand Down
6 changes: 6 additions & 0 deletions docs/cmd/gotk_create_helmrelease.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ gotk create helmrelease [name] [flags]
--source=GitRepository/podinfo \
--chart=./charts/podinfo

# Create a HelmRelease with a chart from a Bucket source
gotk create hr podinfo \
--interval=10m \
--source=Bucket/podinfo \
--chart=./charts/podinfo

# Create a HelmRelease with values from a local YAML file
gotk create hr podinfo \
--source=HelmRepository/podinfo \
Expand Down
16 changes: 6 additions & 10 deletions docs/cmd/gotk_create_kustomization.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Create or update a Kustomization resource

### Synopsis

The kustomization source create command generates a Kustomize resource for a given GitRepository source.
The kustomization source create command generates a Kustomize resource for a given source.

```
gotk create kustomization [name] [flags]
Expand Down Expand Up @@ -33,15 +33,11 @@ gotk create kustomization [name] [flags]
--interval=5m \
--validation=client

# Create a Kustomization resource that runs under a service account
gotk create kustomization webapp \
--source=webapp \
--path="./deploy/overlays/staging" \
# Create a Kustomization resource that references a Bucket
gotk create kustomization secrets \
--source=Bucket/secrets \
--prune=true \
--interval=5m \
--validation=client \
--sa-name=reconclier \
--sa-namespace=staging
--interval=5m

```

Expand All @@ -58,7 +54,7 @@ gotk create kustomization [name] [flags]
--prune enable garbage collection
--sa-name string service account name
--sa-namespace string service account namespace
--source string GitRepository name
--source string source that contains the Kubernetes manifests in the format '[<kind>/]<name>', where kind can be GitRepository or Bucket, if kind is not specified it defaults to GitRepository
--validation string validate the manifests before applying them on the cluster, can be 'client' or 'server'
```

Expand Down