Skip to content

Commit f81cedb

Browse files
authored
refactor: rename getSrosVersionFromImage to srosVersionFromImage and … (#2953)
…improve logging
1 parent a75eabd commit f81cedb

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

nodes/sros/sros.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ func (n *sros) createSROSConfigFiles() error {
10031003
// Get version from image before generating config
10041004
if n.swVersion == nil {
10051005
ctx := context.Background()
1006-
version, err := n.getSrosVersionFromImage(ctx)
1006+
version, err := n.srosVersionFromImage(ctx)
10071007
if err != nil {
10081008
log.Warn("Failed to get SR OS version from image",
10091009
"node", n.Cfg.ShortName, "error", err)

nodes/sros/version.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
clabruntime "github.com/srl-labs/containerlab/runtime"
1616
)
1717

18+
const srosVersionFilePath = "/etc/sros-version"
19+
1820
var (
1921
//go:embed configs/10_snmpv2.cfg
2022
snmpv2Config string
@@ -109,9 +111,9 @@ func (v *SrosVersion) MajorMinorSemverString() string {
109111
return fmt.Sprintf("v%s.%s", v.Major, v.Minor)
110112
}
111113

112-
// getSrosVersionFromImage retrieves the SR OS version from the container image
114+
// srosVersionFromImage retrieves the SR OS version from the container image
113115
// by inspecting the image layers without spawning a container.
114-
func (n *sros) getSrosVersionFromImage(ctx context.Context) (*SrosVersion, error) {
116+
func (n *sros) srosVersionFromImage(ctx context.Context) (*SrosVersion, error) {
115117
// Try to read from image config labels first (if set by image build)
116118
log.Debugf("Inspecting image %v for SR OS version retrieval", n.Cfg.Image)
117119
imageInspect, err := n.Runtime.InspectImage(ctx, n.Cfg.Image)
@@ -120,7 +122,7 @@ func (n *sros) getSrosVersionFromImage(ctx context.Context) (*SrosVersion, error
120122
}
121123

122124
if version, exists := imageInspect.Config.Labels["sros.version"]; exists {
123-
return n.parseVersionString(version), nil
125+
return n.parseVersionString(version), err
124126
}
125127

126128
// Fallback: read directly from image layers via graph driver
@@ -130,25 +132,26 @@ func (n *sros) getSrosVersionFromImage(ctx context.Context) (*SrosVersion, error
130132
version, err := n.readVersionFromImageLayers(ctx, imageInspect)
131133
if err != nil {
132134
log.Warn("Failed to extract SR OS version from image layers, using default",
133-
"node", n.Cfg.ShortName, "error", err)
135+
"node", n.Cfg.ShortName, "image", n.Cfg.Image, "error", err)
134136
// Return nil for version when error occurs
135137
return nil, err
136138
}
137139

138-
return n.parseVersionString(version), nil
140+
return n.parseVersionString(version), err
139141
}
140142

141-
// readVersionFromImageLayers reads the sros-version file directly from image layers
142-
// using the Docker graph driver's UpperDir without extracting the entire image.
143+
// readVersionFromImageLayers reads the SR OS version from the /etc/sros-version file
144+
// directly from image layers using the Docker graph driver's UpperDir
145+
// without extracting the entire image.
143146
func (n *sros) readVersionFromImageLayers(
144147
_ context.Context,
145148
imageInspect *clabruntime.ImageInspect,
146149
) (string, error) {
147150
// First, try to use the GraphDriver.Data.UpperDir if available
148151
if imageInspect.GraphDriver.Data.UpperDir != "" {
149-
versionPath := filepath.Join(imageInspect.GraphDriver.Data.UpperDir, "etc", "sros-version")
152+
versionPath := filepath.Join(imageInspect.GraphDriver.Data.UpperDir, srosVersionFilePath)
150153

151-
log.Debug("Attempting to read sros-version from UpperDir",
154+
log.Debug("Attempting to read SR OS version from UpperDir",
152155
"node", n.Cfg.ShortName,
153156
"path", versionPath)
154157

@@ -163,11 +166,11 @@ func (n *sros) readVersionFromImageLayers(
163166

164167
// Log the error and only fallback if it's a "file not found" error
165168
if !errors.Is(err, os.ErrNotExist) {
166-
log.Warn("Failed to read sros-version from UpperDir",
169+
log.Warn("Failed to read SR OS version from UpperDir",
167170
"node", n.Cfg.ShortName,
168171
"path", versionPath,
169172
"error", err)
170-
return "", fmt.Errorf("failed to read sros-version from UpperDir: %w", err)
173+
return "", fmt.Errorf("failed to read SR OS version from UpperDir: %w", err)
171174
}
172175

173176
log.Debug("sros-version file not found in UpperDir, trying MergedDir",
@@ -176,9 +179,9 @@ func (n *sros) readVersionFromImageLayers(
176179

177180
// Fallback: try MergedDir if available
178181
if imageInspect.GraphDriver.Data.MergedDir != "" {
179-
versionPath := filepath.Join(imageInspect.GraphDriver.Data.MergedDir, "etc", "sros-version")
182+
versionPath := filepath.Join(imageInspect.GraphDriver.Data.MergedDir, srosVersionFilePath)
180183

181-
log.Debug("Attempting to read sros-version from MergedDir",
184+
log.Debug("Attempting to read SR OS version from MergedDir",
182185
"node", n.Cfg.ShortName,
183186
"path", versionPath)
184187

@@ -193,16 +196,18 @@ func (n *sros) readVersionFromImageLayers(
193196

194197
// Log the specific error
195198
if !errors.Is(err, os.ErrNotExist) {
196-
log.Warn("Failed to read sros-version from MergedDir",
199+
log.Warn("Failed to read SR OS version from MergedDir",
197200
"node", n.Cfg.ShortName,
198201
"path", versionPath,
199202
"error", err)
200-
return "", fmt.Errorf("failed to read sros-version from MergedDir: %w", err)
203+
return "", fmt.Errorf("failed to read SR OS version from MergedDir: %w", err)
201204
}
202205

203-
log.Debug("sros-version file not found in MergedDir",
206+
log.Debug("SR OS version file not found in MergedDir",
207+
"path",
208+
srosVersionFilePath,
204209
"node", n.Cfg.ShortName)
205210
}
206211

207-
return "", fmt.Errorf("sros-version file not found in image graph driver directories or layers")
212+
return "", fmt.Errorf("%s file not found in image graph driver directories or layers", srosVersionFilePath)
208213
}

0 commit comments

Comments
 (0)