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)
| 154 | |
| 155 | // CheckKubeContext prints a warning if the last kube context is different than this one |
| 156 | func 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")) |