mirror of https://github.com/k3s-io/k3s
Merge pull request #53609 from juanvallejo/jvallejo/set-user-specified-ns-dry-run-create
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. add user-specified ns to --dry-run created obj Fixes https://github.com/kubernetes/kubernetes/issues/51068 **Release note**: ```release-note NONE ``` Includes a namespace in a created resource's metadata when `--dry-run` is used if: - a namespace was explicitly set by the user (via `--namespace`) - No errors occur accessing the object's metadata cc @fabianofranz @deads2kpull/6/head
commit
eb041b00ea
|
@ -915,6 +915,32 @@ __EOF__
|
|||
set +o errexit
|
||||
}
|
||||
|
||||
# runs specific kubectl create tests
|
||||
run_create_tests() {
|
||||
set -o nounset
|
||||
set -o errexit
|
||||
|
||||
### Create generic secret with explicit namespace
|
||||
# Pre-condition: secret 'mysecret' does not exist
|
||||
output_message=$(! kubectl get secrets mysecret 2>&1 "${kube_flags[@]}")
|
||||
kube::test::if_has_string "${output_message}" 'secrets "mysecret" not found'
|
||||
# Command
|
||||
output_message=$(kubectl create "${kube_flags[@]}" secret generic mysecret --dry-run --from-literal=foo=bar -o jsonpath='{.metadata.namespace}' --namespace=user-specified)
|
||||
# Post-condition: mysecret still not created since --dry-run was used
|
||||
# Output from 'create' command should contain the specified --namespace value
|
||||
failure_message=$(! kubectl get secrets mysecret 2>&1 "${kube_flags[@]}")
|
||||
kube::test::if_has_string "${failure_message}" 'secrets "mysecret" not found'
|
||||
kube::test::if_has_string "${output_message}" 'user-specified'
|
||||
# Command
|
||||
output_message=$(kubectl create "${kube_flags[@]}" secret generic mysecret --dry-run --from-literal=foo=bar -o jsonpath='{.metadata.namespace}')
|
||||
# Post-condition: jsonpath for .metadata.namespace should be empty for object since --namespace was not explicitly specified
|
||||
kube::test::if_empty_string "${output_message}"
|
||||
|
||||
set +o nounset
|
||||
set +o errexit
|
||||
}
|
||||
|
||||
|
||||
# Runs tests related to kubectl apply.
|
||||
run_kubectl_apply_tests() {
|
||||
set -o nounset
|
||||
|
@ -4568,6 +4594,14 @@ runTests() {
|
|||
record_command run_kubectl_get_tests
|
||||
fi
|
||||
|
||||
|
||||
######################
|
||||
# Create #
|
||||
######################
|
||||
if kube::test::if_supports_resource "${secrets}" ; then
|
||||
record_command run_create_tests
|
||||
fi
|
||||
|
||||
##################
|
||||
# Global timeout #
|
||||
##################
|
||||
|
|
|
@ -23,7 +23,9 @@ import (
|
|||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
|
||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||
|
@ -242,7 +244,7 @@ type CreateSubcommandOptions struct {
|
|||
|
||||
// RunCreateSubcommand executes a create subcommand using the specified options
|
||||
func RunCreateSubcommand(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *CreateSubcommandOptions) error {
|
||||
namespace, _, err := f.DefaultNamespace()
|
||||
namespace, nsOverriden, err := f.DefaultNamespace()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -283,6 +285,10 @@ func RunCreateSubcommand(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, o
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if meta, err := meta.Accessor(obj); err == nil && nsOverriden {
|
||||
meta.SetNamespace(namespace)
|
||||
}
|
||||
}
|
||||
|
||||
if useShortOutput := options.OutputFormat == "name"; useShortOutput || len(options.OutputFormat) == 0 {
|
||||
|
|
Loading…
Reference in New Issue