Skip to content

Commit 7bfbcca

Browse files
committed
ua: update build variables
Signed-off-by: Patrick Zhao <zhaoyu@koderover.com>
1 parent 1d2c54a commit 7bfbcca

File tree

1 file changed

+281
-0
lines changed
  • pkg/cli/upgradeassistant/cmd/migrate

1 file changed

+281
-0
lines changed
Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
/*
2+
Copyright 2026 The KodeRover 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 migrate
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"strings"
23+
24+
"go.mongodb.org/mongo-driver/mongo"
25+
26+
"github.com/koderover/zadig/v2/pkg/cli/upgradeassistant/internal/upgradepath"
27+
"github.com/koderover/zadig/v2/pkg/microservice/aslan/config"
28+
commonmodels "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
29+
commonrepo "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/mongodb"
30+
templaterepo "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/mongodb/template"
31+
"github.com/koderover/zadig/v2/pkg/tool/log"
32+
)
33+
34+
const (
35+
ua999ProjectPrefix = "glb-"
36+
ua999BuildTemplate = "qa-product-glb-java-spring"
37+
ua999OtelParamsKey = "OTEL_SETTINGS_OTEL_PARAMS"
38+
ua999LurkerVersionKey = "LURKER_VERSION"
39+
ua999OtelParamsValue = "{{.parameter.qa-product-glb.otel_params}}"
40+
ua999LurkerVersionVal = "{{.parameter.qa-product-glb.lurker_version}}"
41+
)
42+
43+
func init() {
44+
upgradepath.RegisterHandler("4.2.0", "9.9.9", V420ToV999)
45+
}
46+
47+
// V420ToV999 updates specific build variables in both build definitions and workflow build job variable configs.
48+
func V420ToV999() error {
49+
buildTemplate, err := commonrepo.NewBuildTemplateColl().Find(&commonrepo.BuildTemplateQueryOption{Name: ua999BuildTemplate})
50+
if err != nil {
51+
if err == mongo.ErrNoDocuments {
52+
log.Warnf("build template %s not found, skip migration", ua999BuildTemplate)
53+
return nil
54+
}
55+
return fmt.Errorf("failed to find build template %s, err: %s", ua999BuildTemplate, err)
56+
}
57+
58+
projects, err := templaterepo.NewProductColl().List()
59+
if err != nil {
60+
return fmt.Errorf("failed to list projects, err: %s", err)
61+
}
62+
63+
buildUpdated := 0
64+
buildKVUpdated := 0
65+
workflowUpdated := 0
66+
workflowKVUpdated := 0
67+
68+
for _, project := range projects {
69+
if project == nil || !strings.HasPrefix(project.ProductName, ua999ProjectPrefix) {
70+
continue
71+
}
72+
73+
targetBuilds, err := commonrepo.NewBuildColl().List(&commonrepo.BuildListOption{
74+
ProductName: project.ProductName,
75+
TemplateID: buildTemplate.ID.Hex(),
76+
})
77+
if err != nil {
78+
return fmt.Errorf("failed to list builds in project %s, err: %s", project.ProductName, err)
79+
}
80+
if len(targetBuilds) == 0 {
81+
continue
82+
}
83+
84+
buildNameSet := make(map[string]struct{}, len(targetBuilds))
85+
for _, build := range targetBuilds {
86+
if build == nil {
87+
continue
88+
}
89+
90+
buildNameSet[build.Name] = struct{}{}
91+
92+
changed, changedCount := update999BuildVariables(build)
93+
if !changed {
94+
continue
95+
}
96+
97+
if err := commonrepo.NewBuildColl().Update(build); err != nil {
98+
return fmt.Errorf("failed to update build %s in project %s, err: %s", build.Name, project.ProductName, err)
99+
}
100+
buildUpdated++
101+
buildKVUpdated += changedCount
102+
}
103+
104+
workflowCursor, err := commonrepo.NewWorkflowV4Coll().ListByCursor(&commonrepo.ListWorkflowV4Option{
105+
ProjectName: project.ProductName,
106+
})
107+
if err != nil {
108+
return fmt.Errorf("failed to list workflows in project %s, err: %s", project.ProductName, err)
109+
}
110+
111+
for workflowCursor.Next(context.Background()) {
112+
workflow := new(commonmodels.WorkflowV4)
113+
if err := workflowCursor.Decode(workflow); err != nil {
114+
log.Errorf("failed to decode workflow in project %s, err: %s", project.ProductName, err)
115+
continue
116+
}
117+
118+
changed, changedCount := update999WorkflowBuildJobVariables(workflow, buildNameSet)
119+
if !changed {
120+
continue
121+
}
122+
123+
if err := commonrepo.NewWorkflowV4Coll().Update(workflow.ID.Hex(), workflow); err != nil {
124+
log.Warnf("failed to update workflow %s in project %s, err: %s", workflow.Name, workflow.Project, err)
125+
continue
126+
}
127+
128+
workflowUpdated++
129+
workflowKVUpdated += changedCount
130+
}
131+
132+
if err := workflowCursor.Err(); err != nil {
133+
return fmt.Errorf("failed to iterate workflows in project %s, err: %s", project.ProductName, err)
134+
}
135+
_ = workflowCursor.Close(context.Background())
136+
}
137+
138+
log.Infof("ua999 done: build updated=%d, build kv updated=%d, workflow updated=%d, workflow kv updated=%d",
139+
buildUpdated, buildKVUpdated, workflowUpdated, workflowKVUpdated)
140+
return nil
141+
}
142+
143+
func update999BuildVariables(build *commonmodels.Build) (bool, int) {
144+
if build == nil {
145+
return false, 0
146+
}
147+
148+
totalChanged := 0
149+
if build.PreBuild != nil {
150+
totalChanged += update999BuildKeyVals(build.PreBuild.Envs)
151+
}
152+
153+
for _, target := range build.Targets {
154+
if target == nil {
155+
continue
156+
}
157+
totalChanged += update999BuildKeyVals(target.Envs)
158+
}
159+
160+
return totalChanged > 0, totalChanged
161+
}
162+
163+
func update999WorkflowBuildJobVariables(workflow *commonmodels.WorkflowV4, buildNameSet map[string]struct{}) (bool, int) {
164+
if workflow == nil {
165+
return false, 0
166+
}
167+
168+
workflowChanged := false
169+
totalChanged := 0
170+
171+
for _, stage := range workflow.Stages {
172+
for _, job := range stage.Jobs {
173+
if job == nil || job.JobType != config.JobZadigBuild {
174+
continue
175+
}
176+
177+
spec := new(commonmodels.ZadigBuildJobSpec)
178+
if err := commonmodels.IToi(job.Spec, spec); err != nil {
179+
log.Errorf("failed to decode build job %s in workflow %s/%s, err: %s", job.Name, workflow.Project, workflow.Name, err)
180+
continue
181+
}
182+
183+
changed, changedCount := update999BuildJobSpec(spec, buildNameSet)
184+
if !changed {
185+
continue
186+
}
187+
188+
job.Spec = spec
189+
workflowChanged = true
190+
totalChanged += changedCount
191+
}
192+
}
193+
194+
return workflowChanged, totalChanged
195+
}
196+
197+
func update999BuildJobSpec(spec *commonmodels.ZadigBuildJobSpec, buildNameSet map[string]struct{}) (bool, int) {
198+
if spec == nil {
199+
return false, 0
200+
}
201+
202+
totalChanged := 0
203+
204+
changedCount := update999ServiceBuildConfigVariables(spec.ServiceAndBuildsOptions, buildNameSet)
205+
totalChanged += changedCount
206+
207+
return totalChanged > 0, totalChanged
208+
}
209+
210+
func update999ServiceBuildConfigVariables(serviceAndBuilds []*commonmodels.ServiceAndBuild, buildNameSet map[string]struct{}) int {
211+
changed := 0
212+
213+
for _, serviceAndBuild := range serviceAndBuilds {
214+
if serviceAndBuild == nil {
215+
continue
216+
}
217+
if _, ok := buildNameSet[serviceAndBuild.BuildName]; !ok {
218+
continue
219+
}
220+
changed += update999RuntimeKeyVals(serviceAndBuild.KeyVals)
221+
}
222+
223+
return changed
224+
}
225+
226+
func update999BuildKeyVals(kvs commonmodels.KeyValList) int {
227+
changed := 0
228+
for _, kv := range kvs {
229+
if kv == nil {
230+
continue
231+
}
232+
targetValue, ok := get999TargetValue(kv.Key)
233+
if !ok || kv.Value == targetValue {
234+
continue
235+
}
236+
237+
kv.Value = targetValue
238+
changed++
239+
}
240+
return changed
241+
}
242+
243+
func update999RuntimeKeyVals(kvs commonmodels.RuntimeKeyValList) int {
244+
changed := 0
245+
for _, kv := range kvs {
246+
if kv == nil || kv.KeyVal == nil {
247+
continue
248+
}
249+
250+
targetValue, ok := get999TargetValue(kv.Key)
251+
if !ok {
252+
continue
253+
}
254+
255+
updated := false
256+
if kv.Value != targetValue {
257+
kv.Value = targetValue
258+
updated = true
259+
}
260+
if kv.Source != config.ParamSourceReference {
261+
kv.Source = config.ParamSourceReference
262+
updated = true
263+
}
264+
265+
if updated {
266+
changed++
267+
}
268+
}
269+
return changed
270+
}
271+
272+
func get999TargetValue(key string) (string, bool) {
273+
switch key {
274+
case ua999OtelParamsKey:
275+
return ua999OtelParamsValue, true
276+
case ua999LurkerVersionKey:
277+
return ua999LurkerVersionVal, true
278+
default:
279+
return "", false
280+
}
281+
}

0 commit comments

Comments
 (0)