Closed banool closed 3 weeks ago
I see in the generated code that if replicas
is None
then it won't even serialize the field (I think), so I suppose everything is working as expected and the issue is elsewhere:
if let Some(value) = &self.replicas {
crate::serde::ser::SerializeStruct::serialize_field(&mut state, "replicas", value)?;
}
Just weird that every time I update the resource the spec changes to have replicas: 1
again, with the HPA then setting it back to 2, over and over.
I'm getting sort of conflicting information. This source says that you should omit the field, but it also says that the default field is 1: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#migrating-deployments-and-statefulsets-to-horizontal-autoscaling. Anyway, the thing relevant to this crate is whether it is truly leaving replicas
unspecified when making the request to the API. The code would imply as such, but when my code runs, the value is explicitly set regardless. So I suppose I need some way to explicitly not set the field rather than rely on the default behavior?
Yes, in k8s-openapi, a None
field won't be serialized in the request at all. And yes, Kubernetes documents DeploymentSpec
as setting replicas
to 1 if it's not specified, so the API server will do that if you create a Deployment with the field set to None
.
As for updates, when you update the Deployment later and again don't specify the replicas
field, the API server might naively do the same thing and reset replicas
to 1
even though an HPA is in effect, until the HPA runs again and resets it back to the scaled value. You'll have to check Kubernetes source to be sure, but I can imagine the Deployment controller is independent of the HPA controller so this behavior is expected.
Okay got it, thanks for the clarification! I'll investigate the k8s behavior on my side.
Also note that the update strategy might matter, ie whether you're doing strategic merge or JSON merge.
The issue is I was just using the wholesale replace function, I should be using patch
. Thanks!
I am creating a Deployment and a HPA. I'm trying to set the replicas to
None
in the deployment to declare that I don't want to actually set a value for it when I create / update the resource:I then have a HPA like this:
When these resources are updated with the above specs, the target replicas for the Deployment becomes 1 again. The docs imply that I can set
None
to indicate that this is explicitly not set:But maybe I'm misinterpreting it. If so, is there a way to skip the
replicas
field when I update the resource?