Skip to content

Commit

Permalink
feat(pkger): extend tasks with ability to define and rename it
Browse files Browse the repository at this point in the history
closes: #17233
  • Loading branch information
jsteenb2 committed Mar 19, 2020
1 parent b84f5db commit a5c9305
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 98 deletions.
5 changes: 3 additions & 2 deletions cmd/influxd/launcher/pkger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ spec:
pkg, err := pkger.Parse(pkger.EncodingYAML, pkger.FromString(pkgStr))
require.NoError(t, err)

sum, _, err := svc.DryRun(context.Background(), l.Org.ID, l.User.ID, pkg, pkger.ApplyWithEnvRefs(map[string]string{
sum, _, err := svc.DryRun(timedCtx(2*time.Second), l.Org.ID, l.User.ID, pkg, pkger.ApplyWithEnvRefs(map[string]string{
"bkt-1-name-ref": "new-bkt-name",
"label-1-name-ref": "new-label-name",
}))
Expand Down Expand Up @@ -1167,8 +1167,9 @@ spec:
apiVersion: %[1]s
kind: Task
metadata:
name: task_1
name: task_UUID
spec:
name: task_1
description: desc_1
cron: 15 * * * *
query: >
Expand Down
20 changes: 4 additions & 16 deletions pkger/clone_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,21 +886,15 @@ func taskToObject(t influxdb.Task, name string) Object {

query := strings.TrimSpace(taskFluxRegex.ReplaceAllString(t.Flux, ""))

k := Object{
APIVersion: APIVersion,
Kind: KindTask,
Metadata: convertToMetadataResource(name),
Spec: Resource{
fieldQuery: strings.TrimSpace(query),
},
}
assignNonZeroStrings(k.Spec, map[string]string{
o := newObject(KindTask, name)
assignNonZeroStrings(o.Spec, map[string]string{
fieldTaskCron: t.Cron,
fieldDescription: t.Description,
fieldEvery: t.Every,
fieldOffset: durToStr(t.Offset),
fieldQuery: strings.TrimSpace(query),
})
return k
return o
}

func telegrafToObject(t influxdb.TelegrafConfig, name string) Object {
Expand Down Expand Up @@ -954,12 +948,6 @@ func VariableToObject(v influxdb.Variable, name string) Object {
return o
}

func convertToMetadataResource(name string) Resource {
return Resource{
fieldName: name,
}
}

func newObject(kind Kind, name string) Object {
return Object{
APIVersion: APIVersion,
Expand Down
19 changes: 17 additions & 2 deletions pkger/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,7 @@ type task struct {
id influxdb.ID
orgID influxdb.ID
name *references
displayName *references
cron string
description string
every time.Duration
Expand All @@ -1897,6 +1898,13 @@ func (t *task) Labels() []*label {
}

func (t *task) Name() string {
if displayName := t.displayName.String(); displayName != "" {
return displayName
}
return t.name.String()
}

func (t *task) PkgName() string {
return t.name.String()
}

Expand All @@ -1914,7 +1922,7 @@ func (t *task) Status() influxdb.Status {
var fluxRegex = regexp.MustCompile(`import\s+\".*\"`)

func (t *task) flux() string {
taskOpts := []string{fmt.Sprintf("name: %q", t.name)}
taskOpts := []string{fmt.Sprintf("name: %q", t.Name())}
if t.cron != "" {
taskOpts = append(taskOpts, fmt.Sprintf("cron: %q", t.cron))
}
Expand Down Expand Up @@ -1987,7 +1995,14 @@ func (t *task) valid() []validationErr {
Msg: "must be 1 of [active, inactive]",
})
}
return vErrs

if len(vErrs) > 0 {
return []validationErr{
objectValidationErr(fieldSpec, vErrs...),
}
}

return nil
}

type mapperTasks []*task
Expand Down
25 changes: 21 additions & 4 deletions pkger/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ type Pkg struct {
mDashboards map[string]*dashboard
mNotificationEndpoints map[string]*notificationEndpoint
mNotificationRules map[string]*notificationRule
mTasks []*task
mTasks map[string]*task
mTelegrafs map[string]*telegraf
mVariables map[string]*variable

Expand Down Expand Up @@ -528,7 +528,10 @@ func (p *Pkg) missingSecrets() []string {
}

func (p *Pkg) tasks() []*task {
tasks := p.mTasks[:]
tasks := make([]*task, 0, len(p.mTasks))
for _, t := range p.mTasks {
tasks = append(tasks, t)
}

sort.Slice(tasks, func(i, j int) bool { return tasks[i].Name() < tasks[j].Name() })

Expand All @@ -541,7 +544,9 @@ func (p *Pkg) telegrafs() []*telegraf {
t.config.Name = t.Name()
teles = append(teles, t)
}

sort.Slice(teles, func(i, j int) bool { return teles[i].Name() < teles[j].Name() })

return teles
}

Expand Down Expand Up @@ -848,6 +853,7 @@ func (p *Pkg) graphDashboards() *parseErr {
}),
}
}

dash := &dashboard{
name: nameRef,
displayName: p.getRefWithKnownEnvs(o.Spec, fieldName),
Expand Down Expand Up @@ -1031,10 +1037,21 @@ func (p *Pkg) graphNotificationRules() *parseErr {
}

func (p *Pkg) graphTasks() *parseErr {
p.mTasks = make([]*task, 0)
p.mTasks = make(map[string]*task)
return p.eachResource(KindTask, 1, func(o Object) []validationErr {
nameRef := p.getRefWithKnownEnvs(o.Metadata, fieldName)
if _, ok := p.mTasks[nameRef.String()]; ok {
return []validationErr{
objectValidationErr(fieldMetadata, validationErr{
Field: fieldName,
Msg: "duplicate name: " + nameRef.String(),
}),
}
}

t := &task{
name: p.getRefWithKnownEnvs(o.Metadata, fieldName),
displayName: p.getRefWithKnownEnvs(o.Spec, fieldName),
cron: o.Spec.stringShort(fieldTaskCron),
description: o.Spec.stringShort(fieldDescription),
every: o.Spec.durationShort(fieldEvery),
Expand All @@ -1050,7 +1067,7 @@ func (p *Pkg) graphTasks() *parseErr {
})
sort.Sort(t.labels)

p.mTasks = append(p.mTasks, t)
p.mTasks[t.PkgName()] = t
p.setRefs(t.name)
return append(failures, t.valid()...)
})
Expand Down
53 changes: 40 additions & 13 deletions pkger/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3343,14 +3343,14 @@ spec:

require.Len(t, sum.Labels, 1)

task1 := tasks[0]
baseEqual(t, 0, influxdb.Inactive, task1)
assert.Equal(t, (10 * time.Minute).String(), task1.Every)
assert.Equal(t, (15 * time.Second).String(), task1.Offset)

task2 := tasks[1]
baseEqual(t, 1, influxdb.Active, task2)
assert.Equal(t, "15 * * * *", task2.Cron)
task0 := tasks[0]
baseEqual(t, 0, influxdb.Inactive, task0)
assert.Equal(t, (10 * time.Minute).String(), task0.Every)
assert.Equal(t, (15 * time.Second).String(), task0.Offset)

task1 := tasks[1]
baseEqual(t, 1, influxdb.Active, task1)
assert.Equal(t, "15 * * * *", task1.Cron)
})
})

Expand Down Expand Up @@ -3381,7 +3381,7 @@ spec:
resErr: testPkgResourceError{
name: "invalid status",
validationErrs: 1,
valFields: []string{fieldStatus},
valFields: []string{fieldSpec, fieldStatus},
pkgStr: `apiVersion: influxdata.com/v2alpha1
kind: Task
metadata:
Expand All @@ -3399,7 +3399,7 @@ spec:
resErr: testPkgResourceError{
name: "missing query",
validationErrs: 1,
valFields: []string{fieldQuery},
valFields: []string{fieldSpec, fieldQuery},
pkgStr: `apiVersion: influxdata.com/v2alpha1
kind: Task
metadata:
Expand All @@ -3416,7 +3416,7 @@ spec:
resErr: testPkgResourceError{
name: "missing every and cron fields",
validationErrs: 1,
valFields: []string{fieldEvery, fieldTaskCron},
valFields: []string{fieldSpec, fieldEvery, fieldTaskCron},
pkgStr: `apiVersion: influxdata.com/v2alpha1
kind: Task
metadata:
Expand All @@ -3432,7 +3432,7 @@ spec:
resErr: testPkgResourceError{
name: "invalid association",
validationErrs: 1,
valFields: []string{fieldAssociations},
valFields: []string{fieldSpec, fieldAssociations},
pkgStr: `apiVersion: influxdata.com/v2alpha1
kind: Task
metadata:
Expand All @@ -3452,7 +3452,7 @@ spec:
resErr: testPkgResourceError{
name: "duplicate association",
validationErrs: 1,
valFields: []string{fieldAssociations},
valFields: []string{fieldSpec, fieldAssociations},
pkgStr: `---
apiVersion: influxdata.com/v2alpha1
kind: Label
Expand All @@ -3474,6 +3474,33 @@ spec:
name: label_1
- kind: Label
name: label_1
`,
},
},
{
kind: KindTask,
resErr: testPkgResourceError{
name: "duplicate meta names",
validationErrs: 1,
valFields: []string{fieldMetadata, fieldName},
pkgStr: `
apiVersion: influxdata.com/v2alpha1
kind: Task
metadata:
name: task_0
spec:
every: 10m
query: >
from(bucket: "rucket_1") |> yield(name: "mean")
---
apiVersion: influxdata.com/v2alpha1
kind: Task
metadata:
name: task_0
spec:
every: 10m
query: >
from(bucket: "rucket_1") |> yield(name: "mean")
`,
},
},
Expand Down
Loading

0 comments on commit a5c9305

Please sign in to comment.