Error while trying to create HorizontalPodAutoscaler with DynamicResourceClient
See original GitHub issueI couldn’t find a specific HPA namespace for handling APIs, so I noticed it was possible to make generic calls with DynamicResourceClient.
But, during my tests, I received the following error when I tried to create a HPA, although I was filling the ‘Namespace’ property, inside the Metadata object:
Required template parameter 'namespace' is not defined.
Stack Trace
at HTTPlease.Core.Templates.TemplateEvaluationContext.get_Item(String parameterName, Boolean isOptional)
at HTTPlease.Core.Templates.ParameterizedUriSegment.GetValue(ITemplateEvaluationContext evaluationContext)
at HTTPlease.UriTemplate.Populate(Uri baseUri, IDictionary`2 templateParameters)
at HTTPlease.HttpRequest.BuildRequestMessage(HttpMethod httpMethod, HttpContent body, Uri baseUri)
at HTTPlease.FormatterRequestExtensions.BuildRequestMessage(HttpRequest request, HttpMethod httpMethod, Object bodyContent, String mediaType, Encoding encoding, Uri baseUri)
at HTTPlease.FormatterRequestExtensions.BuildRequestMessage(HttpRequest request, HttpMethod httpMethod, Object bodyContent, String mediaType, Uri baseUri)
at HTTPlease.FormatterClientExtensions.<PostAsync>d__0.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at HTTPlease.FormatterResponseExtensions.<ReadContentAsAsync>d__7`2.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at KubeClient.ResourceClients.HttpExtensions.<ReadContentAsObjectV1Async>d__6`1.MoveNext() in C:\Work\dotnet-kube-client\src\KubeClient\ResourceClients\HttpExtensions.cs:line 286
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at KubeClient.ResourceClients.DynamicResourceClient.<Create>d__9`1.MoveNext() in C:\Work\dotnet-kube-client\src\KubeClient\ResourceClients\DynamicResourceClient.cs:line 320
The method I called to create the HPA:
__kubeApiClient__.Dynamic().Create(_hpa, true);
And this is how the object hpa is being created:
HorizontalPodAutoscalerV1 hpa = new HorizontalPodAutoscalerV1()
{
Metadata = new ObjectMetaV1()
{
Name = "hpa_name",
Namespace = "hpa_namespace",
},
Spec = new HorizontalPodAutoscalerSpecV1()
{
MaxReplicas = 4,
MinReplicas = 2,
ScaleTargetRef = new CrossVersionObjectReferenceV1()
{
ApiVersion = "apps/v1",
Kind = "Deployment",
Name = "deployment_name"
},
TargetCPUUtilizationPercentage = 70
}
};
By looking through DynamicResourceClient code, an annonymous object is created with a single property ‘Namespace’ - with a capital “N”
Path: src\KubeClient\ResourceClients\DynamicResourceClient.cs Method: Create Line: 314
request = request.WithTemplateParameters(new
{
Namespace = resource.Metadata?.Namespace ?? KubeClient.DefaultNamespace
});
But, the url parameter is ‘namespace’, with a lower case “n”, as seen below: “apis/autoscaling/v1/namespaces/{namespace}/horizontalpodautoscalers”
I cloned the repository and changed the code metioned above to create an object with a property called ‘namespace’ (lower case “n”). After this change, no error was thrown and my HPA was created.
request = request.WithTemplateParameters(new
{
@namespace = resource.Metadata?.Namespace ?? KubeClient.DefaultNamespace
});
“Patch”, “Get”, “ApplyYaml” and “Delete” methods from the same class also create the request with a lower case “n” property.
So I was wondering if I am doing something wrong. Is there something I am missing? Thanks in advance.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:10
- Comments:7 (4 by maintainers)
Top GitHub Comments
Yep that’s a bug - nice catch 😃
I’ll get that sorted out tomorrow.
Ok, pull request created.