Skip to content
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
22 changes: 15 additions & 7 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ Examples:

cmd.Flags().StringVar(&shell, "shell", "", "Shell to spawn after Central deployment")

cmd.Flags().StringVar(&envrc, "envrc", "", "Write environment to file instead of spawning sub-shell")
registerFlag(cmd, settings, "envrc", "Write environment to file instead of spawning sub-shell",
withApplyFn("file", func(config *deployer.Config, filename string) error {
config.Roxie.Envrc = new(filename)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return nil
}),
)

cmd.Flags().StringVar(&imagePreLoadCommand, "image-preload-command", "",
`Use custom command for pre-loading images to local cluster. Image can be referenced as $IMAGE.
Pre-loading support for Kind and Minikube clusters is built-in. In case this is not sufficient
Expand Down Expand Up @@ -280,9 +286,6 @@ func runDeploy(cmd *cobra.Command, args []string) error {
}
defer d.Cleanup()

if envrc != "" {
d.SetEnvrcFile(envrc)
}
d.SetConfig(deploySettings)

if dryRun {
Expand Down Expand Up @@ -346,16 +349,21 @@ func runDeploy(cmd *cobra.Command, args []string) error {

if !dryRun {
roxieEnv := roxieenv.AssembleRoxieEnvironment(d.GetCentralDeploymentInfo())
// The envrc shall not be persisted as part of the on-cluster manifest.
// Hence we zero it out here prior to applying the manifest to the cluster.
// This code can go once the on-cluster manifest has been reworked completely.
manifestConfig := deploySettings
manifestConfig.Roxie.Envrc = nil
m := manifest.RoxieManifest{
RoxieEnvironment: roxieEnv,
Config: deploySettings,
Config: manifestConfig,
}
if err := manifest.CreateManifestSecretOnCluster(ctx, m); err != nil {
log.Warningf("Failed to save roxie manifest: %v", err)
}
}

if components.IncludesCentral() && envrc == "" {
if components.IncludesCentral() && !deploySettings.Roxie.EnvrcSet() {
if err := spawnSubshellForDeployerEnv(deploySettings.Roxie, d); err != nil {
return fmt.Errorf("failed to spawn subshell: %w", err)
}
Expand Down Expand Up @@ -488,7 +496,7 @@ func deployValidate(components component.Component, deploySettings *deployer.Con
return errors.New("already in a roxie sub-shell (ROXIE_SHELL environment variable is set), please exit the shell and try again")
}

if components.IncludesCentral() && !env.RunningInteractively && envrc == "" {
if components.IncludesCentral() && !env.RunningInteractively && !deploySettings.Roxie.EnvrcSet() {
return errors.New("running without a controlling terminal requires --envrc to be set")
}

Expand Down
51 changes: 51 additions & 0 deletions cmd/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,57 @@ central:
},
},

{
name: "envrc via CLI flag",
args: []string{"--envrc", "/tmp/my.envrc"},
assert: func(t *testing.T, cfg deployer.Config) {
require.NotNil(t, cfg.Roxie.Envrc, "Roxie.Envrc should be set")
assert.Equal(t, "/tmp/my.envrc", *cfg.Roxie.Envrc)
},
},
{
name: "envrc via config file",
config: `
roxie:
envrc: /tmp/from-config.envrc
`,
args: []string{"--config", configFilePath},
assert: func(t *testing.T, cfg deployer.Config) {
require.NotNil(t, cfg.Roxie.Envrc, "Roxie.Envrc should be set")
assert.Equal(t, "/tmp/from-config.envrc", *cfg.Roxie.Envrc)
},
},
{
name: "envrc CLI flag overrides config file",
config: `
roxie:
envrc: /tmp/from-config.envrc
`,
args: []string{"--config", configFilePath, "--envrc", "/tmp/from-cli.envrc"},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
assert: func(t *testing.T, cfg deployer.Config) {
require.NotNil(t, cfg.Roxie.Envrc, "Roxie.Envrc should be set")
assert.Equal(t, "/tmp/from-cli.envrc", *cfg.Roxie.Envrc)
},
},
{
name: "envrc CLI flag can clear config file value",
config: `
roxie:
envrc: /tmp/from-config.envrc
`,
args: []string{"--config", configFilePath, "--envrc", ""},
assert: func(t *testing.T, cfg deployer.Config) {
require.NotNil(t, cfg.Roxie.Envrc, "Roxie.Envrc should be set (to empty)")
assert.Empty(t, *cfg.Roxie.Envrc)
},
},
{
name: "envrc not set by default",
args: []string{"--tag", "4.7.0"},
assert: func(t *testing.T, cfg deployer.Config) {
assert.Nil(t, cfg.Roxie.Envrc, "Roxie.Envrc should not be set")
},
},
{
name: "set expressions can be used",
args: []string{"--set", "roxie.version=0.99.1", "--set", "central.deployTimeout=4m", "--set", "securedCluster.spec.clusterName=foo"},
Expand Down
1 change: 0 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var (
// Global flags
verbose bool
shell string
envrc string
dryRun bool

skipUserConfig bool
Expand Down
5 changes: 5 additions & 0 deletions internal/deployer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type RoxieConfig struct {
FeatureFlags map[string]bool `yaml:"featureFlags,omitempty"`
ClusterType types.ClusterType `yaml:"clusterType,omitempty"`
HAProxy HAProxyConfig `yaml:"haProxy,omitempty"`
Envrc *string `yaml:"envrc,omitempty"`
}

// UsesCustomRegistry returns whether a custom image registry was configured.
Expand All @@ -103,6 +104,10 @@ func (c *RoxieConfig) KonfluxImagesEnabled() bool {
return c.KonfluxImages != nil && *c.KonfluxImages
}

func (c *RoxieConfig) EnvrcSet() bool {
return c.Envrc != nil && *c.Envrc != ""
}

// NewRoxieConfig returns a RoxieConfig with initialized defaults.
func NewRoxieConfig() RoxieConfig {
return RoxieConfig{
Expand Down
2 changes: 1 addition & 1 deletion internal/deployer/deploy_via_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ func (d *Deployer) configureCentralEndpoint(ctx context.Context) error {
}
}

if d.envrcFile != "" {
if d.config.Roxie.EnvrcSet() {
endpoint, pid, err := d.portForward.StartDetached(d.config.Central.Namespace, serviceName, 443, 8443)
if err != nil {
return fmt.Errorf("failed to start detached port-forward: %w", err)
Expand Down
19 changes: 7 additions & 12 deletions internal/deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ type Deployer struct {
dockerAuth *dockerauth.DockerAuth
imageCache *imagecache.ImageCache
dockerCreds *dockerauth.Credentials
envrcFile string

kubeContext string
containerRuntimeSocket string
Expand Down Expand Up @@ -273,7 +272,7 @@ func New() (*Deployer, error) {

// Cleanup cleans up any temporary resources created by the deployer, such as temporary files.
func (d *Deployer) Cleanup() {
if d.tempDir != "" && d.envrcFile == "" {
if d.tempDir != "" && !d.config.Roxie.EnvrcSet() {
Comment thread
mclasmeier marked this conversation as resolved.
Resolved
// In the case of envrc file usage, we need to keep temporary files around after deployment.
// (It contains CA certificates, for example.)
if err := os.RemoveAll(d.tempDir); err != nil {
Expand Down Expand Up @@ -427,9 +426,9 @@ func (d *Deployer) deployCentral(ctx context.Context) error {
return err
}

if d.envrcFile != "" {
log.Dimf("Writing environment variables to %s", d.envrcFile)
if err := d.writeEnvrcFile(ctx); err != nil {
if d.config.Roxie.EnvrcSet() {
log.Dimf("Writing environment variables to %s", *d.config.Roxie.Envrc)
if err := d.writeEnvrcFile(ctx, *d.config.Roxie.Envrc); err != nil {
log.Warningf("Failed to write envrc file: %v", err)
}
}
Expand Down Expand Up @@ -652,10 +651,6 @@ func generatePassword() string {
return string(password)
}

func (d *Deployer) SetEnvrcFile(path string) {
d.envrcFile = path
}

func (d *Deployer) doesResourceExist(ctx context.Context, resourceType, resourceName, namespace string) bool {
_, err := k8s.RunKubectl(ctx, k8s.KubectlOptions{
Args: []string{
Expand Down Expand Up @@ -777,7 +772,7 @@ func (d *Deployer) cleanupTempDir(path string, description string) {
}
}

func (d *Deployer) writeEnvrcFile(ctx context.Context) error {
func (d *Deployer) writeEnvrcFile(ctx context.Context, filename string) error {
var content strings.Builder
for name, val := range roxieenv.AssembleRoxieEnvironment(d.GetCentralDeploymentInfo()).Export() {
fmt.Fprintf(&content, "export %s=%q\n", name, val)
Expand All @@ -787,11 +782,11 @@ func (d *Deployer) writeEnvrcFile(ctx context.Context) error {
fmt.Fprintf(&content, "export ROXIE_PORT_FORWARD_PID=%d\n", d.portForwardPID)
}

if err := os.WriteFile(d.envrcFile, []byte(content.String()), 0600); err != nil {
if err := os.WriteFile(filename, []byte(content.String()), 0600); err != nil {
return fmt.Errorf("failed to write envrc file: %w", err)
}

log.Successf("✓ Environment variables written to %s", d.envrcFile)
log.Successf("✓ Environment variables written to %s", filename)
return nil
}

Expand Down
Loading