Merge pull request #50497 from dixudx/kubectl-include-uninitialized

Automatic merge from submit-queue (batch tested with PRs 51301, 50497, 50112, 48184, 50993)

Introduce new flag "--include-uninitialized" to kubectl

**What this PR does / why we need it**:

Introduce `--include-uninitialized` as a global flag to kubectl

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #49035

**Special notes for your reviewer**:
/assign @caesarxuchao @smarterclayton @ahmetb @deads2k 

**Release note**:

```release-note
Add flag "--include-uninitialized" to kubectl annotate, apply, edit-last-applied, delete, describe, edit, get, label, set. "--include-uninitialized=true" makes kubectl commands apply to uninitialized objects, which by default are ignored if the names of the objects are not provided. "--all" also makes kubectl commands apply to uninitialized objects. Please see the [initializer](https://kubernetes.io/docs/admin/extensible-admission-controllers/) doc for more details.
```
This commit is contained in:
Kubernetes Submit Queue
2017-09-02 23:50:00 -07:00
committed by GitHub
28 changed files with 487 additions and 54 deletions

View File

@@ -52,8 +52,9 @@ import (
)
const (
ApplyAnnotationsFlag = "save-config"
DefaultErrorExitCode = 1
ApplyAnnotationsFlag = "save-config"
DefaultErrorExitCode = 1
IncludeUninitializedFlag = "include-uninitialized"
)
type debugError interface {
@@ -420,6 +421,10 @@ func AddDryRunFlag(cmd *cobra.Command) {
cmd.Flags().Bool("dry-run", false, "If true, only print the object that would be sent, without sending it.")
}
func AddIncludeUninitializedFlag(cmd *cobra.Command) {
cmd.Flags().Bool(IncludeUninitializedFlag, false, `If true, the kubectl command applies to uninitialized objects. If explicitly set to false, this flag overrides other flags that make the kubectl commands apply to uninitialized objects, e.g., "--all". Objects with empty metadata.initializers are regarded as initialized.`)
}
func AddPodRunningTimeoutFlag(cmd *cobra.Command, defaultTimeout time.Duration) {
cmd.Flags().Duration("pod-running-timeout", defaultTimeout, "The length of time (like 5s, 2m, or 3h, higher than zero) to wait until at least one pod is running")
}
@@ -826,3 +831,25 @@ func ManualStrip(file []byte) []byte {
}
return stripped
}
// ShouldIncludeUninitialized identifies whether to include uninitialized objects.
// includeUninitialized is the default value.
// Assume we can parse `all` and `selector` from cmd.
func ShouldIncludeUninitialized(cmd *cobra.Command, includeUninitialized bool) bool {
shouldIncludeUninitialized := includeUninitialized
if cmd.Flags().Lookup("all") != nil && GetFlagBool(cmd, "all") {
// include the uninitialized objects by default
// unless explicitly set --include-uninitialized=false
shouldIncludeUninitialized = true
}
if cmd.Flags().Lookup("selector") != nil && GetFlagString(cmd, "selector") != "" {
// does not include the uninitialized objects by default
// unless explicitly set --include-uninitialized=true
shouldIncludeUninitialized = false
}
if cmd.Flags().Changed(IncludeUninitializedFlag) {
// get explicit value
shouldIncludeUninitialized = GetFlagBool(cmd, IncludeUninitializedFlag)
}
return shouldIncludeUninitialized
}