Skip to content

Commit 9b8048f

Browse files
Merge pull request #1884 from deads2k/mom-filenames
API-1835: make mutation filenames more stable
2 parents f466fdd + e521339 commit 9b8048f

File tree

26 files changed

+53
-11
lines changed

26 files changed

+53
-11
lines changed

pkg/manifestclient/mutation_directory_reader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ func readSerializedRequestsFromActionDirectory(action Action, actionFS fs.FS) ([
9595
}
9696

9797
var (
98-
bodyRegex = regexp.MustCompile(`(\d\d\d)-body-(.+).yaml`)
99-
optionsRegex = regexp.MustCompile(`(\d\d\d)-options-(.+).yaml`)
98+
bodyRegex = regexp.MustCompile(`.*-body-(.+).yaml`)
99+
optionsRegex = regexp.MustCompile(`.*-options-(.+).yaml`)
100100
)
101101

102102
func serializedRequestFromFile(action Action, actionFS fs.FS, bodyFilename string) (*FileOriginatedSerializedRequest, error) {

pkg/manifestclient/serialized_request.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package manifestclient
22

33
import (
44
"bytes"
5+
"crypto/sha256"
56
"fmt"
67
"path/filepath"
78
"strings"
@@ -247,16 +248,16 @@ func (a FileOriginatedSerializedRequest) SuggestedFilenames() (string, string, s
247248
}
248249

249250
func (a TrackedSerializedRequest) SuggestedFilenames() (string, string, string) {
250-
return suggestedFilenames(a.SerializedRequest, a.RequestNumber)
251+
return suggestedFilenames(a.SerializedRequest)
251252
}
252253

253254
func (a SerializedRequest) SuggestedFilenames() (string, string, string) {
254-
// this may very well conflict in some cases. Up to the caller to work out how to fix it.
255-
uniqueNumber := 0 // chosen by fair dice roll. guaranteed to be random. :)
256-
return suggestedFilenames(a, uniqueNumber)
255+
return suggestedFilenames(a)
257256
}
258257

259-
func suggestedFilenames(a SerializedRequest, uniqueNumber int) (string, string, string) {
258+
func suggestedFilenames(a SerializedRequest) (string, string, string) {
259+
bodyHash := hashRequestToPrefix(a.Body, a.Options)
260+
260261
groupName := a.ResourceType.Group
261262
if len(groupName) == 0 {
262263
groupName = "core"
@@ -275,7 +276,7 @@ func suggestedFilenames(a SerializedRequest, uniqueNumber int) (string, string,
275276
scopingString,
276277
groupName,
277278
a.ResourceType.Resource,
278-
fmt.Sprintf("%03d-metadata-%s%s.yaml", uniqueNumber, a.Name, a.GenerateName),
279+
fmt.Sprintf("%s-metadata-%s%s.yaml", bodyHash, a.Name, a.GenerateName),
279280
),
280281
)
281282
bodyFilename := MakeFilenameGoModSafe(
@@ -284,7 +285,7 @@ func suggestedFilenames(a SerializedRequest, uniqueNumber int) (string, string,
284285
scopingString,
285286
groupName,
286287
a.ResourceType.Resource,
287-
fmt.Sprintf("%03d-body-%s%s.yaml", uniqueNumber, a.Name, a.GenerateName),
288+
fmt.Sprintf("%s-body-%s%s.yaml", bodyHash, a.Name, a.GenerateName),
288289
),
289290
)
290291
optionsFilename := ""
@@ -295,13 +296,36 @@ func suggestedFilenames(a SerializedRequest, uniqueNumber int) (string, string,
295296
scopingString,
296297
groupName,
297298
a.ResourceType.Resource,
298-
fmt.Sprintf("%03d-options-%s%s.yaml", uniqueNumber, a.Name, a.GenerateName),
299+
fmt.Sprintf("%s-options-%s%s.yaml", bodyHash, a.Name, a.GenerateName),
299300
),
300301
)
301302
}
302303
return metadataFilename, bodyFilename, optionsFilename
303304
}
304305

306+
func hashRequestToPrefix(data, options []byte) string {
307+
switch {
308+
case len(data) > 0:
309+
return hashForFilenamePrefix(data)
310+
case len(options) > 0:
311+
return hashForFilenamePrefix(options)
312+
default:
313+
return "MISSING"
314+
}
315+
}
316+
317+
func hashForFilenamePrefix(data []byte) string {
318+
if len(data) == 0 {
319+
return "MISSING"
320+
}
321+
hash := sha256.New()
322+
hash.Write(data)
323+
hashBytes := hash.Sum(nil)
324+
325+
// we're looking to deconflict filenames, not protect the crown jewels
326+
return fmt.Sprintf("%x", hashBytes[len(hashBytes)-2:])
327+
}
328+
305329
func (a FileOriginatedSerializedRequest) DeepCopy() SerializedRequestish {
306330
return FileOriginatedSerializedRequest{
307331
MetadataFilename: a.MetadataFilename,

pkg/manifestclienttest/testdata/mutation-tests/APPLY-STATUS-crd-in-dataset-with-options/ApplyStatus/cluster-scoped-resources/config.openshift.io/featuregates/001-body-new-item.yaml renamed to pkg/manifestclienttest/testdata/mutation-tests/APPLY-STATUS-crd-in-dataset-with-options/ApplyStatus/cluster-scoped-resources/config.openshift.io/featuregates/6a7c-body-new-item.yaml

File renamed without changes.

pkg/manifestclienttest/testdata/mutation-tests/APPLY-STATUS-crd-in-dataset-with-options/ApplyStatus/cluster-scoped-resources/config.openshift.io/featuregates/001-metadata-new-item.yaml renamed to pkg/manifestclienttest/testdata/mutation-tests/APPLY-STATUS-crd-in-dataset-with-options/ApplyStatus/cluster-scoped-resources/config.openshift.io/featuregates/6a7c-metadata-new-item.yaml

File renamed without changes.

pkg/manifestclienttest/testdata/mutation-tests/APPLY-STATUS-crd-in-dataset-with-options/ApplyStatus/cluster-scoped-resources/config.openshift.io/featuregates/001-options-new-item.yaml renamed to pkg/manifestclienttest/testdata/mutation-tests/APPLY-STATUS-crd-in-dataset-with-options/ApplyStatus/cluster-scoped-resources/config.openshift.io/featuregates/6a7c-options-new-item.yaml

File renamed without changes.

pkg/manifestclienttest/testdata/mutation-tests/APPLY-crd-in-dataset/Apply/cluster-scoped-resources/config.openshift.io/featuregates/001-body-new-item.yaml renamed to pkg/manifestclienttest/testdata/mutation-tests/APPLY-crd-in-dataset/Apply/cluster-scoped-resources/config.openshift.io/featuregates/57cd-body-new-item.yaml

File renamed without changes.

pkg/manifestclienttest/testdata/mutation-tests/APPLY-crd-in-dataset/Apply/cluster-scoped-resources/config.openshift.io/featuregates/001-metadata-new-item.yaml renamed to pkg/manifestclienttest/testdata/mutation-tests/APPLY-crd-in-dataset/Apply/cluster-scoped-resources/config.openshift.io/featuregates/57cd-metadata-new-item.yaml

File renamed without changes.

pkg/manifestclienttest/testdata/mutation-tests/APPLY-crd-in-dataset/Apply/cluster-scoped-resources/config.openshift.io/featuregates/001-options-new-item.yaml renamed to pkg/manifestclienttest/testdata/mutation-tests/APPLY-crd-in-dataset/Apply/cluster-scoped-resources/config.openshift.io/featuregates/57cd-options-new-item.yaml

File renamed without changes.

pkg/manifestclienttest/testdata/mutation-tests/CREATE-crd-in-dataset/Create/cluster-scoped-resources/config.openshift.io/featuregates/001-body-new-item.yaml renamed to pkg/manifestclienttest/testdata/mutation-tests/CREATE-crd-in-dataset/Create/cluster-scoped-resources/config.openshift.io/featuregates/a20b-body-new-item.yaml

File renamed without changes.

pkg/manifestclienttest/testdata/mutation-tests/CREATE-crd-in-dataset/Create/cluster-scoped-resources/config.openshift.io/featuregates/001-metadata-new-item.yaml renamed to pkg/manifestclienttest/testdata/mutation-tests/CREATE-crd-in-dataset/Create/cluster-scoped-resources/config.openshift.io/featuregates/a20b-metadata-new-item.yaml

File renamed without changes.

0 commit comments

Comments
 (0)