Skip to content

Commit de14d7c

Browse files
committed
refactor: make devspace context an interface
1 parent 853d4a1 commit de14d7c

99 files changed

Lines changed: 1285 additions & 1244 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/attach.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,7 @@ func (cmd *AttachCmd) Run(f factory.Factory, cobraCmd *cobra.Command, args []str
9696
}
9797

9898
// create the context
99-
ctx := &devspacecontext.Context{
100-
Context: context.Background(),
101-
KubeClient: client,
102-
Log: log,
103-
}
99+
ctx := devspacecontext.NewContext(context.Background(), nil, log).WithKubeClient(client)
104100

105101
// Execute plugin hook
106102
err = hook.ExecuteHooks(ctx, nil, "attach")

cmd/enter.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,7 @@ func (cmd *EnterCmd) Run(f factory.Factory, args []string) error {
123123
}
124124

125125
// create the context
126-
ctx := &devspacecontext.Context{
127-
Context: context.Background(),
128-
KubeClient: client,
129-
Log: logger,
130-
}
126+
ctx := devspacecontext.NewContext(context.Background(), nil, logger).WithKubeClient(client)
131127

132128
// Execute plugin hook
133129
err = hook.ExecuteHooks(ctx, nil, "enter")

cmd/flags/flags.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ func SetGlobalFlags(flags *flag.FlagSet) *GlobalFlags {
5151
flags.BoolVar(&globalFlags.Debug, "debug", false, "Prints the stack trace if an error occurs")
5252
flags.BoolVar(&globalFlags.Silent, "silent", false, "Run in silent mode and prevents any devspace log output except panics & fatals")
5353

54-
flags.StringVar(&globalFlags.ConfigPath, "config", "", "The devspace config file to use")
5554
flags.StringSliceVarP(&globalFlags.Profiles, "profile", "p", []string{}, "The DevSpace profiles to apply. Multiple profiles are applied in the order they are specified")
5655
flags.BoolVar(&globalFlags.ProfileRefresh, "profile-refresh", false, "If true will pull and re-download profile parent sources")
5756
flags.BoolVar(&globalFlags.DisableProfileActivation, "disable-profile-activation", false, "If true will ignore all profile activations")
@@ -61,5 +60,28 @@ func SetGlobalFlags(flags *flag.FlagSet) *GlobalFlags {
6160
flags.StringSliceVar(&globalFlags.Vars, "var", []string{}, "Variables to override during execution (e.g. --var=MYVAR=MYVALUE)")
6261

6362
flags.IntVar(&globalFlags.InactivityTimeout, "inactivity-timeout", 0, "Minutes the current user is inactive (no mouse or keyboard interaction) until DevSpace will exit automatically. 0 to disable. Only supported on windows and mac operating systems")
63+
flags.AddFlag(&flag.Flag{
64+
Name: "config",
65+
Usage: "DEPRECATED: please use the DEVSPACE_CONFIG environment variable instead",
66+
Hidden: true,
67+
Value: NewStringValue("", &globalFlags.ConfigPath),
68+
})
6469
return globalFlags
6570
}
71+
72+
type StringValue string
73+
74+
func NewStringValue(val string, p *string) *StringValue {
75+
*p = val
76+
return (*StringValue)(p)
77+
}
78+
79+
func (s *StringValue) Set(val string) error {
80+
*s = StringValue(val)
81+
return nil
82+
}
83+
func (s *StringValue) Type() string {
84+
return "string"
85+
}
86+
87+
func (s *StringValue) String() string { return string(*s) }

cmd/list/deployments.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (cmd *deploymentsCmd) RunDeploymentsStatus(f factory.Factory, cobraCmd *cob
9292
}
9393

9494
// Create conext
95-
ctx := devspacecontext.NewContext(context.Background(), logger).
95+
ctx := devspacecontext.NewContext(context.Background(), configInterface.Variables(), logger).
9696
WithConfig(configInterface).
9797
WithKubeClient(client)
9898

@@ -103,8 +103,8 @@ func (cmd *deploymentsCmd) RunDeploymentsStatus(f factory.Factory, cobraCmd *cob
103103
}
104104
ctx = ctx.WithDependencies(dependencies)
105105

106-
if ctx.Config.Config().Deployments != nil {
107-
for _, deployConfig := range ctx.Config.Config().Deployments {
106+
if ctx.Config().Config().Deployments != nil {
107+
for _, deployConfig := range ctx.Config().Config().Deployments {
108108
var deployClient deployer.Interface
109109

110110
// Delete kubectl engine

cmd/logs.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ func (cmd *LogsCmd) RunLogs(f factory.Factory) error {
113113
}
114114

115115
// create the context
116-
ctx := &devspacecontext.Context{
117-
Context: context.Background(),
118-
KubeClient: client,
119-
Log: log,
120-
}
116+
ctx := devspacecontext.NewContext(context.Background(), nil, log).WithKubeClient(client)
121117

122118
// Execute plugin hook
123119
err = hook.ExecuteHooks(ctx, nil, "logs")
@@ -149,7 +145,7 @@ func (cmd *LogsCmd) RunLogs(f factory.Factory) error {
149145
return nil
150146
}
151147

152-
func getImageSelector(ctx *devspacecontext.Context, configLoader loader.ConfigLoader, configOptions *loader.ConfigOptions, imageSelector string) ([]string, error) {
148+
func getImageSelector(ctx devspacecontext.Context, configLoader loader.ConfigLoader, configOptions *loader.ConfigOptions, imageSelector string) ([]string, error) {
153149
var imageSelectors []string
154150
if imageSelector != "" {
155151
var (
@@ -160,19 +156,19 @@ func getImageSelector(ctx *devspacecontext.Context, configLoader loader.ConfigLo
160156
if !configLoader.Exists() {
161157
config = config2.Ensure(nil)
162158
} else {
163-
config, err = configLoader.Load(ctx.Context, ctx.KubeClient, configOptions, ctx.Log)
159+
config, err = configLoader.Load(ctx.Context(), ctx.KubeClient(), configOptions, ctx.Log())
164160
if err != nil {
165161
return nil, err
166162
}
167163

168164
ctx = ctx.WithConfig(config)
169165
dependencies, err = dependency.NewManager(ctx, configOptions).ResolveAll(ctx, dependency.ResolveOptions{})
170166
if err != nil {
171-
ctx.Log.Warnf("Error resolving dependencies: %v", err)
167+
ctx.Log().Warnf("Error resolving dependencies: %v", err)
172168
}
173169
}
174170

175-
resolved, err := runtimevar.NewRuntimeResolver(".", true).FillRuntimeVariablesAsImageSelector(ctx.Context, imageSelector, config, dependencies)
171+
resolved, err := runtimevar.NewRuntimeResolver(".", true).FillRuntimeVariablesAsImageSelector(ctx.Context(), imageSelector, config, dependencies)
176172
if err != nil {
177173
return nil, err
178174
}

cmd/open.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (cmd *OpenCmd) RunOpen(f factory.Factory) error {
134134
}
135135

136136
// create devspace context
137-
ctx := devspacecontext.NewContext(context.Background(), cmd.log).
137+
ctx := devspacecontext.NewContext(context.Background(), nil, cmd.log).
138138
WithKubeClient(client)
139139

140140
// Execute plugin hook
@@ -282,8 +282,8 @@ func openURL(url string, kubectlClient kubectl.Client, analyzeNamespace string,
282282
return nil
283283
}
284284

285-
func (cmd *OpenCmd) openLocal(ctx *devspacecontext.Context, domain string) error {
286-
_, servicePort, serviceLabels, err := cmd.getService(ctx.KubeClient, ctx.KubeClient.Namespace(), domain, true)
285+
func (cmd *OpenCmd) openLocal(ctx devspacecontext.Context, domain string) error {
286+
_, servicePort, serviceLabels, err := cmd.getService(ctx.KubeClient(), ctx.KubeClient().Namespace(), domain, true)
287287
if err != nil {
288288
return errors.Errorf("Unable to get service: %v", err)
289289
}

cmd/overwrite_command.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ func NewOverwriteCmd(f factory.Factory, globalFlags *flags.GlobalFlags, command
4646
}
4747

4848
runCmd := &cobra.Command{
49-
Use: command.Name,
50-
DisableFlagParsing: true,
51-
Short: description,
52-
Long: longDescription,
53-
Args: cobra.ArbitraryArgs,
49+
Use: command.Name,
50+
Short: description,
51+
Long: longDescription,
52+
Args: cobra.ArbitraryArgs,
5453
RunE: func(cobraCmd *cobra.Command, _ []string) error {
5554
args, err := ParseArgs(cobraCmd, cmd.GlobalFlags, f.GetLog())
5655
if err != nil {
@@ -61,10 +60,11 @@ func NewOverwriteCmd(f factory.Factory, globalFlags *flags.GlobalFlags, command
6160
return cmd.Run(f, args)
6261
},
6362
}
63+
runCmd.DisableFlagParsing = true
6464
return runCmd
6565
}
6666

6767
func (cmd *OverwriteCmd) Run(f factory.Factory, args []string) error {
68-
devCtx := devspacecontext.NewContext(context.Background(), f.GetLog())
69-
return executeCommandWithAfter(devCtx.Context, cmd.Command, args, cmd.Variables, devCtx.WorkingDir, cmd.Stdout, cmd.Stderr, os.Stdin, devCtx.Log)
68+
devCtx := devspacecontext.NewContext(context.Background(), cmd.Variables, f.GetLog())
69+
return executeCommandWithAfter(devCtx.Context(), cmd.Command, args, cmd.Variables, devCtx.WorkingDir(), cmd.Stdout, cmd.Stderr, os.Stdin, devCtx.Log())
7070
}

cmd/print.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (cmd *PrintCmd) Run(f factory.Factory) error {
9191
}
9292

9393
// create devspace context
94-
ctx := devspacecontext.NewContext(context.Background(), log).
94+
ctx := devspacecontext.NewContext(context.Background(), config.Variables(), log).
9595
WithConfig(config).
9696
WithKubeClient(client)
9797

@@ -117,13 +117,13 @@ func (cmd *PrintCmd) Run(f factory.Factory) error {
117117
ctx = ctx.AsDependency(dep)
118118
}
119119

120-
bsConfig, err := yaml.Marshal(ctx.Config.Config())
120+
bsConfig, err := yaml.Marshal(ctx.Config().Config())
121121
if err != nil {
122122
return err
123123
}
124124

125125
if !cmd.SkipInfo {
126-
err = printExtraInfo(ctx.Config, dependencies, log)
126+
err = printExtraInfo(ctx.Config(), dependencies, log)
127127
if err != nil {
128128
return err
129129
}

cmd/reset/pods.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (cmd *podsCmd) RunResetPods(f factory.Factory, cobraCmd *cobra.Command, arg
9494
}
9595

9696
// create devspace context
97-
ctx := devspacecontext.NewContext(context.Background(), cmd.log).
97+
ctx := devspacecontext.NewContext(context.Background(), conf.Variables(), cmd.log).
9898
WithConfig(conf).
9999
WithKubeClient(client)
100100

@@ -111,29 +111,29 @@ func (cmd *podsCmd) RunResetPods(f factory.Factory, cobraCmd *cobra.Command, arg
111111
}
112112

113113
// ResetPods deletes the pods created by dev.replacePods
114-
func ResetPods(ctx *devspacecontext.Context, dependencies, force bool) {
114+
func ResetPods(ctx devspacecontext.Context, dependencies, force bool) {
115115
resetted := ResetPodsRecursive(ctx, dependencies, force)
116116
if resetted == 0 {
117-
ctx.Log.Info("No dev pods to reset found")
117+
ctx.Log().Info("No dev pods to reset found")
118118
} else {
119-
ctx.Log.Donef("Successfully reset %d pods", resetted)
119+
ctx.Log().Donef("Successfully reset %d pods", resetted)
120120
}
121121
}
122122

123-
func ResetPodsRecursive(ctx *devspacecontext.Context, dependencies, force bool) int {
123+
func ResetPodsRecursive(ctx devspacecontext.Context, dependencies, force bool) int {
124124
resetted := 0
125125
if dependencies {
126-
for _, d := range ctx.Dependencies {
126+
for _, d := range ctx.Dependencies() {
127127
resetted += ResetPodsRecursive(ctx.AsDependency(d), dependencies, force)
128128
}
129129
}
130130

131131
// create pod replacer
132132
podReplacer := podreplace.NewPodReplacer()
133-
for _, replacePodCache := range ctx.Config.RemoteCache().ListDevPods() {
133+
for _, replacePodCache := range ctx.Config().RemoteCache().ListDevPods() {
134134
deleted, err := podReplacer.RevertReplacePod(ctx, &replacePodCache, &deploy.PurgeOptions{ForcePurge: force})
135135
if err != nil {
136-
ctx.Log.Warnf("Error resetting replaced pod: %v", err)
136+
ctx.Log().Warnf("Error resetting replaced pod: %v", err)
137137
} else if deleted {
138138
resetted++
139139
}

cmd/restart.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (cmd *RestartCmd) Run(f factory.Factory) error {
8888
}
8989

9090
// Create context
91-
ctx := devspacecontext.NewContext(context.Background(), cmd.log).
91+
ctx := devspacecontext.NewContext(context.Background(), nil, cmd.log).
9292
WithKubeClient(client)
9393

9494
return restartContainer(ctx, targetselector.NewOptionsFromFlags(cmd.Container, cmd.LabelSelector, nil, cmd.Namespace, cmd.Pod).WithPick(cmd.Pick))
@@ -120,7 +120,7 @@ func (cmd *RestartCmd) Run(f factory.Factory) error {
120120
}
121121

122122
// Create context
123-
ctx := devspacecontext.NewContext(context.Background(), cmd.log).
123+
ctx := devspacecontext.NewContext(context.Background(), config.Variables(), cmd.log).
124124
WithConfig(config).
125125
WithKubeClient(client)
126126

@@ -139,7 +139,7 @@ func (cmd *RestartCmd) Run(f factory.Factory) error {
139139

140140
// restart containers
141141
restarts := 0
142-
for _, devPod := range ctx.Config.Config().Dev {
142+
for _, devPod := range ctx.Config().Config().Dev {
143143
if cmd.Name != "" && devPod.Name != cmd.Name {
144144
continue
145145
}
@@ -167,7 +167,7 @@ func (cmd *RestartCmd) Run(f factory.Factory) error {
167167
// create target selector options
168168
var imageSelector []string
169169
if devPod.ImageSelector != "" {
170-
imageSelectorObject, err := runtimevar.NewRuntimeResolver(ctx.WorkingDir, true).FillRuntimeVariablesAsImageSelector(ctx.Context, devPod.ImageSelector, ctx.Config, ctx.Dependencies)
170+
imageSelectorObject, err := runtimevar.NewRuntimeResolver(ctx.WorkingDir(), true).FillRuntimeVariablesAsImageSelector(ctx.Context(), devPod.ImageSelector, ctx.Config(), ctx.Dependencies())
171171
if err != nil {
172172
return err
173173
}
@@ -191,23 +191,23 @@ func (cmd *RestartCmd) Run(f factory.Factory) error {
191191
return nil
192192
}
193193

194-
func restartContainer(ctx *devspacecontext.Context, options targetselector.Options) error {
194+
func restartContainer(ctx devspacecontext.Context, options targetselector.Options) error {
195195
options = options.WithWait(false)
196-
container, err := targetselector.NewTargetSelector(options).SelectSingleContainer(ctx.Context, ctx.KubeClient, ctx.Log)
196+
container, err := targetselector.NewTargetSelector(options).SelectSingleContainer(ctx.Context(), ctx.KubeClient(), ctx.Log())
197197
if err != nil {
198198
return errors.Errorf("Error selecting pod: %v", err)
199199
}
200200

201-
err = inject.InjectDevSpaceHelper(ctx.Context, ctx.KubeClient, container.Pod, container.Container.Name, "", ctx.Log)
201+
err = inject.InjectDevSpaceHelper(ctx.Context(), ctx.KubeClient(), container.Pod, container.Container.Name, "", ctx.Log())
202202
if err != nil {
203203
return errors.Wrap(err, "inject devspace helper")
204204
}
205205

206-
stdOut, stdErr, err := ctx.KubeClient.ExecBuffered(ctx.Context, container.Pod, container.Container.Name, []string{inject.DevSpaceHelperContainerPath, "restart"}, nil)
206+
stdOut, stdErr, err := ctx.KubeClient().ExecBuffered(ctx.Context(), container.Pod, container.Container.Name, []string{inject.DevSpaceHelperContainerPath, "restart"}, nil)
207207
if err != nil {
208208
return fmt.Errorf("error restarting container %s in pod %s/%s: %s %s => %v", container.Container.Name, container.Pod.Namespace, container.Pod.Name, string(stdOut), string(stdErr), err)
209209
}
210210

211-
ctx.Log.Donef("Successfully restarted container %s in pod %s/%s", container.Container.Name, container.Pod.Namespace, container.Pod.Name)
211+
ctx.Log().Donef("Successfully restarted container %s in pod %s/%s", container.Container.Name, container.Pod.Namespace, container.Pod.Name)
212212
return nil
213213
}

0 commit comments

Comments
 (0)