Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CP-5474] Add ML nodes autoscaling support #761

Merged
merged 11 commits into from
Jan 16, 2024
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ resource "ec_deployment" "example_minimal" {
hot = {
autoscaling = {}
}

ml = {
autoscaling = {
autoscale = true
}
}

}

kibana = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package v2

import (
"context"

"github.com/elastic/cloud-sdk-go/pkg/api"
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/deptemplateapi"
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/esremoteclustersapi"
Expand Down Expand Up @@ -84,6 +83,9 @@ func (dep DeploymentTF) CreateRequest(ctx context.Context, client *api.API) (*mo
return nil, diagsnostics
}

// We don't want this setting to be inferred from the deployment template when using TF provider - it should be specified explicitly in resource declaration
removeAutoscalingTierOverridesFromTemplate(template)

baseUpdatePayloads := &models.DeploymentUpdateResources{
Apm: template.DeploymentTemplate.Resources.Apm,
Appsearch: template.DeploymentTemplate.Resources.Appsearch,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 v2

import "github.com/elastic/cloud-sdk-go/pkg/models"

func removeAutoscalingTierOverridesFromTemplate(template *models.DeploymentTemplateInfoV2) {
for _, esResource := range template.DeploymentTemplate.Resources.Elasticsearch {
for _, topologyElem := range esResource.Plan.ClusterTopology {
topologyElem.AutoscalingTierOverride = nil
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func (plan DeploymentTF) getBaseUpdatePayloads(ctx context.Context, client *api.
return nil, err
}

// Similarly to deployment creation, we don't want this setting to be inferred from the deployment template
removeAutoscalingTierOverridesFromTemplate(template)

baseUpdatePayloads := &models.DeploymentUpdateResources{
Apm: template.DeploymentTemplate.Resources.Apm,
Appsearch: template.DeploymentTemplate.Resources.Appsearch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ElasticsearchTopologyAutoscalingTF struct {
MinSizeResource types.String `tfsdk:"min_size_resource"`
MinSize types.String `tfsdk:"min_size"`
PolicyOverrideJson types.String `tfsdk:"policy_override_json"`
TierAutoscale types.Bool `tfsdk:"autoscale"`
}

type ElasticsearchTopologyAutoscaling struct {
Expand All @@ -35,6 +36,7 @@ type ElasticsearchTopologyAutoscaling struct {
MinSizeResource *string `tfsdk:"min_size_resource"`
MinSize *string `tfsdk:"min_size"`
PolicyOverrideJson *string `tfsdk:"policy_override_json"`
TierAutoscale *bool `tfsdk:"autoscale"`
}

type ElasticsearchTopologyAutoscalings []ElasticsearchTopologyAutoscaling
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ func readElasticsearchTopologyAutoscaling(topology *models.ElasticsearchClusterT
a.PolicyOverrideJson = ec.String(string(b))
}

if topology.AutoscalingTierOverride != nil && *topology.AutoscalingTierOverride {
a.TierAutoscale = topology.AutoscalingTierOverride
}

return &a, nil
}

Expand Down Expand Up @@ -357,6 +361,10 @@ func elasticsearchTopologyAutoscalingPayload(ctx context.Context, autoObj attr.V
}
}

if !autoscale.TierAutoscale.IsNull() && !autoscale.TierAutoscale.IsUnknown() {
payload.AutoscalingTierOverride = autoscale.TierAutoscale.ValueBoolPointer()
mieciu marked this conversation as resolved.
Show resolved Hide resolved
}

return diag
}

Expand Down
8 changes: 8 additions & 0 deletions ec/ecresource/deploymentresource/elasticsearch/v2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ func elasticsearchTopologyAutoscalingSchema(topologyAttributeName string) schema
UseTopologyStateForUnknown(topologyAttributeName),
},
},
"autoscale": schema.BoolAttribute{
dimuon marked this conversation as resolved.
Show resolved Hide resolved
Description: `Whether this specific tier should be auto-scaled, overrides deployment-wide setting`,
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.Bool{
UseTopologyStateForUnknown(topologyAttributeName),
},
},
"policy_override_json": schema.StringAttribute{
Description: "Computed policy overrides set directly via the API or other clients.",
Computed: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func (m useTopologyState) PlanModifyInt64(ctx context.Context, req planmodifier.
}
}

func (m useTopologyState) PlanModifyBool(ctx context.Context, req planmodifier.BoolRequest, resp *planmodifier.BoolResponse) {
useState, diags := m.UseState(ctx, req.ConfigValue, req.Plan, req.State, resp.PlanValue)
resp.Diagnostics.Append(diags...)
if useState {
resp.PlanValue = req.StateValue
}
}

type PlanModifierResponse interface {
planmodifier.StringResponse | planmodifier.Int64Response
}
Expand Down
Loading