2019-10-23 17:09:04 +00:00
|
|
|
package pkger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-12-03 18:22:59 +00:00
|
|
|
"github.com/BurntSushi/toml"
|
2019-11-01 18:11:42 +00:00
|
|
|
"github.com/influxdata/influxdb"
|
2019-10-23 17:09:04 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReaderFn is used for functional inputs to abstract the individual
|
|
|
|
// entrypoints for the reader itself.
|
|
|
|
type ReaderFn func() (io.Reader, error)
|
|
|
|
|
|
|
|
// Encoding describes the encoding for the raw package data. The
|
|
|
|
// encoding determines how the raw data is parsed.
|
|
|
|
type Encoding int
|
|
|
|
|
|
|
|
// encoding types
|
|
|
|
const (
|
2019-11-05 01:40:42 +00:00
|
|
|
EncodingUnknown Encoding = iota
|
|
|
|
EncodingYAML
|
2019-10-23 17:09:04 +00:00
|
|
|
EncodingJSON
|
|
|
|
)
|
|
|
|
|
2019-11-05 01:40:42 +00:00
|
|
|
// String provides the string representation of the encoding.
|
|
|
|
func (e Encoding) String() string {
|
|
|
|
switch e {
|
|
|
|
case EncodingJSON:
|
|
|
|
return "json"
|
|
|
|
case EncodingYAML:
|
|
|
|
return "yaml"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 17:55:13 +00:00
|
|
|
// ErrInvalidEncoding indicates the encoding is invalid type for the parser.
|
|
|
|
var ErrInvalidEncoding = errors.New("invalid encoding provided")
|
|
|
|
|
2019-10-23 17:09:04 +00:00
|
|
|
// Parse parses a pkg defined by the encoding and readerFns. As of writing this
|
|
|
|
// we can parse both a YAML and JSON format of the Pkg model.
|
2019-11-18 18:50:45 +00:00
|
|
|
func Parse(encoding Encoding, readerFn ReaderFn, opts ...ValidateOptFn) (*Pkg, error) {
|
2019-10-23 17:09:04 +00:00
|
|
|
r, err := readerFn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch encoding {
|
|
|
|
case EncodingYAML:
|
2019-11-18 18:50:45 +00:00
|
|
|
return parseYAML(r, opts...)
|
2019-10-23 17:09:04 +00:00
|
|
|
case EncodingJSON:
|
2019-11-18 18:50:45 +00:00
|
|
|
return parseJSON(r, opts...)
|
2019-10-23 17:09:04 +00:00
|
|
|
default:
|
2019-10-30 17:55:13 +00:00
|
|
|
return nil, ErrInvalidEncoding
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromFile reads a file from disk and provides a reader from it.
|
|
|
|
func FromFile(filePath string) ReaderFn {
|
|
|
|
return func() (io.Reader, error) {
|
|
|
|
// not using os.Open to avoid having to deal with closing the file in here
|
|
|
|
b, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bytes.NewBuffer(b), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromReader simply passes the reader along. Useful when consuming
|
|
|
|
// this from an HTTP request body. There are a number of other useful
|
|
|
|
// places for this functional input.
|
|
|
|
func FromReader(r io.Reader) ReaderFn {
|
|
|
|
return func() (io.Reader, error) {
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromString parses a pkg from a raw string value. This is very useful
|
|
|
|
// in tests.
|
|
|
|
func FromString(s string) ReaderFn {
|
|
|
|
return func() (io.Reader, error) {
|
|
|
|
return strings.NewReader(s), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 18:50:45 +00:00
|
|
|
func parseYAML(r io.Reader, opts ...ValidateOptFn) (*Pkg, error) {
|
|
|
|
return parse(yaml.NewDecoder(r), opts...)
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-18 18:50:45 +00:00
|
|
|
func parseJSON(r io.Reader, opts ...ValidateOptFn) (*Pkg, error) {
|
|
|
|
return parse(json.NewDecoder(r), opts...)
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type decoder interface {
|
|
|
|
Decode(interface{}) error
|
|
|
|
}
|
|
|
|
|
2019-11-18 18:50:45 +00:00
|
|
|
func parse(dec decoder, opts ...ValidateOptFn) (*Pkg, error) {
|
2019-10-23 17:09:04 +00:00
|
|
|
var pkg Pkg
|
2019-10-30 17:55:13 +00:00
|
|
|
if err := dec.Decode(&pkg); err != nil {
|
2019-10-23 17:09:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-18 18:50:45 +00:00
|
|
|
if err := pkg.Validate(opts...); err != nil {
|
2019-11-05 01:40:42 +00:00
|
|
|
return nil, err
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &pkg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pkg is the model for a package. The resources are more generic that one might
|
|
|
|
// expect at first glance. This was done on purpose. The way json/yaml/toml or
|
|
|
|
// w/e scripting you want to use, can have very different ways of parsing. The
|
|
|
|
// different parsers are limited for the parsers that do not come from the std
|
|
|
|
// lib (looking at you yaml/v2). This allows us to parse it and leave the matching
|
|
|
|
// to another power, the graphing of the package is handled within itself.
|
|
|
|
type Pkg struct {
|
|
|
|
APIVersion string `yaml:"apiVersion" json:"apiVersion"`
|
2019-11-21 00:38:12 +00:00
|
|
|
Kind Kind `yaml:"kind" json:"kind"`
|
2019-10-23 17:09:04 +00:00
|
|
|
Metadata Metadata `yaml:"meta" json:"meta"`
|
|
|
|
Spec struct {
|
|
|
|
Resources []Resource `yaml:"resources" json:"resources"`
|
|
|
|
} `yaml:"spec" json:"spec"`
|
|
|
|
|
2019-12-06 07:05:32 +00:00
|
|
|
mLabels map[string]*label
|
|
|
|
mBuckets map[string]*bucket
|
|
|
|
mDashboards []*dashboard
|
|
|
|
mNotificationEndpoints map[string]*notificationEndpoint
|
|
|
|
mTelegrafs []*telegraf
|
|
|
|
mVariables map[string]*variable
|
2019-10-28 22:23:40 +00:00
|
|
|
|
2019-11-06 18:02:45 +00:00
|
|
|
isVerified bool // dry run has verified pkg resources with existing resources
|
|
|
|
isParsed bool // indicates the pkg has been parsed and all resources graphed accordingly
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 21:13:42 +00:00
|
|
|
// Summary returns a package Summary that describes all the resources and
|
2019-10-23 17:09:04 +00:00
|
|
|
// associations the pkg contains. It is very useful for informing users of
|
|
|
|
// the changes that will take place when this pkg would be applied.
|
2019-10-26 02:11:47 +00:00
|
|
|
func (p *Pkg) Summary() Summary {
|
2019-10-23 17:09:04 +00:00
|
|
|
var sum Summary
|
|
|
|
|
2019-10-30 21:13:42 +00:00
|
|
|
for _, b := range p.buckets() {
|
2019-10-28 22:23:40 +00:00
|
|
|
sum.Buckets = append(sum.Buckets, b.summarize())
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
2019-10-30 21:13:42 +00:00
|
|
|
|
|
|
|
for _, d := range p.dashboards() {
|
|
|
|
sum.Dashboards = append(sum.Dashboards, d.summarize())
|
|
|
|
}
|
2019-10-23 17:09:04 +00:00
|
|
|
|
2019-11-06 22:41:06 +00:00
|
|
|
for _, l := range p.labels() {
|
|
|
|
sum.Labels = append(sum.Labels, l.summarize())
|
|
|
|
}
|
|
|
|
|
2019-10-28 22:23:40 +00:00
|
|
|
for _, m := range p.labelMappings() {
|
2019-10-30 17:55:13 +00:00
|
|
|
sum.LabelMappings = append(sum.LabelMappings, SummaryLabelMapping{
|
2019-10-28 22:23:40 +00:00
|
|
|
ResourceName: m.ResourceName,
|
|
|
|
LabelName: m.LabelName,
|
|
|
|
LabelMapping: m.LabelMapping,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-06 07:05:32 +00:00
|
|
|
for _, n := range p.notificationEndpoints() {
|
|
|
|
sum.NotificationEndpoints = append(sum.NotificationEndpoints, n.summarize())
|
|
|
|
}
|
|
|
|
|
2019-12-03 18:22:59 +00:00
|
|
|
for _, t := range p.telegrafs() {
|
|
|
|
sum.TelegrafConfigs = append(sum.TelegrafConfigs, t.summarize())
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:41:06 +00:00
|
|
|
for _, v := range p.variables() {
|
|
|
|
sum.Variables = append(sum.Variables, v.summarize())
|
|
|
|
}
|
|
|
|
|
2019-10-23 17:09:04 +00:00
|
|
|
return sum
|
|
|
|
}
|
|
|
|
|
2019-11-09 02:12:48 +00:00
|
|
|
type (
|
|
|
|
validateOpt struct {
|
|
|
|
minResources bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateOptFn provides a means to disable desired validation checks.
|
|
|
|
ValidateOptFn func(*validateOpt)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ValidWithoutResources ignores the validation check for minimum number
|
|
|
|
// of resources. This is useful for the service Create to ignore this and
|
|
|
|
// allow the creation of a pkg without resources.
|
|
|
|
func ValidWithoutResources() ValidateOptFn {
|
|
|
|
return func(opt *validateOpt) {
|
|
|
|
opt.minResources = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 01:40:42 +00:00
|
|
|
// Validate will graph all resources and validate every thing is in a useful form.
|
2019-11-09 02:12:48 +00:00
|
|
|
func (p *Pkg) Validate(opts ...ValidateOptFn) error {
|
|
|
|
opt := &validateOpt{minResources: true}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(opt)
|
|
|
|
}
|
2019-11-05 01:40:42 +00:00
|
|
|
setupFns := []func() error{
|
|
|
|
p.validMetadata,
|
|
|
|
}
|
2019-11-09 02:12:48 +00:00
|
|
|
if opt.minResources {
|
|
|
|
setupFns = append(setupFns, p.validResources)
|
|
|
|
}
|
|
|
|
setupFns = append(setupFns, p.graphResources)
|
2019-11-05 01:40:42 +00:00
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
var pErr parseErr
|
2019-11-05 01:40:42 +00:00
|
|
|
for _, fn := range setupFns {
|
|
|
|
if err := fn(); err != nil {
|
2019-11-22 01:07:12 +00:00
|
|
|
if IsParseErr(err) {
|
|
|
|
pErr.append(err.(*parseErr).Resources...)
|
|
|
|
continue
|
|
|
|
}
|
2019-11-05 01:40:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 18:02:45 +00:00
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
if len(pErr.Resources) > 0 {
|
|
|
|
return &pErr
|
|
|
|
}
|
|
|
|
|
2019-11-06 18:02:45 +00:00
|
|
|
p.isParsed = true
|
2019-11-05 01:40:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-26 02:11:47 +00:00
|
|
|
func (p *Pkg) buckets() []*bucket {
|
|
|
|
buckets := make([]*bucket, 0, len(p.mBuckets))
|
|
|
|
for _, b := range p.mBuckets {
|
2019-10-23 17:09:04 +00:00
|
|
|
buckets = append(buckets, b)
|
|
|
|
}
|
|
|
|
|
2019-12-03 02:05:10 +00:00
|
|
|
sort.Slice(buckets, func(i, j int) bool { return buckets[i].name < buckets[j].name })
|
2019-10-23 17:09:04 +00:00
|
|
|
|
|
|
|
return buckets
|
|
|
|
}
|
|
|
|
|
2019-10-26 02:11:47 +00:00
|
|
|
func (p *Pkg) labels() []*label {
|
2019-12-06 00:53:00 +00:00
|
|
|
labels := make(sortedLabels, 0, len(p.mLabels))
|
2019-10-26 02:11:47 +00:00
|
|
|
for _, b := range p.mLabels {
|
2019-10-24 23:59:01 +00:00
|
|
|
labels = append(labels, b)
|
|
|
|
}
|
|
|
|
|
2019-12-03 02:05:10 +00:00
|
|
|
sort.Sort(labels)
|
2019-10-24 23:59:01 +00:00
|
|
|
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
2019-10-30 21:13:42 +00:00
|
|
|
func (p *Pkg) dashboards() []*dashboard {
|
2019-12-03 02:05:10 +00:00
|
|
|
dashes := p.mDashboards[:]
|
|
|
|
sort.Slice(dashes, func(i, j int) bool { return dashes[i].name < dashes[j].name })
|
2019-10-30 21:13:42 +00:00
|
|
|
return dashes
|
|
|
|
}
|
|
|
|
|
2019-12-06 07:05:32 +00:00
|
|
|
func (p *Pkg) notificationEndpoints() []*notificationEndpoint {
|
|
|
|
endpoints := make([]*notificationEndpoint, 0, len(p.mNotificationEndpoints))
|
|
|
|
for _, e := range p.mNotificationEndpoints {
|
|
|
|
endpoints = append(endpoints, e)
|
|
|
|
}
|
|
|
|
sort.Slice(endpoints, func(i, j int) bool {
|
|
|
|
ei, ej := endpoints[i], endpoints[j]
|
|
|
|
if ei.kind == ej.kind {
|
|
|
|
return ei.Name() < ej.Name()
|
|
|
|
}
|
|
|
|
return ei.kind < ej.kind
|
|
|
|
})
|
|
|
|
return endpoints
|
|
|
|
}
|
|
|
|
|
2019-12-03 18:22:59 +00:00
|
|
|
func (p *Pkg) telegrafs() []*telegraf {
|
|
|
|
teles := p.mTelegrafs[:]
|
|
|
|
sort.Slice(teles, func(i, j int) bool { return teles[i].Name() < teles[j].Name() })
|
|
|
|
return teles
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:41:06 +00:00
|
|
|
func (p *Pkg) variables() []*variable {
|
|
|
|
vars := make([]*variable, 0, len(p.mVariables))
|
|
|
|
for _, v := range p.mVariables {
|
|
|
|
vars = append(vars, v)
|
|
|
|
}
|
|
|
|
|
2019-12-03 02:05:10 +00:00
|
|
|
sort.Slice(vars, func(i, j int) bool { return vars[i].name < vars[j].name })
|
2019-11-06 22:41:06 +00:00
|
|
|
|
|
|
|
return vars
|
|
|
|
}
|
|
|
|
|
2019-10-26 02:11:47 +00:00
|
|
|
// labelMappings returns the mappings that will be created for
|
|
|
|
// valid pairs of labels and resources of which all have IDs.
|
|
|
|
// If a resource does not exist yet, a label mapping will not
|
|
|
|
// be returned for it.
|
2019-10-30 17:55:13 +00:00
|
|
|
func (p *Pkg) labelMappings() []SummaryLabelMapping {
|
|
|
|
var mappings []SummaryLabelMapping
|
2019-10-28 22:23:40 +00:00
|
|
|
for _, l := range p.mLabels {
|
|
|
|
mappings = append(mappings, l.mappingSummary()...)
|
2019-10-26 02:11:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 21:13:42 +00:00
|
|
|
// sort by res type ASC, then res name ASC, then label name ASC
|
|
|
|
sort.Slice(mappings, func(i, j int) bool {
|
|
|
|
n, m := mappings[i], mappings[j]
|
|
|
|
if n.ResourceType < m.ResourceType {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if n.ResourceType > m.ResourceType {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if n.ResourceName < m.ResourceName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if n.ResourceName > m.ResourceName {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return n.LabelName < m.LabelName
|
|
|
|
})
|
|
|
|
|
2019-10-26 02:11:47 +00:00
|
|
|
return mappings
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pkg) validMetadata() error {
|
2019-11-22 01:07:12 +00:00
|
|
|
var failures []validationErr
|
2019-11-08 19:33:41 +00:00
|
|
|
if p.APIVersion != APIVersion {
|
2019-11-22 01:07:12 +00:00
|
|
|
failures = append(failures, validationErr{
|
2019-10-26 02:11:47 +00:00
|
|
|
Field: "apiVersion",
|
2019-11-08 19:33:41 +00:00
|
|
|
Msg: "must be version " + APIVersion,
|
2019-10-23 17:09:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-21 00:38:12 +00:00
|
|
|
if !p.Kind.is(KindPackage) {
|
2019-11-22 01:07:12 +00:00
|
|
|
failures = append(failures, validationErr{
|
2019-10-26 02:11:47 +00:00
|
|
|
Field: "kind",
|
|
|
|
Msg: `must be of kind "Package"`,
|
2019-10-23 17:09:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
var metaFails []validationErr
|
2019-10-26 02:11:47 +00:00
|
|
|
if p.Metadata.Version == "" {
|
2019-11-22 01:07:12 +00:00
|
|
|
metaFails = append(metaFails, validationErr{
|
2019-11-14 00:24:05 +00:00
|
|
|
Field: "pkgVersion",
|
2019-10-26 02:11:47 +00:00
|
|
|
Msg: "version is required",
|
2019-10-23 17:09:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-26 02:11:47 +00:00
|
|
|
if p.Metadata.Name == "" {
|
2019-11-22 01:07:12 +00:00
|
|
|
metaFails = append(metaFails, validationErr{
|
2019-11-14 00:24:05 +00:00
|
|
|
Field: "pkgName",
|
2019-10-26 02:11:47 +00:00
|
|
|
Msg: "must be at least 1 char",
|
2019-10-23 17:09:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-14 00:24:05 +00:00
|
|
|
if len(metaFails) > 0 {
|
2019-11-22 01:07:12 +00:00
|
|
|
failures = append(failures, validationErr{
|
2019-11-14 00:24:05 +00:00
|
|
|
Field: "meta",
|
|
|
|
Nested: metaFails,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-23 17:09:04 +00:00
|
|
|
if len(failures) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
var err parseErr
|
|
|
|
err.append(resourceErr{
|
|
|
|
Kind: KindPackage.String(),
|
|
|
|
RootErrs: failures,
|
2019-11-14 00:24:05 +00:00
|
|
|
})
|
2019-10-23 17:09:04 +00:00
|
|
|
return &err
|
|
|
|
}
|
|
|
|
|
2019-10-30 17:55:13 +00:00
|
|
|
func (p *Pkg) validResources() error {
|
|
|
|
if len(p.Spec.Resources) > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
res := resourceErr{
|
2019-11-01 18:11:42 +00:00
|
|
|
Kind: "Package",
|
2019-11-22 01:07:12 +00:00
|
|
|
RootErrs: []validationErr{{
|
|
|
|
Field: "resources",
|
|
|
|
Msg: "at least 1 resource must be provided",
|
|
|
|
}},
|
2019-10-30 17:55:13 +00:00
|
|
|
}
|
2019-11-22 01:07:12 +00:00
|
|
|
var err parseErr
|
2019-10-30 17:55:13 +00:00
|
|
|
err.append(res)
|
|
|
|
return &err
|
|
|
|
}
|
|
|
|
|
2019-10-26 02:11:47 +00:00
|
|
|
func (p *Pkg) graphResources() error {
|
2019-12-06 07:05:32 +00:00
|
|
|
graphFns := []func() *parseErr{
|
|
|
|
// labels are first, this is to validate associations with other resources
|
2019-10-26 02:11:47 +00:00
|
|
|
p.graphLabels,
|
2019-11-06 22:41:06 +00:00
|
|
|
p.graphVariables,
|
2019-10-26 02:11:47 +00:00
|
|
|
p.graphBuckets,
|
2019-10-30 21:13:42 +00:00
|
|
|
p.graphDashboards,
|
2019-12-06 07:05:32 +00:00
|
|
|
p.graphNotificationEndpoints,
|
2019-12-03 18:22:59 +00:00
|
|
|
p.graphTelegrafs,
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
var pErr parseErr
|
2019-10-23 17:09:04 +00:00
|
|
|
for _, fn := range graphFns {
|
|
|
|
if err := fn(); err != nil {
|
2019-12-06 07:05:32 +00:00
|
|
|
pErr.append(err.Resources...)
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
if len(pErr.Resources) > 0 {
|
|
|
|
sort.Slice(pErr.Resources, func(i, j int) bool {
|
|
|
|
ir, jr := pErr.Resources[i], pErr.Resources[j]
|
|
|
|
return *ir.Idx < *jr.Idx
|
2019-11-14 00:24:05 +00:00
|
|
|
})
|
2019-11-22 01:07:12 +00:00
|
|
|
return &pErr
|
2019-11-14 00:24:05 +00:00
|
|
|
}
|
|
|
|
|
2019-10-23 17:09:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-06 07:05:32 +00:00
|
|
|
func (p *Pkg) graphBuckets() *parseErr {
|
2019-10-26 02:11:47 +00:00
|
|
|
p.mBuckets = make(map[string]*bucket)
|
2019-12-03 02:05:10 +00:00
|
|
|
return p.eachResource(KindBucket, 2, func(r Resource) []validationErr {
|
2019-10-26 02:11:47 +00:00
|
|
|
if _, ok := p.mBuckets[r.Name()]; ok {
|
2019-11-22 01:07:12 +00:00
|
|
|
return []validationErr{{
|
2019-10-26 02:11:47 +00:00
|
|
|
Field: "name",
|
|
|
|
Msg: "duplicate name: " + r.Name(),
|
|
|
|
}}
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
2019-10-26 02:11:47 +00:00
|
|
|
|
|
|
|
bkt := &bucket{
|
2019-12-03 02:05:10 +00:00
|
|
|
name: r.Name(),
|
2019-11-22 18:41:08 +00:00
|
|
|
Description: r.stringShort(fieldDescription),
|
|
|
|
}
|
|
|
|
if rules, ok := r[fieldBucketRetentionRules].(retentionRules); ok {
|
|
|
|
bkt.RetentionRules = rules
|
|
|
|
} else {
|
|
|
|
for _, r := range r.slcResource(fieldBucketRetentionRules) {
|
|
|
|
bkt.RetentionRules = append(bkt.RetentionRules, retentionRule{
|
|
|
|
Type: r.stringShort(fieldType),
|
|
|
|
Seconds: r.intShort(fieldRetentionRulesEverySeconds),
|
|
|
|
})
|
|
|
|
}
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 18:11:42 +00:00
|
|
|
failures := p.parseNestedLabels(r, func(l *label) error {
|
2019-10-30 21:13:42 +00:00
|
|
|
bkt.labels = append(bkt.labels, l)
|
2019-12-03 02:05:10 +00:00
|
|
|
p.mLabels[l.Name()].setMapping(bkt, false)
|
2019-10-30 21:13:42 +00:00
|
|
|
return nil
|
|
|
|
})
|
2019-12-03 02:05:10 +00:00
|
|
|
sort.Sort(bkt.labels)
|
2019-10-26 02:11:47 +00:00
|
|
|
|
|
|
|
p.mBuckets[r.Name()] = bkt
|
|
|
|
|
2019-11-22 18:41:08 +00:00
|
|
|
return append(failures, bkt.valid()...)
|
2019-10-23 17:09:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-06 07:05:32 +00:00
|
|
|
func (p *Pkg) graphLabels() *parseErr {
|
2019-10-26 02:11:47 +00:00
|
|
|
p.mLabels = make(map[string]*label)
|
2019-12-03 02:05:10 +00:00
|
|
|
return p.eachResource(KindLabel, 2, func(r Resource) []validationErr {
|
2019-10-26 02:11:47 +00:00
|
|
|
if _, ok := p.mLabels[r.Name()]; ok {
|
2019-11-22 01:07:12 +00:00
|
|
|
return []validationErr{{
|
2019-10-26 02:11:47 +00:00
|
|
|
Field: "name",
|
|
|
|
Msg: "duplicate name: " + r.Name(),
|
|
|
|
}}
|
2019-10-24 23:59:01 +00:00
|
|
|
}
|
2019-10-26 02:11:47 +00:00
|
|
|
p.mLabels[r.Name()] = &label{
|
2019-12-03 02:05:10 +00:00
|
|
|
name: r.Name(),
|
2019-11-08 19:33:41 +00:00
|
|
|
Color: r.stringShort(fieldLabelColor),
|
|
|
|
Description: r.stringShort(fieldDescription),
|
2019-10-24 23:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-06 07:05:32 +00:00
|
|
|
func (p *Pkg) graphDashboards() *parseErr {
|
2019-12-03 02:05:10 +00:00
|
|
|
p.mDashboards = make([]*dashboard, 0)
|
|
|
|
return p.eachResource(KindDashboard, 2, func(r Resource) []validationErr {
|
2019-10-30 21:13:42 +00:00
|
|
|
dash := &dashboard{
|
2019-12-03 02:05:10 +00:00
|
|
|
name: r.Name(),
|
2019-11-08 19:33:41 +00:00
|
|
|
Description: r.stringShort(fieldDescription),
|
2019-10-30 21:13:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 18:11:42 +00:00
|
|
|
failures := p.parseNestedLabels(r, func(l *label) error {
|
2019-10-30 21:13:42 +00:00
|
|
|
dash.labels = append(dash.labels, l)
|
2019-12-03 02:05:10 +00:00
|
|
|
p.mLabels[l.Name()].setMapping(dash, false)
|
2019-10-30 21:13:42 +00:00
|
|
|
return nil
|
|
|
|
})
|
2019-12-03 02:05:10 +00:00
|
|
|
sort.Sort(dash.labels)
|
2019-10-30 21:13:42 +00:00
|
|
|
|
2019-11-08 19:33:41 +00:00
|
|
|
for i, cr := range r.slcResource(fieldDashCharts) {
|
2019-11-01 18:11:42 +00:00
|
|
|
ch, fails := parseChart(cr)
|
|
|
|
if fails != nil {
|
2019-11-22 01:07:12 +00:00
|
|
|
failures = append(failures, validationErr{
|
2019-11-14 00:24:05 +00:00
|
|
|
Field: "charts",
|
|
|
|
Index: intPtr(i),
|
|
|
|
Nested: fails,
|
|
|
|
})
|
2019-11-01 18:11:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
dash.Charts = append(dash.Charts, ch)
|
|
|
|
}
|
|
|
|
|
2019-12-03 02:05:10 +00:00
|
|
|
p.mDashboards = append(p.mDashboards, dash)
|
2019-10-30 21:13:42 +00:00
|
|
|
|
2019-11-14 00:43:28 +00:00
|
|
|
return failures
|
2019-10-30 21:13:42 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-06 07:05:32 +00:00
|
|
|
func (p *Pkg) graphNotificationEndpoints() *parseErr {
|
|
|
|
p.mNotificationEndpoints = make(map[string]*notificationEndpoint)
|
|
|
|
|
|
|
|
notificationKinds := []struct {
|
|
|
|
kind Kind
|
|
|
|
notificationKind notificationKind
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
kind: KindNotificationEndpointHTTP,
|
|
|
|
notificationKind: notificationKindHTTP,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
kind: KindNotificationEndpointPagerDuty,
|
|
|
|
notificationKind: notificationKindPagerDuty,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
kind: KindNotificationEndpointSlack,
|
|
|
|
notificationKind: notificationKindSlack,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var pErr parseErr
|
|
|
|
for _, nk := range notificationKinds {
|
|
|
|
err := p.eachResource(nk.kind, 1, func(r Resource) []validationErr {
|
|
|
|
if _, ok := p.mNotificationEndpoints[r.Name()]; ok {
|
|
|
|
return []validationErr{{
|
|
|
|
Field: "name",
|
|
|
|
Msg: "duplicate name: " + r.Name(),
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint := ¬ificationEndpoint{
|
|
|
|
kind: nk.notificationKind,
|
|
|
|
name: r.Name(),
|
|
|
|
description: r.stringShort(fieldDescription),
|
|
|
|
httpType: strings.ToLower(r.stringShort(fieldType)),
|
|
|
|
password: r.stringShort(fieldNotificationEndpointPassword),
|
|
|
|
routingKey: r.stringShort(fieldNotificationEndpointRoutingKey),
|
|
|
|
status: strings.ToLower(r.stringShort(fieldStatus)),
|
|
|
|
token: r.stringShort(fieldNotificationEndpointToken),
|
|
|
|
url: r.stringShort(fieldNotificationEndpointURL),
|
|
|
|
username: r.stringShort(fieldNotificationEndpointUsername),
|
|
|
|
}
|
|
|
|
failures := p.parseNestedLabels(r, func(l *label) error {
|
|
|
|
endpoint.labels = append(endpoint.labels, l)
|
|
|
|
p.mLabels[l.Name()].setMapping(endpoint, false)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
sort.Sort(endpoint.labels)
|
|
|
|
|
|
|
|
p.mNotificationEndpoints[endpoint.Name()] = endpoint
|
|
|
|
return append(failures, endpoint.valid()...)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
pErr.append(err.Resources...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(pErr.Resources) > 0 {
|
|
|
|
return &pErr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pkg) graphVariables() *parseErr {
|
2019-11-06 22:41:06 +00:00
|
|
|
p.mVariables = make(map[string]*variable)
|
2019-12-03 02:05:10 +00:00
|
|
|
return p.eachResource(KindVariable, 1, func(r Resource) []validationErr {
|
2019-11-06 22:41:06 +00:00
|
|
|
if _, ok := p.mVariables[r.Name()]; ok {
|
2019-11-22 01:07:12 +00:00
|
|
|
return []validationErr{{
|
2019-11-06 22:41:06 +00:00
|
|
|
Field: "name",
|
|
|
|
Msg: "duplicate name: " + r.Name(),
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
newVar := &variable{
|
2019-12-03 02:05:10 +00:00
|
|
|
name: r.Name(),
|
2019-11-08 19:33:41 +00:00
|
|
|
Description: r.stringShort(fieldDescription),
|
|
|
|
Type: strings.ToLower(r.stringShort(fieldType)),
|
|
|
|
Query: strings.TrimSpace(r.stringShort(fieldQuery)),
|
2019-11-22 18:41:08 +00:00
|
|
|
Language: strings.ToLower(strings.TrimSpace(r.stringShort(fieldLanguage))),
|
2019-11-08 19:33:41 +00:00
|
|
|
ConstValues: r.slcStr(fieldValues),
|
|
|
|
MapValues: r.mapStrStr(fieldValues),
|
2019-11-06 22:41:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 00:45:00 +00:00
|
|
|
failures := p.parseNestedLabels(r, func(l *label) error {
|
|
|
|
newVar.labels = append(newVar.labels, l)
|
2019-12-03 02:05:10 +00:00
|
|
|
p.mLabels[l.Name()].setMapping(newVar, false)
|
|
|
|
//p.mLabels[l.Name()].setVariableMapping(newVar, false)
|
2019-11-07 00:45:00 +00:00
|
|
|
return nil
|
|
|
|
})
|
2019-12-03 02:05:10 +00:00
|
|
|
sort.Sort(newVar.labels)
|
2019-11-07 00:45:00 +00:00
|
|
|
|
2019-11-06 22:41:06 +00:00
|
|
|
p.mVariables[r.Name()] = newVar
|
|
|
|
|
2019-11-07 00:45:00 +00:00
|
|
|
return append(failures, newVar.valid()...)
|
2019-11-06 22:41:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-06 07:05:32 +00:00
|
|
|
func (p *Pkg) graphTelegrafs() *parseErr {
|
2019-12-03 18:22:59 +00:00
|
|
|
p.mTelegrafs = make([]*telegraf, 0)
|
|
|
|
return p.eachResource(KindTelegraf, 0, func(r Resource) []validationErr {
|
|
|
|
tele := new(telegraf)
|
2019-12-04 01:00:15 +00:00
|
|
|
tele.config.Name = r.Name()
|
|
|
|
tele.config.Description = r.stringShort(fieldDescription)
|
|
|
|
|
2019-12-03 18:22:59 +00:00
|
|
|
failures := p.parseNestedLabels(r, func(l *label) error {
|
|
|
|
tele.labels = append(tele.labels, l)
|
|
|
|
p.mLabels[l.Name()].setMapping(tele, false)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
sort.Sort(tele.labels)
|
|
|
|
|
|
|
|
cfgBytes := []byte(r.stringShort(fieldTelegrafConfig))
|
|
|
|
if err := toml.Unmarshal(cfgBytes, &tele.config); err != nil {
|
|
|
|
failures = append(failures, validationErr{
|
|
|
|
Field: fieldTelegrafConfig,
|
|
|
|
Msg: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
p.mTelegrafs = append(p.mTelegrafs, tele)
|
|
|
|
|
|
|
|
return failures
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-06 07:05:32 +00:00
|
|
|
func (p *Pkg) eachResource(resourceKind Kind, minNameLen int, fn func(r Resource) []validationErr) *parseErr {
|
2019-11-22 01:07:12 +00:00
|
|
|
var pErr parseErr
|
2019-10-26 02:11:47 +00:00
|
|
|
for i, r := range p.Spec.Resources {
|
2019-10-23 17:09:04 +00:00
|
|
|
k, err := r.kind()
|
|
|
|
if err != nil {
|
2019-11-22 01:07:12 +00:00
|
|
|
pErr.append(resourceErr{
|
2019-11-01 18:11:42 +00:00
|
|
|
Kind: k.String(),
|
2019-11-22 01:07:12 +00:00
|
|
|
Idx: intPtr(i),
|
|
|
|
ValidationErrs: []validationErr{
|
2019-10-23 17:09:04 +00:00
|
|
|
{
|
|
|
|
Field: "kind",
|
|
|
|
Msg: err.Error(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
2019-11-08 19:33:41 +00:00
|
|
|
if !k.is(resourceKind) {
|
2019-10-23 17:09:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-03 02:05:10 +00:00
|
|
|
if len(r.Name()) < minNameLen {
|
|
|
|
pErr.append(resourceErr{
|
|
|
|
Kind: k.String(),
|
|
|
|
Idx: intPtr(i),
|
|
|
|
ValidationErrs: []validationErr{
|
|
|
|
{
|
|
|
|
Field: "name",
|
|
|
|
Msg: fmt.Sprintf("must be a string of at least %d chars in length", minNameLen),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-26 02:11:47 +00:00
|
|
|
if failures := fn(r); failures != nil {
|
2019-11-22 01:07:12 +00:00
|
|
|
err := resourceErr{
|
2019-11-01 18:11:42 +00:00
|
|
|
Kind: resourceKind.String(),
|
2019-11-22 01:07:12 +00:00
|
|
|
Idx: intPtr(i),
|
2019-10-26 02:11:47 +00:00
|
|
|
}
|
|
|
|
for _, f := range failures {
|
2019-11-22 01:07:12 +00:00
|
|
|
vErr := validationErr{
|
2019-11-14 00:24:05 +00:00
|
|
|
Field: f.Field,
|
|
|
|
Msg: f.Msg,
|
|
|
|
Index: f.Index,
|
|
|
|
Nested: f.Nested,
|
|
|
|
}
|
|
|
|
if vErr.Field == "associations" {
|
|
|
|
err.AssociationErrs = append(err.AssociationErrs, vErr)
|
2019-10-26 02:11:47 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-11-14 00:24:05 +00:00
|
|
|
err.ValidationErrs = append(err.ValidationErrs, vErr)
|
2019-10-26 02:11:47 +00:00
|
|
|
}
|
2019-11-22 01:07:12 +00:00
|
|
|
pErr.append(err)
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
if len(pErr.Resources) > 0 {
|
|
|
|
return &pErr
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
func (p *Pkg) parseNestedLabels(r Resource, fn func(lb *label) error) []validationErr {
|
2019-10-30 21:13:42 +00:00
|
|
|
nestedLabels := make(map[string]*label)
|
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
var failures []validationErr
|
2019-11-08 19:33:41 +00:00
|
|
|
for i, nr := range r.slcResource(fieldAssociations) {
|
2019-11-14 00:24:05 +00:00
|
|
|
fail := p.parseNestedLabel(nr, func(l *label) error {
|
2019-12-03 02:05:10 +00:00
|
|
|
if _, ok := nestedLabels[l.Name()]; ok {
|
|
|
|
return fmt.Errorf("duplicate nested label: %q", l.Name())
|
2019-10-30 21:13:42 +00:00
|
|
|
}
|
2019-12-03 02:05:10 +00:00
|
|
|
nestedLabels[l.Name()] = l
|
2019-10-30 21:13:42 +00:00
|
|
|
|
|
|
|
return fn(l)
|
|
|
|
})
|
|
|
|
if fail != nil {
|
2019-11-14 00:24:05 +00:00
|
|
|
fail.Index = intPtr(i)
|
2019-10-30 21:13:42 +00:00
|
|
|
failures = append(failures, *fail)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return failures
|
|
|
|
}
|
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
func (p *Pkg) parseNestedLabel(nr Resource, fn func(lb *label) error) *validationErr {
|
2019-10-26 02:11:47 +00:00
|
|
|
k, err := nr.kind()
|
|
|
|
if err != nil {
|
2019-11-22 01:07:12 +00:00
|
|
|
return &validationErr{
|
2019-11-14 00:24:05 +00:00
|
|
|
Field: "associations",
|
2019-11-22 01:07:12 +00:00
|
|
|
Nested: []validationErr{
|
2019-11-14 00:24:05 +00:00
|
|
|
{
|
|
|
|
Field: "kind",
|
|
|
|
Msg: err.Error(),
|
|
|
|
},
|
|
|
|
},
|
2019-10-26 02:11:47 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-08 19:33:41 +00:00
|
|
|
if !k.is(KindLabel) {
|
2019-10-26 02:11:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lb, found := p.mLabels[nr.Name()]
|
|
|
|
if !found {
|
2019-11-22 01:07:12 +00:00
|
|
|
return &validationErr{
|
2019-11-14 00:24:05 +00:00
|
|
|
Field: "associations",
|
|
|
|
Msg: fmt.Sprintf("label %q does not exist in pkg", nr.Name()),
|
2019-10-26 02:11:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fn(lb); err != nil {
|
2019-11-22 01:07:12 +00:00
|
|
|
return &validationErr{
|
2019-11-14 00:24:05 +00:00
|
|
|
Field: "associations",
|
|
|
|
Msg: err.Error(),
|
2019-10-26 02:11:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
func parseChart(r Resource) (chart, []validationErr) {
|
2019-11-01 18:11:42 +00:00
|
|
|
ck, err := r.chartKind()
|
|
|
|
if err != nil {
|
2019-11-22 01:07:12 +00:00
|
|
|
return chart{}, []validationErr{{
|
2019-11-01 18:11:42 +00:00
|
|
|
Field: "kind",
|
|
|
|
Msg: err.Error(),
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
c := chart{
|
|
|
|
Kind: ck,
|
|
|
|
Name: r.Name(),
|
2019-11-08 19:33:41 +00:00
|
|
|
Prefix: r.stringShort(fieldPrefix),
|
|
|
|
Suffix: r.stringShort(fieldSuffix),
|
|
|
|
Note: r.stringShort(fieldChartNote),
|
|
|
|
NoteOnEmpty: r.boolShort(fieldChartNoteOnEmpty),
|
|
|
|
Shade: r.boolShort(fieldChartShade),
|
|
|
|
XCol: r.stringShort(fieldChartXCol),
|
|
|
|
YCol: r.stringShort(fieldChartYCol),
|
|
|
|
XPos: r.intShort(fieldChartXPos),
|
|
|
|
YPos: r.intShort(fieldChartYPos),
|
|
|
|
Height: r.intShort(fieldChartHeight),
|
|
|
|
Width: r.intShort(fieldChartWidth),
|
|
|
|
Geom: r.stringShort(fieldChartGeom),
|
2019-11-12 20:09:13 +00:00
|
|
|
BinSize: r.intShort(fieldChartBinSize),
|
2019-11-16 20:14:46 +00:00
|
|
|
BinCount: r.intShort(fieldChartBinCount),
|
|
|
|
Position: r.stringShort(fieldChartPosition),
|
2019-11-08 19:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if presLeg, ok := r[fieldChartLegend].(legend); ok {
|
|
|
|
c.Legend = presLeg
|
|
|
|
} else {
|
|
|
|
if leg, ok := ifaceToResource(r[fieldChartLegend]); ok {
|
|
|
|
c.Legend.Type = leg.stringShort(fieldType)
|
|
|
|
c.Legend.Orientation = leg.stringShort(fieldLegendOrientation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dp, ok := r.int(fieldChartDecimalPlaces); ok {
|
2019-11-01 18:11:42 +00:00
|
|
|
c.EnforceDecimals = true
|
|
|
|
c.DecimalPlaces = dp
|
|
|
|
}
|
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
var failures []validationErr
|
2019-11-08 19:33:41 +00:00
|
|
|
if presentQueries, ok := r[fieldChartQueries].(queries); ok {
|
|
|
|
c.Queries = presentQueries
|
|
|
|
} else {
|
|
|
|
for _, rq := range r.slcResource(fieldChartQueries) {
|
|
|
|
c.Queries = append(c.Queries, query{
|
|
|
|
Query: strings.TrimSpace(rq.stringShort(fieldQuery)),
|
|
|
|
})
|
|
|
|
}
|
2019-11-01 18:11:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 19:33:41 +00:00
|
|
|
if presentColors, ok := r[fieldChartColors].(colors); ok {
|
|
|
|
c.Colors = presentColors
|
|
|
|
} else {
|
|
|
|
for _, rc := range r.slcResource(fieldChartColors) {
|
|
|
|
c.Colors = append(c.Colors, &color{
|
|
|
|
// TODO: think we can just axe the stub here
|
|
|
|
id: influxdb.ID(int(time.Now().UnixNano())).String(),
|
|
|
|
Name: rc.Name(),
|
|
|
|
Type: rc.stringShort(fieldType),
|
|
|
|
Hex: rc.stringShort(fieldColorHex),
|
|
|
|
Value: flt64Ptr(rc.float64Short(fieldValue)),
|
|
|
|
})
|
|
|
|
}
|
2019-11-01 18:11:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 19:33:41 +00:00
|
|
|
if presAxes, ok := r[fieldChartAxes].(axes); ok {
|
|
|
|
c.Axes = presAxes
|
|
|
|
} else {
|
|
|
|
for _, ra := range r.slcResource(fieldChartAxes) {
|
2019-11-15 01:05:21 +00:00
|
|
|
domain := []float64{}
|
|
|
|
|
|
|
|
if _, ok := ra[fieldChartDomain]; ok {
|
|
|
|
for _, str := range ra.slcStr(fieldChartDomain) {
|
|
|
|
val, err := strconv.ParseFloat(str, 64)
|
|
|
|
if err != nil {
|
2019-11-22 01:07:12 +00:00
|
|
|
failures = append(failures, validationErr{
|
2019-11-15 01:05:21 +00:00
|
|
|
Field: "axes",
|
|
|
|
Msg: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
domain = append(domain, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:33:41 +00:00
|
|
|
c.Axes = append(c.Axes, axis{
|
|
|
|
Base: ra.stringShort(fieldAxisBase),
|
|
|
|
Label: ra.stringShort(fieldAxisLabel),
|
|
|
|
Name: ra.Name(),
|
|
|
|
Prefix: ra.stringShort(fieldPrefix),
|
|
|
|
Scale: ra.stringShort(fieldAxisScale),
|
|
|
|
Suffix: ra.stringShort(fieldSuffix),
|
2019-11-15 01:05:21 +00:00
|
|
|
Domain: domain,
|
2019-11-08 19:33:41 +00:00
|
|
|
})
|
|
|
|
}
|
2019-11-04 19:16:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 17:17:31 +00:00
|
|
|
if failures = append(failures, c.validProperties()...); len(failures) > 0 {
|
2019-11-01 18:11:42 +00:00
|
|
|
return chart{}, failures
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2019-10-23 17:09:04 +00:00
|
|
|
// Resource is a pkger Resource kind. It can be one of any of
|
|
|
|
// available kinds that are supported.
|
|
|
|
type Resource map[string]interface{}
|
|
|
|
|
2019-11-08 19:33:41 +00:00
|
|
|
// Name returns the name of the resource.
|
2019-11-01 18:11:42 +00:00
|
|
|
func (r Resource) Name() string {
|
2019-11-08 19:33:41 +00:00
|
|
|
return strings.TrimSpace(r.stringShort(fieldName))
|
2019-11-01 18:11:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 19:33:41 +00:00
|
|
|
func (r Resource) kind() (Kind, error) {
|
2019-11-21 00:38:12 +00:00
|
|
|
if k, ok := r[fieldKind].(Kind); ok {
|
|
|
|
return k, k.OK()
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:33:41 +00:00
|
|
|
resKind, ok := r.string(fieldKind)
|
2019-10-23 17:09:04 +00:00
|
|
|
if !ok {
|
2019-11-08 19:33:41 +00:00
|
|
|
return KindUnknown, errors.New("no kind provided")
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 00:38:12 +00:00
|
|
|
k := NewKind(resKind)
|
|
|
|
return k, k.OK()
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 22:08:30 +00:00
|
|
|
func (r Resource) chartKind() (chartKind, error) {
|
2019-11-01 18:11:42 +00:00
|
|
|
ck, _ := r.kind()
|
2019-11-05 22:08:30 +00:00
|
|
|
chartKind := chartKind(ck)
|
2019-11-01 18:11:42 +00:00
|
|
|
if !chartKind.ok() {
|
2019-11-05 22:08:30 +00:00
|
|
|
return chartKindUnknown, errors.New("invalid chart kind provided: " + string(chartKind))
|
2019-11-01 18:11:42 +00:00
|
|
|
}
|
|
|
|
return chartKind, nil
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 18:11:42 +00:00
|
|
|
func (r Resource) bool(key string) (bool, bool) {
|
|
|
|
b, ok := r[key].(bool)
|
|
|
|
return b, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Resource) boolShort(key string) bool {
|
|
|
|
b, _ := r.bool(key)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Resource) float64(key string) (float64, bool) {
|
|
|
|
f, ok := r[key].(float64)
|
|
|
|
if ok {
|
|
|
|
return f, true
|
|
|
|
}
|
|
|
|
|
|
|
|
i, ok := r[key].(int)
|
|
|
|
if ok {
|
|
|
|
return float64(i), true
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Resource) float64Short(key string) float64 {
|
|
|
|
f, _ := r.float64(key)
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Resource) int(key string) (int, bool) {
|
|
|
|
i, ok := r[key].(int)
|
|
|
|
if ok {
|
|
|
|
return i, true
|
|
|
|
}
|
|
|
|
|
|
|
|
f, ok := r[key].(float64)
|
|
|
|
if ok {
|
|
|
|
return int(f), true
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Resource) intShort(key string) int {
|
|
|
|
i, _ := r.int(key)
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2019-10-23 17:09:04 +00:00
|
|
|
func (r Resource) string(key string) (string, bool) {
|
2019-11-06 22:41:06 +00:00
|
|
|
return ifaceToStr(r[key])
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r Resource) stringShort(key string) string {
|
|
|
|
s, _ := r.string(key)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-11-01 18:11:42 +00:00
|
|
|
func (r Resource) slcResource(key string) []Resource {
|
|
|
|
v, ok := r[key]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:33:41 +00:00
|
|
|
if resources, ok := v.([]Resource); ok {
|
|
|
|
return resources
|
|
|
|
}
|
|
|
|
|
2019-11-01 18:11:42 +00:00
|
|
|
iFaceSlc, ok := v.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var newResources []Resource
|
|
|
|
for _, iFace := range iFaceSlc {
|
2019-11-06 22:41:06 +00:00
|
|
|
r, ok := ifaceToResource(iFace)
|
2019-11-01 18:11:42 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newResources = append(newResources, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newResources
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:41:06 +00:00
|
|
|
func (r Resource) slcStr(key string) []string {
|
|
|
|
v, ok := r[key]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:33:41 +00:00
|
|
|
if strSlc, ok := v.([]string); ok {
|
|
|
|
return strSlc
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:41:06 +00:00
|
|
|
iFaceSlc, ok := v.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var out []string
|
|
|
|
for _, iface := range iFaceSlc {
|
|
|
|
s, ok := ifaceToStr(iface)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
out = append(out, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Resource) mapStrStr(key string) map[string]string {
|
2019-11-08 19:33:41 +00:00
|
|
|
v, ok := r[key]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if m, ok := v.(map[string]string); ok {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
res, ok := ifaceToResource(v)
|
2019-11-06 22:41:06 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m := make(map[string]string)
|
|
|
|
for k, v := range res {
|
|
|
|
s, ok := ifaceToStr(v)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m[k] = s
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func ifaceToResource(i interface{}) (Resource, bool) {
|
|
|
|
if i == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:33:41 +00:00
|
|
|
if res, ok := i.(Resource); ok {
|
2019-10-26 02:11:47 +00:00
|
|
|
return res, true
|
|
|
|
}
|
|
|
|
|
|
|
|
if m, ok := i.(map[string]interface{}); ok {
|
|
|
|
return m, true
|
|
|
|
}
|
|
|
|
|
2019-10-23 17:09:04 +00:00
|
|
|
m, ok := i.(map[interface{}]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
newRes := make(Resource)
|
|
|
|
for k, v := range m {
|
|
|
|
s, ok := k.(string)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newRes[s] = v
|
|
|
|
}
|
|
|
|
return newRes, true
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:41:06 +00:00
|
|
|
func ifaceToStr(v interface{}) (string, bool) {
|
|
|
|
if v == nil {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
if s, ok := v.(string); ok {
|
|
|
|
return s, true
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := v.(int); ok {
|
|
|
|
return strconv.Itoa(i), true
|
|
|
|
}
|
|
|
|
|
|
|
|
if f, ok := v.(float64); ok {
|
|
|
|
return strconv.FormatFloat(f, 'f', -1, 64), true
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2019-11-12 20:29:50 +00:00
|
|
|
func uniqResources(resources []Resource) []Resource {
|
|
|
|
type key struct {
|
|
|
|
kind Kind
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
m := make(map[key]bool)
|
|
|
|
|
|
|
|
out := make([]Resource, 0, len(resources))
|
|
|
|
for _, r := range resources {
|
|
|
|
k, err := r.kind()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-21 00:38:12 +00:00
|
|
|
if err := k.OK(); err != nil {
|
2019-11-12 20:29:50 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-11-21 00:38:12 +00:00
|
|
|
switch k {
|
|
|
|
// these 3 kinds are unique, have existing state identifiable by name
|
|
|
|
case KindBucket, KindLabel, KindVariable:
|
|
|
|
rKey := key{kind: k, name: r.Name()}
|
|
|
|
if m[rKey] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m[rKey] = true
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
out = append(out, r)
|
|
|
|
}
|
2019-11-12 20:29:50 +00:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
// ParseError is the error from parsing the given package. The ParseError
|
|
|
|
// behavior provides a list of resources that failed and all validations
|
|
|
|
// that failed for that resource. A resource can multiple errors, and
|
|
|
|
// a parseErr can have multiple resources which themselves can have
|
|
|
|
// multiple validation failures.
|
|
|
|
type ParseError interface {
|
|
|
|
ValidationErrs() []ValidationErr
|
|
|
|
}
|
|
|
|
|
2019-11-14 00:24:05 +00:00
|
|
|
type (
|
2019-11-22 01:07:12 +00:00
|
|
|
parseErr struct {
|
|
|
|
Resources []resourceErr
|
2019-11-14 00:24:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
// resourceErr describes the error for a particular resource. In
|
2019-11-14 00:43:28 +00:00
|
|
|
// which it may have numerous validation and association errors.
|
2019-11-22 01:07:12 +00:00
|
|
|
resourceErr struct {
|
2019-11-01 18:11:42 +00:00
|
|
|
Kind string
|
2019-11-22 01:07:12 +00:00
|
|
|
Idx *int
|
|
|
|
RootErrs []validationErr
|
|
|
|
AssociationErrs []validationErr
|
|
|
|
ValidationErrs []validationErr
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
2019-11-14 00:24:05 +00:00
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
validationErr struct {
|
2019-11-14 00:24:05 +00:00
|
|
|
Field string
|
|
|
|
Msg string
|
|
|
|
Index *int
|
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
Nested []validationErr
|
2019-11-14 00:24:05 +00:00
|
|
|
}
|
|
|
|
)
|
2019-10-23 17:09:04 +00:00
|
|
|
|
|
|
|
// Error implements the error interface.
|
2019-11-22 01:07:12 +00:00
|
|
|
func (e *parseErr) Error() string {
|
2019-10-23 17:09:04 +00:00
|
|
|
var errMsg []string
|
2019-11-22 01:07:12 +00:00
|
|
|
for _, ve := range e.ValidationErrs() {
|
|
|
|
errMsg = append(errMsg, ve.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(errMsg, "\n\t")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *parseErr) ValidationErrs() []ValidationErr {
|
|
|
|
var errs []ValidationErr
|
2019-10-23 17:09:04 +00:00
|
|
|
for _, r := range e.Resources {
|
2019-11-22 01:07:12 +00:00
|
|
|
|
|
|
|
rootErr := ValidationErr{
|
|
|
|
Kind: r.Kind,
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
2019-11-22 01:07:12 +00:00
|
|
|
for _, v := range r.RootErrs {
|
|
|
|
errs = append(errs, traverseErrs(rootErr, v)...)
|
2019-10-26 02:11:47 +00:00
|
|
|
}
|
2019-11-22 01:07:12 +00:00
|
|
|
|
|
|
|
rootErr.Indexes = []*int{r.Idx}
|
|
|
|
rootErr.Fields = []string{"spec.resources"}
|
|
|
|
for _, v := range append(r.ValidationErrs, r.AssociationErrs...) {
|
|
|
|
errs = append(errs, traverseErrs(rootErr, v)...)
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-22 01:07:12 +00:00
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidationErr represents an error during the parsing of a package.
|
|
|
|
type ValidationErr struct {
|
|
|
|
Kind string `json:"kind" yaml:"kind"`
|
|
|
|
Fields []string `json:"fields" yaml:"fields"`
|
|
|
|
Indexes []*int `json:"idxs" yaml:"idxs"`
|
|
|
|
Reason string `json:"reason" yaml:"reason"`
|
|
|
|
}
|
2019-10-23 17:09:04 +00:00
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
func (v ValidationErr) Error() string {
|
|
|
|
fieldPairs := make([]string, 0, len(v.Fields))
|
|
|
|
for i, idx := range v.Indexes {
|
|
|
|
field := v.Fields[i]
|
|
|
|
if idx == nil || *idx == -1 {
|
|
|
|
fieldPairs = append(fieldPairs, field)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fieldPairs = append(fieldPairs, fmt.Sprintf("%s[%d]", field, *idx))
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("kind=%s field=%s reason=%q", v.Kind, strings.Join(fieldPairs, "."), v.Reason)
|
|
|
|
}
|
|
|
|
|
|
|
|
func traverseErrs(root ValidationErr, vErr validationErr) []ValidationErr {
|
|
|
|
root.Fields = append(root.Fields, vErr.Field)
|
|
|
|
root.Indexes = append(root.Indexes, vErr.Index)
|
|
|
|
if len(vErr.Nested) == 0 {
|
|
|
|
root.Reason = vErr.Msg
|
|
|
|
return []ValidationErr{root}
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs []ValidationErr
|
|
|
|
for _, n := range vErr.Nested {
|
|
|
|
errs = append(errs, traverseErrs(root, n)...)
|
|
|
|
}
|
|
|
|
return errs
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 01:07:12 +00:00
|
|
|
func (e *parseErr) append(errs ...resourceErr) {
|
2019-11-14 00:24:05 +00:00
|
|
|
e.Resources = append(e.Resources, errs...)
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsParseErr inspects a given error to determine if it is
|
2019-11-22 01:07:12 +00:00
|
|
|
// a parseErr. If a parseErr it is, it will return it along
|
|
|
|
// with the confirmation boolean. If the error is not a parseErr
|
|
|
|
// it will return nil values for the parseErr, making it unsafe
|
2019-10-23 17:09:04 +00:00
|
|
|
// to use.
|
2019-11-14 00:43:28 +00:00
|
|
|
func IsParseErr(err error) bool {
|
2019-11-22 01:07:12 +00:00
|
|
|
_, ok := err.(*parseErr)
|
2019-11-14 00:43:28 +00:00
|
|
|
return ok
|
2019-10-23 17:09:04 +00:00
|
|
|
}
|