Skip to content

Commit

Permalink
just query
Browse files Browse the repository at this point in the history
  • Loading branch information
parametalol committed Sep 12, 2023
1 parent 651de9d commit 0e41b9a
Showing 1 changed file with 18 additions and 54 deletions.
72 changes: 18 additions & 54 deletions internal/dinosaur/pkg/services/dinosaur.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@ import (
"sync"
"time"

"github.com/aws/aws-sdk-go/service/route53"
"github.com/golang/glog"
dinosaurConstants "github.com/stackrox/acs-fleet-manager/internal/dinosaur/constants"
"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/api/dbapi"
"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/config"
"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/dinosaurs/types"
"github.com/stackrox/acs-fleet-manager/pkg/services"
"github.com/stackrox/acs-fleet-manager/pkg/services/sso"

"github.com/stackrox/acs-fleet-manager/pkg/services/authorization"
coreServices "github.com/stackrox/acs-fleet-manager/pkg/services/queryparser"

"github.com/golang/glog"

"github.com/aws/aws-sdk-go/service/route53"
"github.com/stackrox/acs-fleet-manager/pkg/api"
"github.com/stackrox/acs-fleet-manager/pkg/auth"
"github.com/stackrox/acs-fleet-manager/pkg/client/aws"
Expand All @@ -28,6 +21,10 @@ import (
"github.com/stackrox/acs-fleet-manager/pkg/features"
"github.com/stackrox/acs-fleet-manager/pkg/logger"
"github.com/stackrox/acs-fleet-manager/pkg/metrics"
"github.com/stackrox/acs-fleet-manager/pkg/services"
"github.com/stackrox/acs-fleet-manager/pkg/services/authorization"
coreServices "github.com/stackrox/acs-fleet-manager/pkg/services/queryparser"
"github.com/stackrox/acs-fleet-manager/pkg/services/sso"
)

var (
Expand Down Expand Up @@ -502,62 +499,29 @@ func (k *dinosaurService) DeprovisionDinosaurForUsers(users []string) *errors.Se
return nil
}

func (k *dinosaurService) centralWithExpiredGracePeriod(centralRequest *dbapi.CentralRequest, currentTime time.Time) bool {
glog.V(10).Infof("Evaluating expiration time of central request '%s' with instance type '%s' and status '%s'",
centralRequest.ID, centralRequest.InstanceType, centralRequest.Status)
if currentTime.Sub(*centralRequest.GraceFrom) > gracePeriod {
glog.V(10).Infof("Central ID '%s' has expired", centralRequest.ID)
return true
}

glog.V(10).Infof("Central ID '%s' has not expired", centralRequest.ID)

return false
}

// DeprovisionExpiredDinosaurs cleaning up expired dinosaurs
func (k *dinosaurService) DeprovisionExpiredDinosaurs(dinosaurAgeInHours int) *errors.ServiceError {
now := time.Now()
var existingCentralRequests []dbapi.CentralRequest

dbConn := k.connectionFactory.New().
Model(&dbapi.CentralRequest{})

db := dbConn.
Where("instance_type = ?", types.EVAL.String()).
Where("created_at <= ?", now.Add(-1*time.Duration(dinosaurAgeInHours)*time.Hour)).
Where("status NOT IN (?)", dinosaurDeletionStatuses).
Or("grace_from IS NOT NULL").
Scan(&existingCentralRequests)
// WHERE ( instance_type = 'eval' AND created_at <= ? OR grace_from IS NOT NULL AND grace_from < ? ) AND status NOT IN ( 'deleting', 'deprovision' )
dbConn = dbConn.Where(
dbConn.Where("instance_type = ?", types.EVAL.String()).
Where("created_at <= ?", now.Add(-1*time.Duration(dinosaurAgeInHours)*time.Hour)).
Or(dbConn.Where("grace_from IS NOT NULL").
Where("grace_from < ", now.Add(-gracePeriod)))).
Where("status NOT IN (?)", dinosaurDeletionStatuses)

db := dbConn.Updates(map[string]interface{}{
"status": dinosaurConstants.CentralRequestStatusDeprovision,
"deletion_timestamp": now,
})
err := db.Error
if err != nil {
return errors.NewWithCause(errors.ErrorGeneral, err, "unable to deprovision expired centrals")
}

var centralsToDeprovisionIDs []string

for idx := range existingCentralRequests {
existingCentralRequest := &existingCentralRequests[idx]
shouldBeDeprovisioned := k.centralWithExpiredGracePeriod(existingCentralRequest, now)
if shouldBeDeprovisioned {
centralsToDeprovisionIDs = append(centralsToDeprovisionIDs, existingCentralRequest.ID)
}
}

if len(centralsToDeprovisionIDs) == 0 {
return nil
}

glog.V(10).Infof("Central IDs to mark with status %s: %+v", dinosaurConstants.CentralRequestStatusDeprovision, centralsToDeprovisionIDs)
db = dbConn.Where("id IN (?)", centralsToDeprovisionIDs).
Updates(map[string]interface{}{
"status": dinosaurConstants.CentralRequestStatusDeprovision,
"deletion_timestamp": now,
})
err = db.Error
if err != nil {
return errors.NewWithCause(errors.ErrorGeneral, err, "unable to deprovision expired centrals")
}
if db.RowsAffected >= 1 {
glog.Infof("%v central_request's lifespans are over %d hours and have had their status updated to deprovisioning", db.RowsAffected, dinosaurAgeInHours)
var counter int64
Expand Down

0 comments on commit 0e41b9a

Please sign in to comment.