-
Notifications
You must be signed in to change notification settings - Fork 123
Fleet agent policy host name format field #1521
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
Merged
dimuon
merged 11 commits into
elastic:main
from
dimuon:fleet-agent-policy-host-name-format-field
Dec 9, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
643cadd
Move rerun_fails to env var
dimuon 4784e95
Support host name format in fleet agent policy.
dimuon f8eaefe
fmt
dimuon b058f82
update docs
dimuon d73a3d1
Update changelog
dimuon 82eca3e
Enforce version
dimuon 7a95c25
Update internal/fleet/agent_policy/schema.go
dimuon 70d66c3
Update internal/fleet/agent_policy/schema.go
dimuon 2aef2e3
Merge branch 'main' into fleet-agent-policy-host-name-format-field
dimuon a70d44f
Don't fail for 8.6.2 if host_name_format is hostname
dimuon 3679778
Merge branch 'fleet-agent-policy-host-name-format-field' of github.co…
dimuon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,13 +17,29 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // HostNameFormatHostname represents the short hostname format (e.g., "myhost") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HostNameFormatHostname = "hostname" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // HostNameFormatFQDN represents the fully qualified domain name format (e.g., "myhost.example.com") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HostNameFormatFQDN = "fqdn" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // agentFeatureFQDN is the name of the agent feature that enables FQDN host name format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| agentFeatureFQDN = "fqdn" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // apiAgentFeature is the type expected by the generated API for agent features | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type apiAgentFeature = struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Enabled bool `json:"enabled"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Name string `json:"name"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type features struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SupportsGlobalDataTags bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SupportsSupportsAgentless bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SupportsInactivityTimeout bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SupportsUnenrollmentTimeout bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SupportsSpaceIds bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SupportsRequiredVersions bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SupportsAgentFeatures bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type globalDataTagsItemModel struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -45,6 +61,7 @@ type agentPolicyModel struct { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| MonitorMetrics types.Bool `tfsdk:"monitor_metrics"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SysMonitoring types.Bool `tfsdk:"sys_monitoring"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SkipDestroy types.Bool `tfsdk:"skip_destroy"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HostNameFormat types.String `tfsdk:"host_name_format"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SupportsAgentless types.Bool `tfsdk:"supports_agentless"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| InactivityTimeout customtypes.Duration `tfsdk:"inactivity_timeout"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| UnenrollmentTimeout customtypes.Duration `tfsdk:"unenrollment_timeout"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -84,6 +101,20 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi. | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.Name = types.StringValue(data.Name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.Namespace = types.StringValue(data.Namespace) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.SupportsAgentless = types.BoolPointerValue(data.SupportsAgentless) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Determine host_name_format from AgentFeatures | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // If AgentFeatures contains {"enabled": true, "name": "fqdn"}, then host_name_format is "fqdn" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Otherwise, it defaults to "hostname" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.HostNameFormat = types.StringValue(HostNameFormatHostname) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if data.AgentFeatures != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, feature := range *data.AgentFeatures { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if feature.Name == agentFeatureFQDN && feature.Enabled { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model.HostNameFormat = types.StringValue(HostNameFormatFQDN) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if data.InactivityTimeout != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Convert seconds to duration string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| seconds := int64(*data.InactivityTimeout) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -377,10 +408,30 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, feat featur | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body.RequiredVersions = requiredVersions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Handle host_name_format via AgentFeatures | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if agentFeature := model.convertHostNameFormatToAgentFeature(); agentFeature != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !feat.SupportsAgentFeatures { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Only error if user explicitly requests FQDN on unsupported version | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Default "hostname" is fine - just don't send agent_features | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if agentFeature.Enabled { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return kbapi.PostFleetAgentPoliciesJSONRequestBody{}, diag.Diagnostics{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| diag.NewAttributeErrorDiagnostic( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| path.Root("host_name_format"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Unsupported Elasticsearch version", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Sprintf("host_name_format (agent_features) is only supported in Elastic Stack %s and above", MinVersionAgentFeatures), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // On unsupported version with default "hostname", don't send agent_features | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body.AgentFeatures = &[]apiAgentFeature{*agentFeature} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return body, nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, feat features) (kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody, diag.Diagnostics) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, feat features, existingFeatures []apiAgentFeature) (kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody, diag.Diagnostics) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| monitoring := make([]kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBodyMonitoringEnabled, 0, 2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if model.MonitorLogs.ValueBool() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| monitoring = append(monitoring, kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBodyMonitoringEnabledLogs) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -481,5 +532,76 @@ func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, feat featur | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body.RequiredVersions = requiredVersions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Handle host_name_format via AgentFeatures, preserving other existing features | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if agentFeature := model.convertHostNameFormatToAgentFeature(); agentFeature != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !feat.SupportsAgentFeatures { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Only error if user explicitly requests FQDN on unsupported version | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Default "hostname" is fine - just don't send agent_features | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if agentFeature.Enabled { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody{}, diag.Diagnostics{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| diag.NewAttributeErrorDiagnostic( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| path.Root("host_name_format"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Unsupported Elasticsearch version", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Sprintf("host_name_format (agent_features) is only supported in Elastic Stack %s and above", MinVersionAgentFeatures), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // On unsupported version with default "hostname", don't send agent_features | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body.AgentFeatures = mergeAgentFeature(existingFeatures, agentFeature) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if feat.SupportsAgentFeatures && len(existingFeatures) > 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Preserve existing features even when host_name_format is not set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body.AgentFeatures = &existingFeatures | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return body, nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // convertHostNameFormatToAgentFeature converts the host_name_format field to a single AgentFeature. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // - When host_name_format is "fqdn": returns {"name": "fqdn", "enabled": true} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // - When host_name_format is "hostname": returns {"name": "fqdn", "enabled": false} to explicitly disable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // - When not set: returns nil (no change to existing features) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (model *agentPolicyModel) convertHostNameFormatToAgentFeature() *apiAgentFeature { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // If host_name_format is not set or unknown, don't modify AgentFeatures | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if model.HostNameFormat.IsNull() || model.HostNameFormat.IsUnknown() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Explicitly set enabled based on the host_name_format value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // We need to send enabled: false when hostname is selected to override any existing fqdn setting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return &apiAgentFeature{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Enabled: model.HostNameFormat.ValueString() == HostNameFormatFQDN, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Name: agentFeatureFQDN, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // mergeAgentFeature merges a single feature into existing features, replacing any feature with the same name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // If newFeature is nil, returns existing features unchanged (nil if existing is empty). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func mergeAgentFeature(existing []apiAgentFeature, newFeature *apiAgentFeature) *[]apiAgentFeature { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if newFeature == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(existing) == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return &existing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Check if the feature already exists and replace it, otherwise append | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result := make([]apiAgentFeature, 0, len(existing)+1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| found := false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, f := range existing { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if f.Name == newFeature.Name { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = append(result, *newFeature) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| found = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+590
to
+596
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result := make([]apiAgentFeature, 0, len(existing)+1) | |
| found := false | |
| for _, f := range existing { | |
| if f.Name == newFeature.Name { | |
| result = append(result, *newFeature) | |
| found = true | |
| // Pre-scan to determine if we are replacing or appending | |
| found := false | |
| for _, f := range existing { | |
| if f.Name == newFeature.Name { | |
| found = true | |
| break | |
| } | |
| } | |
| // Allocate with correct capacity: len(existing) if replacing, len(existing)+1 if appending | |
| capacity := len(existing) | |
| if !found { | |
| capacity++ | |
| } | |
| result := make([]apiAgentFeature, 0, capacity) | |
| for _, f := range existing { | |
| if f.Name == newFeature.Name { | |
| result = append(result, *newFeature) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type alias
apiAgentFeatureshould be a named type rather than a type alias. Usingtype apiAgentFeature = structcreates an alias which may cause confusion when comparing types. Change totype apiAgentFeature structto define a proper named type.