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 configMetadata map[string]any
|
||||
var agentPort = 0
|
||||
if infraProvider.ProvisioningType() == enum.InfraProvisioningTypeNew {
|
||||
inputParams, err = i.paramsForProvisioningTypeNew(ctx, gitspaceConfig)
|
||||
inputParams, configMetadata, err = i.paramsForProvisioningTypeNew(ctx, gitspaceConfig)
|
||||
if err != nil {
|
||||
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?
|
||||
agentPort = i.config.AgentPort
|
||||
} else {
|
||||
inputParams, err = i.paramsForProvisioningTypeExisting(ctx, infraProviderResource, infraProvider)
|
||||
inputParams, configMetadata, err = i.paramsForProvisioningTypeExisting(ctx, infraProviderResource, infraProvider)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -60,7 +61,7 @@ func (i InfraProvisioner) Find(
|
||||
|
||||
infra, err := infraProvider.Find(ctx, gitspaceConfig.SpaceID, gitspaceConfig.SpacePath,
|
||||
gitspaceConfig.Identifier, gitspaceConfig.GitspaceInstance.Identifier,
|
||||
agentPort, requiredGitspacePorts, inputParams)
|
||||
agentPort, requiredGitspacePorts, inputParams, configMetadata)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find infrastructure: %w", err)
|
||||
}
|
||||
@ -83,23 +84,27 @@ func (i InfraProvisioner) Find(
|
||||
func (i InfraProvisioner) paramsForProvisioningTypeNew(
|
||||
ctx context.Context,
|
||||
gitspaceConfig types.GitspaceConfig,
|
||||
) ([]types.InfraProviderParameter, error) {
|
||||
) ([]types.InfraProviderParameter, map[string]any, error) {
|
||||
infraProvisionedLatest, err := i.infraProvisionedStore.FindLatestByGitspaceInstanceID(
|
||||
ctx, gitspaceConfig.GitspaceInstance.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
return nil, nil, fmt.Errorf(
|
||||
"could not find latest infra provisioned entity for instance %d: %w",
|
||||
gitspaceConfig.GitspaceInstance.ID, err)
|
||||
}
|
||||
if infraProvisionedLatest.InputParams == "" {
|
||||
return []types.InfraProviderParameter{}, err
|
||||
return []types.InfraProviderParameter{}, nil, err
|
||||
}
|
||||
allParams, err := deserializeInfraProviderParams(infraProvisionedLatest.InputParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return allParams, nil
|
||||
infraProviderConfig, err := i.infraProviderConfigStore.Find(ctx,
|
||||
gitspaceConfig.InfraProviderResource.InfraProviderConfigID)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return allParams, infraProviderConfig.Metadata, nil
|
||||
}
|
||||
|
||||
func deserializeInfraProviderParams(in string) ([]types.InfraProviderParameter, error) {
|
||||
@ -115,13 +120,13 @@ func (i InfraProvisioner) paramsForProvisioningTypeExisting(
|
||||
ctx context.Context,
|
||||
infraProviderResource types.InfraProviderResource,
|
||||
infraProvider infraprovider.InfraProvider,
|
||||
) ([]types.InfraProviderParameter, error) {
|
||||
allParams, err := i.getAllParamsFromDB(ctx, infraProviderResource, infraProvider)
|
||||
) ([]types.InfraProviderParameter, map[string]any, error) {
|
||||
allParams, configMetadata, err := i.getAllParamsFromDB(ctx, infraProviderResource, infraProvider)
|
||||
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) {
|
||||
@ -185,12 +190,12 @@ func (i InfraProvisioner) getAllParamsFromDB(
|
||||
ctx context.Context,
|
||||
infraProviderResource types.InfraProviderResource,
|
||||
infraProvider infraprovider.InfraProvider,
|
||||
) ([]types.InfraProviderParameter, error) {
|
||||
) ([]types.InfraProviderParameter, map[string]any, error) {
|
||||
var allParams []types.InfraProviderParameter
|
||||
|
||||
templateParams, err := i.getTemplateParams(ctx, infraProvider, infraProviderResource)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
allParams = append(allParams, templateParams...)
|
||||
@ -199,7 +204,12 @@ func (i InfraProvisioner) getAllParamsFromDB(
|
||||
|
||||
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(
|
||||
@ -256,3 +266,14 @@ func (i InfraProvisioner) paramsFromResource(
|
||||
}
|
||||
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
|
||||
allParams, err := i.getAllParamsFromDB(ctx, infraProviderResource, infraProvider)
|
||||
allParams, configMetadata, err := i.getAllParamsFromDB(ctx, infraProviderResource, infraProvider)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get all params from DB while provisioning: %w", err)
|
||||
}
|
||||
@ -190,6 +190,7 @@ func (i InfraProvisioner) provisionNewInfrastructure(
|
||||
agentPort,
|
||||
requiredGitspacePorts,
|
||||
allParams,
|
||||
configMetadata,
|
||||
)
|
||||
if err != nil {
|
||||
infraProvisioned.InfraStatus = enum.InfraStatusUnknown
|
||||
@ -215,7 +216,7 @@ func (i InfraProvisioner) provisionExistingInfrastructure(
|
||||
gitspaceConfig types.GitspaceConfig,
|
||||
requiredGitspacePorts []types.GitspacePort,
|
||||
) error {
|
||||
allParams, err := i.getAllParamsFromDB(ctx, gitspaceConfig.InfraProviderResource, infraProvider)
|
||||
allParams, configMetadata, err := i.getAllParamsFromDB(ctx, gitspaceConfig.InfraProviderResource, infraProvider)
|
||||
if err != nil {
|
||||
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.
|
||||
requiredGitspacePorts,
|
||||
allParams,
|
||||
configMetadata,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf(
|
||||
|
@ -28,6 +28,14 @@ func (c *Service) UpdateConfig(ctx context.Context, infraProviderConfig *types.I
|
||||
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()
|
||||
err = c.infraProviderConfigStore.Update(ctx, infraProviderConfig)
|
||||
if err != nil {
|
||||
|
@ -222,9 +222,13 @@ func (i infraProviderConfigStore) mapToInfraProviderConfigs(
|
||||
func (i infraProviderConfigStore) mapToInternalInfraProviderConfig(
|
||||
in *types.InfraProviderConfig,
|
||||
) (*infraProviderConfig, error) {
|
||||
jsonBytes, marshalErr := json.Marshal(in.Metadata)
|
||||
if marshalErr != nil {
|
||||
return nil, marshalErr
|
||||
var jsonBytes []byte
|
||||
var marshalErr error
|
||||
if len(in.Metadata) > 0 {
|
||||
jsonBytes, marshalErr = json.Marshal(in.Metadata)
|
||||
if marshalErr != nil {
|
||||
return nil, marshalErr
|
||||
}
|
||||
}
|
||||
infraProviderConfigEntity := &infraProviderConfig{
|
||||
Identifier: in.Identifier,
|
||||
|
@ -59,6 +59,7 @@ func (d DockerProvider) Provision(
|
||||
_ int,
|
||||
requiredGitspacePorts []types.GitspacePort,
|
||||
inputParameters []types.InfraProviderParameter,
|
||||
_ map[string]any,
|
||||
) error {
|
||||
dockerClient, err := d.dockerClientFactory.NewDockerClient(ctx, types.Infrastructure{
|
||||
ProviderType: enum.InfraProviderTypeDocker,
|
||||
@ -128,6 +129,7 @@ func (d DockerProvider) Find(
|
||||
_ int,
|
||||
_ []types.GitspacePort,
|
||||
inputParameters []types.InfraProviderParameter,
|
||||
_ map[string]any,
|
||||
) (*types.Infrastructure, error) {
|
||||
dockerClient, err := d.dockerClientFactory.NewDockerClient(ctx, types.Infrastructure{
|
||||
ProviderType: enum.InfraProviderTypeDocker,
|
||||
|
@ -32,6 +32,7 @@ type InfraProvider interface {
|
||||
agentPort int,
|
||||
requiredGitspacePorts []types.GitspacePort,
|
||||
inputParameters []types.InfraProviderParameter,
|
||||
configMetadata map[string]any,
|
||||
) error
|
||||
|
||||
// Find finds infrastructure provisioned against a gitspace.
|
||||
@ -44,6 +45,7 @@ type InfraProvider interface {
|
||||
agentPort int,
|
||||
requiredGitspacePorts []types.GitspacePort,
|
||||
inputParameters []types.InfraProviderParameter,
|
||||
configMetadata map[string]any,
|
||||
) (*types.Infrastructure, error)
|
||||
|
||||
// Stop frees up the resources allocated against a gitspace, which can be freed.
|
||||
|
Loading…
Reference in New Issue
Block a user