MCPcopy Create free account
hub / github.com/devspace-sh/devspace / CheckKubeContext

Function CheckKubeContext

pkg/devspace/kubectl/client.go:156–297  ·  view source on GitHub ↗

CheckKubeContext prints a warning if the last kube context is different than this one

(client Client, localCache localcache.Cache, noWarning, autoSwitch, skipWakeUpPing bool, log log.Logger)

Source from the content-addressed store, hash-verified

154
155// CheckKubeContext prints a warning if the last kube context is different than this one
156func CheckKubeContext(client Client, localCache localcache.Cache, noWarning, autoSwitch, skipWakeUpPing bool, log log.Logger) (Client, error) {
157 currentConfigContext := &localcache.LastContextConfig{
158 Namespace: client.Namespace(),
159 Context: client.CurrentContext(),
160 }
161
162 resetClient := false
163 if localCache != nil && !noWarning {
164 lastConfigContext := localCache.GetLastContext()
165
166 // print warning if context or namespace has changed since last deployment process (expect if explicitly provided as flags)
167 if lastConfigContext != nil {
168 // if the current kubeContext!=last kubeContext
169 // then ask which kubeContext to use
170 // else if the current namespace!=last namespace
171 // then ask which namespace to use
172 if lastConfigContext.Context != "" &&
173 lastConfigContext.Context != currentConfigContext.Context {
174 if autoSwitch {
175 currentConfigContext.Context = lastConfigContext.Context
176 currentConfigContext.Namespace = lastConfigContext.Namespace
177 resetClient = true
178 } else if log.GetLevel() >= logrus.InfoLevel {
179 log.WriteString(logrus.WarnLevel, "\n")
180 log.Warnf(ansi.Color("Are you using the correct kube context?", "white+b"))
181 log.Warnf("Current kube context: '%s'", ansi.Color(currentConfigContext.Context, "white+b"))
182 log.Warnf("Last kube context: '%s'", ansi.Color(lastConfigContext.Context, "white+b"))
183
184 // if terminal is not interactive then return the same client
185 if !isTerminalIn {
186 return client, nil
187 }
188
189 kc, err := log.Question(&survey.QuestionOptions{
190 Question: "Which context do you want to use?",
191 DefaultValue: currentConfigContext.Context,
192 Options: []string{
193 currentConfigContext.Context,
194 lastConfigContext.Context,
195 },
196 })
197 if err != nil {
198 return client, err
199 }
200 if kc != currentConfigContext.Context {
201 currentConfigContext.Context = kc
202 currentConfigContext.Namespace = lastConfigContext.Namespace
203 resetClient = true
204 }
205 }
206 } else if lastConfigContext.Namespace != "" &&
207 lastConfigContext.Namespace != currentConfigContext.Namespace {
208 if autoSwitch {
209 currentConfigContext.Namespace = lastConfigContext.Namespace
210 resetClient = true
211 } else if log.GetLevel() >= logrus.InfoLevel {
212 log.WriteString(logrus.WarnLevel, "\n")
213 log.Warnf(ansi.Color("Are you using the correct namespace?", "white+b"))

Callers 15

RunLogsMethod · 0.92
RunMethod · 0.92
RunMethod · 0.92
initializeFunction · 0.92
RunMethod · 0.92
RunMethod · 0.92
RunAnalyzeMethod · 0.92
RunOpenMethod · 0.92
LoadCommandsConfigMethod · 0.92
RunUIMethod · 0.92
RunResetPodsMethod · 0.92
RunDeploymentsStatusMethod · 0.92

Calls 15

NewClientFromContextFunction · 0.85
wakeUpAndPingFunction · 0.85
NamespaceMethod · 0.65
CurrentContextMethod · 0.65
GetLastContextMethod · 0.65
WriteStringMethod · 0.65
QuestionMethod · 0.65
SetLastContextMethod · 0.65
SaveMethod · 0.65
KubeConfigLoaderMethod · 0.65
GetLevelMethod · 0.45
WarnfMethod · 0.45

Tested by 2

TestCheckKubeContextFunction · 0.68