Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.

Commit 193fd17

Browse files
Copilotdevantler
andauthored
Implement HorizontalPodAutoscalerGenerator with custom model and focused tests (#362)
* Initial plan * Implement HorizontalPodAutoscalerGenerator with custom models Co-authored-by: devantler <[email protected]> * refactor: consolidate redundant HorizontalPodAutoscalerGenerator tests Co-authored-by: devantler <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: devantler <[email protected]> Co-authored-by: Nikolai Emil Damm <[email protected]>
1 parent fb09917 commit 193fd17

File tree

8 files changed

+168
-20
lines changed

8 files changed

+168
-20
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using DevantlerTech.KubernetesGenerator.Core;
2-
using k8s.Models;
2+
using DevantlerTech.KubernetesGenerator.Native.Models;
33

44
namespace DevantlerTech.KubernetesGenerator.Native;
55

66
/// <summary>
7-
/// A generator for Kubernetes HorizontalPodAutoscaler objects.
7+
/// A generator for Kubernetes HorizontalPodAutoscaler objects with custom model.
88
/// </summary>
9-
public class HorizontalPodAutoscalerGenerator : BaseKubernetesGenerator<V2HorizontalPodAutoscaler>
9+
public class HorizontalPodAutoscalerGenerator : BaseKubernetesGenerator<HorizontalPodAutoscaler>
1010
{
1111
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace DevantlerTech.KubernetesGenerator.Native.Models;
2+
3+
/// <summary>
4+
/// Represents a Kubernetes HorizontalPodAutoscaler for use with kubectl autoscale.
5+
/// </summary>
6+
public class HorizontalPodAutoscaler
7+
{
8+
/// <summary>
9+
/// Gets or sets the API version of this HorizontalPodAutoscaler.
10+
/// </summary>
11+
public string ApiVersion { get; set; } = "autoscaling/v2";
12+
13+
/// <summary>
14+
/// Gets or sets the kind of this resource.
15+
/// </summary>
16+
public string Kind { get; set; } = "HorizontalPodAutoscaler";
17+
18+
/// <summary>
19+
/// Gets or sets the metadata for the HorizontalPodAutoscaler.
20+
/// </summary>
21+
public required Metadata Metadata { get; set; }
22+
23+
/// <summary>
24+
/// Gets or sets the specification for the HorizontalPodAutoscaler.
25+
/// </summary>
26+
public required HorizontalPodAutoscalerSpec Spec { get; set; }
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace DevantlerTech.KubernetesGenerator.Native.Models;
2+
3+
/// <summary>
4+
/// Represents the target resource reference for HorizontalPodAutoscaler.
5+
/// </summary>
6+
public class HorizontalPodAutoscalerScaleTargetRef
7+
{
8+
/// <summary>
9+
/// Gets or sets the API version of the target resource.
10+
/// </summary>
11+
public string ApiVersion { get; set; } = "apps/v1";
12+
13+
/// <summary>
14+
/// Gets or sets the kind of the target resource.
15+
/// </summary>
16+
public required HorizontalPodAutoscalerTargetKind Kind { get; set; }
17+
18+
/// <summary>
19+
/// Gets or sets the name of the target resource.
20+
/// </summary>
21+
public required string Name { get; set; }
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace DevantlerTech.KubernetesGenerator.Native.Models;
2+
3+
/// <summary>
4+
/// Represents the specification for a HorizontalPodAutoscaler using kubectl autoscale.
5+
/// </summary>
6+
public class HorizontalPodAutoscalerSpec
7+
{
8+
/// <summary>
9+
/// Gets or sets the target resource reference.
10+
/// </summary>
11+
public required HorizontalPodAutoscalerScaleTargetRef ScaleTargetRef { get; set; }
12+
13+
/// <summary>
14+
/// Gets or sets the minimum number of replicas.
15+
/// If null or negative, kubectl will apply a default value.
16+
/// </summary>
17+
public int? MinReplicas { get; set; }
18+
19+
/// <summary>
20+
/// Gets or sets the maximum number of replicas. This is required.
21+
/// </summary>
22+
public required int MaxReplicas { get; set; }
23+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace DevantlerTech.KubernetesGenerator.Native.Models;
2+
3+
/// <summary>
4+
/// Represents the valid resource kinds that can be targeted by HorizontalPodAutoscaler.
5+
/// </summary>
6+
public enum HorizontalPodAutoscalerTargetKind
7+
{
8+
/// <summary>
9+
/// Deployment resource.
10+
/// </summary>
11+
Deployment,
12+
13+
/// <summary>
14+
/// ReplicaSet resource.
15+
/// </summary>
16+
ReplicaSet,
17+
18+
/// <summary>
19+
/// StatefulSet resource.
20+
/// </summary>
21+
StatefulSet,
22+
23+
/// <summary>
24+
/// ReplicationController resource.
25+
/// </summary>
26+
ReplicationController
27+
}
Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using k8s.Models;
1+
using DevantlerTech.KubernetesGenerator.Native.Models;
22

33
namespace DevantlerTech.KubernetesGenerator.Native.Tests.HorizontalPodAutoscalerGeneratorTests;
44

@@ -9,33 +9,30 @@ namespace DevantlerTech.KubernetesGenerator.Native.Tests.HorizontalPodAutoscaler
99
public sealed class GenerateAsyncTests
1010
{
1111
/// <summary>
12-
/// Verifies the generated HorizontalPodAutoscaler object.
12+
/// Verifies the generated HorizontalPodAutoscaler object with full configuration.
1313
/// </summary>
1414
/// <returns></returns>
1515
[Fact]
16-
public async Task GenerateAsync_WithAllPropertiesSet_ShouldGenerateAValidHorizontalPodAutoscaler()
16+
public async Task GenerateAsync_WithFullConfiguration_ShouldGenerateAValidHorizontalPodAutoscaler()
1717
{
1818
// Arrange
1919
var generator = new HorizontalPodAutoscalerGenerator();
20-
var model = new V2HorizontalPodAutoscaler
20+
var model = new HorizontalPodAutoscaler
2121
{
22-
ApiVersion = "autoscaling/v2",
23-
Kind = "HorizontalPodAutoscaler",
24-
Metadata = new V1ObjectMeta
22+
Metadata = new Metadata
2523
{
2624
Name = "horizontal-pod-autoscaler",
27-
NamespaceProperty = "default"
25+
Namespace = "default"
2826
},
29-
Spec = new V2HorizontalPodAutoscalerSpec
27+
Spec = new HorizontalPodAutoscalerSpec
3028
{
31-
ScaleTargetRef = new V2CrossVersionObjectReference
29+
ScaleTargetRef = new HorizontalPodAutoscalerScaleTargetRef
3230
{
33-
ApiVersion = "apps/v1",
34-
Kind = "Deployment",
31+
Kind = HorizontalPodAutoscalerTargetKind.Deployment,
3532
Name = "deployment-name"
3633
},
3734
MinReplicas = 1,
38-
MaxReplicas = 10,
35+
MaxReplicas = 10
3936
}
4037
};
4138

@@ -53,5 +50,46 @@ public async Task GenerateAsync_WithAllPropertiesSet_ShouldGenerateAValidHorizon
5350
// Cleanup
5451
File.Delete(outputPath);
5552
}
53+
54+
/// <summary>
55+
/// Verifies the generated HorizontalPodAutoscaler object with minimal required properties.
56+
/// </summary>
57+
/// <returns></returns>
58+
[Fact]
59+
public async Task GenerateAsync_WithMinimalConfiguration_ShouldGenerateAValidHorizontalPodAutoscaler()
60+
{
61+
// Arrange
62+
var generator = new HorizontalPodAutoscalerGenerator();
63+
var model = new HorizontalPodAutoscaler
64+
{
65+
Metadata = new Metadata
66+
{
67+
Name = "minimal-hpa"
68+
},
69+
Spec = new HorizontalPodAutoscalerSpec
70+
{
71+
ScaleTargetRef = new HorizontalPodAutoscalerScaleTargetRef
72+
{
73+
Kind = HorizontalPodAutoscalerTargetKind.ReplicaSet,
74+
Name = "replicaset-name"
75+
},
76+
MaxReplicas = 5
77+
}
78+
};
79+
80+
// Act
81+
string fileName = "minimal-hpa.yaml";
82+
string outputPath = Path.Combine(Path.GetTempPath(), fileName);
83+
if (File.Exists(outputPath))
84+
File.Delete(outputPath);
85+
await generator.GenerateAsync(model, outputPath);
86+
string fileContent = await File.ReadAllTextAsync(outputPath);
87+
88+
// Assert
89+
_ = await Verify(fileContent, extension: "yaml").UseFileName(fileName);
90+
91+
// Cleanup
92+
File.Delete(outputPath);
93+
}
5694
}
5795

Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
---
1+
---
22
apiVersion: autoscaling/v2
33
kind: HorizontalPodAutoscaler
44
metadata:
5-
name: horizontal-pod-autoscaler
65
namespace: default
6+
name: horizontal-pod-autoscaler
77
spec:
8-
maxReplicas: 10
9-
minReplicas: 1
108
scaleTargetRef:
119
apiVersion: apps/v1
1210
kind: Deployment
1311
name: deployment-name
12+
minReplicas: 1
13+
maxReplicas: 10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
apiVersion: autoscaling/v2
3+
kind: HorizontalPodAutoscaler
4+
metadata:
5+
name: minimal-hpa
6+
spec:
7+
scaleTargetRef:
8+
apiVersion: apps/v1
9+
kind: ReplicaSet
10+
name: replicaset-name
11+
maxReplicas: 5

0 commit comments

Comments
 (0)