Skip to content

Commit a29cf76

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

1 file changed

Lines changed: 276 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)