feat: [CDE-220]: Adding gitspace instance identifier to infra provisioning. Adding checks to ensure infra status is always same as required. Adding infra status type error enum. Adding util method to derive gitspace container name. Cleaning parameters to use values instead of references. Refactoring fields for infra struct to make them more explicit. Adding config to read agent port and pass it on to infra provider for provisioning and finding. (#2411)

* feat: [CDE:220]: Adding util method to get working dir from repo name.
* feat: [CDE:220]: Addressing review comments.
* feat: [CDE-220]: Adding gitspace instance identifier to infra provisioning. Adding checks to ensure infra status is always same as required. Adding infra status type error enum. Adding util method to derive gitspace container name. Cleaning parameters to use values instead of references. Refactoring fields for infra struct to make them more explicit. Adding config to read agent port and pass it on to infra provider for provisioning and finding.
pull/3545/head
Dhruv Dhruv 2024-08-07 02:29:10 +00:00 committed by Harness
parent 0c5c478652
commit 52bfdfb3f8
25 changed files with 302 additions and 185 deletions

View File

@ -32,8 +32,8 @@ const (
type (
GitspaceInfraEventPayload struct {
Infra *types.Infrastructure `json:"infra,omitempty"`
Type enum.InfraEvent `json:"type"`
Infra types.Infrastructure `json:"infra,omitempty"`
Type enum.InfraEvent `json:"type"`
}
)

View File

@ -25,9 +25,9 @@ import (
func (i infraProvisioner) TriggerDeprovision(
ctx context.Context,
infraProviderResource *types.InfraProviderResource,
infraProviderResource types.InfraProviderResource,
gitspaceConfig types.GitspaceConfig,
infra *types.Infrastructure,
infra types.Infrastructure,
) error {
infraProviderEntity, err := i.getConfigFromResource(ctx, infraProviderResource)
if err != nil {
@ -49,7 +49,7 @@ func (i infraProvisioner) triggerDeprovisionForNewProvisioning(
ctx context.Context,
infraProvider infraprovider.InfraProvider,
gitspaceConfig types.GitspaceConfig,
infra *types.Infrastructure,
infra types.Infrastructure,
) error {
infraProvisionedLatest, err := i.infraProvisionedStore.FindLatestByGitspaceInstanceID(
ctx, gitspaceConfig.SpaceID, gitspaceConfig.GitspaceInstance.ID)
@ -75,7 +75,7 @@ func (i infraProvisioner) triggerDeprovisionForNewProvisioning(
func (i infraProvisioner) triggerDeprovisionForExistingProvisioning(
ctx context.Context,
infraProvider infraprovider.InfraProvider,
infra *types.Infrastructure,
infra types.Infrastructure,
) error {
err := infraProvider.Deprovision(ctx, infra)
if err != nil {
@ -88,7 +88,7 @@ func (i infraProvisioner) triggerDeprovisionForExistingProvisioning(
func (i infraProvisioner) ResumeDeprovision(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
deprovisionedInfra *types.Infrastructure,
deprovisionedInfra types.Infrastructure,
) error {
infraProvider, err := i.getInfraProvider(deprovisionedInfra.ProviderType)
if err != nil {
@ -104,7 +104,7 @@ func (i infraProvisioner) ResumeDeprovision(
func (i infraProvisioner) resumeDeprovisionForNewProvisioning(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
deprovisionedInfra *types.Infrastructure,
deprovisionedInfra types.Infrastructure,
) error {
err := i.updateInfraProvisionedRecord(ctx, gitspaceConfig, deprovisionedInfra)
if err != nil {

View File

@ -25,8 +25,9 @@ import (
func (i infraProvisioner) Find(
ctx context.Context,
infraProviderResource *types.InfraProviderResource,
infraProviderResource types.InfraProviderResource,
gitspaceConfig types.GitspaceConfig,
requiredGitspacePorts []int,
) (*types.Infrastructure, error) {
infraProviderEntity, err := i.getConfigFromResource(ctx, infraProviderResource)
if err != nil {
@ -38,20 +39,26 @@ func (i infraProvisioner) Find(
return nil, err
}
var params []types.InfraProviderParameter
var inputParams []types.InfraProviderParameter
var agentPort = 0
if infraProvider.ProvisioningType() == enum.InfraProvisioningTypeNew {
params, err = i.paramsForProvisioningTypeNew(ctx, gitspaceConfig)
inputParams, err = i.paramsForProvisioningTypeNew(ctx, gitspaceConfig)
if err != nil {
return nil, err
}
// TODO: What if the agent port has deviated from when the last instance was created?
agentPort = i.config.AgentPort
} else {
params, err = i.paramsForProvisioningTypeExisting(ctx, infraProviderResource, infraProvider)
inputParams, err = i.paramsForProvisioningTypeExisting(ctx, infraProviderResource, infraProvider)
if err != nil {
return nil, err
}
}
return infraProvider.Find(ctx, gitspaceConfig.SpaceID, gitspaceConfig.SpacePath, gitspaceConfig.Identifier, params)
return infraProvider.Find(ctx, gitspaceConfig.SpaceID, gitspaceConfig.SpacePath,
gitspaceConfig.Identifier, gitspaceConfig.GitspaceInstance.Identifier,
agentPort, requiredGitspacePorts, inputParams)
}
func (i infraProvisioner) paramsForProvisioningTypeNew(
@ -66,14 +73,14 @@ func (i infraProvisioner) paramsForProvisioningTypeNew(
gitspaceConfig.GitspaceInstance.ID, err)
}
allParams := stringToParams(infraProvisionedLatest.Params)
allParams := stringToParams(infraProvisionedLatest.InputParams)
return allParams, nil
}
func (i infraProvisioner) paramsForProvisioningTypeExisting(
ctx context.Context,
infraProviderResource *types.InfraProviderResource,
infraProviderResource types.InfraProviderResource,
infraProvider infraprovider.InfraProvider,
) ([]types.InfraProviderParameter, error) {
allParams, err := i.getAllParamsFromDB(ctx, infraProviderResource, infraProvider)

View File

@ -29,7 +29,7 @@ import (
func (i infraProvisioner) TriggerProvision(
ctx context.Context,
infraProviderResource *types.InfraProviderResource,
infraProviderResource types.InfraProviderResource,
gitspaceConfig types.GitspaceConfig,
requiredPorts []int,
) error {
@ -53,19 +53,14 @@ func (i infraProvisioner) TriggerProvision(
func (i infraProvisioner) triggerProvisionForNewProvisioning(
ctx context.Context,
infraProviderResource *types.InfraProviderResource,
infraProviderResource types.InfraProviderResource,
infraProvider infraprovider.InfraProvider,
infraProviderType enum.InfraProviderType,
gitspaceConfig types.GitspaceConfig,
requiredPorts []int,
requiredGitspacePorts []int,
) error {
infraProvisionedLatest, err := i.infraProvisionedStore.FindLatestByGitspaceInstanceID(
infraProvisionedLatest, _ := i.infraProvisionedStore.FindLatestByGitspaceInstanceID(
ctx, gitspaceConfig.SpaceID, gitspaceConfig.GitspaceInstance.ID)
if err != nil {
return fmt.Errorf(
"could not find latest infra provisioned entity for instance %d: %w",
gitspaceConfig.GitspaceInstance.ID, err)
}
if infraProvisionedLatest != nil &&
infraProvisionedLatest.InfraStatus == enum.InfraStatusPending &&
@ -73,7 +68,7 @@ func (i infraProvisioner) triggerProvisionForNewProvisioning(
return fmt.Errorf("there is already infra provisioning in pending state %d", infraProvisionedLatest.ID)
} else if infraProvisionedLatest != nil {
infraProvisionedLatest.InfraStatus = enum.InfraStatusUnknown
err = i.infraProvisionedStore.Update(ctx, infraProvisionedLatest)
err := i.infraProvisionedStore.Update(ctx, infraProvisionedLatest)
if err != nil {
return fmt.Errorf("could not update Infra Provisioned entity: %w", err)
}
@ -97,7 +92,7 @@ func (i infraProvisioner) triggerProvisionForNewProvisioning(
InfraProviderResourceID: infraProviderResource.ID,
Created: now.UnixMilli(),
Updated: now.UnixMilli(),
Params: paramsToString(allParams),
InputParams: paramsToString(allParams),
InfraStatus: enum.InfraStatusPending,
SpaceID: gitspaceConfig.SpaceID,
}
@ -107,12 +102,16 @@ func (i infraProvisioner) triggerProvisionForNewProvisioning(
return fmt.Errorf("unable to create infraProvisioned entry for %d", gitspaceConfig.GitspaceInstance.ID)
}
agentPort := i.config.AgentPort
err = infraProvider.Provision(
ctx,
gitspaceConfig.SpaceID,
gitspaceConfig.SpacePath,
gitspaceConfig.Identifier,
requiredPorts,
gitspaceConfig.GitspaceInstance.Identifier,
agentPort,
requiredGitspacePorts,
allParams,
)
if err != nil {
@ -135,7 +134,7 @@ func (i infraProvisioner) triggerProvisionForNewProvisioning(
func (i infraProvisioner) triggerProvisionForExistingProvisioning(
ctx context.Context,
infraProviderResource *types.InfraProviderResource,
infraProviderResource types.InfraProviderResource,
infraProvider infraprovider.InfraProvider,
gitspaceConfig types.GitspaceConfig,
requiredPorts []int,
@ -155,6 +154,8 @@ func (i infraProvisioner) triggerProvisionForExistingProvisioning(
gitspaceConfig.SpaceID,
gitspaceConfig.SpacePath,
gitspaceConfig.Identifier,
gitspaceConfig.GitspaceInstance.Identifier,
0, // NOTE: Agent port is not required for provisioning type Existing.
requiredPorts,
allParams,
)
@ -172,36 +173,36 @@ func (i infraProvisioner) triggerProvisionForExistingProvisioning(
func (i infraProvisioner) ResumeProvision(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
provisionedInfra *types.Infrastructure,
) (*types.Infrastructure, error) {
provisionedInfra types.Infrastructure,
) error {
infraProvider, err := i.getInfraProvider(provisionedInfra.ProviderType)
if err != nil {
return nil, err
return err
}
if infraProvider.ProvisioningType() == enum.InfraProvisioningTypeNew {
return i.resumeProvisionForNewProvisioning(ctx, gitspaceConfig, provisionedInfra)
}
return provisionedInfra, nil
return nil
}
func (i infraProvisioner) resumeProvisionForNewProvisioning(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
provisionedInfra *types.Infrastructure,
) (*types.Infrastructure, error) {
provisionedInfra types.Infrastructure,
) error {
infraProvisionedLatest, err := i.infraProvisionedStore.FindLatestByGitspaceInstanceID(
ctx, gitspaceConfig.SpaceID, gitspaceConfig.GitspaceInstance.ID)
if err != nil {
return nil, fmt.Errorf(
return fmt.Errorf(
"could not find latest infra provisioned entity for instance %d: %w",
gitspaceConfig.GitspaceInstance.ID, err)
}
responseMetadata, err := i.responseMetadata(*provisionedInfra)
responseMetadata, err := i.responseMetadata(provisionedInfra)
if err != nil {
return nil, err
return err
}
infraProvisionedLatest.InfraStatus = provisionedInfra.Status
@ -214,8 +215,8 @@ func (i infraProvisioner) resumeProvisionForNewProvisioning(
err = i.infraProvisionedStore.Update(ctx, infraProvisionedLatest)
if err != nil {
return nil, fmt.Errorf("unable to update infraProvisioned Entry %d", infraProvisionedLatest.ID)
return fmt.Errorf("unable to update infraProvisioned Entry %d", infraProvisionedLatest.ID)
}
return provisionedInfra, nil
return nil
}

View File

@ -27,51 +27,52 @@ type InfraProvisioner interface {
// infra providers.
TriggerProvision(
ctx context.Context,
infraProviderResource *types.InfraProviderResource,
infraProviderResource types.InfraProviderResource,
gitspaceConfig types.GitspaceConfig,
requiredPorts []int,
requiredGitspacePorts []int,
) error
// ResumeProvision stores the provisioned infra details in the db depending on the provisioning type.
ResumeProvision(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
provisionedInfra *types.Infrastructure,
) (*types.Infrastructure, error)
provisionedInfra types.Infrastructure,
) error
// TriggerStop triggers deprovisioning of those resources which can be stopped without losing the Gitspace data.
TriggerStop(
ctx context.Context,
infraProviderResource *types.InfraProviderResource,
infra *types.Infrastructure,
infraProviderResource types.InfraProviderResource,
infra types.Infrastructure,
) error
// ResumeStop stores the deprovisioned infra details in the db depending on the provisioning type.
ResumeStop(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
deprovisionedInfra *types.Infrastructure,
deprovisionedInfra types.Infrastructure,
) error
// TriggerDeprovision triggers deprovisionign of all the resources created for the Gitspace.
TriggerDeprovision(
ctx context.Context,
infraProviderResource *types.InfraProviderResource,
infraProviderResource types.InfraProviderResource,
gitspaceConfig types.GitspaceConfig,
infra *types.Infrastructure,
infra types.Infrastructure,
) error
// ResumeDeprovision stores the deprovisioned infra details in the db depending on the provisioning type.
ResumeDeprovision(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
deprovisionedInfra *types.Infrastructure,
deprovisionedInfra types.Infrastructure,
) error
// Find finds the provisioned infra resources for the gitspace instance.
Find(
ctx context.Context,
infraProviderResource *types.InfraProviderResource,
infraProviderResource types.InfraProviderResource,
gitspaceConfig types.GitspaceConfig,
requiredGitspacePorts []int,
) (*types.Infrastructure, error)
}

View File

@ -32,12 +32,17 @@ var _ InfraProvisioner = (*infraProvisioner)(nil)
const paramSeparator = "\n===============\n"
type Config struct {
AgentPort int
}
type infraProvisioner struct {
infraProviderConfigStore store.InfraProviderConfigStore
infraProviderResourceStore store.InfraProviderResourceStore
providerFactory infraprovider.Factory
infraProviderTemplateStore store.InfraProviderTemplateStore
infraProvisionedStore store.InfraProvisionedStore
config *Config
}
func NewInfraProvisionerService(
@ -46,6 +51,7 @@ func NewInfraProvisionerService(
providerFactory infraprovider.Factory,
infraProviderTemplateStore store.InfraProviderTemplateStore,
infraProvisionedStore store.InfraProvisionedStore,
config *Config,
) InfraProvisioner {
return &infraProvisioner{
infraProviderConfigStore: infraProviderConfigStore,
@ -53,17 +59,19 @@ func NewInfraProvisionerService(
providerFactory: providerFactory,
infraProviderTemplateStore: infraProviderTemplateStore,
infraProvisionedStore: infraProvisionedStore,
config: config,
}
}
func (i infraProvisioner) getConfigFromResource(
ctx context.Context,
infraProviderResource *types.InfraProviderResource,
infraProviderResource types.InfraProviderResource,
) (*types.InfraProviderConfig, error) {
config, err := i.infraProviderConfigStore.Find(ctx, infraProviderResource.InfraProviderConfigID)
if err != nil {
return nil, fmt.Errorf(
"unable to get infra provider details for ID %d: %w", infraProviderResource.InfraProviderConfigID, err)
"unable to get infra provider details for ID %d: %w",
infraProviderResource.InfraProviderConfigID, err)
}
return config, nil
}
@ -81,7 +89,7 @@ func (i infraProvisioner) getInfraProvider(
func (i infraProvisioner) getTemplateParams(
ctx context.Context,
infraProvider infraprovider.InfraProvider,
infraProviderResource *types.InfraProviderResource,
infraProviderResource types.InfraProviderResource,
) ([]types.InfraProviderParameter, error) {
var params []types.InfraProviderParameter
templateParams := infraProvider.TemplateParams()
@ -109,7 +117,7 @@ func (i infraProvisioner) getTemplateParams(
}
func (i infraProvisioner) paramsFromResource(
infraProviderResource *types.InfraProviderResource,
infraProviderResource types.InfraProviderResource,
) []types.InfraProviderParameter {
params := make([]types.InfraProviderParameter, 0)
for key, value := range infraProviderResource.Metadata {
@ -162,7 +170,7 @@ func (i infraProvisioner) responseMetadata(infra types.Infrastructure) (string,
func (i infraProvisioner) getAllParamsFromDB(
ctx context.Context,
infraProviderResource *types.InfraProviderResource,
infraProviderResource types.InfraProviderResource,
infraProvider infraprovider.InfraProvider,
) ([]types.InfraProviderParameter, error) {
var allParams []types.InfraProviderParameter
@ -184,7 +192,7 @@ func (i infraProvisioner) getAllParamsFromDB(
func (i infraProvisioner) updateInfraProvisionedRecord(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
deprovisionedInfra *types.Infrastructure,
deprovisionedInfra types.Infrastructure,
) error {
infraProvisionedLatest, err := i.infraProvisionedStore.FindLatestByGitspaceInstanceID(
ctx, gitspaceConfig.SpaceID, gitspaceConfig.GitspaceInstance.ID)
@ -194,7 +202,7 @@ func (i infraProvisioner) updateInfraProvisionedRecord(
gitspaceConfig.GitspaceInstance.ID, err)
}
responseMetadata, err := i.responseMetadata(*deprovisionedInfra)
responseMetadata, err := i.responseMetadata(deprovisionedInfra)
if err != nil {
return err
}

View File

@ -24,8 +24,8 @@ import (
func (i infraProvisioner) TriggerStop(
ctx context.Context,
infraProviderResource *types.InfraProviderResource,
infra *types.Infrastructure,
infraProviderResource types.InfraProviderResource,
infra types.Infrastructure,
) error {
infraProviderEntity, err := i.getConfigFromResource(ctx, infraProviderResource)
if err != nil {
@ -48,7 +48,7 @@ func (i infraProvisioner) TriggerStop(
func (i infraProvisioner) ResumeStop(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
stoppedInfra *types.Infrastructure,
stoppedInfra types.Infrastructure,
) error {
infraProvider, err := i.getInfraProvider(stoppedInfra.ProviderType)
if err != nil {
@ -65,7 +65,7 @@ func (i infraProvisioner) ResumeStop(
func (i infraProvisioner) resumeStopForNewProvisioning(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
stoppedInfra *types.Infrastructure,
stoppedInfra types.Infrastructure,
) error {
err := i.updateInfraProvisionedRecord(ctx, gitspaceConfig, stoppedInfra)
if err != nil {

View File

@ -32,6 +32,7 @@ func ProvideInfraProvisionerService(
providerFactory infraprovider.Factory,
infraProviderTemplateStore store.InfraProviderTemplateStore,
infraProvisionedStore store.InfraProvisionedStore,
config *Config,
) InfraProvisioner {
return NewInfraProvisionerService(
infraProviderConfig,
@ -39,5 +40,6 @@ func ProvideInfraProvisionerService(
providerFactory,
infraProviderTemplateStore,
infraProvisionedStore,
config,
)
}

View File

@ -29,18 +29,18 @@ type Orchestrator interface {
CreateAndStartGitspace(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
infra *types.Infrastructure,
resolvedDetails *scm.ResolvedDetails,
infra types.Infrastructure,
resolvedDetails scm.ResolvedDetails,
defaultBaseImage string,
ideService ide.IDE,
) (*StartResponse, error)
// StopGitspace stops the gitspace container.
StopGitspace(ctx context.Context, config types.GitspaceConfig, infra *types.Infrastructure) error
StopGitspace(ctx context.Context, config types.GitspaceConfig, infra types.Infrastructure) error
// StopAndRemoveGitspace stops and removes the gitspace container.
StopAndRemoveGitspace(ctx context.Context, config types.GitspaceConfig, infra *types.Infrastructure) error
StopAndRemoveGitspace(ctx context.Context, config types.GitspaceConfig, infra types.Infrastructure) error
// Status checks if the infra is reachable and ready to orchestrate containers.
Status(ctx context.Context, infra *types.Infrastructure) error
Status(ctx context.Context, infra types.Infrastructure) error
}

View File

@ -75,12 +75,12 @@ func NewEmbeddedDockerOrchestrator(
func (e *EmbeddedDockerOrchestrator) CreateAndStartGitspace(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
infra *types.Infrastructure,
resolvedRepoDetails *scm.ResolvedDetails,
infra types.Infrastructure,
resolvedRepoDetails scm.ResolvedDetails,
defaultBaseImage string,
ideService ide.IDE,
) (*StartResponse, error) {
containerName := getGitspaceContainerName(gitspaceConfig)
containerName := GetGitspaceContainerName(gitspaceConfig)
log := log.Ctx(ctx).With().Str(loggingKey, containerName).Logger()
@ -128,7 +128,7 @@ func (e *EmbeddedDockerOrchestrator) CreateAndStartGitspace(
devcontainer := &devcontainer.Exec{
ContainerName: containerName,
WorkingDir: e.getWorkingDir(resolvedRepoDetails.RepoName),
WorkingDir: GetWorkingDir(resolvedRepoDetails.RepoName),
DockerClient: dockerClient,
}
@ -151,7 +151,8 @@ func (e *EmbeddedDockerOrchestrator) CreateAndStartGitspace(
logStreamInstance, loggerErr := e.statefulLogger.CreateLogStream(ctx, gitspaceConfig.ID)
if loggerErr != nil {
return nil, fmt.Errorf("error getting log stream for gitspace ID %d: %w", gitspaceConfig.ID, loggerErr)
return nil,
fmt.Errorf("error getting log stream for gitspace ID %d: %w", gitspaceConfig.ID, loggerErr)
}
defer func() {
@ -169,9 +170,9 @@ func (e *EmbeddedDockerOrchestrator) CreateAndStartGitspace(
ideService,
logStreamInstance,
infra.Storage,
e.getWorkingDir(resolvedRepoDetails.RepoName),
GetWorkingDir(resolvedRepoDetails.RepoName),
resolvedRepoDetails,
infra.PortMappings,
infra.GitspacePortMappings,
defaultBaseImage,
)
if startErr != nil {
@ -185,7 +186,7 @@ func (e *EmbeddedDockerOrchestrator) CreateAndStartGitspace(
return nil, fmt.Errorf("gitspace %s is in a bad state: %s", containerName, state)
}
id, ports, startErr := e.getContainerInfo(ctx, containerName, dockerClient, infra.PortMappings)
id, ports, startErr := e.getContainerInfo(ctx, containerName, dockerClient, infra.GitspacePortMappings)
if startErr != nil {
return nil, startErr
}
@ -197,10 +198,6 @@ func (e *EmbeddedDockerOrchestrator) CreateAndStartGitspace(
}, nil
}
func (e *EmbeddedDockerOrchestrator) getWorkingDir(repoName string) string {
return "/" + repoName
}
func (e *EmbeddedDockerOrchestrator) startGitspace(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
@ -210,7 +207,7 @@ func (e *EmbeddedDockerOrchestrator) startGitspace(
logStreamInstance *logutil.LogStreamInstance,
volumeName string,
workingDirectory string,
resolvedRepoDetails *scm.ResolvedDetails,
resolvedRepoDetails scm.ResolvedDetails,
portMappings map[int]*types.PortMapping,
defaultBaseImage string,
) error {
@ -384,7 +381,7 @@ func (e *EmbeddedDockerOrchestrator) getContainerInfo(
func (e *EmbeddedDockerOrchestrator) authenticateGit(
ctx context.Context,
devcontainer *devcontainer.Exec,
resolvedRepoDetails *scm.ResolvedDetails,
resolvedRepoDetails scm.ResolvedDetails,
) error {
data := &template.AuthenticateGitPayload{
Email: resolvedRepoDetails.Credentials.Email,
@ -458,7 +455,7 @@ func (e *EmbeddedDockerOrchestrator) cloneCode(
devcontainer *devcontainer.Exec,
defaultBaseImage string,
logStreamInstance *logutil.LogStreamInstance,
resolvedRepoDetails *scm.ResolvedDetails,
resolvedRepoDetails scm.ResolvedDetails,
) error {
data := &template.CloneGitPayload{
RepoURL: resolvedRepoDetails.CloneURL,
@ -717,9 +714,9 @@ func (e *EmbeddedDockerOrchestrator) pullImage(
func (e EmbeddedDockerOrchestrator) StopGitspace(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
infra *types.Infrastructure,
infra types.Infrastructure,
) error {
containerName := getGitspaceContainerName(gitspaceConfig)
containerName := GetGitspaceContainerName(gitspaceConfig)
log := log.Ctx(ctx).With().Str(loggingKey, containerName).Logger()
@ -806,12 +803,8 @@ func (e EmbeddedDockerOrchestrator) stopContainer(
return nil
}
func getGitspaceContainerName(config types.GitspaceConfig) string {
return "gitspace-" + config.UserID + "-" + config.Identifier
}
// Status is NOOP for EmbeddedDockerOrchestrator as the docker host is verified by the infra provisioner.
func (e *EmbeddedDockerOrchestrator) Status(_ context.Context, _ *types.Infrastructure) error {
func (e *EmbeddedDockerOrchestrator) Status(_ context.Context, _ types.Infrastructure) error {
return nil
}
@ -840,9 +833,9 @@ func (e *EmbeddedDockerOrchestrator) containerState(
func (e *EmbeddedDockerOrchestrator) StopAndRemoveGitspace(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
infra *types.Infrastructure,
infra types.Infrastructure,
) error {
containerName := getGitspaceContainerName(gitspaceConfig)
containerName := GetGitspaceContainerName(gitspaceConfig)
log := log.Ctx(ctx).With().Str(loggingKey, containerName).Logger()

View File

@ -0,0 +1,25 @@
// Copyright 2023 Harness, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package container
import "github.com/harness/gitness/types"
func GetGitspaceContainerName(config types.GitspaceConfig) string {
return "gitspace-" + config.UserID + "-" + config.Identifier
}
func GetWorkingDir(repoName string) string {
return "/" + repoName
}

View File

@ -29,7 +29,7 @@ type Orchestrator interface {
ResumeStartGitspace(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
provisionedInfra *types.Infrastructure,
provisionedInfra types.Infrastructure,
) (types.GitspaceInstance, error)
// TriggerStopGitspace stops the Gitspace container and triggers infra deprovisioning to deprovision
@ -40,7 +40,7 @@ type Orchestrator interface {
ResumeStopGitspace(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
stoppedInfra *types.Infrastructure,
stoppedInfra types.Infrastructure,
) (enum.GitspaceInstanceStateType, error)
// TriggerDeleteGitspace removes the Gitspace container and triggers infra deprovisioning to deprovision
@ -51,6 +51,6 @@ type Orchestrator interface {
ResumeDeleteGitspace(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
deprovisionedInfra *types.Infrastructure,
deprovisionedInfra types.Infrastructure,
) (enum.GitspaceInstanceStateType, error)
}

View File

@ -84,16 +84,14 @@ func (o orchestrator) TriggerStartGitspace(
gitspaceConfig.InfraProviderResourceID, err)
}
ideSvc, err := o.getIDEService(gitspaceConfig)
requiredGitspacePorts, err := o.getPortsRequiredForGitspace(gitspaceConfig)
if err != nil {
return instanceState, err
return instanceState, fmt.Errorf("cannot get the ports required for gitspace during start: %w", err)
}
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeInfraProvisioningStart)
idePort := ideSvc.Port()
err = o.infraProvisioner.TriggerProvision(ctx, infraProviderResource, gitspaceConfig, []int{idePort})
err = o.infraProvisioner.TriggerProvision(ctx, *infraProviderResource, gitspaceConfig, requiredGitspacePorts)
if err != nil {
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeInfraProvisioningFailed)
@ -118,19 +116,24 @@ func (o orchestrator) TriggerStopGitspace(
"cannot get the infraProviderResource with ID %d: %w", gitspaceConfig.InfraProviderResourceID, err)
}
infra, err := o.infraProvisioner.Find(ctx, infraProviderResource, gitspaceConfig)
requiredGitspacePorts, err := o.getPortsRequiredForGitspace(gitspaceConfig)
if err != nil {
return instanceState, fmt.Errorf("cannot get the ports required for gitspace during start: %w", err)
}
infra, err := o.infraProvisioner.Find(ctx, *infraProviderResource, gitspaceConfig, requiredGitspacePorts)
if err != nil {
return instanceState, fmt.Errorf("cannot find the provisioned infra: %w", err)
}
err = o.stopGitspaceContainer(ctx, gitspaceConfig, infra)
err = o.stopGitspaceContainer(ctx, gitspaceConfig, *infra)
if err != nil {
return instanceState, err
}
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeInfraStopStart)
err = o.infraProvisioner.TriggerStop(ctx, infraProviderResource, infra)
err = o.infraProvisioner.TriggerStop(ctx, *infraProviderResource, *infra)
if err != nil {
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeInfraStopFailed)
@ -146,7 +149,7 @@ func (o orchestrator) TriggerStopGitspace(
func (o orchestrator) stopGitspaceContainer(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
infra *types.Infrastructure,
infra types.Infrastructure,
) error {
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeAgentConnectStart)
@ -175,7 +178,7 @@ func (o orchestrator) stopGitspaceContainer(
func (o orchestrator) stopAndRemoveGitspaceContainer(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
infra *types.Infrastructure,
infra types.Infrastructure,
) error {
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeAgentConnectStart)
@ -213,19 +216,24 @@ func (o orchestrator) TriggerDeleteGitspace(
"cannot get the infraProviderResource with ID %d: %w", gitspaceConfig.InfraProviderResourceID, err)
}
infra, err := o.infraProvisioner.Find(ctx, infraProviderResource, gitspaceConfig)
requiredGitspacePorts, err := o.getPortsRequiredForGitspace(gitspaceConfig)
if err != nil {
return instanceState, fmt.Errorf("cannot get the ports required for gitspace during start: %w", err)
}
infra, err := o.infraProvisioner.Find(ctx, *infraProviderResource, gitspaceConfig, requiredGitspacePorts)
if err != nil {
return instanceState, fmt.Errorf("cannot find the provisioned infra: %w", err)
}
err = o.stopAndRemoveGitspaceContainer(ctx, gitspaceConfig, infra)
err = o.stopAndRemoveGitspaceContainer(ctx, gitspaceConfig, *infra)
if err != nil {
return instanceState, err
}
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeInfraDeprovisioningStart)
err = o.infraProvisioner.TriggerDeprovision(ctx, infraProviderResource, gitspaceConfig, infra)
err = o.infraProvisioner.TriggerDeprovision(ctx, *infraProviderResource, gitspaceConfig, *infra)
if err != nil {
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeInfraDeprovisioningFailed)
@ -272,7 +280,7 @@ func (o orchestrator) getIDEService(gitspaceConfig types.GitspaceConfig) (ide.ID
func (o orchestrator) ResumeStartGitspace(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
provisionedInfra *types.Infrastructure,
provisionedInfra types.Infrastructure,
) (types.GitspaceInstance, error) {
gitspaceInstance := *gitspaceConfig.GitspaceInstance
gitspaceInstance.State = enum.GitspaceInstanceStateError
@ -284,7 +292,7 @@ func (o orchestrator) ResumeStartGitspace(
idePort := ideSvc.Port()
provisionedInfra, err = o.infraProvisioner.ResumeProvision(ctx, gitspaceConfig, provisionedInfra)
err = o.infraProvisioner.ResumeProvision(ctx, gitspaceConfig, provisionedInfra)
if err != nil {
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeInfraProvisioningFailed)
@ -292,6 +300,16 @@ func (o orchestrator) ResumeStartGitspace(
"cannot provision infrastructure for ID %d: %w", gitspaceConfig.InfraProviderResourceID, err)
}
if provisionedInfra.Status != enum.InfraStatusProvisioned {
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeInfraProvisioningFailed)
return gitspaceInstance, fmt.Errorf(
"infra state is %v, should be %v for gitspace instance identifier %s",
provisionedInfra.Status,
enum.InfraStatusProvisioned,
gitspaceConfig.GitspaceInstance.Identifier)
}
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeInfraProvisioningCompleted)
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeFetchDevcontainerStart)
@ -326,7 +344,7 @@ func (o orchestrator) ResumeStartGitspace(
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeAgentGitspaceCreationStart)
startResponse, err := o.containerOrchestrator.CreateAndStartGitspace(
ctx, gitspaceConfig, provisionedInfra, scmResolvedDetails, o.config.DefaultBaseImage, ideSvc)
ctx, gitspaceConfig, provisionedInfra, *scmResolvedDetails, o.config.DefaultBaseImage, ideSvc)
if err != nil {
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeAgentGitspaceCreationFailed)
@ -339,10 +357,10 @@ func (o orchestrator) ResumeStartGitspace(
var forwardedPort string
if provisionedInfra.PortMappings[idePort].PublishedPort == 0 {
if provisionedInfra.GitspacePortMappings[idePort].PublishedPort == 0 {
forwardedPort = startResponse.PublishedPorts[idePort]
} else {
forwardedPort = strconv.Itoa(provisionedInfra.PortMappings[idePort].ForwardedPort)
forwardedPort = strconv.Itoa(provisionedInfra.GitspacePortMappings[idePort].ForwardedPort)
}
host := provisionedInfra.Host
@ -384,7 +402,7 @@ func (o orchestrator) ResumeStartGitspace(
func (o orchestrator) ResumeStopGitspace(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
stoppedInfra *types.Infrastructure,
stoppedInfra types.Infrastructure,
) (enum.GitspaceInstanceStateType, error) {
instanceState := enum.GitspaceInstanceStateError
@ -396,6 +414,16 @@ func (o orchestrator) ResumeStopGitspace(
"cannot stop provisioned infrastructure with ID %d: %w", gitspaceConfig.InfraProviderResourceID, err)
}
if stoppedInfra.Status != enum.InfraStatusDestroyed {
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeInfraStopFailed)
return instanceState, fmt.Errorf(
"infra state is %v, should be %v for gitspace instance identifier %s",
stoppedInfra.Status,
enum.InfraStatusDestroyed,
gitspaceConfig.GitspaceInstance.Identifier)
}
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeInfraStopCompleted)
instanceState = enum.GitspaceInstanceStateDeleted
@ -408,7 +436,7 @@ func (o orchestrator) ResumeStopGitspace(
func (o orchestrator) ResumeDeleteGitspace(
ctx context.Context,
gitspaceConfig types.GitspaceConfig,
deprovisionedInfra *types.Infrastructure,
deprovisionedInfra types.Infrastructure,
) (enum.GitspaceInstanceStateType, error) {
instanceState := enum.GitspaceInstanceStateError
@ -419,6 +447,16 @@ func (o orchestrator) ResumeDeleteGitspace(
"cannot deprovision infrastructure with ID %d: %w", gitspaceConfig.InfraProviderResourceID, err)
}
if deprovisionedInfra.Status != enum.InfraStatusDestroyed {
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeInfraDeprovisioningFailed)
return instanceState, fmt.Errorf(
"infra state is %v, should be %v for gitspace instance identifier %s",
deprovisionedInfra.Status,
enum.InfraStatusDestroyed,
gitspaceConfig.GitspaceInstance.Identifier)
}
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeInfraDeprovisioningCompleted)
instanceState = enum.GitspaceInstanceStateDeleted
@ -426,3 +464,16 @@ func (o orchestrator) ResumeDeleteGitspace(
o.emitGitspaceEvent(ctx, gitspaceConfig, enum.GitspaceEventTypeGitspaceActionStopCompleted)
return instanceState, nil
}
func (o orchestrator) getPortsRequiredForGitspace(
gitspaceConfig types.GitspaceConfig,
) ([]int, error) {
// TODO: What if the required ports in the config have deviated from when the last instance was created?
ideSvc, err := o.getIDEService(gitspaceConfig)
if err != nil {
return nil, fmt.Errorf("unable to get IDE service while checking required Gitspace ports: %w", err)
}
idePort := ideSvc.Port()
return []int{idePort}, nil
}

View File

@ -32,7 +32,8 @@ func (s *Service) handleGitspaceInfraEvent(
) error {
payload := event.Payload
config, fetchErr := s.getConfig(ctx, payload.Infra.SpaceID, payload.Infra.SpacePath, payload.Infra.ResourceKey)
config, fetchErr := s.getConfig(
ctx, payload.Infra.SpaceID, payload.Infra.SpacePath, payload.Infra.GitspaceConfigIdentifier)
if fetchErr != nil {
return fetchErr
}

View File

@ -65,7 +65,7 @@ type infraProvisioned struct {
Created int64 `db:"iprov_created"`
Updated int64 `db:"iprov_updated"`
ResponseMetadata *string `db:"iprov_response_metadata"`
Params string `db:"iprov_opentofu_params"`
InputParams string `db:"iprov_opentofu_params"`
InfraStatus enum.InfraStatus `db:"iprov_infra_status"`
ServerHostIP string `db:"iprov_server_host_ip"`
ServerHostPort string `db:"iprov_server_host_port"`
@ -90,7 +90,7 @@ func (i infraProvisionedStore) Find(ctx context.Context, id int64) (*types.Infra
stmt := database.Builder.
Select(infraProvisionedSelectColumns).
From(infraProvisionedTable).
Where(infraProvisionedIDColumn+" = $1", id)
Where(infraProvisionedIDColumn+" = ?", id) // nolint:goconst
sql, args, err := stmt.ToSql()
if err != nil {
@ -153,8 +153,8 @@ func (i infraProvisionedStore) FindLatestByGitspaceInstanceID(
stmt := database.Builder.
Select(infraProvisionedSelectColumns).
From(infraProvisionedTable).
Where("iprov_gitspace_id = $1", gitspaceInstanceID).
Where("iprov_space_id = $2", spaceID).
Where("iprov_gitspace_id = ?", gitspaceInstanceID).
Where("iprov_space_id = ?", spaceID).
OrderBy("iprov_created DESC")
sql, args, err := stmt.ToSql()
@ -210,7 +210,7 @@ func (i infraProvisionedStore) Create(ctx context.Context, infraProvisioned *typ
infraProvisioned.Created,
infraProvisioned.Updated,
infraProvisioned.ResponseMetadata,
infraProvisioned.Params,
infraProvisioned.InputParams,
infraProvisioned.InfraStatus,
infraProvisioned.ServerHostIP,
infraProvisioned.ServerHostPort,
@ -235,7 +235,7 @@ func (i infraProvisionedStore) Create(ctx context.Context, infraProvisioned *typ
func (i infraProvisionedStore) Delete(ctx context.Context, id int64) error {
stmt := database.Builder.
Delete(infraProvisionedTable).
Where(infraProvisionedIDColumn+" = $1", id)
Where(infraProvisionedIDColumn+" = ?", id)
sql, args, err := stmt.ToSql()
if err != nil {
@ -257,7 +257,7 @@ func (i infraProvisionedStore) Update(ctx context.Context, infraProvisioned *typ
Set("iprov_infra_status", infraProvisioned.InfraStatus).
Set("iprov_server_host_ip", infraProvisioned.ServerHostIP).
Set("iprov_server_host_port", infraProvisioned.ServerHostPort).
Set("iprov_opentofu_params", infraProvisioned.Params).
Set("iprov_opentofu_params", infraProvisioned.InputParams).
Set("iprov_updated", infraProvisioned.Updated).
Set("iprov_proxy_host", infraProvisioned.ProxyHost).
Set("iprov_proxy_port", infraProvisioned.ProxyPort).
@ -286,7 +286,7 @@ func (entity infraProvisioned) toDTO() *types.InfraProvisioned {
Created: entity.Created,
Updated: entity.Updated,
ResponseMetadata: entity.ResponseMetadata,
Params: entity.Params,
InputParams: entity.InputParams,
InfraStatus: entity.InfraStatus,
ServerHostIP: entity.ServerHostIP,
ServerHostPort: entity.ServerHostPort,

View File

@ -22,6 +22,7 @@ import (
"strings"
"unicode"
"github.com/harness/gitness/app/gitspace/infrastructure"
"github.com/harness/gitness/app/gitspace/orchestrator"
"github.com/harness/gitness/app/gitspace/orchestrator/ide"
"github.com/harness/gitness/app/services/cleanup"
@ -412,6 +413,13 @@ func ProvideGitspaceOrchestratorConfig(config *types.Config) *orchestrator.Confi
}
}
// ProvideGitspaceInfraProvisionerConfig loads the Gitspace infra provisioner config from the main config.
func ProvideGitspaceInfraProvisionerConfig(config *types.Config) *infrastructure.Config {
return &infrastructure.Config{
AgentPort: config.Gitspace.AgentPort,
}
}
// ProvideGitspaceEventConfig loads the gitspace event service config from the main config.
func ProvideGitspaceEventConfig(config *types.Config) *gitspaceevent.Config {
return &gitspaceevent.Config{

View File

@ -232,6 +232,7 @@ func initSystem(ctx context.Context, config *types.Config) (*cliserver.System, e
ide.WireSet,
gitspaceinfraevents.WireSet,
gitspaceservice.WireSet,
cliserver.ProvideGitspaceInfraProvisionerConfig,
)
return &cliserver.System{}, nil
}

View File

@ -355,7 +355,8 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro
scmSCM := scm.ProvideSCM(scmFactory)
infraProviderTemplateStore := database.ProvideInfraProviderTemplateStore(db)
infraProvisionedStore := database.ProvideInfraProvisionedStore(db)
infraProvisioner := infrastructure.ProvideInfraProvisionerService(infraProviderConfigStore, infraProviderResourceStore, factory, infraProviderTemplateStore, infraProvisionedStore)
infrastructureConfig := server.ProvideGitspaceInfraProvisionerConfig(config)
infraProvisioner := infrastructure.ProvideInfraProvisionerService(infraProviderConfigStore, infraProviderResourceStore, factory, infraProviderTemplateStore, infraProvisionedStore, infrastructureConfig)
statefulLogger := logutil.ProvideStatefulLogger(logStream)
containerOrchestrator := container.ProvideEmbeddedDockerOrchestrator(dockerClientFactory, statefulLogger)
orchestratorConfig := server.ProvideGitspaceOrchestratorConfig(config)

View File

@ -38,12 +38,12 @@ func NewDockerClientFactory(config *DockerConfig) *DockerClientFactory {
// NewDockerClient returns a new docker client created using the docker config and infra.
func (d *DockerClientFactory) NewDockerClient(
_ context.Context,
infra *types.Infrastructure,
infra types.Infrastructure,
) (*client.Client, error) {
if infra.ProviderType != enum.InfraProviderTypeDocker {
return nil, fmt.Errorf("infra provider type %s not supported", infra.ProviderType)
}
dockerClient, err := d.getClient(infra.Parameters)
dockerClient, err := d.getClient(infra.InputParameters)
if err != nil {
return nil, fmt.Errorf("error creating docker client using infra %+v: %w", infra, err)
}

View File

@ -49,18 +49,20 @@ func NewDockerProvider(
}
// Provision assumes a docker engine is already running on the gitness host machine and re-uses that as infra.
// It does not start docker engine. It creates a directory in the host machine using the given resource key.
// It does not start docker engine. It creates a docker volume using the given gitspace config identifier.
func (d DockerProvider) Provision(
ctx context.Context,
spaceID int64,
spacePath string,
resourceKey string,
requiredPorts []int,
params []types.InfraProviderParameter,
gitspaceConfigIdentifier string,
_ string,
_ int,
requiredGitspacePorts []int,
inputParameters []types.InfraProviderParameter,
) error {
dockerClient, err := d.dockerClientFactory.NewDockerClient(ctx, &types.Infrastructure{
ProviderType: enum.InfraProviderTypeDocker,
Parameters: params,
dockerClient, err := d.dockerClientFactory.NewDockerClient(ctx, types.Infrastructure{
ProviderType: enum.InfraProviderTypeDocker,
InputParameters: inputParameters,
})
if err != nil {
return fmt.Errorf("error getting docker client from docker client factory: %w", err)
@ -80,18 +82,18 @@ func (d DockerProvider) Provision(
infrastructure.SpaceID = spaceID
infrastructure.SpacePath = spacePath
infrastructure.ResourceKey = resourceKey
infrastructure.GitspaceConfigIdentifier = gitspaceConfigIdentifier
storageName, err := d.createNamedVolume(ctx, spacePath, resourceKey, dockerClient)
storageName, err := d.createNamedVolume(ctx, spacePath, gitspaceConfigIdentifier, dockerClient)
if err != nil {
return err
}
infrastructure.Storage = storageName
var portMappings = make(map[int]*types.PortMapping, len(requiredPorts))
var portMappings = make(map[int]*types.PortMapping, len(requiredGitspacePorts))
for _, requiredPort := range requiredPorts {
for _, requiredPort := range requiredGitspacePorts {
portMapping := &types.PortMapping{
PublishedPort: 0,
ForwardedPort: 0,
@ -100,10 +102,10 @@ func (d DockerProvider) Provision(
portMappings[requiredPort] = portMapping
}
infrastructure.PortMappings = portMappings
infrastructure.GitspacePortMappings = portMappings
event := &events.GitspaceInfraEventPayload{
Infra: infrastructure,
Infra: *infrastructure,
Type: enum.InfraEventProvision,
}
@ -117,12 +119,15 @@ func (d DockerProvider) Find(
ctx context.Context,
spaceID int64,
spacePath string,
resourceKey string,
params []types.InfraProviderParameter,
gitspaceConfigIdentifier string,
_ string,
_ int,
_ []int,
inputParameters []types.InfraProviderParameter,
) (*types.Infrastructure, error) {
dockerClient, err := d.dockerClientFactory.NewDockerClient(ctx, &types.Infrastructure{
ProviderType: enum.InfraProviderTypeDocker,
Parameters: params,
dockerClient, err := d.dockerClientFactory.NewDockerClient(ctx, types.Infrastructure{
ProviderType: enum.InfraProviderTypeDocker,
InputParameters: inputParameters,
})
if err != nil {
@ -143,9 +148,9 @@ func (d DockerProvider) Find(
infrastructure.SpaceID = spaceID
infrastructure.SpacePath = spacePath
infrastructure.ResourceKey = resourceKey
infrastructure.GitspaceConfigIdentifier = gitspaceConfigIdentifier
name := volumeName(spacePath, resourceKey)
name := volumeName(spacePath, gitspaceConfigIdentifier)
volumeInspect, err := dockerClient.VolumeInspect(ctx, name)
if err != nil {
@ -158,7 +163,9 @@ func (d DockerProvider) Find(
}
// Stop is NOOP as this provider uses already running docker engine. It does not stop the docker engine.
func (d DockerProvider) Stop(ctx context.Context, infra *types.Infrastructure) error {
func (d DockerProvider) Stop(ctx context.Context, infra types.Infrastructure) error {
infra.Status = enum.InfraStatusDestroyed
event := &events.GitspaceInfraEventPayload{
Infra: infra,
Type: enum.InfraEventStop,
@ -170,25 +177,29 @@ func (d DockerProvider) Stop(ctx context.Context, infra *types.Infrastructure) e
}
// Deprovision deletes the volume created by Provision. It does not stop the docker engine.
func (d DockerProvider) Deprovision(ctx context.Context, infra *types.Infrastructure) error {
dockerClient, err := d.dockerClientFactory.NewDockerClient(ctx, &types.Infrastructure{
ProviderType: enum.InfraProviderTypeDocker,
Parameters: infra.Parameters,
func (d DockerProvider) Deprovision(ctx context.Context, infra types.Infrastructure) error {
dockerClient, err := d.dockerClientFactory.NewDockerClient(ctx, types.Infrastructure{
ProviderType: enum.InfraProviderTypeDocker,
InputParameters: infra.InputParameters,
})
if err != nil {
return fmt.Errorf("error getting docker client from docker client factory: %w", err)
}
defer func() {
closingErr := dockerClient.Close()
if closingErr != nil {
log.Ctx(ctx).Warn().Err(closingErr).Msg("failed to close docker client")
}
}()
err = dockerClient.VolumeRemove(ctx, infra.Storage, true)
if err != nil {
return fmt.Errorf("couldn't delete volume for %s : %w", infra.Storage, err)
}
infra.Status = enum.InfraStatusDestroyed
event := &events.GitspaceInfraEventPayload{
Infra: infra,
Type: enum.InfraEventDeprovision,

View File

@ -22,31 +22,36 @@ import (
)
type InfraProvider interface {
// Provision provisions infrastructure against a resourceKey with the provided parameters.
// Provision provisions infrastructure against a gitspace with the provided parameters.
Provision(
ctx context.Context,
spaceID int64,
spacePath string,
resourceKey string,
requiredPorts []int,
parameters []types.InfraProviderParameter,
gitspaceConfigIdentifier string,
gitspaceInstanceIdentifier string,
agentPort int,
requiredGitspacePorts []int,
inputParameters []types.InfraProviderParameter,
) error
// Find finds infrastructure provisioned against a resourceKey.
// Find finds infrastructure provisioned against a gitspace.
Find(
ctx context.Context,
spaceID int64,
spacePath string,
resourceKey string,
parameters []types.InfraProviderParameter,
gitspaceConfigIdentifier string,
gitspaceInstanceIdentifier string,
agentPort int,
requiredGitspacePorts []int,
inputParameters []types.InfraProviderParameter,
) (*types.Infrastructure, error)
// Stop frees up the resources allocated against a resourceKey, which can be freed.
Stop(ctx context.Context, infra *types.Infrastructure) error
// Deprovision removes all infrastructure provisioned againest the resourceKey.
Deprovision(ctx context.Context, infra *types.Infrastructure) error
// Stop frees up the resources allocated against a gitspace, which can be freed.
Stop(ctx context.Context, infra types.Infrastructure) error
// Deprovision removes all infrastructure provisioned against the gitspace.
Deprovision(ctx context.Context, infra types.Infrastructure) error
// AvailableParams provides a schema to define the infrastructure.
AvailableParams() []types.InfraProviderParameterSchema
// ValidateParams validates the supplied params before defining the infrastructure resource .
ValidateParams(parameters []types.InfraProviderParameter) error
ValidateParams(inputParameters []types.InfraProviderParameter) error
// TemplateParams provides a list of params which are of type template.
TemplateParams() []types.InfraProviderParameterSchema
// ProvisioningType specifies whether the provider will provision new infra resources or it will reuse existing.

View File

@ -411,6 +411,8 @@ type Config struct {
Enable bool `envconfig:"GITNESS_GITSPACE_ENABLE" default:"false"`
AgentPort int `envconfig:"GITNESS_GITSPACE_AGENT_PORT" default:"8083"`
Events struct {
Concurrency int `envconfig:"GITNESS_GITSPACE_EVENTS_CONCURRENCY" default:"4"`
MaxRetries int `envconfig:"GITNESS_GITSPACE_EVENTS_MAX_RETRIES" default:"3"`

View File

@ -24,16 +24,14 @@ var infraStatuses = []InfraStatus{
InfraStatusPending,
InfraStatusProvisioned,
InfraStatusDestroyed,
InfraStatusMarkedForDestroy,
InfraStatusUnknown,
InfraStatusSuspended,
InfraStatusError,
}
const (
InfraStatusPending InfraStatus = "pending"
InfraStatusProvisioned InfraStatus = "provisioned"
InfraStatusDestroyed InfraStatus = "destroyed"
InfraStatusMarkedForDestroy InfraStatus = "marked_for_destroy"
InfraStatusUnknown InfraStatus = "unknown"
InfraStatusSuspended InfraStatus = "suspended"
InfraStatusPending InfraStatus = "pending"
InfraStatusProvisioned InfraStatus = "provisioned"
InfraStatusDestroyed InfraStatus = "destroyed"
InfraStatusUnknown InfraStatus = "unknown"
InfraStatusError InfraStatus = "error"
)

View File

@ -25,7 +25,7 @@ type InfraProvisioned struct {
Created int64
Updated int64
ResponseMetadata *string
Params string
InputParams string
InfraStatus enum.InfraStatus
ServerHostIP string
ServerHostPort string
@ -41,15 +41,15 @@ type InfraProvisionedGatewayView struct {
}
type InfraProvisionedUpdateGatewayRequest struct {
GitspaceUID string `json:"gitspace_id"`
SpaceID int64 `json:"space_id"`
GatewayHost string `json:"gateway_host"`
GatewayPort int32 `json:"gateway_port"`
GitspaceInstanceIdentifier string `json:"gitspace_id"`
SpaceID int64 `json:"space_id"`
GatewayHost string `json:"gateway_host"`
GatewayPort int32 `json:"gateway_port"`
}
type InfraProvisionedResponse struct {
ServerHost string `json:"server_host"`
ServerPort string `json:"server_port"`
GitspaceUID string `json:"gitspace_id"`
SpaceID int64 `json:"space_id"`
ServerHost string `json:"server_host"`
ServerPort string `json:"server_port"`
GitspaceInstanceIdentifier string `json:"gitspace_id"`
SpaceID int64 `json:"space_id"`
}

View File

@ -40,16 +40,18 @@ type PortMapping struct {
type Infrastructure struct {
// Identifier identifies the provisioned infra.
Identifier string
// ResourceKey is the key for which the infra is provisioned.
ResourceKey string
// SpaceID for the resource key.
SpaceID int64
// SpacePath for the resource key.
SpacePath string
// GitspaceConfigIdentifier is the gitspace config for which the infra is provisioned.
GitspaceConfigIdentifier string
// GitspaceInstanceIdentifier is the gitspace instance for which the infra is provisioned.
GitspaceInstanceIdentifier string
// ProviderType specifies the type of the infra provider.
ProviderType enum.InfraProviderType
// Parameters which are required by the provider to provision the infra.
Parameters []InfraProviderParameter
// InputParameters which are required by the provider to provision the infra.
InputParameters []InfraProviderParameter
// Status of the infra.
Status enum.InfraStatus
// Host through which the infra can be accessed.
@ -60,6 +62,6 @@ type Infrastructure struct {
ProxyPort int
// Storage is the name of the volume or disk created for the resource.
Storage string
// PortMappings contains the ports assigned for every requested port.
PortMappings map[int]*PortMapping
// GitspacePortMappings contains the ports assigned for every requested port.
GitspacePortMappings map[int]*PortMapping
}