Skip to content

Commit

Permalink
cfg: add configuration for json index
Browse files Browse the repository at this point in the history
  • Loading branch information
MeurillonGuillaume committed Dec 6, 2023
1 parent abd614c commit 2b58a8b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
1 change: 1 addition & 0 deletions cmd/chartmuseum/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func cliHandler(c *cli.Context) {
WebTemplatePath: conf.GetString("web-template-path"),
ArtifactHubRepoID: conf.GetStringMapString("artifact-hub-repo-id"),
AlwaysRegenerateIndex: conf.GetBool("always-regenerate-chart-index"),
JSONIndex: conf.GetBool("json-index"),
}

server, err := newServer(options)
Expand Down
2 changes: 2 additions & 0 deletions pkg/chartmuseum/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type (
// AlwaysRegenerateIndex represents if the museum always return the up-to-date chart
// which means that the GetChart will increase its latency , be careful to enable this .
AlwaysRegenerateIndex bool
JSONIndex bool
}

// Server is a generic interface for web servers
Expand Down Expand Up @@ -151,6 +152,7 @@ func NewServer(options ServerOptions) (Server, error) {
// EnforceSemver2 - see https://github.com/helm/chartmuseum/issues/485 for more info
EnforceSemver2: options.EnforceSemver2,
AlwaysRegenerateIndex: options.AlwaysRegenerateIndex,
JSONIndex: options.JSONIndex,
})

return server, err
Expand Down
35 changes: 21 additions & 14 deletions pkg/chartmuseum/server/multitenant/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ func (server *MultiTenantServer) regenerateRepositoryIndexWorker(log cm_logger.L
"repo", repo,
)
index := &cm_repo.Index{
IndexFile: entry.RepoIndex.IndexFile,
RepoName: repo,
Raw: entry.RepoIndex.Raw,
ChartURL: entry.RepoIndex.ChartURL,
IndexLock: sync.RWMutex{},
IndexFile: entry.RepoIndex.IndexFile,
RepoName: repo,
Raw: entry.RepoIndex.Raw,
ChartURL: entry.RepoIndex.ChartURL,
IndexLock: sync.RWMutex{},
OutputJSON: server.JSONIndex,
}

for _, object := range diff.Removed {
Expand Down Expand Up @@ -442,35 +443,41 @@ func (server *MultiTenantServer) newRepositoryIndex(log cm_logger.LoggingFn, rep
}

if !server.UseStatefiles {
return cm_repo.NewIndex(chartURL, repo, serverInfo)
return cm_repo.NewIndex(chartURL, repo, serverInfo, server.JSONIndex)
}

objectPath := pathutil.Join(repo, cm_repo.StatefileFilename)
object, err := server.StorageBackend.GetObject(objectPath)
if err != nil {
return cm_repo.NewIndex(chartURL, repo, serverInfo)
return cm_repo.NewIndex(chartURL, repo, serverInfo, server.JSONIndex)
}

indexFile := &cm_repo.IndexFile{}
err = yaml.Unmarshal(object.Content, indexFile)
if json.Valid(object.Content) {
err = json.Unmarshal(object.Content, indexFile)
} else {
err = yaml.Unmarshal(object.Content, indexFile)
}

if err != nil {
log(cm_logger.WarnLevel, "index-cache.yaml found but could not be parsed",
"repo", repo,
"error", err.Error(),
)
return cm_repo.NewIndex(chartURL, repo, serverInfo)
return cm_repo.NewIndex(chartURL, repo, serverInfo, server.JSONIndex)
}

log(cm_logger.DebugLevel, "index-cache.yaml loaded",
"repo", repo,
)

return &cm_repo.Index{
IndexFile: indexFile,
RepoName: repo,
Raw: object.Content,
ChartURL: chartURL,
IndexLock: sync.RWMutex{},
IndexFile: indexFile,
RepoName: repo,
Raw: object.Content,
ChartURL: chartURL,
IndexLock: sync.RWMutex{},
OutputJSON: server.JSONIndex,
}
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/chartmuseum/server/multitenant/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type (
EnforceSemver2 bool
WebTemplatePath string
AlwaysRegenerateIndex bool
JSONIndex bool
}

ObjectsPerChartLimit struct {
Expand Down Expand Up @@ -106,6 +107,7 @@ type (
// Deprecated: see https://github.com/helm/chartmuseum/issues/485 for more info
EnforceSemver2 bool
AlwaysRegenerateIndex bool
JSONIndex bool
}

tenantInternals struct {
Expand Down Expand Up @@ -166,6 +168,7 @@ func NewMultiTenantServer(options MultiTenantServerOptions) (*MultiTenantServer,
WebTemplatePath: options.WebTemplatePath,
ArtifactHubRepoID: options.ArtifactHubRepoID,
AlwaysRegenerateIndex: options.AlwaysRegenerateIndex,
JSONIndex: options.JSONIndex,
}

if server.WebTemplatePath != "" {
Expand Down
9 changes: 9 additions & 0 deletions pkg/config/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ var configVars = map[string]configVar{
EnvVar: "DISABLE_STATEFILES",
},
},
"json-index": {
Type: boolType,
Default: false,
CLIFlag: cli.BoolFlag{
Name: "json-index",
Usage: "generates an index in JSON format, improves parsing performance for large index files",
EnvVar: "JSON_INDEX",
},
},
"allowoverwrite": {
Type: boolType,
Default: false,
Expand Down
4 changes: 2 additions & 2 deletions pkg/repo/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type (
Raw []byte `json:"c"`
ChartURL string `json:"d"`
IndexLock sync.RWMutex
outputJSON bool
OutputJSON bool
}
)

Expand All @@ -75,7 +75,7 @@ func (index *Index) Regenerate() (err error) {
index.Generated = time.Now().Round(time.Second)

var raw []byte
if index.outputJSON {
if index.OutputJSON {
raw, err = json.Marshal(index.IndexFile)
} else {
raw, err = yaml.Marshal(index.IndexFile)
Expand Down

0 comments on commit 2b58a8b

Please sign in to comment.