Skip to content

feat: Support existing log analytics workspace (#211)#213

Merged
lonegunmanb merged 5 commits intoAzure:masterfrom
viters:feat/211
Jul 8, 2022
Merged

feat: Support existing log analytics workspace (#211)#213
lonegunmanb merged 5 commits intoAzure:masterfrom
viters:feat/211

Conversation

@viters
Copy link
Copy Markdown
Contributor

@viters viters commented Jul 6, 2022

Fixes #211

Changes proposed in the pull request:

Provides a way to attach existing Log Analytics Workspace to AKS through Container Insights (azurerm_log_analytics_solution).

@ghost
Copy link
Copy Markdown

ghost commented Jul 6, 2022

CLA assistant check
All CLA requirements met.

Copy link
Copy Markdown
Member

@lonegunmanb lonegunmanb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @viters , thanks for submitting this pr. Almost LGTM but only one question.

Comment thread main.tf Outdated
resource_group_name = var.resource_group_name
workspace_resource_id = azurerm_log_analytics_workspace.main[0].id
workspace_name = azurerm_log_analytics_workspace.main[0].name
workspace_resource_id = var.log_analytics_workspace == null ? azurerm_log_analytics_workspace.main[0].id : var.log_analytics_workspace.id
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, do we need add this var.log_analytics_workspace == null criteria to azurerm_log_analytics_solution's count expression, since the log analytics resource is injected by the module caller?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

azurerm_log_analytics_solution is ContainerInsights solution connecting newly created AKS and existing (or just created) Workspace. I think that current count expression var.enable_log_analytics_workspace ? 1 : 0 is valid. If caller specifies enable_log_analytics_workspace = true then caller either wants to create a new Workspace or use existing. In other words ContainerInsights solution is created always when enable_log_analytics_workspace = true, but the created solution can be connected to different Workspaces, which is solved later by conditions on workspace_resource_id and workspace_name.

Copy link
Copy Markdown
Contributor Author

@viters viters Jul 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a question though, because I checked in azurerm_log_analytics_solution docs that:

image

In my current solution, existing Workspace var.log_analytics_workspace must be placed in the same RG as var.resource_group_name. Do you think it is valid assumption? It was valid in my use case.

Otherwise I can add resource_group_name field to var.log_analytics_workspace. It would mean that caller can create azurerm_log_analytics_solution in RG different than AKS resource.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

azurerm_log_analytics_solution is ContainerInsights solution connecting newly created AKS and existing (or just created) Workspace. I think that current count expression var.enable_log_analytics_workspace ? 1 : 0 is valid. If caller specifies enable_log_analytics_workspace = true then caller either wants to create a new Workspace or use existing. In other words ContainerInsights solution is created always when enable_log_analytics_workspace = true, but the created solution can be connected to different Workspaces, which is solved later by conditions on workspace_resource_id and workspace_name.

My concern is what if the caller has used another module that creates log analytics domain resources? It looks like we need both workspace and solution to make log work, so maybe we should treat them as an inseparable entity?

Copy link
Copy Markdown
Member

@lonegunmanb lonegunmanb Jul 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a question though, because I checked in azurerm_log_analytics_solution docs that:

image

In my current solution, existing Workspace var.log_analytics_workspace must be placed in the same RG as var.resource_group_name. Do you think it is valid assumption? It was valid in my use case.

Otherwise I can add resource_group_name field to var.log_analytics_workspace. It would mean that caller can create azurerm_log_analytics_solution in RG different than AKS resource.

Good question! I don't know whether it's valid to use different resource groups for aks cluster and log analytics workspace.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've verified that the workspace can be put in different rg, but must be in the same rg with solution, so I prefer we add this var.log_analytics_workspace == null criteria to solution's count expression, leave the choice to the caller.

Copy link
Copy Markdown
Contributor Author

@viters viters Jul 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not get why should we add

var.log_analytics_workspace == null criteria to solution's count expression

The idea of that feature is not to use existing azurerm_log_analytics_solution, but to use existing azurerm_log_analytics_workspace. We need to create azurerm_log_analytics_solution irregardless of var.log_analytics_workspace value. I do not understand why var.log_analytics_workspace == null should be a part of criteria to solution's count expression.

I've verified that the workspace can be put in different rg, but must be in the same rg with solution

Awesome! I will add a way to set Workspace/Solution RG then.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @viters my concern is can our module be composited with other modules easily. When you opened this pr I do agree with you because what if the caller want to use another module to manage log analytics related resources? Your pr makes it possible to avoid a duplicated workspace and leaves the choice to the caller.

So what if the caller has a module that manage all log analytics related resources, even in a standalone repo along with its own state backend? There's no way for the caller to avoid bind the workspace to a solution that he doesn't want.

I think you pr's value is giving callers the right of composition, so can we give the caller more? Thanks!

Copy link
Copy Markdown
Contributor Author

@viters viters Jul 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lonegunmanb
Oh, I got it. Your idea is to add a way to use existing azurerm_log_analytics_solution and skip creation of it as well! I understand now, I will get to it!

Copy link
Copy Markdown
Contributor Author

@viters viters Jul 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lonegunmanb
As we discussed, I've added var.log_analytics_solution_id and azurerm_log_analytics_solution resource will not be created if var.log_analytics_solution_id != null.

I've used string id instead of bool to avoid breaking changes in future - we do not need the solution ID now, but it may be required in future Azure API somewhere, e.g. in oms_agent block.

@viters
Copy link
Copy Markdown
Contributor Author

viters commented Jul 7, 2022

@lonegunmanb
I've added a support for Log Analytics Workspace (both existing and newly created) with Container Insights Solution to be created in different RG than AKS.

Comment thread main.tf Outdated
name = var.cluster_log_analytics_workspace_name == null ? "${var.prefix}-workspace" : var.cluster_log_analytics_workspace_name
location = coalesce(var.location, data.azurerm_resource_group.main.location)
resource_group_name = var.resource_group_name
resource_group_name = var.log_analytics_workspace_resource_group_name != null ? var.log_analytics_workspace_resource_group_name : var.resource_group_name
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would coalesce(var.log_analytics_workspace_resource_group_name, var.resource_group_name) here be better?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will check how coalesce works and update!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lonegunmanb Used coalesce as you suggested!

Comment thread main.tf Outdated
resource_group_name = var.resource_group_name
workspace_resource_id = azurerm_log_analytics_workspace.main[0].id
workspace_name = azurerm_log_analytics_workspace.main[0].name
resource_group_name = var.log_analytics_workspace_resource_group_name != null ? var.log_analytics_workspace_resource_group_name : var.resource_group_name
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would coalesce(var.log_analytics_workspace_resource_group_name, var.resource_group_name) here be better?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will check how coalesce works and update!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lonegunmanb Used coalesce as you suggested!

Comment thread main.tf Outdated
resource_group_name = var.resource_group_name
workspace_resource_id = azurerm_log_analytics_workspace.main[0].id
workspace_name = azurerm_log_analytics_workspace.main[0].name
workspace_resource_id = var.log_analytics_workspace == null ? azurerm_log_analytics_workspace.main[0].id : var.log_analytics_workspace.id
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @viters my concern is can our module be composited with other modules easily. When you opened this pr I do agree with you because what if the caller want to use another module to manage log analytics related resources? Your pr makes it possible to avoid a duplicated workspace and leaves the choice to the caller.

So what if the caller has a module that manage all log analytics related resources, even in a standalone repo along with its own state backend? There's no way for the caller to avoid bind the workspace to a solution that he doesn't want.

I think you pr's value is giving callers the right of composition, so can we give the caller more? Thanks!

Copy link
Copy Markdown
Member

@lonegunmanb lonegunmanb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @viters, LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

enable_log_analytics_workspace cannot use existing Log Analytics Workspace

2 participants