-
Notifications
You must be signed in to change notification settings - Fork 380
core/config: Add Git variables to lab topology names #2951
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
Draft
vista-
wants to merge
1
commit into
main
Choose a base branch
from
feature/toponame-git-variables
base: main
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.
Draft
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,9 @@ import ( | |
| "sort" | ||
| "strings" | ||
|
|
||
| gogit "github.com/go-git/go-git/v5" | ||
| "github.com/go-git/go-git/v5/plumbing" | ||
|
|
||
| "github.com/charmbracelet/log" | ||
| "github.com/pmorjan/kmod" | ||
| clabconstants "github.com/srl-labs/containerlab/constants" | ||
|
|
@@ -37,6 +40,10 @@ const ( | |
| clabDirVar = "__clabDir__" | ||
| nodeDirVar = "__clabNodeDir__" | ||
| nodeNameVar = "__clabNodeName__" | ||
|
|
||
| // clab name specific variables | ||
| gitBranchVar = "__gitBranch__" | ||
| gitHashVar = "__gitHash__" | ||
| ) | ||
|
|
||
| // Config defines lab configuration as it is provided in the YAML file. | ||
|
|
@@ -55,6 +62,13 @@ type Config struct { | |
| func (c *CLab) parseTopology() error { | ||
| log.Info("Parsing & checking topology", "file", c.TopoPaths.TopologyFilenameBase()) | ||
|
|
||
| if strings.Contains(c.Config.Name, gitBranchVar) || strings.Contains(c.Config.Name, gitHashVar) { | ||
| r := c.magicTopoNameReplacer() | ||
| oldName := c.Config.Name | ||
| c.Config.Name = r.Replace(c.Config.Name) | ||
| log.Debugf("Topology name contains Git variables, substituted topology name: %q -> %q", oldName, c.Config.Name) | ||
| } | ||
|
|
||
| err := c.TopoPaths.SetLabDirByPrefix(c.Config.Name) | ||
| if err != nil { | ||
| return err | ||
|
|
@@ -777,3 +791,47 @@ func (c *CLab) magicVarReplacer(nodeName string) *strings.Replacer { | |
| nodeNameVar, nodeName, | ||
| ) | ||
| } | ||
|
|
||
| // magicVarReplacer returns a string replacer that replaces all supported magic variables. | ||
|
Member
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. looks like the comment needs to be adjusted to only mention specific variables? |
||
| func (c *CLab) magicTopoNameReplacer() *strings.Replacer { | ||
| gitBranch := "none" | ||
| gitHash := "none" | ||
|
|
||
| repo, err := gogit.PlainOpen(c.TopoPaths.TopologyFileDir()) | ||
| if err != nil { | ||
| log.Warnf("topology name uses git variables, but no Git repository found at %q - variables will be replaced with 'none'", c.TopoPaths.TopologyFileDir()) | ||
| return strings.NewReplacer( | ||
| gitBranchVar, gitBranch, | ||
| gitHashVar, gitHash, | ||
| ) | ||
| } | ||
|
|
||
| repoHead, err := repo.Head() | ||
| if err != nil { | ||
| log.Warn("no commits have been made yet on the current topology Git branch - hash will be 'none'") | ||
| // Try symbolic head | ||
| symbolicHead, err := repo.Storer.Reference(plumbing.HEAD) | ||
| if err != nil || symbolicHead.Type() != plumbing.SymbolicReference { | ||
| log.Error("could not get information about the head of Git repository while resolving topology git variables") | ||
| return strings.NewReplacer( | ||
| gitBranchVar, gitBranch, | ||
| gitHashVar, gitHash, | ||
| ) | ||
| } | ||
| gitBranch = strings.TrimPrefix(symbolicHead.Target().String(), "refs/heads/") | ||
| } else { | ||
| gitBranch = repoHead.Name().Short() | ||
| gitHash = repoHead.Hash().String() | ||
|
|
||
| } | ||
|
|
||
| log.Debugf("Git repo variables will be the following: branch: %q hash: %q", gitBranch, gitHash) | ||
|
|
||
| // Replace illegal characters in branch name | ||
| gitBranch = strings.Replace(gitBranch, "/", "-", -1) | ||
|
|
||
| return strings.NewReplacer( | ||
| gitBranchVar, gitBranch, | ||
| gitHashVar, gitHash, | ||
| ) | ||
| } | ||
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.
I think it would make sense to record the branch and hash as node labels as well
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.
I'll add this and move the PR to a ready to review state once I have some free time - hopefully soon.