github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/tenantfetchersvc/resync/builder.go (about) 1 package resync 2 3 import ( 4 "context" 5 6 "github.com/kyma-incubator/compass/components/director/internal/domain/api" 7 "github.com/kyma-incubator/compass/components/director/internal/domain/application" 8 "github.com/kyma-incubator/compass/components/director/internal/domain/auth" 9 bundleutil "github.com/kyma-incubator/compass/components/director/internal/domain/bundle" 10 "github.com/kyma-incubator/compass/components/director/internal/domain/document" 11 "github.com/kyma-incubator/compass/components/director/internal/domain/eventdef" 12 "github.com/kyma-incubator/compass/components/director/internal/domain/fetchrequest" 13 "github.com/kyma-incubator/compass/components/director/internal/domain/formation" 14 "github.com/kyma-incubator/compass/components/director/internal/domain/formationassignment" 15 "github.com/kyma-incubator/compass/components/director/internal/domain/formationconstraint" 16 "github.com/kyma-incubator/compass/components/director/internal/domain/formationconstraint/operators" 17 "github.com/kyma-incubator/compass/components/director/internal/domain/formationtemplate" 18 "github.com/kyma-incubator/compass/components/director/internal/domain/formationtemplateconstraintreferences" 19 "github.com/kyma-incubator/compass/components/director/internal/domain/label" 20 "github.com/kyma-incubator/compass/components/director/internal/domain/labeldef" 21 "github.com/kyma-incubator/compass/components/director/internal/domain/runtime" 22 runtimectx "github.com/kyma-incubator/compass/components/director/internal/domain/runtime_context" 23 "github.com/kyma-incubator/compass/components/director/internal/domain/scenarioassignment" 24 "github.com/kyma-incubator/compass/components/director/internal/domain/spec" 25 "github.com/kyma-incubator/compass/components/director/internal/domain/tenant" 26 "github.com/kyma-incubator/compass/components/director/internal/domain/version" 27 "github.com/kyma-incubator/compass/components/director/internal/domain/webhook" 28 "github.com/kyma-incubator/compass/components/director/internal/features" 29 "github.com/kyma-incubator/compass/components/director/internal/uid" 30 graphqlclient "github.com/kyma-incubator/compass/components/director/pkg/graphql_client" 31 "github.com/kyma-incubator/compass/components/director/pkg/persistence" 32 tenantpkg "github.com/kyma-incubator/compass/components/director/pkg/tenant" 33 "github.com/pkg/errors" 34 ) 35 36 type synchronizerBuilder struct { 37 jobConfig JobConfig 38 featuresConfig features.Config 39 transact persistence.Transactioner 40 directorClient *graphqlclient.Director 41 aggregationFailurePusher AggregationFailurePusher 42 } 43 44 // NewSynchronizerBuilder returns an entity that will use the provided configuration to create a tenant synchronizer. 45 func NewSynchronizerBuilder(jobConfig JobConfig, featuresConfig features.Config, transact persistence.Transactioner, directorClient *graphqlclient.Director, aggregationFailurePusher AggregationFailurePusher) *synchronizerBuilder { 46 return &synchronizerBuilder{ 47 jobConfig: jobConfig, 48 featuresConfig: featuresConfig, 49 transact: transact, 50 directorClient: directorClient, 51 aggregationFailurePusher: aggregationFailurePusher, 52 } 53 } 54 55 // Build returns a tenants synchronizer created with the initially provided configuration of the builder. 56 func (b *synchronizerBuilder) Build(ctx context.Context) (*TenantsSynchronizer, error) { 57 kubeClient, err := NewKubernetesClient(ctx, b.jobConfig.KubeConfig) 58 if err != nil { 59 return nil, errors.Wrapf(err, "while creating Kubernetes client for job %s", b.jobConfig.JobName) 60 } 61 62 universalEventAPIClient, additionalRegionalEventAPIClients, err := b.eventAPIClients() 63 if err != nil { 64 return nil, err 65 } 66 67 tenantSvc, tenantConverter, runtimeSvc, labelRepo := b.domainServices(b.featuresConfig) 68 tenantManager, err := NewTenantsManager(b.jobConfig, b.directorClient, universalEventAPIClient, additionalRegionalEventAPIClients, tenantConverter) 69 if err != nil { 70 return nil, err 71 } 72 73 var mover TenantMover 74 if b.jobConfig.TenantType == tenantpkg.Subaccount { 75 mover = NewSubaccountsMover(b.jobConfig, b.transact, b.directorClient, universalEventAPIClient, tenantConverter, tenantSvc, runtimeSvc, labelRepo) 76 } else { 77 mover = newNoOpsMover() 78 } 79 80 ts := NewTenantSynchronizer(b.jobConfig, b.transact, tenantSvc, tenantManager, mover, tenantManager, kubeClient, b.aggregationFailurePusher) 81 return ts, nil 82 } 83 84 func (b *synchronizerBuilder) eventAPIClients() (EventAPIClient, map[string]EventAPIClient, error) { 85 clientCfg := ClientConfig{ 86 TenantProvider: b.jobConfig.TenantProvider, 87 APIConfig: b.jobConfig.APIConfig.APIEndpointsConfig, 88 FieldMapping: b.jobConfig.APIConfig.TenantFieldMapping, 89 MovedSAFieldMapping: b.jobConfig.APIConfig.MovedSubaccountsFieldMapping, 90 } 91 eventAPIClient, err := NewClient(b.jobConfig.APIConfig.OAuthConfig, b.jobConfig.APIConfig.AuthMode, clientCfg, b.jobConfig.APIConfig.ClientTimeout) 92 if err != nil { 93 return nil, nil, errors.Wrap(err, "while creating a Event API client") 94 } 95 96 regionalEventAPIClients := map[string]EventAPIClient{} 97 for _, config := range b.jobConfig.RegionalAPIConfigs { 98 clientConfig := ClientConfig{ 99 TenantProvider: b.jobConfig.TenantProvider, 100 APIConfig: config.APIEndpointsConfig, 101 FieldMapping: config.TenantFieldMapping, 102 MovedSAFieldMapping: config.MovedSubaccountsFieldMapping, 103 } 104 eventAPIClient, err := NewClient(config.OAuthConfig, config.AuthMode, clientConfig, config.ClientTimeout) 105 if err != nil { 106 return nil, nil, errors.Wrapf(err, "while creating a regional Event API client for region %s", config.RegionName) 107 } 108 regionalEventAPIClients[config.RegionName] = eventAPIClient 109 } 110 111 return eventAPIClient, regionalEventAPIClients, nil 112 } 113 114 func (b *synchronizerBuilder) domainServices(featuresConfig features.Config) (TenantStorageService, TenantConverter, RuntimeService, LabelRepo) { 115 uidSvc := uid.NewService() 116 117 labelDefConverter := labeldef.NewConverter() 118 tenantStorageConverter := tenant.NewConverter() 119 labelConverter := label.NewConverter() 120 authConverter := auth.NewConverter() 121 webhookConverter := webhook.NewConverter(authConverter) 122 frConverter := fetchrequest.NewConverter(authConverter) 123 versionConverter := version.NewConverter() 124 specConverter := spec.NewConverter(frConverter) 125 docConverter := document.NewConverter(frConverter) 126 apiConverter := api.NewConverter(versionConverter, specConverter) 127 eventAPIConverter := eventdef.NewConverter(versionConverter, specConverter) 128 bundleConverter := bundleutil.NewConverter(authConverter, apiConverter, eventAPIConverter, docConverter) 129 appConverter := application.NewConverter(webhookConverter, bundleConverter) 130 runtimeConverter := runtime.NewConverter(webhookConverter) 131 scenarioAssignConverter := scenarioassignment.NewConverter() 132 runtimeContextConverter := runtimectx.NewConverter() 133 tenantConverter := tenant.NewConverter() 134 formationConv := formation.NewConverter() 135 formationConstraintConverter := formationconstraint.NewConverter() 136 formationTemplateConstraintReferencesConverter := formationtemplateconstraintreferences.NewConverter() 137 formationTemplateConverter := formationtemplate.NewConverter(webhookConverter) 138 139 webhookRepo := webhook.NewRepository(webhookConverter) 140 labelDefRepo := labeldef.NewRepository(labelDefConverter) 141 labelRepo := label.NewRepository(labelConverter) 142 tenantStorageRepo := tenant.NewRepository(tenantStorageConverter) 143 applicationRepo := application.NewRepository(appConverter) 144 runtimeRepo := runtime.NewRepository(runtimeConverter) 145 scenarioAssignmentRepo := scenarioassignment.NewRepository(scenarioAssignConverter) 146 runtimeContextRepo := runtimectx.NewRepository(runtimeContextConverter) 147 tenantRepo := tenant.NewRepository(tenantConverter) 148 formationRepo := formation.NewRepository(formationConv) 149 formationTemplateRepo := formationtemplate.NewRepository(formationTemplateConverter) 150 formationConstraintRepo := formationconstraint.NewRepository(formationConstraintConverter) 151 formationTemplateConstraintReferencesRepo := formationtemplateconstraintreferences.NewRepository(formationTemplateConstraintReferencesConverter) 152 153 labelSvc := label.NewLabelService(labelRepo, labelDefRepo, uidSvc) 154 tenantStorageSvc := tenant.NewServiceWithLabels(tenantStorageRepo, uidSvc, labelRepo, labelSvc, tenantStorageConverter) 155 tenantSvc := tenant.NewServiceWithLabels(tenantRepo, uidSvc, labelRepo, labelSvc, tenantConverter) 156 webhookSvc := webhook.NewService(webhookRepo, applicationRepo, uidSvc, tenantSvc, nil, "") 157 labelDefSvc := labeldef.NewService(labelDefRepo, labelRepo, scenarioAssignmentRepo, tenantStorageRepo, uidSvc) 158 scenarioAssignmentSvc := scenarioassignment.NewService(scenarioAssignmentRepo, labelDefSvc) 159 formationAssignmentConv := formationassignment.NewConverter() 160 formationAssignmentRepo := formationassignment.NewRepository(formationAssignmentConv) 161 formationConstraintSvc := formationconstraint.NewService(formationConstraintRepo, formationTemplateConstraintReferencesRepo, uidSvc, formationConstraintConverter) 162 constraintEngine := operators.NewConstraintEngine(b.transact, formationConstraintSvc, tenantSvc, scenarioAssignmentSvc, nil, formationRepo, labelRepo, labelSvc, applicationRepo, runtimeContextRepo, formationTemplateRepo, formationAssignmentRepo, featuresConfig.RuntimeTypeLabelKey, featuresConfig.ApplicationTypeLabelKey) 163 notificationsBuilder := formation.NewNotificationsBuilder(webhookConverter, constraintEngine, featuresConfig.RuntimeTypeLabelKey, featuresConfig.ApplicationTypeLabelKey) 164 faNotificationSvc := formationassignment.NewFormationAssignmentNotificationService(formationAssignmentRepo, webhookConverter, webhookRepo, tenantRepo, nil, formationRepo, notificationsBuilder, runtimeContextRepo, labelSvc, featuresConfig.RuntimeTypeLabelKey, featuresConfig.ApplicationTypeLabelKey) 165 formationAssignmentStatusSvc := formationassignment.NewFormationAssignmentStatusService(formationAssignmentRepo, constraintEngine, faNotificationSvc) 166 formationAssignmentSvc := formationassignment.NewService(formationAssignmentRepo, uidSvc, applicationRepo, runtimeRepo, runtimeContextRepo, nil, faNotificationSvc, labelSvc, formationRepo, formationAssignmentStatusSvc, featuresConfig.RuntimeTypeLabelKey, featuresConfig.ApplicationTypeLabelKey) 167 formationSvc := formation.NewService(b.transact, applicationRepo, labelDefRepo, labelRepo, formationRepo, formationTemplateRepo, labelSvc, uidSvc, labelDefSvc, scenarioAssignmentRepo, scenarioAssignmentSvc, tenantSvc, runtimeRepo, runtimeContextRepo, formationAssignmentSvc, faNotificationSvc, nil, constraintEngine, webhookRepo, nil, featuresConfig.RuntimeTypeLabelKey, featuresConfig.ApplicationTypeLabelKey) 168 runtimeContextSvc := runtimectx.NewService(runtimeContextRepo, labelRepo, runtimeRepo, labelSvc, formationSvc, tenantSvc, uidSvc) 169 runtimeSvc := runtime.NewService(runtimeRepo, labelRepo, labelSvc, uidSvc, formationSvc, tenantStorageSvc, webhookSvc, runtimeContextSvc, featuresConfig.ProtectedLabelPattern, featuresConfig.ImmutableLabelPattern, featuresConfig.RuntimeTypeLabelKey, featuresConfig.KymaRuntimeTypeLabelValue, featuresConfig.KymaApplicationNamespaceValue) 170 171 return tenantSvc, tenantConverter, runtimeSvc, labelRepo 172 }