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 expandEnv #256

Merged
merged 6 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 7 additions & 3 deletions api/v1alpha1/controllermanagerconfig_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
)

func LoadOptionsFromFile(path string, scheme *runtime.Scheme, options *ctrl.Options, config *ControllerManagerConfig) error {
if err := loadFile(path, scheme, config); err != nil {
func LoadOptionsFromFile(path string, scheme *runtime.Scheme, options *ctrl.Options, config *ControllerManagerConfig, expandEnv bool) error {
if err := loadFile(path, scheme, config, expandEnv); err != nil {
return err
}

Expand All @@ -21,12 +21,16 @@ func LoadOptionsFromFile(path string, scheme *runtime.Scheme, options *ctrl.Opti
return nil
}

func loadFile(path string, scheme *runtime.Scheme, config *ControllerManagerConfig) error {
func loadFile(path string, scheme *runtime.Scheme, config *ControllerManagerConfig, expandEnv bool) error {
content, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("could not read file at %s: %w", path, err)
}

if expandEnv {
content = []byte(os.ExpandEnv(string(content)))
}

codecs := serializer.NewCodecFactory(scheme)

// Regardless of if the bytes are of any external version,
Expand Down
46 changes: 43 additions & 3 deletions api/v1alpha1/controllermanagerconfig_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ ignoreNamespaces:
- kube-public
- spire-system
- local-path-storage
`

fileContentExpandEnv = `
apiVersion: spire.spiffe.io/v1alpha1
kind: ControllerManagerConfig
clusterName: cluster2
trustDomain: $TRUST_DOMAIN
`
)

Expand All @@ -56,7 +63,7 @@ func TestLoadOptionsFromFileReplaceDefaultValues(t *testing.T) {
ValidatingWebhookConfigurationName: "foo-webhook",
}

err := spirev1alpha1.LoadOptionsFromFile(path, scheme, &options, &ctrlConfig)
err := spirev1alpha1.LoadOptionsFromFile(path, scheme, &options, &ctrlConfig, false)
require.NoError(t, err)

ok := true
Expand Down Expand Up @@ -107,10 +114,43 @@ func TestLoadOptionsFromFileInvalidPath(t *testing.T) {
ValidatingWebhookConfigurationName: "foo-webhook",
}

err := spirev1alpha1.LoadOptionsFromFile("", scheme, &options, &ctrlConfig)
err := spirev1alpha1.LoadOptionsFromFile("", scheme, &options, &ctrlConfig, false)
require.EqualError(t, err, "could not read file at : open : no such file or directory")

err = spirev1alpha1.LoadOptionsFromFile("foo.yaml", scheme, &options, &ctrlConfig)
err = spirev1alpha1.LoadOptionsFromFile("foo.yaml", scheme, &options, &ctrlConfig, false)
fmt.Printf("err :%v\n", err)
require.EqualError(t, err, "could not read file at foo.yaml: open foo.yaml: no such file or directory")
}

func TestLoadOptionsFromFileExpandEnv(t *testing.T) {
require.NoError(t, os.Setenv("TRUST_DOMAIN", "example.org"))
faisal-memon marked this conversation as resolved.
Show resolved Hide resolved

tempDir := t.TempDir()
path := filepath.Join(tempDir, "config.yaml")
require.NoError(t, os.WriteFile(path, []byte(fileContentExpandEnv), 0600))

scheme := runtime.NewScheme()
options := ctrl.Options{Scheme: scheme}

ctrlConfig := spirev1alpha1.ControllerManagerConfig{}

tests := []struct {
expandEnv bool
expectedValue string
}{
{
expandEnv: true,
expectedValue: "example.org",
},
{
expandEnv: false,
expectedValue: "$TRUST_DOMAIN",
},
}

for _, test := range tests {
err := spirev1alpha1.LoadOptionsFromFile(path, scheme, &options, &ctrlConfig, test.expandEnv)
require.NoError(t, err)
require.Equal(t, test.expectedValue, ctrlConfig.TrustDomain)
}
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ func main() {
func parseConfig() (spirev1alpha1.ControllerManagerConfig, ctrl.Options, []*regexp.Regexp, error) {
var configFileFlag string
var spireAPISocketFlag string
var expandEnvFlag bool
flag.StringVar(&configFileFlag, "config", "",
"The controller will load its initial configuration from this file. "+
"Omit this flag to use the default configuration values. "+
"Command-line flags override configuration from this file.")
flag.StringVar(&spireAPISocketFlag, "spire-api-socket", "", "The path to the SPIRE API socket (deprecated; use the config file)")
flag.BoolVar(&expandEnvFlag, "expandEnv", false, "Expand environment variables in SPIRE Controller Manager config file")
faisal-memon marked this conversation as resolved.
Show resolved Hide resolved

// Parse log flags
opts := zap.Options{
Expand All @@ -113,7 +115,7 @@ func parseConfig() (spirev1alpha1.ControllerManagerConfig, ctrl.Options, []*rege
var ignoreNamespacesRegex []*regexp.Regexp

if configFileFlag != "" {
if err := spirev1alpha1.LoadOptionsFromFile(configFileFlag, scheme, &options, &ctrlConfig); err != nil {
if err := spirev1alpha1.LoadOptionsFromFile(configFileFlag, scheme, &options, &ctrlConfig, expandEnvFlag); err != nil {
return ctrlConfig, options, ignoreNamespacesRegex, fmt.Errorf("unable to load the config file: %w", err)
}

Expand Down