warn user if they try to apply on an object without the annotation

This commit is contained in:
ymqytw
2016-11-11 14:38:36 -08:00
parent 8820922997
commit b6cfa9aa98
3 changed files with 81 additions and 9 deletions

View File

@@ -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{