mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 23:47:50 +00:00
warn user if they try to apply on an object without the annotation
This commit is contained in:
@@ -61,6 +61,8 @@ const (
|
||||
backOffPeriod = 1 * time.Second
|
||||
// how many times we can retry before back off
|
||||
triesBeforeBackOff = 1
|
||||
|
||||
warningNoLastAppliedConfigAnnotation = "Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply\n"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -88,7 +90,7 @@ var (
|
||||
kubectl apply --prune -f manifest.yaml --all --prune-whitelist=core/v1/ConfigMap`)
|
||||
)
|
||||
|
||||
func NewCmdApply(f cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
func NewCmdApply(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command {
|
||||
var options ApplyOptions
|
||||
|
||||
cmd := &cobra.Command{
|
||||
@@ -100,7 +102,7 @@ func NewCmdApply(f cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
cmdutil.CheckErr(validateArgs(cmd, args))
|
||||
cmdutil.CheckErr(cmdutil.ValidateOutputArgs(cmd))
|
||||
cmdutil.CheckErr(validatePruneAll(options.Prune, cmdutil.GetFlagBool(cmd, "all"), options.Selector))
|
||||
cmdutil.CheckErr(RunApply(f, cmd, out, &options))
|
||||
cmdutil.CheckErr(RunApply(f, cmd, out, errOut, &options))
|
||||
},
|
||||
}
|
||||
|
||||
@@ -161,7 +163,7 @@ func parsePruneResources(gvks []string) ([]pruneResource, error) {
|
||||
return pruneResources, nil
|
||||
}
|
||||
|
||||
func RunApply(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *ApplyOptions) error {
|
||||
func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, options *ApplyOptions) error {
|
||||
shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
|
||||
schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"), cmdutil.GetFlagString(cmd, "schema-cache-dir"))
|
||||
if err != nil {
|
||||
@@ -256,6 +258,13 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *App
|
||||
}
|
||||
|
||||
if !dryRun {
|
||||
annotationMap, err := info.Mapping.MetadataAccessor.Annotations(info.Object)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, ok := annotationMap[annotations.LastAppliedConfigAnnotation]; !ok {
|
||||
fmt.Fprintf(errOut, warningNoLastAppliedConfigAnnotation)
|
||||
}
|
||||
overwrite := cmdutil.GetFlagBool(cmd, "overwrite")
|
||||
helper := resource.NewHelper(info.Client, info.Mapping)
|
||||
patcher := &patcher{
|
||||
|
||||
Reference in New Issue
Block a user