forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the
operator usage instances
command and api endpoint (hashicor…
…p#16205) This endpoint shows total services, connect service instances and billable service instances in the local datacenter or globally. Billable instances = total service instances - connect services - consul server instances.
- Loading branch information
Showing
24 changed files
with
943 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:feature | ||
command: Adds the `operator usage instances` subcommand for displaying total services, connect service instances and billable service instances in the local datacenter or globally. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package consul | ||
|
||
import ( | ||
"github.com/hashicorp/consul/acl" | ||
"github.com/hashicorp/consul/agent/consul/state" | ||
"github.com/hashicorp/consul/agent/structs" | ||
"github.com/hashicorp/go-memdb" | ||
) | ||
|
||
// Usage returns counts for service usage within catalog. | ||
func (op *Operator) Usage(args *structs.OperatorUsageRequest, reply *structs.Usage) error { | ||
reply.Usage = make(map[string]structs.ServiceUsage) | ||
|
||
if args.Global { | ||
remoteDCs := op.srv.router.GetDatacenters() | ||
for _, dc := range remoteDCs { | ||
remoteArgs := &structs.OperatorUsageRequest{ | ||
DCSpecificRequest: structs.DCSpecificRequest{ | ||
Datacenter: dc, | ||
QueryOptions: structs.QueryOptions{ | ||
Token: args.Token, | ||
}, | ||
}, | ||
} | ||
var resp structs.Usage | ||
if _, err := op.srv.ForwardRPC("Operator.Usage", remoteArgs, &resp); err != nil { | ||
op.logger.Warn("error forwarding usage request to remote datacenter", "datacenter", dc, "error", err) | ||
} | ||
if usage, ok := resp.Usage[dc]; ok { | ||
reply.Usage[dc] = usage | ||
} | ||
} | ||
} | ||
|
||
var authzContext acl.AuthorizerContext | ||
authz, err := op.srv.ResolveTokenAndDefaultMeta(args.Token, structs.DefaultEnterpriseMetaInDefaultPartition(), &authzContext) | ||
if err != nil { | ||
return err | ||
} | ||
err = authz.ToAllowAuthorizer().OperatorReadAllowed(&authzContext) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = op.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil { | ||
return err | ||
} | ||
|
||
return op.srv.blockingQuery( | ||
&args.QueryOptions, | ||
&reply.QueryMeta, | ||
func(ws memdb.WatchSet, state *state.Store) error { | ||
// Get service usage. | ||
index, serviceUsage, err := state.ServiceUsage(ws) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
reply.QueryMeta.Index, reply.Usage[op.srv.config.Datacenter] = index, serviceUsage | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.