github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/schema/util/upgrade_pipelines.go (about) 1 /* 2 Copyright 2019 The Skaffold Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "errors" 21 "fmt" 22 "reflect" 23 ) 24 25 type pipelineUpgrader struct { 26 oldConfig reflect.Value 27 newConfig reflect.Value 28 upgrade func(o, n interface{}) error 29 } 30 31 func UpgradePipelines(oldConfig, newConfig interface{}, upgrade func(o, n interface{}) error) (err error) { 32 defer func() { 33 if r := recover(); r != nil { 34 switch x := r.(type) { 35 case string: 36 err = fmt.Errorf("upgrading pipelines failed: %s", x) 37 case error: 38 err = x 39 default: 40 err = errors.New("unknown panic") 41 } 42 } 43 }() 44 45 upgrader := pipelineUpgrader{ 46 oldConfig: reflect.Indirect(reflect.ValueOf(oldConfig)), 47 newConfig: reflect.Indirect(reflect.ValueOf(newConfig)), 48 upgrade: upgrade, 49 } 50 51 if err := upgrader.mainPipeline(); err != nil { 52 return err 53 } 54 55 return upgrader.profiles() 56 } 57 58 func (u *pipelineUpgrader) mainPipeline() error { 59 const fieldMainPipeline = "Pipeline" 60 61 oldPipeline := u.oldConfig.FieldByName(fieldMainPipeline).Addr().Interface() 62 newPipeline := u.newConfig.FieldByName(fieldMainPipeline).Addr().Interface() 63 64 err := u.upgrade(oldPipeline, newPipeline) 65 if err != nil { 66 return fmt.Errorf("upgrading main pipeline: %w", err) 67 } 68 69 return nil 70 } 71 72 func (u *pipelineUpgrader) profiles() error { 73 const ( 74 fieldProfilePipeline = "Pipeline" 75 fieldProfiles = "Profiles" 76 ) 77 78 profilesOld := u.oldConfig.FieldByName(fieldProfiles) 79 profilesNew := u.newConfig.FieldByName(fieldProfiles) 80 81 if profilesOld.Len() != profilesNew.Len() { 82 return fmt.Errorf("lengths of old and new profiles differ") 83 } 84 85 for i := 0; i < profilesOld.Len(); i++ { 86 oldPipeline := profilesOld.Index(i).FieldByName(fieldProfilePipeline).Addr().Interface() 87 newPipeline := profilesNew.Index(i).FieldByName(fieldProfilePipeline).Addr().Interface() 88 89 if err := u.upgrade(oldPipeline, newPipeline); err != nil { 90 return fmt.Errorf("upgrading pipeline of profile %d: %w", i+1, err) 91 } 92 } 93 94 return nil 95 }