Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
infering the baseline flow ID from base cluster topology namespace value
Browse files Browse the repository at this point in the history
  • Loading branch information
leoporoli committed Sep 11, 2024
1 parent 07346c8 commit 9e86ca1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 81 deletions.
34 changes: 9 additions & 25 deletions kontrol-service/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,15 @@ func (sv *Server) DeleteTenantUuidFlowFlowId(_ context.Context, request api.Dele
logrus.Infof("deleting dev flow for tenant '%s'", request.Uuid)
sv.analyticsWrapper.TrackEvent(EVENT_FLOW_DELETE, request.Uuid)

_, allFlows, _, _, _, err := getTenantTopologies(sv, request.Uuid)
baseClusterTopology, allFlows, _, _, _, err := getTenantTopologies(sv, request.Uuid)
if err != nil {
resourceType := "tenant"
missing := api.NotFoundJSONResponse{ResourceType: resourceType, Id: request.Uuid}
return api.DeleteTenantUuidFlowFlowId404JSONResponse{NotFoundJSONResponse: missing}, nil
}

flowObj, err := sv.db.GetFlow(request.FlowId)
if err != nil {
errMsg := fmt.Sprintf("An error occurred getting flow '%v'", request.FlowId)
errResp := api.ErrorJSONResponse{
Error: err.Error(),
Msg: &errMsg,
}
return api.DeleteTenantUuidFlowFlowId500JSONResponse{errResp}, nil
}

if flowObj.IsBaselineFlow() {
// the baseline flow ID uses the base cluster topology namespace name
if request.FlowId == baseClusterTopology.Namespace {
// We received a request to delete the base topology, so we do that + the flows
err = deleteTenantTopologies(sv, request.Uuid)
if err != nil {
Expand Down Expand Up @@ -470,12 +461,10 @@ func applyProdDevFlow(sv *Server, tenantUuidStr string, patches []flow_spec.Serv
}
serviceConfigs = template.ApplyTemplateOverrides(serviceConfigs, templateSpec)

baselineFlow, err := sv.db.GetBaselineFlow()
if err != nil {
return nil, []string{}, stacktrace.Propagate(err, "an error occurred getting the baseline flow")
}
// the baseline flow ID uses the base cluster topology namespace name
baselineFlowID := baseClusterTopologyMaybeWithTemplateOverrides.Namespace

baseClusterTopologyWithTemplateOverridesPtr, err := engine.GenerateProdOnlyCluster(baselineFlow.FlowId, serviceConfigs, ingressConfigs, baseTopology.Namespace)
baseClusterTopologyWithTemplateOverridesPtr, err := engine.GenerateProdOnlyCluster(baselineFlowID, serviceConfigs, ingressConfigs, baseTopology.Namespace)
if err != nil {
return nil, []string{}, fmt.Errorf("an error occurred while creating base cluster topology from templates:\n %s", err)
}
Expand Down Expand Up @@ -518,7 +507,7 @@ func applyProdDevFlow(sv *Server, tenantUuidStr string, patches []flow_spec.Serv
// - Templates
// - Base service configs
// - Base ingress configs
// TOOD: Could return a struct if it becomes too heavy to manipulate the return values.
// TODO: Could return a struct if it becomes too heavy to manipulate the return values.
func getTenantTopologies(sv *Server, tenantUuidStr string) (*resolved.ClusterTopology, map[string]resolved.ClusterTopology, map[string]templates.Template, []apitypes.ServiceConfig, []apitypes.IngressConfig, error) {
tenant, err := sv.db.GetTenant(tenantUuidStr)
if err != nil {
Expand Down Expand Up @@ -560,13 +549,8 @@ func getTenantTopologies(sv *Server, tenantUuidStr string) (*resolved.ClusterTop
return nil, nil, nil, nil, nil, err
}
} else {
baselineFlow, err := sv.db.GetBaselineFlow()
if err != nil {
return nil, nil, nil, nil, nil, stacktrace.Propagate(err, "an error occurred getting the baseline flow")
}

baseClusterTopology.FlowID = baselineFlow.FlowId
baseClusterTopology.Namespace = baselineFlow.FlowId
baseClusterTopology.FlowID = defaultBaselineFlowId
baseClusterTopology.Namespace = defaultBaselineFlowId
}

var serviceConfigs []apitypes.ServiceConfig
Expand Down
56 changes: 0 additions & 56 deletions kontrol-service/database/flow.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
package database

import (
"errors"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
"gorm.io/datatypes"
"gorm.io/gorm"
)

const (
// TODO retro compatibility value, we should remove this when we are sure that none is using it
// TODO this was the first name of the default baseline which was used for all the baseline flows at the beginning, now we use the namespace value
oldDefaultBaselineFlowId = "prod"
)

type Flow struct {
gorm.Model
FlowId string `gorm:"uniqueIndex:idx_tenant_flow"`
ClusterTopology datatypes.JSON
TenantId string `gorm:"uniqueIndex:idx_tenant_flow"`
IsBaseline bool
}

func (f *Flow) IsBaselineFlow() bool {
// TODO retro compatibility value, we should remove this when we are sure that none is using it
if f.FlowId == oldDefaultBaselineFlowId {
return true
}
return f.IsBaseline
}

func (db *Db) CreateFlow(
Expand All @@ -48,46 +32,6 @@ func (db *Db) CreateFlow(
return flow, nil
}

func (db *Db) GetFlow(flowId string) (*Flow, error) {
var flow Flow
result := db.db.Where("flow_id = ?", flowId).First(&flow)
if result.Error != nil {
return nil, stacktrace.Propagate(result.Error, "An internal error has occurred getting the flow '%v'", flowId)
}
return &flow, nil
}

func (db *Db) GetFlows() ([]Flow, error) {
var flows []Flow
result := db.db.Find(&flows)
if result.Error != nil {
return nil, stacktrace.Propagate(result.Error, "An internal error has occurred getting all flows")
}
return flows, nil
}

func (db *Db) GetBaselineFlow() (*Flow, error) {
var flow Flow
result := db.db.Where("is_baseline = ?", true).First(&flow)
if result.Error != nil {
// TODO retro compatibility implementation, we should remove this when we are sure that none is using it
// TODO we infer the baselineFlow if flowID = "prod" which was the first implementation for baseline flow
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
allFlows, err := db.GetFlows()
if err != nil {
return nil, stacktrace.Propagate(err, "An internal error has occurred getting all flows to infer the baseline flow")
}
for _, flowObj := range allFlows {
if flowObj.IsBaselineFlow() {
return &flowObj, nil
}
}
}
return nil, stacktrace.Propagate(result.Error, "An internal error has occurred getting the baseline flow")
}
return &flow, nil
}

func (db *Db) DeleteFlow(
tenantId string,
flowId string,
Expand Down

0 comments on commit 9e86ca1

Please sign in to comment.