chore: keep entitlements in the options only, simplify fields (#14434)

* chore: refactor entitlements to keep it in just the options

Duplicating the reference did not feel valuable, just confusing
This commit is contained in:
Steven Masley
2024-08-26 13:05:03 -05:00
committed by GitHub
parent fb6b954222
commit 93eef7b542
11 changed files with 27 additions and 25 deletions

View File

@ -75,6 +75,9 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
// from when an additional replica was started.
options.ReplicaErrorGracePeriod = time.Minute
}
if options.Entitlements == nil {
options.Entitlements = entitlements.New()
}
ctx, cancelFunc := context.WithCancel(ctx)
@ -105,25 +108,22 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
return nil, xerrors.Errorf("init database encryption: %w", err)
}
entitlementsSet := entitlements.New()
options.Database = cryptDB
api := &API{
ctx: ctx,
cancel: cancelFunc,
Options: options,
entitlements: entitlementsSet,
ctx: ctx,
cancel: cancelFunc,
Options: options,
provisionerDaemonAuth: &provisionerDaemonAuth{
psk: options.ProvisionerDaemonPSK,
authorizer: options.Authorizer,
db: options.Database,
},
licenseMetricsCollector: &license.MetricsCollector{
Entitlements: entitlementsSet,
Entitlements: options.Entitlements,
},
}
// This must happen before coderd initialization!
options.PostAuthAdditionalHeadersFunc = api.writeEntitlementWarningsHeader
options.Options.Entitlements = api.entitlements
api.AGPL = coderd.New(options.Options)
defer func() {
if err != nil {
@ -561,8 +561,6 @@ type API struct {
// ProxyHealth checks the reachability of all workspace proxies.
ProxyHealth *proxyhealth.ProxyHealth
entitlements *entitlements.Set
provisionerDaemonAuth *provisionerDaemonAuth
licenseMetricsCollector *license.MetricsCollector
@ -595,7 +593,7 @@ func (api *API) writeEntitlementWarningsHeader(a rbac.Subject, header http.Heade
return
}
api.entitlements.WriteEntitlementWarningHeaders(header)
api.Entitlements.WriteEntitlementWarningHeaders(header)
}
func (api *API) Close() error {
@ -658,7 +656,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
//
// We don't simply append to entitlement.Errors since we don't want any
// enterprise features enabled.
api.entitlements.Update(func(entitlements *codersdk.Entitlements) {
api.Entitlements.Update(func(entitlements *codersdk.Entitlements) {
entitlements.Errors = []string{
"License requires telemetry but telemetry is disabled",
}
@ -669,7 +667,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
}
featureChanged := func(featureName codersdk.FeatureName) (initial, changed, enabled bool) {
return api.entitlements.FeatureChanged(featureName, reloadedEntitlements.Features[featureName])
return api.Entitlements.FeatureChanged(featureName, reloadedEntitlements.Features[featureName])
}
shouldUpdate := func(initial, changed, enabled bool) bool {
@ -835,7 +833,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
}
reloadedEntitlements.Features[codersdk.FeatureExternalTokenEncryption] = featureExternalTokenEncryption
api.entitlements.Replace(reloadedEntitlements)
api.Entitlements.Replace(reloadedEntitlements)
return nil
}
@ -1015,7 +1013,7 @@ func derpMapper(logger slog.Logger, proxyHealth *proxyhealth.ProxyHealth) func(*
// @Router /entitlements [get]
func (api *API) serveEntitlements(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
httpapi.Write(ctx, rw, http.StatusOK, api.entitlements.AsJSON())
httpapi.Write(ctx, rw, http.StatusOK, api.Entitlements.AsJSON())
}
func (api *API) runEntitlementsLoop(ctx context.Context) {