mirror of
https://github.com/harness/drone.git
synced 2025-05-17 01:20:13 +08:00
feat: [CDE-636]: Adding config metadata to infra provider methods parameters. (#3487)
* feat: [CDE-636]: Fixing infra provider config update logic. * feat: [CDE-636]: Adding config metadata to infra provider methods parameters. * feat: [CDE-636]: Adding config metadata to infra provider methods parameters.
This commit is contained in:
parent
11dddf1221
commit
26cda57360
@ -42,9 +42,10 @@ func (i InfraProvisioner) Find(
|
|||||||
}
|
}
|
||||||
|
|
||||||
var inputParams []types.InfraProviderParameter
|
var inputParams []types.InfraProviderParameter
|
||||||
|
var configMetadata map[string]any
|
||||||
var agentPort = 0
|
var agentPort = 0
|
||||||
if infraProvider.ProvisioningType() == enum.InfraProvisioningTypeNew {
|
if infraProvider.ProvisioningType() == enum.InfraProvisioningTypeNew {
|
||||||
inputParams, err = i.paramsForProvisioningTypeNew(ctx, gitspaceConfig)
|
inputParams, configMetadata, err = i.paramsForProvisioningTypeNew(ctx, gitspaceConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -52,7 +53,7 @@ func (i InfraProvisioner) Find(
|
|||||||
// TODO: What if the agent port has deviated from when the last instance was created?
|
// TODO: What if the agent port has deviated from when the last instance was created?
|
||||||
agentPort = i.config.AgentPort
|
agentPort = i.config.AgentPort
|
||||||
} else {
|
} else {
|
||||||
inputParams, err = i.paramsForProvisioningTypeExisting(ctx, infraProviderResource, infraProvider)
|
inputParams, configMetadata, err = i.paramsForProvisioningTypeExisting(ctx, infraProviderResource, infraProvider)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -60,7 +61,7 @@ func (i InfraProvisioner) Find(
|
|||||||
|
|
||||||
infra, err := infraProvider.Find(ctx, gitspaceConfig.SpaceID, gitspaceConfig.SpacePath,
|
infra, err := infraProvider.Find(ctx, gitspaceConfig.SpaceID, gitspaceConfig.SpacePath,
|
||||||
gitspaceConfig.Identifier, gitspaceConfig.GitspaceInstance.Identifier,
|
gitspaceConfig.Identifier, gitspaceConfig.GitspaceInstance.Identifier,
|
||||||
agentPort, requiredGitspacePorts, inputParams)
|
agentPort, requiredGitspacePorts, inputParams, configMetadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to find infrastructure: %w", err)
|
return nil, fmt.Errorf("failed to find infrastructure: %w", err)
|
||||||
}
|
}
|
||||||
@ -83,23 +84,27 @@ func (i InfraProvisioner) Find(
|
|||||||
func (i InfraProvisioner) paramsForProvisioningTypeNew(
|
func (i InfraProvisioner) paramsForProvisioningTypeNew(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
gitspaceConfig types.GitspaceConfig,
|
gitspaceConfig types.GitspaceConfig,
|
||||||
) ([]types.InfraProviderParameter, error) {
|
) ([]types.InfraProviderParameter, map[string]any, error) {
|
||||||
infraProvisionedLatest, err := i.infraProvisionedStore.FindLatestByGitspaceInstanceID(
|
infraProvisionedLatest, err := i.infraProvisionedStore.FindLatestByGitspaceInstanceID(
|
||||||
ctx, gitspaceConfig.GitspaceInstance.ID)
|
ctx, gitspaceConfig.GitspaceInstance.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf(
|
return nil, nil, fmt.Errorf(
|
||||||
"could not find latest infra provisioned entity for instance %d: %w",
|
"could not find latest infra provisioned entity for instance %d: %w",
|
||||||
gitspaceConfig.GitspaceInstance.ID, err)
|
gitspaceConfig.GitspaceInstance.ID, err)
|
||||||
}
|
}
|
||||||
if infraProvisionedLatest.InputParams == "" {
|
if infraProvisionedLatest.InputParams == "" {
|
||||||
return []types.InfraProviderParameter{}, err
|
return []types.InfraProviderParameter{}, nil, err
|
||||||
}
|
}
|
||||||
allParams, err := deserializeInfraProviderParams(infraProvisionedLatest.InputParams)
|
allParams, err := deserializeInfraProviderParams(infraProvisionedLatest.InputParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
infraProviderConfig, err := i.infraProviderConfigStore.Find(ctx,
|
||||||
return allParams, nil
|
gitspaceConfig.InfraProviderResource.InfraProviderConfigID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
return allParams, infraProviderConfig.Metadata, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func deserializeInfraProviderParams(in string) ([]types.InfraProviderParameter, error) {
|
func deserializeInfraProviderParams(in string) ([]types.InfraProviderParameter, error) {
|
||||||
@ -115,13 +120,13 @@ func (i InfraProvisioner) paramsForProvisioningTypeExisting(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
infraProviderResource types.InfraProviderResource,
|
infraProviderResource types.InfraProviderResource,
|
||||||
infraProvider infraprovider.InfraProvider,
|
infraProvider infraprovider.InfraProvider,
|
||||||
) ([]types.InfraProviderParameter, error) {
|
) ([]types.InfraProviderParameter, map[string]any, error) {
|
||||||
allParams, err := i.getAllParamsFromDB(ctx, infraProviderResource, infraProvider)
|
allParams, configMetadata, err := i.getAllParamsFromDB(ctx, infraProviderResource, infraProvider)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not get all params from DB while finding: %w", err)
|
return nil, nil, fmt.Errorf("could not get all params from DB while finding: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return allParams, nil
|
return allParams, configMetadata, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getGitspaceScheme(ideType enum.IDEType, gitspaceSchemeFromMetadata string) (string, error) {
|
func getGitspaceScheme(ideType enum.IDEType, gitspaceSchemeFromMetadata string) (string, error) {
|
||||||
@ -185,12 +190,12 @@ func (i InfraProvisioner) getAllParamsFromDB(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
infraProviderResource types.InfraProviderResource,
|
infraProviderResource types.InfraProviderResource,
|
||||||
infraProvider infraprovider.InfraProvider,
|
infraProvider infraprovider.InfraProvider,
|
||||||
) ([]types.InfraProviderParameter, error) {
|
) ([]types.InfraProviderParameter, map[string]any, error) {
|
||||||
var allParams []types.InfraProviderParameter
|
var allParams []types.InfraProviderParameter
|
||||||
|
|
||||||
templateParams, err := i.getTemplateParams(ctx, infraProvider, infraProviderResource)
|
templateParams, err := i.getTemplateParams(ctx, infraProvider, infraProviderResource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
allParams = append(allParams, templateParams...)
|
allParams = append(allParams, templateParams...)
|
||||||
@ -199,7 +204,12 @@ func (i InfraProvisioner) getAllParamsFromDB(
|
|||||||
|
|
||||||
allParams = append(allParams, params...)
|
allParams = append(allParams, params...)
|
||||||
|
|
||||||
return allParams, nil
|
configMetadata, err := i.configMetadata(ctx, infraProviderResource)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return allParams, configMetadata, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i InfraProvisioner) getTemplateParams(
|
func (i InfraProvisioner) getTemplateParams(
|
||||||
@ -256,3 +266,14 @@ func (i InfraProvisioner) paramsFromResource(
|
|||||||
}
|
}
|
||||||
return params
|
return params
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i InfraProvisioner) configMetadata(
|
||||||
|
ctx context.Context,
|
||||||
|
infraProviderResource types.InfraProviderResource,
|
||||||
|
) (map[string]any, error) {
|
||||||
|
infraProviderConfig, err := i.infraProviderConfigStore.Find(ctx, infraProviderResource.InfraProviderConfigID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return infraProviderConfig.Metadata, nil
|
||||||
|
}
|
||||||
|
@ -148,7 +148,7 @@ func (i InfraProvisioner) provisionNewInfrastructure(
|
|||||||
}
|
}
|
||||||
|
|
||||||
infraProviderResource := gitspaceConfig.InfraProviderResource
|
infraProviderResource := gitspaceConfig.InfraProviderResource
|
||||||
allParams, err := i.getAllParamsFromDB(ctx, infraProviderResource, infraProvider)
|
allParams, configMetadata, err := i.getAllParamsFromDB(ctx, infraProviderResource, infraProvider)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not get all params from DB while provisioning: %w", err)
|
return fmt.Errorf("could not get all params from DB while provisioning: %w", err)
|
||||||
}
|
}
|
||||||
@ -190,6 +190,7 @@ func (i InfraProvisioner) provisionNewInfrastructure(
|
|||||||
agentPort,
|
agentPort,
|
||||||
requiredGitspacePorts,
|
requiredGitspacePorts,
|
||||||
allParams,
|
allParams,
|
||||||
|
configMetadata,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
infraProvisioned.InfraStatus = enum.InfraStatusUnknown
|
infraProvisioned.InfraStatus = enum.InfraStatusUnknown
|
||||||
@ -215,7 +216,7 @@ func (i InfraProvisioner) provisionExistingInfrastructure(
|
|||||||
gitspaceConfig types.GitspaceConfig,
|
gitspaceConfig types.GitspaceConfig,
|
||||||
requiredGitspacePorts []types.GitspacePort,
|
requiredGitspacePorts []types.GitspacePort,
|
||||||
) error {
|
) error {
|
||||||
allParams, err := i.getAllParamsFromDB(ctx, gitspaceConfig.InfraProviderResource, infraProvider)
|
allParams, configMetadata, err := i.getAllParamsFromDB(ctx, gitspaceConfig.InfraProviderResource, infraProvider)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not get all params from DB while provisioning: %w", err)
|
return fmt.Errorf("could not get all params from DB while provisioning: %w", err)
|
||||||
}
|
}
|
||||||
@ -234,6 +235,7 @@ func (i InfraProvisioner) provisionExistingInfrastructure(
|
|||||||
0, // NOTE: Agent port is not required for provisioning type Existing.
|
0, // NOTE: Agent port is not required for provisioning type Existing.
|
||||||
requiredGitspacePorts,
|
requiredGitspacePorts,
|
||||||
allParams,
|
allParams,
|
||||||
|
configMetadata,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
|
@ -28,6 +28,14 @@ func (c *Service) UpdateConfig(ctx context.Context, infraProviderConfig *types.I
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
existingConfig, err := c.infraProviderConfigStore.FindByIdentifier(ctx, infraProviderConfig.SpaceID,
|
||||||
|
infraProviderConfig.Identifier)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not find infraprovider config %s before updating: %w",
|
||||||
|
infraProviderConfig.Identifier, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
infraProviderConfig.ID = existingConfig.ID
|
||||||
infraProviderConfig.Updated = time.Now().UnixMilli()
|
infraProviderConfig.Updated = time.Now().UnixMilli()
|
||||||
err = c.infraProviderConfigStore.Update(ctx, infraProviderConfig)
|
err = c.infraProviderConfigStore.Update(ctx, infraProviderConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -222,9 +222,13 @@ func (i infraProviderConfigStore) mapToInfraProviderConfigs(
|
|||||||
func (i infraProviderConfigStore) mapToInternalInfraProviderConfig(
|
func (i infraProviderConfigStore) mapToInternalInfraProviderConfig(
|
||||||
in *types.InfraProviderConfig,
|
in *types.InfraProviderConfig,
|
||||||
) (*infraProviderConfig, error) {
|
) (*infraProviderConfig, error) {
|
||||||
jsonBytes, marshalErr := json.Marshal(in.Metadata)
|
var jsonBytes []byte
|
||||||
if marshalErr != nil {
|
var marshalErr error
|
||||||
return nil, marshalErr
|
if len(in.Metadata) > 0 {
|
||||||
|
jsonBytes, marshalErr = json.Marshal(in.Metadata)
|
||||||
|
if marshalErr != nil {
|
||||||
|
return nil, marshalErr
|
||||||
|
}
|
||||||
}
|
}
|
||||||
infraProviderConfigEntity := &infraProviderConfig{
|
infraProviderConfigEntity := &infraProviderConfig{
|
||||||
Identifier: in.Identifier,
|
Identifier: in.Identifier,
|
||||||
|
@ -59,6 +59,7 @@ func (d DockerProvider) Provision(
|
|||||||
_ int,
|
_ int,
|
||||||
requiredGitspacePorts []types.GitspacePort,
|
requiredGitspacePorts []types.GitspacePort,
|
||||||
inputParameters []types.InfraProviderParameter,
|
inputParameters []types.InfraProviderParameter,
|
||||||
|
_ map[string]any,
|
||||||
) error {
|
) error {
|
||||||
dockerClient, err := d.dockerClientFactory.NewDockerClient(ctx, types.Infrastructure{
|
dockerClient, err := d.dockerClientFactory.NewDockerClient(ctx, types.Infrastructure{
|
||||||
ProviderType: enum.InfraProviderTypeDocker,
|
ProviderType: enum.InfraProviderTypeDocker,
|
||||||
@ -128,6 +129,7 @@ func (d DockerProvider) Find(
|
|||||||
_ int,
|
_ int,
|
||||||
_ []types.GitspacePort,
|
_ []types.GitspacePort,
|
||||||
inputParameters []types.InfraProviderParameter,
|
inputParameters []types.InfraProviderParameter,
|
||||||
|
_ map[string]any,
|
||||||
) (*types.Infrastructure, error) {
|
) (*types.Infrastructure, error) {
|
||||||
dockerClient, err := d.dockerClientFactory.NewDockerClient(ctx, types.Infrastructure{
|
dockerClient, err := d.dockerClientFactory.NewDockerClient(ctx, types.Infrastructure{
|
||||||
ProviderType: enum.InfraProviderTypeDocker,
|
ProviderType: enum.InfraProviderTypeDocker,
|
||||||
|
@ -32,6 +32,7 @@ type InfraProvider interface {
|
|||||||
agentPort int,
|
agentPort int,
|
||||||
requiredGitspacePorts []types.GitspacePort,
|
requiredGitspacePorts []types.GitspacePort,
|
||||||
inputParameters []types.InfraProviderParameter,
|
inputParameters []types.InfraProviderParameter,
|
||||||
|
configMetadata map[string]any,
|
||||||
) error
|
) error
|
||||||
|
|
||||||
// Find finds infrastructure provisioned against a gitspace.
|
// Find finds infrastructure provisioned against a gitspace.
|
||||||
@ -44,6 +45,7 @@ type InfraProvider interface {
|
|||||||
agentPort int,
|
agentPort int,
|
||||||
requiredGitspacePorts []types.GitspacePort,
|
requiredGitspacePorts []types.GitspacePort,
|
||||||
inputParameters []types.InfraProviderParameter,
|
inputParameters []types.InfraProviderParameter,
|
||||||
|
configMetadata map[string]any,
|
||||||
) (*types.Infrastructure, error)
|
) (*types.Infrastructure, error)
|
||||||
|
|
||||||
// Stop frees up the resources allocated against a gitspace, which can be freed.
|
// Stop frees up the resources allocated against a gitspace, which can be freed.
|
||||||
|
Loading…
Reference in New Issue
Block a user