Skip to content

Commit

Permalink
fix: make order of apis in generated catalog-info stable
Browse files Browse the repository at this point in the history
We need to make sure that we always produce a reproducible output when
generating catalog-info.yaml, it should be a pure function of the vervet
config and open api specs.

We are unmarshalling the vervet config into a go map then iterating over
that. Go randomises the order of values in a map so if a project
declares multiple apis in a vervet config then the order of the
resulting api objects in the catalog-info would be random. The only way
to get stability is to use an external ordered structure, in this case
we can use a string slice of the keys.
  • Loading branch information
jgresty committed Nov 28, 2022
1 parent 0d195c2 commit 503755d
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion internal/cmd/backstage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"sort"

"github.com/urfave/cli/v2"

Expand Down Expand Up @@ -156,7 +157,17 @@ func processCatalog(ctx *cli.Context, w io.Writer) error {
return true
}
}
for _, apiConf := range proj.APIs {

// range over maps does not specify order and is not guaranteed to be the
// same from one iteration to the next, stability is important when
// generating catalog-info to produce reproducible results
var apiNames []string
for k := range proj.APIs {
apiNames = append(apiNames, k)
}
sort.Strings(apiNames)
for _, apiName := range apiNames {
apiConf := proj.APIs[apiName]
outputPaths := apiConf.Output.ResolvePaths()
for _, outputPath := range outputPaths {
outputPath = filepath.Join(projectDir, outputPath)
Expand Down

0 comments on commit 503755d

Please sign in to comment.