Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/scheduled-jobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ jobs:
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}

- name: Save performance log
uses: actions/upload-artifact@v4
with:
name: performance.log
path: build/ephemeral/performance.log
retention-days: 14

- uses: actions/checkout@v6
with:
repository: datreeio/CRDs-catalog
Expand Down
31 changes: 24 additions & 7 deletions internal/command/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/CustomResourceDefinition/catalog/internal/crd"
"github.com/CustomResourceDefinition/catalog/internal/generator"
"github.com/CustomResourceDefinition/catalog/internal/registry"
"github.com/CustomResourceDefinition/catalog/internal/timing"
)

type Updater struct {
Expand All @@ -22,16 +23,18 @@ type Updater struct {
reader crd.CrdReader
registry *registry.SourceRegistry
registryPath string
performanceLog string
}

func NewUpdater(configuration, schema, definitions, registryPath string, logger io.Writer, flags *flag.FlagSet) Updater {
func NewUpdater(configuration, schema, definitions, registryPath, performancePath string, logger io.Writer, flags *flag.FlagSet) Updater {
return Updater{
flags: flags,
Configuration: configuration,
Schema: schema,
Definitions: definitions,
registryPath: registryPath,
Logger: logger,
flags: flags,
Configuration: configuration,
Schema: schema,
Definitions: definitions,
registryPath: registryPath,
Logger: logger,
performanceLog: performancePath,
}
}

Expand Down Expand Up @@ -68,6 +71,13 @@ func (cmd Updater) Run() error {
}
defer os.RemoveAll(tmpDir)

totalStats := timing.NewStats()

if err := totalStats.OpenLogFile(cmd.performanceLog); err != nil {
return fmt.Errorf("failed to open performance log: %w", err)
}
defer totalStats.CloseLogFile()

for _, config := range splitConfigurations(configurations) {
runtime.GC()

Expand All @@ -82,6 +92,11 @@ func (cmd Updater) Run() error {
fmt.Fprintf(cmd.Logger, "::warning:: build of %s failed: %v\n", config.Name, err)
continue
}

stats := build.Stats()
for _, op := range stats.GetAllStats() {
totalStats.Record(op.Category, op.Type, op.Name, op.Duration, op.Success, op.StartTime)
}
}

if cmd.registry != nil && cmd.registryPath != "" {
Expand All @@ -90,6 +105,8 @@ func (cmd Updater) Run() error {
}
}

totalStats.PrintSummary(cmd.Logger)

return merge(tmpDir, cmd.Schema)
}

Expand Down
137 changes: 132 additions & 5 deletions internal/command/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestRun(t *testing.T) {
server, config, tmpDir := setup(t)
defer server.Close()

updater := NewUpdater(config, tmpDir, tmpDir, "", bytes.NewBuffer([]byte{}), nil)
updater := NewUpdater(config, tmpDir, tmpDir, "", "", bytes.NewBuffer([]byte{}), nil)

err := updater.Run()
assert.Nil(t, err)
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestRunWithRegistryLoadError(t *testing.T) {
registryPath := path.Join(tmpDir, "registry.yaml")
os.WriteFile(registryPath, []byte("invalid: yaml: content:"), 0644)

updater := NewUpdater(unused, tmpDir, tmpDir, registryPath, bytes.NewBuffer([]byte{}), nil)
updater := NewUpdater(unused, tmpDir, tmpDir, registryPath, "", bytes.NewBuffer([]byte{}), nil)

err := updater.Run()
assert.NotNil(t, err)
Expand All @@ -236,7 +236,7 @@ func TestRunWithRegistrySavesUpdates(t *testing.T) {
initialContent := "sources: {}\n"
os.WriteFile(registryPath, []byte(initialContent), 0664)

updater := NewUpdater(config, tmpDir, tmpDir, registryPath, bytes.NewBuffer([]byte{}), nil)
updater := NewUpdater(config, tmpDir, tmpDir, registryPath, "", bytes.NewBuffer([]byte{}), nil)

err := updater.Run()
assert.Nil(t, err)
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestRunWithRegistryRemovesStaleEntries(t *testing.T) {
`
os.WriteFile(registryPath, []byte(initialContent), 0664)

updater := NewUpdater(config, tmpDir, tmpDir, registryPath, bytes.NewBuffer([]byte{}), nil)
updater := NewUpdater(config, tmpDir, tmpDir, registryPath, "", bytes.NewBuffer([]byte{}), nil)

err := updater.Run()
assert.Nil(t, err)
Expand All @@ -287,9 +287,136 @@ func TestRunWithRegistryRemovesStaleEntries(t *testing.T) {
func TestCheckLocal(t *testing.T) {
output := "../../build/ephemeral/schema"
config := "../../build/configuration.yaml"
updater := NewUpdater(config, output, output, "", nil, nil)
updater := NewUpdater(config, output, output, "", "", nil, nil)

err := updater.Run()
assert.Nil(t, err)
assert.True(t, false, "this test should always be skipped and/or ignored")
}

func TestRunAggregatesStats(t *testing.T) {
b, err := os.ReadFile("testdata/updater/multiple.yaml")
assert.Nil(t, err)

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(b)
}))
defer server.Close()

template := `
- apiGroups:
- chart.uri
crds:
- baseUri: {{ server }}
paths:
- chart-1.0.0.yaml
version: 1.0.0
kind: http
name: http
`
tmpDir := t.TempDir()

config := path.Join(tmpDir, "config.yaml")
os.WriteFile(config, []byte(strings.ReplaceAll(template, "{{ server }}", server.URL)), 0664)

output := bytes.NewBuffer([]byte{})
updater := NewUpdater(config, tmpDir, tmpDir, "", "", output, nil)

err = updater.Run()
assert.Nil(t, err)

outStr := output.String()
assert.Contains(t, outStr, "Update Statistics")
assert.Contains(t, outStr, "Overall:")
assert.Contains(t, outStr, "operations")
assert.Contains(t, outStr, "Http:")
assert.Contains(t, outStr, "api_fetch")
}

func TestRunWithMultipleConfigsAggregatesStats(t *testing.T) {
b, err := os.ReadFile("testdata/updater/multiple.yaml")
assert.Nil(t, err)

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(b)
}))
defer server.Close()

template := `
- apiGroups:
- chart.uri
crds:
- baseUri: {{ server }}
paths:
- chart-1.0.0.yaml
version: 1.0.0
kind: http
name: http1
- apiGroups:
- chart.uri
crds:
- baseUri: {{ server }}
paths:
- chart-1.0.0.yaml
version: 1.0.0
kind: http
name: http2
`
tmpDir := t.TempDir()

config := path.Join(tmpDir, "config.yaml")
os.WriteFile(config, []byte(strings.ReplaceAll(template, "{{ server }}", server.URL)), 0664)

output := bytes.NewBuffer([]byte{})
updater := NewUpdater(config, tmpDir, tmpDir, "", "", output, nil)

err = updater.Run()
assert.Nil(t, err)

outStr := output.String()
assert.Contains(t, outStr, "Overall:")
assert.Contains(t, outStr, "api_fetch")
}

func TestRunWithPerformanceLog(t *testing.T) {
b, err := os.ReadFile("testdata/updater/multiple.yaml")
assert.Nil(t, err)

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(b)
}))
defer server.Close()

template := `
- apiGroups:
- chart.uri
crds:
- baseUri: {{ server }}
paths:
- chart-1.0.0.yaml
version: 1.0.0
kind: http
name: http
`
tmpDir := t.TempDir()

config := path.Join(tmpDir, "config.yaml")
os.WriteFile(config, []byte(strings.ReplaceAll(template, "{{ server }}", server.URL)), 0664)

logPath := path.Join(tmpDir, "perf.log")

output := bytes.NewBuffer([]byte{})
updater := NewUpdater(config, tmpDir, tmpDir, "", logPath, output, nil)

err = updater.Run()
assert.Nil(t, err)

perfContent, err := os.ReadFile(logPath)
assert.Nil(t, err)
assert.NotEmpty(t, string(perfContent))
assert.Contains(t, string(perfContent), "http")
assert.Contains(t, string(perfContent), "api_fetch")
}
Loading