question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error while trying to create HorizontalPodAutoscaler with DynamicResourceClient

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
tintoycommented, Sep 16, 2020

Yep that’s a bug - nice catch 😃

I’ll get that sorted out tomorrow.

0reactions
diogoaraujo9commented, Sep 16, 2020

Ok, pull request created.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HorizontalPodAutoscaler Walkthrough
This document walks you through an example of enabling HorizontalPodAutoscaler to automatically manage scale for an example web app. This example workload is ......
Read more >
Why is there an error in Horizontal Pod Autoscaler Yaml?
I tried the memory "averageUtilization" values with 1.5Gi and "1.5Gi" with double quotes. Can anyone check and let me know if I am...
Read more >
Kubernetes horizontal pod autoscaling not working
After looking at the controller manager code, I'm able to figure out the reason for this failure. Controller manager is hardcoded to lookup...
Read more >
HPA not working: failed to get cpu utilization: unable ...
I am trying to make it work by changing a particular parameter(--horizontal-pod-autoscaler-use-rest-clients) in kube-controller-manager ...
Read more >
How to fix failed get resource metric in Kubernetes HPA
Learn how to resolve the confusing FailedGetResourceMetric error in HPA and get your pods auto scaling back on track!
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found