* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
161 lines
4.7 KiB
Go
161 lines
4.7 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
"github.com/hashicorp/boundary/internal/kms"
|
|
"github.com/hashicorp/boundary/internal/oplog"
|
|
"github.com/hashicorp/boundary/internal/scheduler"
|
|
ua "go.uber.org/atomic"
|
|
)
|
|
|
|
const (
|
|
orphanedHostCleanupJobName = "plugin_host_orpahend_hosts_cleanup"
|
|
orphanedHostCleanupJobRunInterval = 5 * time.Minute
|
|
)
|
|
|
|
// OrphanedHostCleanupJob is the recurring job that syncs hosts from sets that are.
|
|
// The OrphanedHostCleanupJob is not thread safe,
|
|
// an attempt to Run the job concurrently will result in an JobAlreadyRunning error.
|
|
type OrphanedHostCleanupJob struct {
|
|
reader db.Reader
|
|
writer db.Writer
|
|
kms *kms.Kms
|
|
limit int
|
|
|
|
running ua.Bool
|
|
numHosts int
|
|
numProcessed int
|
|
}
|
|
|
|
// newOrphanedHostCleanupJob creates a new in-memory OrphanedHostCleanupJob.
|
|
func newOrphanedHostCleanupJob(ctx context.Context, r db.Reader, w db.Writer, kms *kms.Kms, _ ...Option) (*OrphanedHostCleanupJob, error) {
|
|
const op = "plugin.newOrphanedHostCleanupJob"
|
|
switch {
|
|
case r == nil:
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing db.Reader")
|
|
case w == nil:
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing db.Writer")
|
|
case kms == nil:
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing kms")
|
|
}
|
|
|
|
return &OrphanedHostCleanupJob{
|
|
reader: r,
|
|
writer: w,
|
|
kms: kms,
|
|
}, nil
|
|
}
|
|
|
|
// Status returns the current status of the set sync job. Total is the total number
|
|
// of sets that are to be synced. Completed is the number of sets already synced.
|
|
func (r *OrphanedHostCleanupJob) Status() scheduler.JobStatus {
|
|
return scheduler.JobStatus{
|
|
Completed: r.numProcessed,
|
|
Total: r.numHosts,
|
|
}
|
|
}
|
|
|
|
// Run queries the plugin host repo hosts with no memberships, it then
|
|
// deletes those hosts. Can not be run in parallel, if
|
|
// Run is invoked while already running an error with code JobAlreadyRunning
|
|
// will be returned.
|
|
func (r *OrphanedHostCleanupJob) Run(ctx context.Context) error {
|
|
const op = "plugin.(OrphanedHostCleanupJob).Run"
|
|
if !r.running.CAS(r.running.Load(), true) {
|
|
return errors.New(ctx, errors.JobAlreadyRunning, op, "job already running")
|
|
}
|
|
defer r.running.Store(false)
|
|
|
|
// Verify context is not done before running
|
|
if err := ctx.Err(); err != nil {
|
|
return errors.Wrap(ctx, err, op)
|
|
}
|
|
|
|
i, err := r.deleteOrphanedHosts(ctx)
|
|
if err != nil {
|
|
return errors.Wrap(ctx, err, op)
|
|
}
|
|
|
|
// Set numProcessed and numHosts for status report
|
|
r.numProcessed, r.numHosts = i, i
|
|
return nil
|
|
}
|
|
|
|
// NextRunIn returns the default run frequency of the cleanup job.
|
|
func (r *OrphanedHostCleanupJob) NextRunIn(_ context.Context) (time.Duration, error) {
|
|
return orphanedHostCleanupJobRunInterval, nil
|
|
}
|
|
|
|
// Name is the unique name of the job.
|
|
func (r *OrphanedHostCleanupJob) Name() string {
|
|
return orphanedHostCleanupJobName
|
|
}
|
|
|
|
// Description is the human readable description of the job.
|
|
func (r *OrphanedHostCleanupJob) Description() string {
|
|
return "Periodically deletes plugin based hosts which are not members of any sets."
|
|
}
|
|
|
|
// deleteOrphanedHosts deletes any hosts that no longer belong to any set.
|
|
// WithLimit is the only option supported. No options are currently supported.
|
|
func (r *OrphanedHostCleanupJob) deleteOrphanedHosts(ctx context.Context, _ ...Option) (int, error) {
|
|
const op = "plugin.(OrphanedHostCleanupJob).deleteOrphanedHosts"
|
|
|
|
query := `
|
|
public_id in
|
|
(select public_id
|
|
from host_plugin_host
|
|
where public_id not in
|
|
(select host_id from host_plugin_set_member))
|
|
`
|
|
|
|
var hostAggs []*hostAgg
|
|
err := r.reader.SearchWhere(ctx, &hostAggs, query, nil)
|
|
switch {
|
|
case err != nil:
|
|
return db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
|
case len(hostAggs) == 0:
|
|
return db.NoRowsAffected, nil
|
|
}
|
|
|
|
projectToHost := make(map[string][]*Host)
|
|
for _, ha := range hostAggs {
|
|
h := allocHost()
|
|
h.PublicId = ha.PublicId
|
|
projectToHost[ha.ProjectId] = append(projectToHost[ha.ProjectId], h)
|
|
}
|
|
|
|
_, err = r.writer.DoTx(
|
|
ctx,
|
|
db.StdRetryCnt,
|
|
db.ExpBackoff{},
|
|
func(_ db.Reader, w db.Writer) error {
|
|
for projectId, hosts := range projectToHost {
|
|
oplogWrapper, err := r.kms.GetWrapper(ctx, projectId, kms.KeyPurposeOplog)
|
|
if err != nil {
|
|
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
|
}
|
|
for _, h := range hosts {
|
|
metadata := h.oplog(oplog.OpType_OP_TYPE_DELETE)
|
|
dHost := h.clone()
|
|
if _, err := w.Delete(ctx, dHost, db.WithOplog(oplogWrapper, metadata)); err != nil {
|
|
return errors.Wrap(ctx, err, op)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
|
}
|
|
|
|
return len(hostAggs), nil
|
|
}
|