Skip to content

Commit

Permalink
chore(pkger): accomodate skipping resources that are part of an assoc…
Browse files Browse the repository at this point in the history
…iation
  • Loading branch information
jsteenb2 committed Jun 22, 2020
1 parent 6d20ef4 commit 2d4a12f
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 116 deletions.
123 changes: 98 additions & 25 deletions cmd/influxd/launcher/pkger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1551,12 +1551,29 @@ func TestLauncher_Pkger(t *testing.T) {
variablePkgName = "laces-out-dan"
)

defaultPkgFn := func(*testing.T) *pkger.Pkg {
return newPkg(
newBucketObject(bucketPkgName, "", ""),
newCheckDeadmanObject(t, checkPkgName, "", time.Hour),
newDashObject(dashPkgName, "", ""),
newEndpointHTTP(endpointPkgName, "", ""),
newLabelObject(labelPkgName, "", "", ""),
newRuleObject(t, rulePkgName, "", endpointPkgName, ""),
newTaskObject(taskPkgName, "", ""),
newTelegrafObject(telegrafPkgName, "", ""),
newVariableObject(variablePkgName, "", ""),
)
}

tests := []struct {
name string
pkgFn func(t *testing.T) *pkger.Pkg
applyOpts []pkger.ApplyOptFn
assertFn func(t *testing.T, impact pkger.PkgImpactSummary)
}{
{
name: "skip resource",
name: "skip resource",
pkgFn: defaultPkgFn,
applyOpts: []pkger.ApplyOptFn{
pkger.ApplyWithResourceSkip(pkger.ActionSkipResource{
Kind: pkger.KindBucket,
Expand Down Expand Up @@ -1595,9 +1612,22 @@ func TestLauncher_Pkger(t *testing.T) {
MetaName: variablePkgName,
}),
},
assertFn: func(t *testing.T, impact pkger.PkgImpactSummary) {
summary := impact.Summary
assert.Empty(t, summary.Buckets)
assert.Empty(t, summary.Checks)
assert.Empty(t, summary.Dashboards)
assert.Empty(t, summary.NotificationEndpoints)
assert.Empty(t, summary.Labels)
assert.Empty(t, summary.NotificationRules, 0)
assert.Empty(t, summary.Tasks)
assert.Empty(t, summary.TelegrafConfigs)
assert.Empty(t, summary.Variables)
},
},
{
name: "skip kind",
name: "skip kind",
pkgFn: defaultPkgFn,
applyOpts: []pkger.ApplyOptFn{
pkger.ApplyWithKindSkip(pkger.ActionSkipKind{
Kind: pkger.KindBucket,
Expand Down Expand Up @@ -1627,6 +1657,70 @@ func TestLauncher_Pkger(t *testing.T) {
Kind: pkger.KindVariable,
}),
},
assertFn: func(t *testing.T, impact pkger.PkgImpactSummary) {
summary := impact.Summary
assert.Empty(t, summary.Buckets)
assert.Empty(t, summary.Checks)
assert.Empty(t, summary.Dashboards)
assert.Empty(t, summary.NotificationEndpoints)
assert.Empty(t, summary.Labels)
assert.Empty(t, summary.NotificationRules, 0)
assert.Empty(t, summary.Tasks)
assert.Empty(t, summary.TelegrafConfigs)
assert.Empty(t, summary.Variables)
},
},
{
name: "skip label and assoications should be dropped",
pkgFn: func(t *testing.T) *pkger.Pkg {
objs := []pkger.Object{
newBucketObject(bucketPkgName, "", ""),
newCheckDeadmanObject(t, checkPkgName, "", time.Hour),
newDashObject(dashPkgName, "", ""),
newEndpointHTTP(endpointPkgName, "", ""),
newRuleObject(t, rulePkgName, "", endpointPkgName, ""),
newTaskObject(taskPkgName, "", ""),
newTelegrafObject(telegrafPkgName, "", ""),
newVariableObject(variablePkgName, "", ""),
}
for _, obj := range objs {
obj.AddAssociations(pkger.ObjectAssociation{
Kind: pkger.KindLabel,
PkgName: labelPkgName,
})
}

objs = append(objs, newLabelObject(labelPkgName, "", "", ""))

return newPkg(objs...)
},
applyOpts: []pkger.ApplyOptFn{
pkger.ApplyWithKindSkip(pkger.ActionSkipKind{
Kind: pkger.KindLabel,
}),
},
assertFn: func(t *testing.T, impact pkger.PkgImpactSummary) {
summary := impact.Summary
assert.Empty(t, summary.Labels, 0)
assert.Empty(t, summary.LabelMappings)

assert.Len(t, summary.Buckets, 1)
assert.Empty(t, summary.Buckets[0].LabelAssociations)
assert.Len(t, summary.Checks, 1)
assert.Empty(t, summary.Checks[0].LabelAssociations)
assert.Len(t, summary.Dashboards, 1)
assert.Empty(t, summary.Dashboards[0].LabelAssociations)
assert.Len(t, summary.NotificationEndpoints, 1)
assert.Empty(t, summary.NotificationEndpoints[0].LabelAssociations)
assert.Len(t, summary.NotificationRules, 1)
assert.Empty(t, summary.NotificationRules[0].LabelAssociations)
assert.Len(t, summary.Tasks, 1)
assert.Empty(t, summary.Tasks[0].LabelAssociations)
assert.Len(t, summary.TelegrafConfigs, 1)
assert.Empty(t, summary.TelegrafConfigs[0].LabelAssociations)
assert.Len(t, summary.Variables, 1)
assert.Empty(t, summary.Variables[0].LabelAssociations)
},
},
}

Expand All @@ -1635,37 +1729,16 @@ func TestLauncher_Pkger(t *testing.T) {
stack, cleanup := newStackFn(t, pkger.Stack{})
defer cleanup()

pkg := newPkg(
newBucketObject(bucketPkgName, "", ""),
newCheckDeadmanObject(t, checkPkgName, "", time.Hour),
newDashObject(dashPkgName, "", ""),
newEndpointHTTP(endpointPkgName, "", ""),
newLabelObject(labelPkgName, "", "", ""),
newRuleObject(t, rulePkgName, "", endpointPkgName, ""),
newTaskObject(taskPkgName, "", ""),
newTelegrafObject(telegrafPkgName, "", ""),
newVariableObject(variablePkgName, "", ""),
)

impact, err := svc.Apply(ctx, l.Org.ID, l.User.ID,
append(
tt.applyOpts,
pkger.ApplyWithPkg(pkg),
pkger.ApplyWithPkg(tt.pkgFn(t)),
pkger.ApplyWithStackID(stack.ID),
)...,
)
require.NoError(t, err)

summary := impact.Summary
assert.Empty(t, summary.Buckets)
assert.Empty(t, summary.Checks)
assert.Empty(t, summary.Dashboards)
assert.Empty(t, summary.NotificationEndpoints)
assert.Empty(t, summary.Labels)
assert.Empty(t, summary.NotificationRules, 0)
assert.Empty(t, summary.Tasks)
assert.Empty(t, summary.TelegrafConfigs)
assert.Empty(t, summary.Variables)
tt.assertFn(t, impact)
}

t.Run(tt.name, fn)
Expand Down
40 changes: 18 additions & 22 deletions pkger/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ func (s *Service) dryRunLabelMappings(ctx context.Context, state *stateCoordinat
}

func (s *Service) dryRunResourceLabelMapping(ctx context.Context, state *stateCoordinator, stateLabelsByResName map[string]*stateLabel, associatedResource interface {
labels() []*label
labels() []*stateLabel
stateIdentity() stateIdentity
}) ([]stateLabelMapping, error) {

Expand All @@ -1316,7 +1316,7 @@ func (s *Service) dryRunResourceLabelMapping(ctx context.Context, state *stateCo
mappings = append(mappings, stateLabelMapping{
status: StateStatusNew,
resource: associatedResource,
label: state.getLabelByPkgName(l.PkgName()),
label: l,
})
}
return mappings, nil
Expand Down Expand Up @@ -1347,10 +1347,14 @@ func (s *Service) dryRunResourceLabelMapping(ctx context.Context, state *stateCo

// now we add labels that do not exist
for _, l := range pkgLabels {
stLabel, found := state.getLabelByPkgName(l.PkgName())
if !found {
continue
}
mappings = append(mappings, stateLabelMapping{
status: StateStatusNew,
resource: associatedResource,
label: state.getLabelByPkgName(l.PkgName()),
label: stLabel,
})
}

Expand Down Expand Up @@ -3032,52 +3036,48 @@ func (s *Service) updateStackAfterSuccess(ctx context.Context, stackID influxdb.
if IsRemoval(b.stateStatus) {
continue
}
associatedLabels := state.labelAssociations(KindBucket, b.parserBkt.PkgName())
stackResources = append(stackResources, StackResource{
APIVersion: APIVersion,
ID: b.ID(),
Kind: KindBucket,
PkgName: b.parserBkt.PkgName(),
Associations: stateLabelsToStackAssociations(associatedLabels),
Associations: stateLabelsToStackAssociations(b.labels()),
})
}
for _, c := range state.mChecks {
if IsRemoval(c.stateStatus) {
continue
}
associatedLabels := state.labelAssociations(KindCheck, c.parserCheck.PkgName())
stackResources = append(stackResources, StackResource{
APIVersion: APIVersion,
ID: c.ID(),
Kind: KindCheck,
PkgName: c.parserCheck.PkgName(),
Associations: stateLabelsToStackAssociations(associatedLabels),
Associations: stateLabelsToStackAssociations(c.labels()),
})
}
for _, d := range state.mDashboards {
if IsRemoval(d.stateStatus) {
continue
}
associatedLabels := state.labelAssociations(KindDashboard, d.parserDash.PkgName())
stackResources = append(stackResources, StackResource{
APIVersion: APIVersion,
ID: d.ID(),
Kind: KindDashboard,
PkgName: d.parserDash.PkgName(),
Associations: stateLabelsToStackAssociations(associatedLabels),
Associations: stateLabelsToStackAssociations(d.labels()),
})
}
for _, n := range state.mEndpoints {
if IsRemoval(n.stateStatus) {
continue
}
associatedLabels := state.labelAssociations(KindNotificationEndpoint, n.parserEndpoint.PkgName())
stackResources = append(stackResources, StackResource{
APIVersion: APIVersion,
ID: n.ID(),
Kind: KindNotificationEndpoint,
PkgName: n.parserEndpoint.PkgName(),
Associations: stateLabelsToStackAssociations(associatedLabels),
Associations: stateLabelsToStackAssociations(n.labels()),
})
}
for _, l := range state.mLabels {
Expand All @@ -3095,14 +3095,13 @@ func (s *Service) updateStackAfterSuccess(ctx context.Context, stackID influxdb.
if IsRemoval(r.stateStatus) {
continue
}
associatedLabels := state.labelAssociations(KindNotificationRule, r.parserRule.PkgName())
stackResources = append(stackResources, StackResource{
APIVersion: APIVersion,
ID: r.ID(),
Kind: KindNotificationRule,
PkgName: r.parserRule.PkgName(),
Associations: append(
stateLabelsToStackAssociations(associatedLabels),
stateLabelsToStackAssociations(r.labels()),
r.endpointAssociation(),
),
})
Expand All @@ -3111,39 +3110,36 @@ func (s *Service) updateStackAfterSuccess(ctx context.Context, stackID influxdb.
if IsRemoval(t.stateStatus) {
continue
}
associatedLabels := state.labelAssociations(KindTask, t.parserTask.PkgName())
stackResources = append(stackResources, StackResource{
APIVersion: APIVersion,
ID: t.ID(),
Kind: KindTask,
PkgName: t.parserTask.PkgName(),
Associations: stateLabelsToStackAssociations(associatedLabels),
Associations: stateLabelsToStackAssociations(t.labels()),
})
}
for _, t := range state.mTelegrafs {
if IsRemoval(t.stateStatus) {
continue
}
associatedLabels := state.labelAssociations(KindTelegraf, t.parserTelegraf.PkgName())
stackResources = append(stackResources, StackResource{
APIVersion: APIVersion,
ID: t.ID(),
Kind: KindTelegraf,
PkgName: t.parserTelegraf.PkgName(),
Associations: stateLabelsToStackAssociations(associatedLabels),
Associations: stateLabelsToStackAssociations(t.labels()),
})
}
for _, v := range state.mVariables {
if IsRemoval(v.stateStatus) {
continue
}
associatedLabels := state.labelAssociations(KindVariable, v.parserVar.PkgName())
stackResources = append(stackResources, StackResource{
APIVersion: APIVersion,
ID: v.ID(),
Kind: KindVariable,
PkgName: v.parserVar.PkgName(),
Associations: stateLabelsToStackAssociations(associatedLabels),
Associations: stateLabelsToStackAssociations(v.labels()),
})
}
stack.Resources = stackResources
Expand Down Expand Up @@ -3436,7 +3432,7 @@ func (r *rollbackCoordinator) runTilEnd(ctx context.Context, orgID, userID influ

defer func() {
if recover() != nil {
r.logger.Panic(
r.logger.Error(
"panic applying "+resource,
zap.String("stack_trace", fmt.Sprintf("%+v", stack.Trace())),
)
Expand Down Expand Up @@ -3564,8 +3560,8 @@ func validURLs(urls []string) error {
return nil
}

func labelSlcToMap(labels []*label) map[string]*label {
m := make(map[string]*label)
func labelSlcToMap(labels []*stateLabel) map[string]*stateLabel {
m := make(map[string]*stateLabel)
for i := range labels {
m[labels[i].Name()] = labels[i]
}
Expand Down
Loading

0 comments on commit 2d4a12f

Please sign in to comment.