MCPcopy
hub / github.com/kubernetes/client-go / Migrate

Method Migrate

tools/clientcmd/loader.go:241–287  ·  view source on GitHub ↗

Migrate uses the MigrationRules map. If a destination file is not present, then the source file is checked. If the source file is present, then it is copied to the destination file BEFORE any further loading happens.

()

Source from the content-addressed store, hash-verified

239// Migrate uses the MigrationRules map. If a destination file is not present, then the source file is checked.
240// If the source file is present, then it is copied to the destination file BEFORE any further loading happens.
241func (rules *ClientConfigLoadingRules) Migrate() error {
242 if rules.MigrationRules == nil {
243 return nil
244 }
245
246 for destination, source := range rules.MigrationRules {
247 if _, err := os.Stat(destination); err == nil {
248 // if the destination already exists, do nothing
249 continue
250 } else if os.IsPermission(err) {
251 // if we can't access the file, skip it
252 continue
253 } else if !os.IsNotExist(err) {
254 // if we had an error other than non-existence, fail
255 return err
256 }
257
258 if sourceInfo, err := os.Stat(source); err != nil {
259 if os.IsNotExist(err) || os.IsPermission(err) {
260 // if the source file doesn't exist or we can't access it, there's no work to do.
261 continue
262 }
263
264 // if we had an error other than non-existence, fail
265 return err
266 } else if sourceInfo.IsDir() {
267 return fmt.Errorf("cannot migrate %v to %v because it is a directory", source, destination)
268 }
269
270 in, err := os.Open(source)
271 if err != nil {
272 return err
273 }
274 defer in.Close()
275 out, err := os.Create(destination)
276 if err != nil {
277 return err
278 }
279 defer out.Close()
280
281 if _, err = io.Copy(out, in); err != nil {
282 return err
283 }
284 }
285
286 return nil
287}
288
289// GetLoadingPrecedence implements ConfigAccess
290func (rules *ClientConfigLoadingRules) GetLoadingPrecedence() []string {

Callers 1

LoadMethod · 0.95

Calls 3

ErrorfMethod · 0.65
CloseMethod · 0.65
CreateMethod · 0.65

Tested by

no test coverage detected