Merge pull request #163 from safing/feature/export-improvements

Export Improvements
This commit is contained in:
Daniel 2022-06-22 15:19:15 +02:00 committed by GitHub
commit d32e46aca3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 76 additions and 13 deletions

View file

@ -3,9 +3,11 @@ package api
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"path"
"runtime/debug"
"strings"
"sync"
"time"
@ -226,13 +228,34 @@ func (mh *mainHandler) handle(w http.ResponseWriter, r *http.Request) error {
w.Header().Add("Vary", "Origin")
}
// Handle request.
if handler != nil {
handler.ServeHTTP(lrw, r)
} else {
// Check if we have a handler.
if handler == nil {
http.Error(lrw, "Not found.", http.StatusNotFound)
return nil
}
// Format panics in handler.
defer func() {
if panicValue := recover(); panicValue != nil {
if devMode() {
http.Error(
lrw,
fmt.Sprintf(
"Internal Server Error: %s\n\n%s",
panicValue,
debug.Stack(),
),
http.StatusInternalServerError,
)
} else {
http.Error(lrw, "Internal Server Error.", http.StatusInternalServerError)
}
}
}()
// Handle with registered handler.
handler.ServeHTTP(lrw, r)
return nil
}

View file

@ -112,3 +112,22 @@ func AddToDebugInfo(di *debug.Info) {
lines...,
)
}
// GetActiveConfigValues returns a map with the active config values.
func GetActiveConfigValues() map[string]interface{} {
values := make(map[string]interface{})
// Collect active values from options.
_ = ForEachOption(func(opt *Option) error {
opt.Lock()
defer opt.Unlock()
if opt.ReleaseLevel <= getReleaseLevel() && opt.activeValue != nil {
values[opt.Key] = opt.activeValue.getData(opt)
}
return nil
})
return values
}

View file

@ -76,12 +76,12 @@ func (reg *Registry) Migrate(ctx context.Context) (err error) {
defer reg.lock.Unlock()
start := time.Now()
log.Infof("[migration] migration of %s started", reg.key)
log.Infof("migration: migration of %s started", reg.key)
defer func() {
if err != nil {
log.Errorf("[migration] migration of %s failed after %s: %s", reg.key, time.Since(start), err)
log.Errorf("migration: migration of %s failed after %s: %s", reg.key, time.Since(start), err)
} else {
log.Infof("[migration] migration of %s finished after %s", reg.key, time.Since(start))
log.Infof("migration: migration of %s finished after %s", reg.key, time.Since(start))
}
}()
@ -114,7 +114,7 @@ func (reg *Registry) Migrate(ctx context.Context) (err error) {
if err := m.MigrateFunc(migrationCtx, lastAppliedMigration, target, db); err != nil {
diag.Wrapped = err
diag.FailedMigration = m.Description
tracer.Infof("[migration] applied migration for %s: %s - %s", reg.key, target.String(), m.Description)
tracer.Infof("migration: applied migration for %s: %s - %s", reg.key, target.String(), m.Description)
tracer.Submit()
return diag
}
@ -127,7 +127,7 @@ func (reg *Registry) Migrate(ctx context.Context) (err error) {
diag.Wrapped = err
diag.FailedMigration = m.Description
}
tracer.Infof("[migration] applied migration for %s: %s - %s", reg.key, target.String(), m.Description)
tracer.Infof("migration: applied migration for %s: %s - %s", reg.key, target.String(), m.Description)
tracer.Submit()
}

View file

@ -180,7 +180,7 @@ func (m *Module) Resolve(failureID string) {
// Propagate failure status.
if failureUpdateNotifyFuncReady.IsSet() {
_ = m.RunWorker("failure status updater", func(context.Context) error {
m.StartWorker("failure status updater", func(context.Context) error {
// Only use data in worker that won't change anymore.
failureUpdateNotifyFunc(FailureNone, resolveFailureID, "", "")
return nil

View file

@ -1,7 +1,6 @@
package updater
// Export exports the list of resources. All resources must be
// locked when accessed.
// Export exports the list of resources.
func (reg *ResourceRegistry) Export() map[string]*Resource {
reg.RLock()
defer reg.RUnlock()
@ -9,7 +8,7 @@ func (reg *ResourceRegistry) Export() map[string]*Resource {
// copy the map
copiedResources := make(map[string]*Resource)
for key, val := range reg.resources {
copiedResources[key] = val
copiedResources[key] = val.Export()
}
return copiedResources

View file

@ -117,6 +117,28 @@ func (rv *ResourceVersion) isBetaVersionNumber() bool { //nolint:unused
}
}
// Export makes a copy of the resource with only the exposed information.
// Attributes are copied and safe to access.
// Any ResourceVersion must not be modified.
func (res *Resource) Export() *Resource {
res.Lock()
defer res.Unlock()
// Copy attibutes.
export := &Resource{
Identifier: res.Identifier,
Versions: make([]*ResourceVersion, len(res.Versions)),
ActiveVersion: res.ActiveVersion,
SelectedVersion: res.SelectedVersion,
}
// Copy Versions slice.
for i := 0; i < len(res.Versions); i++ {
export.Versions[i] = res.Versions[i]
}
return export
}
// Len is the number of elements in the collection.
// It implements sort.Interface for ResourceVersion.
func (res *Resource) Len() int {