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

fix: make the usage of '--env' flag more precisely #4060

Merged
merged 1 commit into from
Oct 10, 2023
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
2 changes: 2 additions & 0 deletions cmd/sealos/cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/spf13/cobra"

"github.com/labring/sealos/pkg/apply"
"github.com/labring/sealos/pkg/utils/logger"
)

var exampleGen = `
Expand Down Expand Up @@ -65,6 +66,7 @@ func newGenCmd() *cobra.Command {
var outputWriter io.WriteCloser
switch out {
case "", "stdout":
logger.Info("if you want to save the output of gen command, use '--output' option instead of redirecting to file")
outputWriter = os.Stdout
default:
outputWriter, err = os.Create(out)
Expand Down
4 changes: 2 additions & 2 deletions pkg/apply/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type RunArgs struct {
func (arg *RunArgs) RegisterFlags(fs *pflag.FlagSet) {
arg.Cluster.RegisterFlags(fs, "run with", "run")
arg.SSH.RegisterFlags(fs)
fs.StringSliceVarP(&arg.CustomEnv, "env", "e", []string{}, "environment variables to set during command execution")
fs.StringSliceVarP(&arg.CustomEnv, "env", "e", []string{}, "environment variables to be set for images")
fs.StringSliceVar(&arg.CustomCMD, "cmd", []string{}, "override CMD directive in images")
fs.StringSliceVar(&arg.CustomConfigFiles, "config-file", []string{}, "path of custom config files, to use to replace the resource")
}
Expand All @@ -80,7 +80,7 @@ type Args struct {
func (arg *Args) RegisterFlags(fs *pflag.FlagSet) {
fs.StringSliceVar(&arg.Values, "values", []string{}, "values file to apply into Clusterfile")
fs.StringSliceVar(&arg.Sets, "set", []string{}, "set values on the command line")
fs.StringSliceVar(&arg.CustomEnv, "env", []string{}, "environment variables to set during command execution")
fs.StringSliceVar(&arg.CustomEnv, "env", []string{}, "environment variables to be set for images")
fs.StringSliceVar(&arg.CustomConfigFiles, "config-file", []string{}, "path of custom config files, to use to replace the resource")
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/apply/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/labring/sealos/pkg/runtime/factory"
"github.com/labring/sealos/pkg/types/v1beta1"
"github.com/labring/sealos/pkg/utils/iputils"
"github.com/labring/sealos/pkg/utils/logger"
)

func NewClusterFromGenArgs(cmd *cobra.Command, args *RunArgs, imageNames []string) ([]byte, error) {
Expand All @@ -43,6 +44,11 @@ func NewClusterFromGenArgs(cmd *cobra.Command, args *RunArgs, imageNames []strin
if err := c.runArgs(cmd, args, imageNames); err != nil {
return nil, err
}
if flagChanged(cmd, "env") {
logger.Info("setting global envs for cluster, will be used in all run commands later")
v, _ := cmd.Flags().GetStringSlice("env")
cluster.Spec.Env = append(cluster.Spec.Env, v...)
}

img, err := genImageInfo(imageNames[0])
if err != nil {
Expand Down
7 changes: 0 additions & 7 deletions pkg/clusterfile/clusterfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type ClusterFile struct {
customValues []string
customSets []string
customEnvs []string
setDefaults bool

cluster *v2.Cluster
configs []v2.Config
Expand Down Expand Up @@ -61,12 +60,6 @@ func (c *ClusterFile) GetRuntimeConfig() runtime.Config {

type OptionFunc func(*ClusterFile)

func WithSetDefaults(v bool) OptionFunc {
return func(c *ClusterFile) {
c.setDefaults = v
}
}

func WithCustomConfigFiles(files []string) OptionFunc {
return func(c *ClusterFile) {
c.customConfigFiles = files
Expand Down
10 changes: 1 addition & 9 deletions pkg/clusterfile/pre_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package clusterfile
import (
"bytes"
"errors"
"os"
"strings"

"helm.sh/helm/v3/pkg/cli/values"
"helm.sh/helm/v3/pkg/getter"
Expand All @@ -45,12 +43,6 @@ func (c *ClusterFile) Process() (err error) {
}
c.once.Do(func() {
err = func() error {
for i := range c.customEnvs {
kv := strings.SplitN(c.customEnvs[i], "=", 2)
if len(kv) == 2 {
_ = os.Setenv(kv[0], kv[1])
}
}
clusterFileData, err := c.loadClusterFile()
if err != nil {
return err
Expand Down Expand Up @@ -161,7 +153,7 @@ func (c *ClusterFile) DecodeRuntimeConfig(data []byte) error {
if cfg != nil {
c.runtimeConfig = cfg
} else {
kubeadmConfig, err := types.LoadKubeadmConfigs(string(data), c.setDefaults, decode.CRDFromString)
kubeadmConfig, err := types.LoadKubeadmConfigs(string(data), false, decode.CRDFromString)
if err != nil {
return err
}
Expand Down