-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide scraper for Azure Kubernetes Service (#1322)
Co-authored-by: Tom Kerkhove <[email protected]> Co-authored-by: Janne Kataja <[email protected]>
- Loading branch information
1 parent
31b7d09
commit c6aab27
Showing
19 changed files
with
355 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
layout: default | ||
title: Azure Kubernetes Service Declaration | ||
--- | ||
|
||
## Azure Kubernetes Service | ||
|
||
![Availability Badge](https://img.shields.io/badge/Available%20Starting-v2.0-green.svg)![Resource Discovery Support Badge](https://img.shields.io/badge/Support%20for%20Resource%20Discovery-Yes-green.svg) | ||
|
||
You can declare to scrape an Azure Kubernetes Service (AKS) | ||
via the `KubernetesService` resource type. | ||
|
||
The following fields need to be provided: | ||
|
||
- `clusterName` - The name of the Azure Kubernetes Service | ||
|
||
All supported metrics are documented in the official [Azure Monitor documentation](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/metrics-supported#microsoftcontainerservicemanagedclusters). | ||
|
||
Example: | ||
|
||
```yaml | ||
name: azure_kubernetes_available_cpu_cores | ||
description: "Available CPU cores in cluster" | ||
resourceType: KubernetesService | ||
azureMetricConfiguration: | ||
metricName: kube_node_status_allocatable_cpu_cores | ||
aggregation: | ||
type: Average | ||
resources: | ||
- clusterName: promitor-aks | ||
``` | ||
<!-- markdownlint-disable MD033 --> | ||
[← back to metrics declarations](/configuration/v2.x/metrics)<br /> | ||
[← back to introduction](/) | ||
<!-- markdownlint-enable --> |
This file contains 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 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
21 changes: 21 additions & 0 deletions
21
src/Promitor.Agents.ResourceDiscovery/Graph/ResourceTypes/KubernetesServiceDiscoveryQuery.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using GuardNet; | ||
using Newtonsoft.Json.Linq; | ||
using Promitor.Core.Contracts; | ||
using Promitor.Core.Contracts.ResourceTypes; | ||
|
||
namespace Promitor.Agents.ResourceDiscovery.Graph.ResourceTypes | ||
{ | ||
public class KubernetesServiceDiscoveryQuery : ResourceDiscoveryQuery | ||
{ | ||
public override string[] ResourceTypes => new[] { "Microsoft.ContainerService/managedClusters" }; | ||
public override string[] ProjectedFieldNames => new[] { "subscriptionId", "resourceGroup", "type", "name" }; | ||
|
||
public override AzureResourceDefinition ParseResults(JToken resultRowEntry) | ||
{ | ||
Guard.NotNull(resultRowEntry, nameof(resultRowEntry)); | ||
|
||
var resource = new KubernetesServiceResourceDefinition(resultRowEntry[0]?.ToString(), resultRowEntry[1]?.ToString(), resultRowEntry[3]?.ToString()); | ||
return resource; | ||
} | ||
} | ||
} |
This file contains 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
25 changes: 25 additions & 0 deletions
25
...ts.Scraper/Validation/MetricDefinitions/ResourceTypes/KubernetesServiceMetricValidator.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using GuardNet; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics; | ||
using Promitor.Agents.Scraper.Validation.MetricDefinitions.Interfaces; | ||
using Promitor.Core.Contracts.ResourceTypes; | ||
|
||
namespace Promitor.Agents.Scraper.Validation.MetricDefinitions.ResourceTypes | ||
{ | ||
internal class KubernetesServiceMetricValidator : IMetricValidator | ||
{ | ||
public IEnumerable<string> Validate(MetricDefinition metricDefinition) | ||
{ | ||
Guard.NotNull(metricDefinition, nameof(metricDefinition)); | ||
|
||
foreach (var resourceDefinition in metricDefinition.Resources.Cast<KubernetesServiceResourceDefinition>()) | ||
{ | ||
if (string.IsNullOrWhiteSpace(resourceDefinition.ClusterName)) | ||
{ | ||
yield return "No Azure Kubernetes Service cluster name is configured"; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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
13 changes: 13 additions & 0 deletions
13
src/Promitor.Core.Contracts/ResourceTypes/KubernetesServiceResourceDefinition.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Promitor.Core.Contracts.ResourceTypes | ||
{ | ||
public class KubernetesServiceResourceDefinition : AzureResourceDefinition | ||
{ | ||
public KubernetesServiceResourceDefinition(string subscriptionId, string resourceGroupName, string clusterName) | ||
: base(ResourceType.KubernetesService, subscriptionId, resourceGroupName, clusterName) | ||
{ | ||
ClusterName = clusterName; | ||
} | ||
|
||
public string ClusterName { get; } | ||
} | ||
} |
This file contains 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 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
13 changes: 13 additions & 0 deletions
13
...craping/Configuration/Serialization/v1/Model/ResourceTypes/KubernetesServiceResourceV1.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Promitor.Core.Scraping.Configuration.Serialization.v1.Model.ResourceTypes | ||
{ | ||
/// <summary> | ||
/// Contains the configuration required to scrape an Azure Kubernetes Service. | ||
/// </summary> | ||
public class KubernetesServiceResourceV1 : AzureResourceDefinitionV1 | ||
{ | ||
/// <summary> | ||
/// The name of the Azure Kubernetes Service to get metrics for. | ||
/// </summary> | ||
public string ClusterName { get; set; } | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...r.Core.Scraping/Configuration/Serialization/v1/Providers/KubernetesServiceDeserializer.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Microsoft.Extensions.Logging; | ||
using Promitor.Core.Scraping.Configuration.Serialization.v1.Model.ResourceTypes; | ||
|
||
namespace Promitor.Core.Scraping.Configuration.Serialization.v1.Providers | ||
{ | ||
public class KubernetesServiceDeserializer : ResourceDeserializer<KubernetesServiceResourceV1> | ||
{ | ||
public KubernetesServiceDeserializer(ILogger<KubernetesServiceDeserializer> logger) : base(logger) | ||
{ | ||
Map(resource => resource.ClusterName) | ||
.IsRequired(); | ||
} | ||
} | ||
} |
This file contains 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
22 changes: 22 additions & 0 deletions
22
src/Promitor.Core.Scraping/ResourceTypes/KubernetesServiceScraper.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Promitor.Core.Contracts; | ||
using Promitor.Core.Contracts.ResourceTypes; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics; | ||
|
||
namespace Promitor.Core.Scraping.ResourceTypes | ||
{ | ||
public class KubernetesServiceScraper : AzureMonitorScraper<KubernetesServiceResourceDefinition> | ||
{ | ||
private const string ResourceUriTemplate = "subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.ContainerService/managedClusters/{2}"; | ||
|
||
public KubernetesServiceScraper(ScraperConfiguration scraperConfiguration) : | ||
base(scraperConfiguration) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override string BuildResourceUri(string subscriptionId, ScrapeDefinition<IAzureResourceDefinition> scrapeDefinition, KubernetesServiceResourceDefinition resource) | ||
{ | ||
return string.Format(ResourceUriTemplate, subscriptionId, scrapeDefinition.ResourceGroupName, resource.ClusterName); | ||
} | ||
} | ||
} |
This file contains 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
45 changes: 45 additions & 0 deletions
45
src/Promitor.Tests.Unit/Serialization/v1/Providers/KubernetesServiceDeserializerTests.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using Promitor.Core.Scraping.Configuration.Serialization; | ||
using Promitor.Core.Scraping.Configuration.Serialization.v1.Model; | ||
using Promitor.Core.Scraping.Configuration.Serialization.v1.Model.ResourceTypes; | ||
using Promitor.Core.Scraping.Configuration.Serialization.v1.Providers; | ||
using System.ComponentModel; | ||
using Xunit; | ||
|
||
namespace Promitor.Tests.Unit.Serialization.v1.Providers | ||
{ | ||
[Category("Unit")] | ||
public class KubernetesServiceDeserializerTests : ResourceDeserializerTest<KubernetesServiceDeserializer> | ||
{ | ||
private readonly KubernetesServiceDeserializer _deserializer; | ||
|
||
public KubernetesServiceDeserializerTests() | ||
{ | ||
_deserializer = new KubernetesServiceDeserializer(Logger); | ||
} | ||
|
||
[Fact] | ||
public void Deserialize_ClusterNameSupplied_SetsClusterName() | ||
{ | ||
const string clusterName = "promitor-aks"; | ||
YamlAssert.PropertySet<KubernetesServiceResourceV1, AzureResourceDefinitionV1, string>( | ||
_deserializer, | ||
$"clusterName: {clusterName}", | ||
clusterName, | ||
r => r.ClusterName); | ||
} | ||
|
||
[Fact] | ||
public void Deserialize_ClusterNameNotSupplied_Null() | ||
{ | ||
YamlAssert.PropertyNull<KubernetesServiceResourceV1, AzureResourceDefinitionV1>( | ||
_deserializer, | ||
"resourceGroupName: promitor-group", | ||
r => r.ClusterName); | ||
} | ||
|
||
protected override IDeserializer<AzureResourceDefinitionV1> CreateDeserializer() | ||
{ | ||
return new KubernetesServiceDeserializer(Logger); | ||
} | ||
} | ||
} |
Oops, something went wrong.