Skip to content

Commit 01745bf

Browse files
committed
Rework error handling in matching manifest layers to config
1 parent 96ea879 commit 01745bf

File tree

1 file changed

+25
-56
lines changed

1 file changed

+25
-56
lines changed

pkg/cmd/unpack/unpack.go

Lines changed: 25 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,17 @@ func runUnpackRecursive(ctx context.Context, opts *unpackOptions, visitedRefs []
8585
// layer path). For current ModelKits, this will be empty
8686
var relPath string
8787

88+
// Grab path + layer info from the config object corresponding to this layer
89+
var layerPath string
90+
var layerInfo *artifact.LayerInfo
8891
mediaType := constants.ParseMediaType(layerDesc.MediaType)
8992
switch mediaType.BaseType {
9093
case constants.ModelType:
9194
if !shouldUnpackLayer(config.Model, opts.filterConfs) {
9295
continue
9396
}
94-
if config.Model.LayerInfo != nil {
95-
if config.Model.LayerInfo.Digest != layerDesc.Digest.String() {
96-
return fmt.Errorf("digest in config and manifest do not match in model")
97-
}
98-
relPath = ""
99-
} else {
100-
_, relPath, err = filesystem.VerifySubpath(opts.unpackDir, config.Model.Path)
101-
if err != nil {
102-
return fmt.Errorf("error resolving model path: %w", err)
103-
}
104-
}
105-
97+
layerInfo = config.Model.LayerInfo
98+
layerPath = config.Model.Path
10699
output.Infof("Unpacking model %s to %s", config.Model.Name, config.Model.Path)
107100

108101
case constants.ModelPartType:
@@ -111,17 +104,8 @@ func runUnpackRecursive(ctx context.Context, opts *unpackOptions, visitedRefs []
111104
modelPartIdx += 1
112105
continue
113106
}
114-
if part.LayerInfo != nil {
115-
if part.LayerInfo.Digest != layerDesc.Digest.String() {
116-
return fmt.Errorf("digest in config and manifest do not match in modelpart")
117-
}
118-
relPath = ""
119-
} else {
120-
_, relPath, err = filesystem.VerifySubpath(opts.unpackDir, part.Path)
121-
if err != nil {
122-
return fmt.Errorf("error resolving code path: %w", err)
123-
}
124-
}
107+
layerInfo = part.LayerInfo
108+
layerPath = part.Path
125109
output.Infof("Unpacking model part %s to %s", part.Name, part.Path)
126110
modelPartIdx += 1
127111

@@ -131,17 +115,8 @@ func runUnpackRecursive(ctx context.Context, opts *unpackOptions, visitedRefs []
131115
codeIdx += 1
132116
continue
133117
}
134-
if codeEntry.LayerInfo != nil {
135-
if codeEntry.LayerInfo.Digest != layerDesc.Digest.String() {
136-
return fmt.Errorf("digest in config and manifest do not match in code layer")
137-
}
138-
relPath = ""
139-
} else {
140-
_, relPath, err = filesystem.VerifySubpath(opts.unpackDir, codeEntry.Path)
141-
if err != nil {
142-
return fmt.Errorf("error resolving code path: %w", err)
143-
}
144-
}
118+
layerInfo = codeEntry.LayerInfo
119+
layerPath = codeEntry.Path
145120
output.Infof("Unpacking code to %s", codeEntry.Path)
146121
codeIdx += 1
147122

@@ -151,17 +126,8 @@ func runUnpackRecursive(ctx context.Context, opts *unpackOptions, visitedRefs []
151126
datasetIdx += 1
152127
continue
153128
}
154-
if datasetEntry.LayerInfo != nil {
155-
if datasetEntry.LayerInfo.Digest != layerDesc.Digest.String() {
156-
return fmt.Errorf("digest in config and manifest do not match in dataset layer")
157-
}
158-
relPath = ""
159-
} else {
160-
_, relPath, err = filesystem.VerifySubpath(opts.unpackDir, datasetEntry.Path)
161-
if err != nil {
162-
return fmt.Errorf("error resolving dataset path for dataset %s: %w", datasetEntry.Name, err)
163-
}
164-
}
129+
layerInfo = datasetEntry.LayerInfo
130+
layerPath = datasetEntry.Path
165131
output.Infof("Unpacking dataset %s to %s", datasetEntry.Name, datasetEntry.Path)
166132
datasetIdx += 1
167133

@@ -171,21 +137,24 @@ func runUnpackRecursive(ctx context.Context, opts *unpackOptions, visitedRefs []
171137
docsIdx += 1
172138
continue
173139
}
174-
if docsEntry.LayerInfo != nil {
175-
if docsEntry.LayerInfo.Digest != layerDesc.Digest.String() {
176-
return fmt.Errorf("digest in config and manifest do not match in docs layer")
177-
}
178-
relPath = ""
179-
} else {
180-
_, relPath, err = filesystem.VerifySubpath(opts.unpackDir, docsEntry.Path)
181-
if err != nil {
182-
return fmt.Errorf("error resolving path %s for docs: %w", docsEntry.Path, err)
183-
}
184-
}
140+
layerInfo = docsEntry.LayerInfo
141+
layerPath = docsEntry.Path
185142
output.Infof("Unpacking docs to %s", docsEntry.Path)
186143
docsIdx += 1
187144
}
188145

146+
if layerInfo != nil {
147+
if layerInfo.Digest != layerDesc.Digest.String() {
148+
return fmt.Errorf("digest in config and manifest do not match in %s", mediaType.BaseType)
149+
}
150+
relPath = ""
151+
} else {
152+
_, relPath, err = filesystem.VerifySubpath(opts.unpackDir, layerPath)
153+
if err != nil {
154+
return fmt.Errorf("error resolving %s path: %w", mediaType.BaseType, err)
155+
}
156+
}
157+
189158
if err := unpackLayer(ctx, store, layerDesc, relPath, opts.overwrite, mediaType.Compression); err != nil {
190159
return fmt.Errorf("failed to unpack: %w", err)
191160
}

0 commit comments

Comments
 (0)