Skip to content

Commit

Permalink
ROX-21070: Add fleetshard addon installation status to private api (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kovayur authored Dec 19, 2023
1 parent 17de9b2 commit e0a0005
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 82 deletions.
2 changes: 1 addition & 1 deletion fleetshard/pkg/central/reconciler/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func centralDBPasswordSecretObject() *v1.Secret {
}
}

func conditionForType(conditions []private.DataPlaneClusterUpdateStatusRequestConditions, conditionType string) (*private.DataPlaneClusterUpdateStatusRequestConditions, bool) {
func conditionForType(conditions []private.DataPlaneCentralStatusConditions, conditionType string) (*private.DataPlaneCentralStatusConditions, bool) {
for _, c := range conditions {
if c.Type == conditionType {
return &c, true
Expand Down
6 changes: 3 additions & 3 deletions fleetshard/pkg/central/reconciler/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/api/private"

func readyStatus() *private.DataPlaneCentralStatus {
return &private.DataPlaneCentralStatus{
Conditions: []private.DataPlaneClusterUpdateStatusRequestConditions{
Conditions: []private.DataPlaneCentralStatusConditions{
{
Type: "Ready",
Status: "True",
Expand All @@ -15,7 +15,7 @@ func readyStatus() *private.DataPlaneCentralStatus {

func deletedStatus() *private.DataPlaneCentralStatus {
return &private.DataPlaneCentralStatus{
Conditions: []private.DataPlaneClusterUpdateStatusRequestConditions{
Conditions: []private.DataPlaneCentralStatusConditions{
{
Type: "Ready",
Status: "False",
Expand All @@ -27,7 +27,7 @@ func deletedStatus() *private.DataPlaneCentralStatus {

func installingStatus() *private.DataPlaneCentralStatus {
return &private.DataPlaneCentralStatus{
Conditions: []private.DataPlaneClusterUpdateStatusRequestConditions{
Conditions: []private.DataPlaneCentralStatusConditions{
{
Type: "Ready",
Status: "False",
Expand Down
19 changes: 10 additions & 9 deletions internal/dinosaur/pkg/api/dbapi/data_plane_cluster_status.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package dbapi

// DataPlaneClusterStatus ...
type DataPlaneClusterStatus struct {
Conditions []DataPlaneClusterStatusCondition
// AddonInstallation represents the actual information about addons installed on the cluster
type AddonInstallation struct {
ID string
Version string
SourceImage string
PackageImage string
ParametersSHA256Sum string
}

// DataPlaneClusterStatusCondition ...
type DataPlaneClusterStatusCondition struct {
Type string
Reason string
Status string
Message string
// DataPlaneClusterStatus is the actual state reported from the data plane cluster
type DataPlaneClusterStatus struct {
Addons []AddonInstallation
}

// DataPlaneClusterConfigObservability ...
Expand Down
50 changes: 32 additions & 18 deletions internal/dinosaur/pkg/api/private/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -345,20 +345,21 @@ components:
DataPlaneClusterUpdateStatusRequest:
description: Schema for the request to update a data plane cluster's status
example:
conditions:
- reason: reason
type: type
message: message
status: status
- reason: reason
type: type
message: message
status: status
addons:
- packageImage: packageImage
parametersSHA256Sum: parametersSHA256Sum
id: id
sourceImage: sourceImage
version: version
- packageImage: packageImage
parametersSHA256Sum: parametersSHA256Sum
id: id
sourceImage: sourceImage
version: version
properties:
conditions:
description: The cluster data plane conditions
addons:
items:
$ref: '#/components/schemas/DataPlaneClusterUpdateStatusRequest_conditions'
$ref: '#/components/schemas/DataPlaneClusterUpdateStatusRequest_addons'
type: array
type: object
DataPlaneCentralStatus:
Expand All @@ -369,7 +370,7 @@ components:
conditions:
description: The status conditions of a Central
items:
$ref: '#/components/schemas/DataPlaneClusterUpdateStatusRequest_conditions'
$ref: '#/components/schemas/DataPlaneCentralStatus_conditions'
type: array
routes:
description: Routes created for a Central
Expand Down Expand Up @@ -514,12 +515,25 @@ components:
type: array
rhacs_operators:
$ref: '#/components/schemas/RHACSOperatorConfigs'
DataPlaneClusterUpdateStatusRequest_conditions:
DataPlaneClusterUpdateStatusRequest_addons:
example:
reason: reason
type: type
message: message
status: status
packageImage: packageImage
parametersSHA256Sum: parametersSHA256Sum
id: id
sourceImage: sourceImage
version: version
properties:
id:
type: string
version:
type: string
sourceImage:
type: string
packageImage:
type: string
parametersSHA256Sum:
type: string
DataPlaneCentralStatus_conditions:
properties:
type:
type: string
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions internal/dinosaur/pkg/handlers/data_plane_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ func (h *dataPlaneClusterHandler) UpdateDataPlaneClusterStatus(w http.ResponseWr
handlers.ValidateLength(&dataPlaneClusterID, "id", &handlers.MinRequiredFieldLength, nil),
},
Action: func() (interface{}, *errors.ServiceError) {
ctx := r.Context()
dataPlaneClusterStatus, err := presenters.ConvertDataPlaneClusterStatus(dataPlaneClusterUpdateRequest)
if err != nil {
return nil, errors.Validation(err.Error())
}
svcErr := h.service.UpdateDataPlaneClusterStatus(ctx, dataPlaneClusterID, dataPlaneClusterStatus)
dataPlaneClusterStatus := presenters.ConvertDataPlaneClusterStatus(dataPlaneClusterUpdateRequest)
svcErr := h.service.UpdateDataPlaneClusterStatus(dataPlaneClusterID, dataPlaneClusterStatus)
return nil, svcErr
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package migrations

import (
"github.com/go-gormigrate/gormigrate/v2"
"github.com/pkg/errors"
"github.com/stackrox/acs-fleet-manager/pkg/api"
"github.com/stackrox/acs-fleet-manager/pkg/db"
"gorm.io/gorm"
)

func addClusterAddons() *gormigrate.Migration {
type Cluster struct {
db.Model
Addons api.JSON `json:"addons"`
}

migrationID := "20231123000000"
colName := "Addons"

return &gormigrate.Migration{
ID: migrationID,
Migrate: func(tx *gorm.DB) error {
if !tx.Migrator().HasColumn(&Cluster{}, colName) {
if err := tx.Migrator().AddColumn(&Cluster{}, colName); err != nil {
return errors.Wrapf(err, "adding column %s in migration %s", colName, migrationID)
}
}
return nil
},
Rollback: func(tx *gorm.DB) error {
if tx.Migrator().HasColumn(&Cluster{}, colName) {
if err := tx.Migrator().DropColumn(&Cluster{}, colName); err != nil {
return errors.Wrapf(err, "rolling back from column %s in migration %s", colName, migrationID)
}
}
return nil
},
}
}
1 change: 1 addition & 0 deletions internal/dinosaur/pkg/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func getMigrations() []*gormigrate.Migration {
removeCentralAndScannerOverrides(),
addExpiredAtFieldToCentralRequests(),
addExpirationLeaseType(),
addClusterAddons(),
}
}

Expand Down
26 changes: 14 additions & 12 deletions internal/dinosaur/pkg/presenters/data_plane_cluster_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ import (
)

// ConvertDataPlaneClusterStatus ...
func ConvertDataPlaneClusterStatus(status private.DataPlaneClusterUpdateStatusRequest) (*dbapi.DataPlaneClusterStatus, error) {
var res dbapi.DataPlaneClusterStatus
res.Conditions = make([]dbapi.DataPlaneClusterStatusCondition, len(status.Conditions))
for i, cond := range status.Conditions {
res.Conditions[i] = dbapi.DataPlaneClusterStatusCondition{
Type: cond.Type,
Reason: cond.Reason,
Status: cond.Status,
Message: cond.Message,
}
func ConvertDataPlaneClusterStatus(status private.DataPlaneClusterUpdateStatusRequest) dbapi.DataPlaneClusterStatus {
var addonInstallations []dbapi.AddonInstallation
for _, addon := range status.Addons {
addonInstallations = append(addonInstallations, dbapi.AddonInstallation{
ID: addon.Id,
Version: addon.Version,
SourceImage: addon.SourceImage,
PackageImage: addon.PackageImage,
ParametersSHA256Sum: addon.ParametersSHA256Sum,
})
}

return dbapi.DataPlaneClusterStatus{
Addons: addonInstallations,
}
return &res, nil
}

// PresentDataPlaneClusterConfig ...
Expand All @@ -32,6 +35,5 @@ func PresentDataPlaneClusterConfig(config *dbapi.DataPlaneClusterConfig) private
},
},
}

return res
}
31 changes: 11 additions & 20 deletions internal/dinosaur/pkg/services/data_plane_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ package services

import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/goava/di"
"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/pkg/client/observatorium"

"github.com/stackrox/acs-fleet-manager/pkg/metrics"

"github.com/stackrox/acs-fleet-manager/pkg/api"
"github.com/stackrox/acs-fleet-manager/pkg/errors"
)

// DataPlaneClusterService ...
type DataPlaneClusterService interface {
UpdateDataPlaneClusterStatus(ctx context.Context, clusterID string, status *dbapi.DataPlaneClusterStatus) *errors.ServiceError
UpdateDataPlaneClusterStatus(clusterID string, status dbapi.DataPlaneClusterStatus) *errors.ServiceError
GetDataPlaneClusterConfig(ctx context.Context, clusterID string) (*dbapi.DataPlaneClusterConfig, *errors.ServiceError)
}

Expand Down Expand Up @@ -61,7 +59,7 @@ func (d *dataPlaneClusterService) GetDataPlaneClusterConfig(ctx context.Context,
}

// UpdateDataPlaneClusterStatus ...
func (d *dataPlaneClusterService) UpdateDataPlaneClusterStatus(ctx context.Context, clusterID string, status *dbapi.DataPlaneClusterStatus) *errors.ServiceError {
func (d *dataPlaneClusterService) UpdateDataPlaneClusterStatus(clusterID string, status dbapi.DataPlaneClusterStatus) *errors.ServiceError {
cluster, svcErr := d.ClusterService.FindClusterByID(clusterID)
if svcErr != nil {
return svcErr
Expand All @@ -71,9 +69,6 @@ func (d *dataPlaneClusterService) UpdateDataPlaneClusterStatus(ctx context.Conte
return errors.BadRequest("Cluster agent with ID '%s' not found", clusterID)
}

// We calculate the status based on the stats received by the Fleet operator
// BEFORE performing the scaling actions. If scaling actions are performed later
// then it will be reflected on the next data plane cluster status report
err := d.setClusterStatus(cluster, status)
if err != nil {
return errors.ToServiceError(err)
Expand All @@ -82,19 +77,15 @@ func (d *dataPlaneClusterService) UpdateDataPlaneClusterStatus(ctx context.Conte
return nil
}

func (d *dataPlaneClusterService) setClusterStatus(cluster *api.Cluster, status *dbapi.DataPlaneClusterStatus) error {
if cluster.Status != api.ClusterReady {
clusterIsWaitingForFleetShardOperator := cluster.Status == api.ClusterWaitingForFleetShardOperator
err := d.ClusterService.UpdateStatus(*cluster, api.ClusterReady)
if err != nil {
return fmt.Errorf("updating cluster status to %s: %w", api.ClusterReady, err)
}
if clusterIsWaitingForFleetShardOperator {
metrics.UpdateClusterCreationDurationMetric(metrics.JobTypeClusterCreate, time.Since(cluster.CreatedAt))
}
metrics.UpdateClusterStatusSinceCreatedMetric(*cluster, api.ClusterReady)
func (d *dataPlaneClusterService) setClusterStatus(cluster *api.Cluster, status dbapi.DataPlaneClusterStatus) error {
addonsJSON, err := json.Marshal(status.Addons)
if err != nil {
return fmt.Errorf("marshal fleetshardAddonStatus to JSON: %w", err)
}
cluster.Addons = addonsJSON
if err := d.ClusterService.Update(*cluster); err != nil {
return fmt.Errorf("updating cluster status: %w", err)
}

return nil
}

Expand Down
Loading

0 comments on commit e0a0005

Please sign in to comment.