Skip to content

chore: add managed_agent_limit licensing feature #18876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
PR comments and additional changes
  • Loading branch information
deansheather committed Jul 17, 2025
commit c7d0a84d31b36d103bc3366f68c7f72067a0c8ad
59 changes: 28 additions & 31 deletions codersdk/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ var (

// FeatureNamesMap is a map of all feature names for quick lookups.
FeatureNamesMap = func() map[FeatureName]struct{} {
featureNamesMap := make(map[FeatureName]struct{})
featureNamesMap := make(map[FeatureName]struct{}, len(FeatureNames))
for _, featureName := range FeatureNames {
featureNamesMap[featureName] = struct{}{}
}
Expand Down Expand Up @@ -222,9 +222,6 @@ func (set FeatureSet) Features() []FeatureName {
})
// FeatureSetPremium is just all features.
return premiumFeatures
case FeatureSetNone:
default:
panic("unexpected codersdk.FeatureSet")
}
// By default, return an empty set.
return []FeatureName{}
Expand All @@ -242,7 +239,7 @@ type Feature struct {
// included limits in the dashboard. No license validation or warnings are
// generated from this value.
SoftLimit *int64 `json:"soft_limit,omitempty"`
// Usage period denotes that the usage is a counter that accumulates over
// UsagePeriod denotes that the usage is a counter that accumulates over
// this period (and most likely resets with the issuance of the next
// license).
//
Expand All @@ -251,9 +248,13 @@ type Feature struct {
//
// Only certain features set these fields:
// - FeatureManagedAgentLimit
UsagePeriodIssuedAt *time.Time `json:"usage_period_issued_at,omitempty" format:"date-time"`
UsagePeriodStart *time.Time `json:"usage_period_start,omitempty" format:"date-time"`
UsagePeriodEnd *time.Time `json:"usage_period_end,omitempty" format:"date-time"`
UsagePeriod *UsagePeriod `json:"usage_period,omitempty"`
}

type UsagePeriod struct {
IssuedAt time.Time `json:"issued_at" format:"date-time"`
Start time.Time `json:"start" format:"date-time"`
End time.Time `json:"end" format:"date-time"`
}

// Compare compares two features and returns an integer representing
Expand All @@ -262,14 +263,28 @@ type Feature struct {
// than the second feature. It is assumed the features are for the same FeatureName.
//
// A feature is considered greater than another feature if:
// 1. Graceful & capable > Entitled & not capable
// 1. The usage period has a greater issued at date (note: only certain features use usage periods)
// 2. The usage period has a greater end date (note: only certain features use usage periods)
// 3. The usage period has a greater issued at date (note: only certain features use usage periods)
// 3. Graceful & capable > Entitled & not capable (only if both have "Actual" values)
// 4. The entitlement is greater
// 5. The limit is greater
// 6. Enabled is greater than disabled
// 7. The actual is greater
func (f Feature) Compare(b Feature) int {
// For features with usage period constraints only, check the issued at and
// end dates.
bothHaveUsagePeriod := f.UsagePeriod != nil && b.UsagePeriod != nil
if bothHaveUsagePeriod {
issuedAtCmp := f.UsagePeriod.IssuedAt.Compare(b.UsagePeriod.IssuedAt)
if issuedAtCmp != 0 {
return issuedAtCmp
}
endCmp := f.UsagePeriod.End.Compare(b.UsagePeriod.End)
if endCmp != 0 {
return endCmp
}
}

// Only perform capability comparisons if both features have actual values.
if f.Actual != nil && b.Actual != nil && (!f.Capable() || !b.Capable()) {
// If either is incapable, then it is possible a grace period
Expand All @@ -289,26 +304,11 @@ func (f Feature) Compare(b Feature) int {
// Strict entitlement check. Higher is better. We don't apply this check for
// usage period features as we always want the issued at date to be the main
// decision maker.
bothHaveIssuedAt := f.UsagePeriodIssuedAt != nil && b.UsagePeriodIssuedAt != nil
entitlementDifference := f.Entitlement.Weight() - b.Entitlement.Weight()
if !bothHaveIssuedAt && entitlementDifference != 0 {
if entitlementDifference != 0 {
return entitlementDifference
}

// For features with usage period constraints only:
if bothHaveIssuedAt {
cmp := f.UsagePeriodIssuedAt.Compare(*b.UsagePeriodIssuedAt)
if cmp != 0 {
return cmp
}
}
if f.UsagePeriodEnd != nil && b.UsagePeriodEnd != nil {
cmp := f.UsagePeriodEnd.Compare(*b.UsagePeriodEnd)
if cmp != 0 {
return cmp
}
}

// If the entitlement is the same, then we can compare the limits.
if f.Limit == nil && b.Limit != nil {
return -1
Expand Down Expand Up @@ -394,14 +394,11 @@ func (e *Entitlements) AddFeature(name FeatureName, add Feature) {
// If we're trying to add a feature that uses a usage period and it's not
// set, then we should not add it.
if name.UsesUsagePeriod() {
if add.UsagePeriodIssuedAt == nil || add.UsagePeriodStart == nil || add.UsagePeriodEnd == nil {
if add.UsagePeriod == nil || add.UsagePeriod.IssuedAt.IsZero() || add.UsagePeriod.Start.IsZero() || add.UsagePeriod.End.IsZero() {
return
}
} else {
// Ensure the usage period values are not set.
add.UsagePeriodIssuedAt = nil
add.UsagePeriodStart = nil
add.UsagePeriodEnd = nil
add.UsagePeriod = nil
}

// Compare the features, keep the one that is "better"
Expand Down
100 changes: 60 additions & 40 deletions enterprise/coderd/license/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ import (
)

const (
// These features are only included in the license and are not actually
// entitlements after the licenses are processed. These values will be
// merged into the codersdk.FeatureManagedAgentLimit feature.
//
// The reason we need two separate features is because the License v3 format
// uses map[string]int64 for features, so we're unable to use a single value
// with a struct like `{"soft": 100, "hard": 200}`. This is unfortunate and
// we should fix this with a new license format v4 in the future.
//
// These are intentionally not exported as they should not be used outside
// of this package (except tests).
featureManagedAgentLimitHard codersdk.FeatureName = "managed_agent_limit_hard"
featureManagedAgentLimitSoft codersdk.FeatureName = "managed_agent_limit_soft"
)
Expand Down Expand Up @@ -88,8 +99,9 @@ func Entitlements(
ActiveUserCount: activeUserCount,
ReplicaCount: replicaCount,
ExternalAuthCount: externalAuthCount,
ManagedAgentCountFn: func(ctx context.Context, from time.Time, to time.Time) (int64, error) {
// TODO: this
ManagedAgentCountFn: func(_ context.Context, _ time.Time, _ time.Time) (int64, error) {
// TODO(@deansheather): replace this with a real implementation in a
// follow up PR.
return 0, nil
},
})
Expand All @@ -107,9 +119,11 @@ type FeatureArguments struct {
// Unfortunately, managed agent count is not a simple count of the current
// state of the world, but a count between two points in time determined by
// the licenses.
ManagedAgentCountFn func(ctx context.Context, from time.Time, to time.Time) (int64, error)
ManagedAgentCountFn ManagedAgentCountFn
}

type ManagedAgentCountFn func(ctx context.Context, from time.Time, to time.Time) (int64, error)

// LicensesEntitlements returns the entitlements for licenses. Entitlements are
// merged from all licenses and the highest entitlement is used for each feature.
// Arguments:
Expand Down Expand Up @@ -297,13 +311,15 @@ func LicensesEntitlements(
defaultHardAgentLimit = 1000 * featureValue
)
entitlements.AddFeature(codersdk.FeatureManagedAgentLimit, codersdk.Feature{
Enabled: true,
Entitlement: entitlement,
SoftLimit: &defaultSoftAgentLimit,
Limit: &defaultHardAgentLimit,
UsagePeriodIssuedAt: &issueTime,
UsagePeriodStart: &usagePeriodStart,
UsagePeriodEnd: &usagePeriodEnd,
Enabled: true,
Entitlement: entitlement,
SoftLimit: &defaultSoftAgentLimit,
Limit: &defaultHardAgentLimit,
UsagePeriod: &codersdk.UsagePeriod{
IssuedAt: issueTime,
Start: usagePeriodStart,
End: usagePeriodEnd,
},
})
}
default:
Expand Down Expand Up @@ -342,11 +358,12 @@ func LicensesEntitlements(
Entitlement: entitlement,
SoftLimit: ul.Soft,
Limit: ul.Hard,
// Actual value will be populated below when warnings are
// generated.
UsagePeriodIssuedAt: &claims.IssuedAt.Time,
UsagePeriodStart: &usagePeriodStart,
UsagePeriodEnd: &usagePeriodEnd,
// `Actual` will be populated below when warnings are generated.
UsagePeriod: &codersdk.UsagePeriod{
IssuedAt: claims.IssuedAt.Time,
Start: usagePeriodStart,
End: usagePeriodEnd,
},
}
// If the hard limit is 0, the feature is disabled.
if *ul.Hard <= 0 {
Expand Down Expand Up @@ -404,15 +421,15 @@ func LicensesEntitlements(
// generate a warning if the license actually has managed agents.
// Note that agents are free when unlicensed.
agentLimit := entitlements.Features[codersdk.FeatureManagedAgentLimit]
if entitlements.HasLicense && agentLimit.UsagePeriodStart != nil && agentLimit.UsagePeriodEnd != nil {
if entitlements.HasLicense && agentLimit.UsagePeriod != nil {
// Calculate the amount of agents between the usage period start and
// end.
var (
managedAgentCount int64
err = xerrors.New("dev error: managed agent count function is not set")
)
if featureArguments.ManagedAgentCountFn != nil {
managedAgentCount, err = featureArguments.ManagedAgentCountFn(ctx, *agentLimit.UsagePeriodStart, *agentLimit.UsagePeriodEnd)
managedAgentCount, err = featureArguments.ManagedAgentCountFn(ctx, agentLimit.UsagePeriod.Start, agentLimit.UsagePeriod.End)
}
if err != nil {
entitlements.Errors = append(entitlements.Errors,
Expand All @@ -421,30 +438,33 @@ func LicensesEntitlements(
agentLimit.Actual = &managedAgentCount
entitlements.AddFeature(codersdk.FeatureManagedAgentLimit, agentLimit)

var softLimit int64
if agentLimit.SoftLimit != nil {
softLimit = *agentLimit.SoftLimit
}
var hardLimit int64
if agentLimit.Limit != nil {
hardLimit = *agentLimit.Limit
}
// Only issue warnings if the feature is enabled.
if agentLimit.Enabled {
var softLimit int64
if agentLimit.SoftLimit != nil {
softLimit = *agentLimit.SoftLimit
}
var hardLimit int64
if agentLimit.Limit != nil {
hardLimit = *agentLimit.Limit
}

// Issue a warning early:
// 1. If the soft limit and hard limit are equal, at 75% of the hard
// limit.
// 2. If the limit is greater than the soft limit, at 75% of the
// difference between the hard limit and the soft limit.
softWarningThreshold := int64(float64(hardLimit) * 0.75)
if hardLimit > softLimit && softLimit > 0 {
softWarningThreshold = softLimit + int64(float64(hardLimit-softLimit)*0.75)
}
if managedAgentCount >= *agentLimit.Limit {
entitlements.Warnings = append(entitlements.Warnings,
"You have built more workspaces with managed agents than your license allows. Further managed agent builds will be blocked.")
} else if managedAgentCount >= softWarningThreshold {
entitlements.Warnings = append(entitlements.Warnings,
"You are approaching the managed agent limit in your license. Please refer to the Deployment Licenses page for more information.")
// Issue a warning early:
// 1. If the soft limit and hard limit are equal, at 75% of the hard
// limit.
// 2. If the limit is greater than the soft limit, at 75% of the
// difference between the hard limit and the soft limit.
softWarningThreshold := int64(float64(hardLimit) * 0.75)
if hardLimit > softLimit && softLimit > 0 {
softWarningThreshold = softLimit + int64(float64(hardLimit-softLimit)*0.75)
}
if managedAgentCount >= *agentLimit.Limit {
entitlements.Warnings = append(entitlements.Warnings,
"You have built more workspaces with managed agents than your license allows. Further managed agent builds will be blocked.")
} else if managedAgentCount >= softWarningThreshold {
entitlements.Warnings = append(entitlements.Warnings,
"You are approaching the managed agent limit in your license. Please refer to the Deployment Licenses page for more information.")
}
}
}
}
Expand Down
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy