diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/autoscale/autoscale.go b/staging/src/k8s.io/kubectl/pkg/cmd/autoscale/autoscale.go index 834902278c6..92f60b4941a 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/autoscale/autoscale.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/autoscale/autoscale.go @@ -362,6 +362,10 @@ func (o *AutoscaleOptions) createHorizontalPodAutoscalerV2(refName string, mappi scaler.Spec.MinReplicas = &o.Min } + if o.enforceNamespace { + scaler.ObjectMeta.Namespace = o.namespace + } + metrics := []autoscalingv2.MetricSpec{} // add CPU metric if any of the CPU targets are specified @@ -470,6 +474,10 @@ func (o *AutoscaleOptions) createHorizontalPodAutoscalerV1(refName string, mappi scaler.Spec.TargetCPUUtilizationPercentage = &c } + if o.enforceNamespace { + scaler.ObjectMeta.Namespace = o.namespace + } + return &scaler } diff --git a/test/cmd/apps.sh b/test/cmd/apps.sh index 979c113007e..5098c019084 100755 --- a/test/cmd/apps.sh +++ b/test/cmd/apps.sh @@ -811,6 +811,10 @@ run_rs_tests() { kube::test::if_has_string "${output_message}" 'kubectl-autoscale' # Clean up kubectl delete hpa frontend "${kube_flags[@]:?}" + # autoscale with --dry-run=client should include namespace in output + autoscale_namespace=$(kubectl get rs frontend "${kube_flags[@]:?}" -o jsonpath='{.metadata.namespace}') + output_message=$(kubectl autoscale rs frontend "${kube_flags[@]:?}" --max=3 -n "$autoscale_namespace" --dry-run=client -o yaml 2>&1) + kube::test::if_has_string "${output_message}" 'namespace:' # autoscale without specifying --max should fail ! kubectl autoscale rs frontend "${kube_flags[@]:?}" || exit 1 # Clean up