Skip to content

Commit

Permalink
optimize configure try handling multiple matching workloads by filter…
Browse files Browse the repository at this point in the history
…ing out inactive entities
  • Loading branch information
linkous8 committed Apr 11, 2024
1 parent 0d176ca commit 3efcdf1
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions cmd/optimize/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ and push the configuration to the knowledge store. You may optionally override t
func configureOptimizer(flags *configureFlags) func(cmd *cobra.Command, args []string) error {
var workloadTemplate = template.Must(template.New("").Parse(`
SINCE -1w
FETCH id
FETCH id, isActive
FROM entities(k8s:deployment)[attributes("k8s.cluster.name") = "{{.Cluster}}" && attributes("k8s.namespace.name") = "{{.Namespace}}" && attributes("k8s.workload.name") = "{{.WorkloadName}}"]
`))

Expand Down Expand Up @@ -182,13 +182,32 @@ FROM entities(k8s:deployment)[attributes("k8s.cluster.name") = "{{.Cluster}}" &&
if mainDataSet == nil {
return errors.New("unable to configure optimizer; UQL main data set was nil for the given criteria")
}
if workloadIdsFound := len(mainDataSet.Data); workloadIdsFound != 1 {
return fmt.Errorf("unable to configure optimizer; found %v workload IDs for the given criteria", workloadIdsFound)

var workloadIdAny any
workloadIdsFound := len(mainDataSet.Data)
if workloadIdsFound < 1 {
return errors.New("unable to configure optimizer; no workload IDs matched the given criteria")
} else if workloadIdsFound > 1 {
log.Warnf("found %v workload IDs for the given criteria, pruning inactive results")

var activeIds []any
for _, workloadRow := range mainDataSet.Data {
if workloadRow[1].(string) == "true" {
activeIds = append(activeIds, workloadRow[0])
}
}
if activeIdsFound := len(activeIds); activeIdsFound != 1 {
return fmt.Errorf("unable to configure optimizer; found %v active workload IDs for the given criteria", activeIdsFound)
}

workloadIdAny = activeIds[0]
} else {
workloadIdAny = mainDataSet.Data[0][0]
}
var ok bool
workloadId, ok = mainDataSet.Data[0][0].(string)
workloadId, ok = workloadIdAny.(string)
if !ok {
return fmt.Errorf("unable to convert workloadId query value %q to string", mainDataSet.Data[0][0])
return fmt.Errorf("unable to convert workloadId query value %q to string", workloadIdAny)
}

profilerReport, err = getProfilerReport(workloadId)
Expand Down

0 comments on commit 3efcdf1

Please sign in to comment.