-
Notifications
You must be signed in to change notification settings - Fork 5
fix(versions): log release versions when config is missing #745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pau-hedgehog
wants to merge
5
commits into
master
Choose a base branch
from
pau/hhfab_versions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a2d1823
fix(versions): log release versions when config is missing
pau-hedgehog 46bb8dc
chore: add version override query from the API
pau-hedgehog a593c1d
ci: show hhfab versions
pau-hedgehog 8d2a76b
fix: use CalculateVersions from fabricator API
pau-hedgehog fc41208
ci: fix hhfab versions in vlab upgrade jobs
pau-hedgehog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,171 @@ | ||||||||||||||
// Copyright 2024 Hedgehog | ||||||||||||||
// SPDX-License-Identifier: Apache-2.0 | ||||||||||||||
|
||||||||||||||
package hhfab | ||||||||||||||
|
||||||||||||||
import ( | ||||||||||||||
"context" | ||||||||||||||
"errors" | ||||||||||||||
"fmt" | ||||||||||||||
"log/slog" | ||||||||||||||
"os" | ||||||||||||||
"path/filepath" | ||||||||||||||
|
||||||||||||||
"go.githedgehog.com/fabric/pkg/util/kubeutil" | ||||||||||||||
fabapi "go.githedgehog.com/fabricator/api/fabricator/v1beta1" | ||||||||||||||
"go.githedgehog.com/fabricator/pkg/fab" | ||||||||||||||
kclient "sigs.k8s.io/controller-runtime/pkg/client" | ||||||||||||||
kyaml "sigs.k8s.io/yaml" | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
func Versions(ctx context.Context, workDir, cacheDir string, hMode HydrateMode, live bool) error { | ||||||||||||||
if live { | ||||||||||||||
cfg := &Config{ | ||||||||||||||
WorkDir: workDir, | ||||||||||||||
CacheDir: cacheDir, | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return getVersionsFromCluster(ctx, cfg) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
configPath := filepath.Join(workDir, FabConfigFile) | ||||||||||||||
if _, err := os.Stat(configPath); err != nil && errors.Is(err, os.ErrNotExist) { | ||||||||||||||
slog.Info("No configuration found", "file", FabConfigFile, "action", "Showing release versions") | ||||||||||||||
freshFab := fabapi.Fabricator{} | ||||||||||||||
if err := freshFab.CalculateVersions(fab.Versions); err != nil { | ||||||||||||||
return fmt.Errorf("calculating default versions: %w", err) | ||||||||||||||
} | ||||||||||||||
data, err := kyaml.Marshal(freshFab.Status.Versions) | ||||||||||||||
if err != nil { | ||||||||||||||
return fmt.Errorf("marshalling versions: %w", err) | ||||||||||||||
} | ||||||||||||||
fmt.Println(string(data)) | ||||||||||||||
|
||||||||||||||
return nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
cfg, err := load(ctx, workDir, cacheDir, true, hMode, "") | ||||||||||||||
if err != nil { | ||||||||||||||
return err | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
freshFab := fabapi.Fabricator{} | ||||||||||||||
if err := freshFab.CalculateVersions(fab.Versions); err != nil { | ||||||||||||||
return fmt.Errorf("calculating default versions: %w", err) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
versionsData, err := formatVersions(freshFab.Status.Versions, cfg.Fab.Status.Versions) | ||||||||||||||
if err != nil { | ||||||||||||||
return fmt.Errorf("formatting versions: %w", err) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
fmt.Println(versionsData) | ||||||||||||||
|
||||||||||||||
return nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func getVersionsFromCluster(ctx context.Context, c *Config) error { | ||||||||||||||
kubeconfig := filepath.Join(c.WorkDir, VLABDir, VLABKubeConfig) | ||||||||||||||
kube, err := kubeutil.NewClient(ctx, kubeconfig, fabapi.SchemeBuilder) | ||||||||||||||
if err != nil { | ||||||||||||||
return fmt.Errorf("creating kube client: %w", err) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
fabObj := &fabapi.Fabricator{} | ||||||||||||||
if err := kube.Get(ctx, kclient.ObjectKey{Name: "default", Namespace: "fab"}, fabObj); err != nil { | ||||||||||||||
return fmt.Errorf("getting fabricator object: %w", err) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
slog.Info("Printing versions from live cluster") | ||||||||||||||
|
||||||||||||||
freshFab := fabapi.Fabricator{} | ||||||||||||||
if err := freshFab.CalculateVersions(fab.Versions); err != nil { | ||||||||||||||
return fmt.Errorf("calculating default versions: %w", err) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
versionsData, err := formatVersions(freshFab.Status.Versions, fabObj.Status.Versions) | ||||||||||||||
if err != nil { | ||||||||||||||
return fmt.Errorf("formatting versions: %w", err) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
fmt.Println(versionsData) | ||||||||||||||
|
||||||||||||||
return nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func formatVersions(releaseVersions, overriddenVersions fabapi.Versions) (string, error) { | ||||||||||||||
releaseMap, err := convertToMap(releaseVersions) | ||||||||||||||
if err != nil { | ||||||||||||||
return "", fmt.Errorf("converting release versions to map: %w", err) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
overriddenMap, err := convertToMap(overriddenVersions) | ||||||||||||||
if err != nil { | ||||||||||||||
slog.Warn("Failed to convert overridden versions", "error", err) | ||||||||||||||
data, _ := kyaml.Marshal(releaseVersions) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid ignoring the error from kyaml.Marshal here; handle or log it so that serialization failures in the fallback path are not silently dropped.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||
|
||||||||||||||
return string(data), nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
slog.Info("Printing versions of all components (release → override)") | ||||||||||||||
|
||||||||||||||
result := compareVersionMaps(releaseMap, overriddenMap) | ||||||||||||||
|
||||||||||||||
resultData, err := kyaml.Marshal(result) | ||||||||||||||
if err != nil { | ||||||||||||||
return "", fmt.Errorf("marshalling result: %w", err) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return string(resultData), nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func convertToMap(v interface{}) (map[string]interface{}, error) { | ||||||||||||||
data, err := kyaml.Marshal(v) | ||||||||||||||
if err != nil { | ||||||||||||||
return nil, fmt.Errorf("marshalling: %w", err) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
var result map[string]interface{} | ||||||||||||||
if err := kyaml.Unmarshal(data, &result); err != nil { | ||||||||||||||
return nil, fmt.Errorf("unmarshalling: %w", err) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return result, nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func compareVersionMaps(releases, overridden map[string]interface{}) map[string]interface{} { | ||||||||||||||
result := make(map[string]interface{}) | ||||||||||||||
|
||||||||||||||
for key, releaseValue := range releases { | ||||||||||||||
releaseMap, isMap := releaseValue.(map[string]interface{}) | ||||||||||||||
if isMap { | ||||||||||||||
overriddenMap, hasOverridden := overridden[key].(map[string]interface{}) | ||||||||||||||
if hasOverridden { | ||||||||||||||
result[key] = compareVersionMaps(releaseMap, overriddenMap) | ||||||||||||||
} else { | ||||||||||||||
result[key] = releaseMap | ||||||||||||||
} | ||||||||||||||
} else { | ||||||||||||||
releaseStr, isString := releaseValue.(string) | ||||||||||||||
if !isString { | ||||||||||||||
result[key] = releaseValue | ||||||||||||||
|
||||||||||||||
continue | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
overriddenValue, hasOverridden := overridden[key] | ||||||||||||||
if hasOverridden { | ||||||||||||||
overriddenStr, isString := overriddenValue.(string) | ||||||||||||||
if isString && overriddenStr != "" && overriddenStr != releaseStr { | ||||||||||||||
result[key] = fmt.Sprintf("%s → %s", releaseStr, overriddenStr) | ||||||||||||||
} else { | ||||||||||||||
result[key] = releaseValue | ||||||||||||||
} | ||||||||||||||
} else { | ||||||||||||||
result[key] = releaseValue | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return result | ||||||||||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] There are multiple duplicated blocks that call CalculateVersions and print results; consider extracting common code into a helper to reduce duplication.
Copilot uses AI. Check for mistakes.