Skip to content

Commit

Permalink
Add a build kustomization feature
Browse files Browse the repository at this point in the history
If implemented it will permit queriying the Kubernetes API to fetch the specified
Flux Kustomization, then uses the specified path to build
the overlay.

Signed-off-by: Soule BA <soule@weave.works>
  • Loading branch information
souleb committed Dec 2, 2021
1 parent adf5a52 commit 5665027
Show file tree
Hide file tree
Showing 9 changed files with 1,015 additions and 16 deletions.
31 changes: 31 additions & 0 deletions cmd/flux/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2021 The Flux authors
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 main

import (
"github.com/spf13/cobra"
)

var buildCmd = &cobra.Command{
Use: "build",
Short: "Build a flux resource",
Long: "The build command is used to build flux resources.",
}

func init() {
rootCmd.AddCommand(buildCmd)
}
80 changes: 80 additions & 0 deletions cmd/flux/build_kustomization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2021 The Flux authors
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 main

import (
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/fluxcd/flux2/internal/kustomization"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta2"
)

var buildKsCmd = &cobra.Command{
Use: "kustomization",
Aliases: []string{"ks"},
Short: "Build Kustomization",
Long: `The build command queries the Kubernetes API and fetches the specified Flux Kustomization,
then it uses the specified files or path to build the overlay to write the resulting multi-doc YAML to stdout.`,
Example: `# Create a new overlay.
flux build kustomization my-app --resources ./path/to/local/manifests`,
ValidArgsFunction: resourceNamesCompletionFunc(kustomizev1.GroupVersion.WithKind(kustomizev1.KustomizationKind)),
RunE: buildKsCmdRun,
}

type ksFlags struct {
resources string
}

var ksArgs ksFlags

func init() {
buildKsCmd.Flags().StringVar(&ksArgs.resources, "resources", "", "Name of a file containing a file to add to the kustomization file.)")
buildCmd.AddCommand(buildKsCmd)
}

func buildKsCmdRun(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("%s name is required", kustomizationType.humanKind)
}
name := args[0]

if ksArgs.resources == "" {
return fmt.Errorf("invalid resource path %q", ksArgs.resources)
}

if fs, err := os.Stat(ksArgs.resources); err != nil || !fs.IsDir() {
return fmt.Errorf("invalid resource path %q", ksArgs.resources)
}

builder, err := kustomization.NewBuilder(rootArgs.kubeconfig, rootArgs.kubecontext, rootArgs.namespace, name, ksArgs.resources, kustomization.WithTimeout(rootArgs.timeout))
if err != nil {
return err
}

manifests, err := builder.Build()
if err != nil {
return err
}

cmd.Print(string(manifests))

return nil

}
18 changes: 13 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,48 @@ module github.com/fluxcd/flux2
go 1.16

require (
filippo.io/age v1.0.0
github.com/Masterminds/semver/v3 v3.1.0
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7
github.com/ProtonMail/go-crypto v0.0.0-20211112122917-428f8eabeeb3
github.com/cyphar/filepath-securejoin v0.2.2
github.com/drone/envsubst v1.0.3-0.20200804185402-58bc65f69603
github.com/fluxcd/go-git-providers v0.4.0
github.com/fluxcd/helm-controller/api v0.14.0
github.com/fluxcd/image-automation-controller/api v0.18.0
github.com/fluxcd/image-reflector-controller/api v0.14.0
github.com/fluxcd/kustomize-controller/api v0.18.1
github.com/fluxcd/notification-controller/api v0.19.0
github.com/fluxcd/pkg/apis/kustomize v0.2.0
github.com/fluxcd/pkg/apis/meta v0.10.1
github.com/fluxcd/pkg/runtime v0.12.2
github.com/fluxcd/pkg/ssa v0.3.1
github.com/fluxcd/pkg/ssh v0.0.5
github.com/fluxcd/pkg/untar v0.0.5
github.com/fluxcd/pkg/untar v0.1.0
github.com/fluxcd/pkg/version v0.0.1
github.com/fluxcd/source-controller/api v0.19.0
github.com/go-errors/errors v1.4.0 // indirect
github.com/go-git/go-git/v5 v5.4.2
github.com/google/go-cmp v0.5.6
github.com/google/go-containerregistry v0.2.0
github.com/hashicorp/go-retryablehttp v0.7.0 // indirect
github.com/manifoldco/promptui v0.9.0
github.com/mattn/go-shellwords v1.0.12
github.com/olekukonko/tablewriter v0.0.4
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d
go.mozilla.org/sops/v3 v3.7.1
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b
google.golang.org/grpc v1.42.0
k8s.io/api v0.22.2
k8s.io/apiextensions-apiserver v0.22.2
k8s.io/apimachinery v0.22.2
k8s.io/client-go v0.22.2
k8s.io/kubectl v0.21.1
sigs.k8s.io/cli-utils v0.26.0
sigs.k8s.io/controller-runtime v0.10.2
sigs.k8s.io/kustomize/api v0.8.10
sigs.k8s.io/kustomize/api v0.10.1
sigs.k8s.io/kustomize/kyaml v0.13.0
sigs.k8s.io/yaml v1.3.0
)
Loading

0 comments on commit 5665027

Please sign in to comment.