Like Prometheus, but for logs.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
loki/operator/internal/manifests/storage/schema.go

67 lines
1.8 KiB

package storage
import (
"sort"
"time"
"github.com/ViaQ/logerr/v2/kverrors"
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
)
// BuildSchemaConfig creates a list of schemas to be used to configure
// the storage schemas for the cluster. This method assumes that the following
// validation has been done to the statuses and specs:
//
// 1. All EffectiveDate fields are able to be parsed
// 2. All EffectiveDate fields are unique in their respective list
//
func BuildSchemaConfig(
utcTime time.Time,
spec lokiv1.ObjectStorageSpec,
status lokiv1.LokiStackStorageStatus,
) ([]lokiv1.ObjectStorageSchema, error) {
if len(spec.Schemas) == 0 {
return nil, kverrors.New("spec does not contain any schemas")
}
errors := spec.ValidateSchemas(utcTime, status)
if len(errors) != 0 {
return nil, kverrors.Wrap(errors[0], "spec contains invalid schema entry")
}
schemas := buildSchemas(spec.Schemas)
return schemas, nil
}
// buildSchemas creates a sorted and reduced list of schemaConfigs
func buildSchemas(schemas []lokiv1.ObjectStorageSchema) []lokiv1.ObjectStorageSchema {
sortedSchemas := make([]lokiv1.ObjectStorageSchema, len(schemas))
copy(sortedSchemas, schemas)
sort.SliceStable(sortedSchemas, func(i, j int) bool {
iDate, _ := sortedSchemas[i].EffectiveDate.UTCTime()
jDate, _ := sortedSchemas[j].EffectiveDate.UTCTime()
return iDate.Before(jDate)
})
return reduceSortedSchemas(sortedSchemas)
}
// reduceSortedSchemas returns a list of schemas that have removed redundant entries.
func reduceSortedSchemas(schemas []lokiv1.ObjectStorageSchema) []lokiv1.ObjectStorageSchema {
version := ""
reduced := []lokiv1.ObjectStorageSchema{}
for _, schema := range schemas {
strSchemaVersion := string(schema.Version)
if version != strSchemaVersion {
version = strSchemaVersion
reduced = append(reduced, schema)
}
}
return reduced
}