-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4710 from fluxcd/envsubst-cmd
Add `flux envsubst` command
- Loading branch information
Showing
6 changed files
with
171 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
Copyright 2024 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 ( | ||
"bufio" | ||
"fmt" | ||
|
||
"github.com/fluxcd/pkg/envsubst" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var envsubstCmd = &cobra.Command{ | ||
Use: "envsubst", | ||
Args: cobra.NoArgs, | ||
Short: "envsubst substitutes the values of environment variables", | ||
Long: withPreviewNote(`The envsubst command substitutes the values of environment variables | ||
in the string piped as standard input and writes the result to the standard output. This command can be used | ||
to replicate the behavior of the Flux Kustomization post-build substitutions.`), | ||
Example: ` # Run env var substitutions on the kustomization build output | ||
export cluster_region=eu-central-1 | ||
kustomize build . | flux envsubst | ||
# Run env var substitutions and error out if a variable is not set | ||
kustomize build . | flux envsubst --strict | ||
`, | ||
RunE: runEnvsubstCmd, | ||
} | ||
|
||
type envsubstFlags struct { | ||
strict bool | ||
} | ||
|
||
var envsubstArgs envsubstFlags | ||
|
||
func init() { | ||
envsubstCmd.Flags().BoolVar(&envsubstArgs.strict, "strict", false, | ||
"fail if a variable without a default value is declared in the input but is missing from the environment") | ||
rootCmd.AddCommand(envsubstCmd) | ||
} | ||
|
||
func runEnvsubstCmd(cmd *cobra.Command, args []string) error { | ||
stdin := bufio.NewScanner(rootCmd.InOrStdin()) | ||
stdout := bufio.NewWriter(rootCmd.OutOrStdout()) | ||
for stdin.Scan() { | ||
line, err := envsubst.EvalEnv(stdin.Text(), envsubstArgs.strict) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = fmt.Fprintln(stdout, line) | ||
if err != nil { | ||
return err | ||
} | ||
err = stdout.Flush() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright 2024 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 ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestEnvsubst(t *testing.T) { | ||
g := NewWithT(t) | ||
input, err := os.ReadFile("testdata/envsubst/file.yaml") | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
|
||
t.Setenv("REPO_NAME", "test") | ||
|
||
output, err := executeCommandWithIn("envsubst", bytes.NewReader(input)) | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
|
||
expected, err := os.ReadFile("testdata/envsubst/file.gold") | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
g.Expect(output).To(Equal(string(expected))) | ||
} | ||
|
||
func TestEnvsubst_Strinct(t *testing.T) { | ||
g := NewWithT(t) | ||
input, err := os.ReadFile("testdata/envsubst/file.yaml") | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err = executeCommandWithIn("envsubst --strict", bytes.NewReader(input)) | ||
g.Expect(err).To(HaveOccurred()) | ||
g.Expect(err.Error()).To(ContainSubstring("variable not set (strict mode)")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
apiVersion: source.toolkit.fluxcd.io/v1 | ||
kind: GitRepository | ||
metadata: | ||
name: test | ||
namespace: flux-system | ||
spec: | ||
ref: | ||
branch: main | ||
interval: 5m | ||
url: ssh://git@github.com/example/test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
apiVersion: source.toolkit.fluxcd.io/v1 | ||
kind: GitRepository | ||
metadata: | ||
name: ${REPO_NAME} | ||
namespace: ${REPO_NAMESPACE:=flux-system} | ||
spec: | ||
ref: | ||
branch: main | ||
interval: 5m | ||
url: ssh://git@github.com/example/${REPO_NAME} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters