Skip to content

Commit 8d2a76b

Browse files
committed
fix: use CalculateVersions from fabricator API
Signed-off-by: Pau Capdevila <[email protected]>
1 parent a593c1d commit 8d2a76b

File tree

1 file changed

+62
-101
lines changed

1 file changed

+62
-101
lines changed

pkg/hhfab/cmdversions.go

Lines changed: 62 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -53,50 +53,13 @@ func Versions(ctx context.Context, workDir, cacheDir string, hMode HydrateMode,
5353
if err := freshFab.CalculateVersions(fab.Versions); err != nil {
5454
return fmt.Errorf("calculating default versions: %w", err)
5555
}
56-
releaseData, err := kyaml.Marshal(freshFab.Status.Versions)
57-
if err != nil {
58-
return fmt.Errorf("marshalling release versions: %w", err)
59-
}
60-
var release map[string]interface{}
61-
if err := kyaml.Unmarshal(releaseData, &release); err != nil {
62-
return fmt.Errorf("unmarshalling release versions: %w", err)
63-
}
6456

65-
overridesRaw, err := kyaml.Marshal(cfg.Fab.Spec.Overrides.Versions)
57+
versionsData, err := formatVersions(freshFab.Status.Versions, cfg.Fab.Status.Versions)
6658
if err != nil {
67-
slog.Warn("Failed to marshal overrides", "error", err)
68-
fmt.Println(string(releaseData))
69-
70-
return nil
59+
return fmt.Errorf("formatting versions: %w", err)
7160
}
7261

73-
var overrides map[string]interface{}
74-
if err := kyaml.Unmarshal(overridesRaw, &overrides); err != nil {
75-
slog.Warn("Failed to unmarshal overrides", "error", err)
76-
fmt.Println(string(releaseData))
77-
78-
return nil
79-
}
80-
81-
if len(overrides) == 0 {
82-
slog.Info("Printing versions of all components")
83-
fmt.Println(string(releaseData))
84-
85-
return nil
86-
}
87-
88-
slog.Info("Printing versions of all components (release → override)")
89-
merged := make(map[string]interface{})
90-
91-
for category, value := range release {
92-
merged[category] = processVersionCategory(category, value, overrides)
93-
}
94-
95-
mergedData, err := kyaml.Marshal(merged)
96-
if err != nil {
97-
return fmt.Errorf("marshalling merged versions: %w", err)
98-
}
99-
fmt.Println(string(mergedData))
62+
fmt.Println(versionsData)
10063

10164
return nil
10265
}
@@ -108,101 +71,99 @@ func getVersionsFromCluster(ctx context.Context, c *Config) error {
10871
return fmt.Errorf("creating kube client: %w", err)
10972
}
11073

111-
fab := &fabapi.Fabricator{}
112-
if err := kube.Get(ctx, kclient.ObjectKey{Name: "default", Namespace: "fab"}, fab); err != nil {
74+
fabObj := &fabapi.Fabricator{}
75+
if err := kube.Get(ctx, kclient.ObjectKey{Name: "default", Namespace: "fab"}, fabObj); err != nil {
11376
return fmt.Errorf("getting fabricator object: %w", err)
11477
}
11578

11679
slog.Info("Printing versions from live cluster")
11780

118-
releaseData, err := kyaml.Marshal(fab.Status.Versions)
119-
if err != nil {
120-
return fmt.Errorf("marshalling versions: %w", err)
81+
freshFab := fabapi.Fabricator{}
82+
if err := freshFab.CalculateVersions(fab.Versions); err != nil {
83+
return fmt.Errorf("calculating default versions: %w", err)
12184
}
12285

123-
overridesRaw, err := kyaml.Marshal(fab.Spec.Overrides.Versions)
86+
versionsData, err := formatVersions(freshFab.Status.Versions, fabObj.Status.Versions)
12487
if err != nil {
125-
slog.Warn("Failed to marshal overrides", "error", err)
126-
fmt.Println(string(releaseData))
127-
128-
return nil
88+
return fmt.Errorf("formatting versions: %w", err)
12989
}
13090

131-
var overrides map[string]interface{}
132-
if err := kyaml.Unmarshal(overridesRaw, &overrides); err != nil {
133-
slog.Warn("Failed to unmarshal overrides", "error", err)
134-
fmt.Println(string(releaseData))
91+
fmt.Println(versionsData)
13592

136-
return nil
137-
}
138-
139-
if len(overrides) == 0 {
140-
slog.Info("Printing versions of all components")
141-
fmt.Println(string(releaseData))
93+
return nil
94+
}
14295

143-
return nil
96+
func formatVersions(releaseVersions, overriddenVersions fabapi.Versions) (string, error) {
97+
releaseMap, err := convertToMap(releaseVersions)
98+
if err != nil {
99+
return "", fmt.Errorf("converting release versions to map: %w", err)
144100
}
145101

146-
var release map[string]interface{}
147-
if err := kyaml.Unmarshal(releaseData, &release); err != nil {
148-
slog.Warn("Failed to unmarshal release versions", "error", err)
149-
fmt.Println(string(releaseData))
102+
overriddenMap, err := convertToMap(overriddenVersions)
103+
if err != nil {
104+
slog.Warn("Failed to convert overridden versions", "error", err)
105+
data, _ := kyaml.Marshal(releaseVersions)
150106

151-
return nil
107+
return string(data), nil
152108
}
153109

154110
slog.Info("Printing versions of all components (release → override)")
155-
merged := make(map[string]interface{})
156111

157-
for category, value := range release {
158-
merged[category] = processVersionCategory(category, value, overrides)
159-
}
112+
result := compareVersionMaps(releaseMap, overriddenMap)
160113

161-
mergedData, err := kyaml.Marshal(merged)
114+
resultData, err := kyaml.Marshal(result)
162115
if err != nil {
163-
return fmt.Errorf("marshalling merged versions: %w", err)
116+
return "", fmt.Errorf("marshalling result: %w", err)
164117
}
165-
fmt.Println(string(mergedData))
166118

167-
return nil
119+
return string(resultData), nil
168120
}
169121

170-
func processVersionCategory(category string, releaseValue interface{}, overrides map[string]interface{}) interface{} {
171-
releaseCat, isMapRelease := releaseValue.(map[string]interface{})
172-
if !isMapRelease {
173-
return releaseValue
174-
}
175-
176-
result := make(map[string]interface{})
177-
178-
overrideCat, overrideExists := overrides[category]
179-
if !overrideExists {
180-
return releaseCat
122+
func convertToMap(v interface{}) (map[string]interface{}, error) {
123+
data, err := kyaml.Marshal(v)
124+
if err != nil {
125+
return nil, fmt.Errorf("marshalling: %w", err)
181126
}
182127

183-
overrideMap, isMapOverride := overrideCat.(map[string]interface{})
184-
if !isMapOverride {
185-
return releaseCat
128+
var result map[string]interface{}
129+
if err := kyaml.Unmarshal(data, &result); err != nil {
130+
return nil, fmt.Errorf("unmarshalling: %w", err)
186131
}
187132

188-
for compName, releaseVer := range releaseCat {
189-
releaseVerStr, isString := releaseVer.(string)
190-
if !isString {
191-
nestedResult := processVersionCategory(compName, releaseVer, overrideMap)
192-
result[compName] = nestedResult
133+
return result, nil
134+
}
193135

194-
continue
195-
}
136+
func compareVersionMaps(releases, overridden map[string]interface{}) map[string]interface{} {
137+
result := make(map[string]interface{})
196138

197-
if overrideComp, exists := overrideMap[compName]; exists {
198-
overrideVerStr, isOverrideString := overrideComp.(string)
199-
if isOverrideString {
200-
result[compName] = fmt.Sprintf("%s → %s", releaseVerStr, overrideVerStr)
139+
for key, releaseValue := range releases {
140+
releaseMap, isMap := releaseValue.(map[string]interface{})
141+
if isMap {
142+
overriddenMap, hasOverridden := overridden[key].(map[string]interface{})
143+
if hasOverridden {
144+
result[key] = compareVersionMaps(releaseMap, overriddenMap)
201145
} else {
202-
result[compName] = releaseVer
146+
result[key] = releaseMap
203147
}
204148
} else {
205-
result[compName] = releaseVer
149+
releaseStr, isString := releaseValue.(string)
150+
if !isString {
151+
result[key] = releaseValue
152+
153+
continue
154+
}
155+
156+
overriddenValue, hasOverridden := overridden[key]
157+
if hasOverridden {
158+
overriddenStr, isString := overriddenValue.(string)
159+
if isString && overriddenStr != "" && overriddenStr != releaseStr {
160+
result[key] = fmt.Sprintf("%s → %s", releaseStr, overriddenStr)
161+
} else {
162+
result[key] = releaseValue
163+
}
164+
} else {
165+
result[key] = releaseValue
166+
}
206167
}
207168
}
208169

0 commit comments

Comments
 (0)