Unverified Commit 76ffe9e8 authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #58787 from Lion-Wei/kubectl-3

Automatic merge from submit-queue (batch tested with PRs 60499, 61715, 61688, 61300, 58787). 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>. fix kubectl apply error message **What this PR does / why we need it**: Fix messy code in kubectl apply error message. **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes kubernetes/kubectl#197 **Special notes for your reviewer**: **Release note**: ```release-note NONE ``` /sig cli
parents bb6f0bf1 e2dedc76
...@@ -284,12 +284,12 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti ...@@ -284,12 +284,12 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti
// in the patch sent to the server. // in the patch sent to the server.
modified, err := kubectl.GetModifiedConfiguration(info, true, encoder) modified, err := kubectl.GetModifiedConfiguration(info, true, encoder)
if err != nil { if err != nil {
return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving modified configuration from:\n%v\nfor:", info), info.Source, err) return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving modified configuration from:\n%s\nfor:", info.String()), info.Source, err)
} }
if err := info.Get(); err != nil { if err := info.Get(); err != nil {
if !errors.IsNotFound(err) { if !errors.IsNotFound(err) {
return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving current configuration of:\n%v\nfrom server for:", info), info.Source, err) return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving current configuration of:\n%s\nfrom server for:", info.String()), info.Source, err)
} }
// Create the resource if it doesn't exist // Create the resource if it doesn't exist
// First, update the annotation used by kubectl apply // First, update the annotation used by kubectl apply
......
...@@ -143,12 +143,12 @@ func (o *SetLastAppliedOptions) Validate(f cmdutil.Factory, cmd *cobra.Command) ...@@ -143,12 +143,12 @@ func (o *SetLastAppliedOptions) Validate(f cmdutil.Factory, cmd *cobra.Command)
if errors.IsNotFound(err) { if errors.IsNotFound(err) {
return err return err
} else { } else {
return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving current configuration of:\n%v\nfrom server for:", info), info.Source, err) return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving current configuration of:\n%s\nfrom server for:", info.String()), info.Source, err)
} }
} }
originalBuf, err := kubectl.GetOriginalConfiguration(info.Mapping, info.Object) originalBuf, err := kubectl.GetOriginalConfiguration(info.Mapping, info.Object)
if err != nil { if err != nil {
return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving current configuration of:\n%v\nfrom server for:", info), info.Source, err) return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving current configuration of:\n%s\nfrom server for:", info.String()), info.Source, err)
} }
if originalBuf == nil && !o.CreateAnnotation { if originalBuf == nil && !o.CreateAnnotation {
return cmdutil.UsageErrorf(cmd, "no last-applied-configuration annotation found on resource: %s, to create the annotation, run the command with --create-annotation", info.Name) return cmdutil.UsageErrorf(cmd, "no last-applied-configuration annotation found on resource: %s, to create the annotation, run the command with --create-annotation", info.Name)
......
...@@ -431,7 +431,7 @@ func RunDiff(f cmdutil.Factory, diff *DiffProgram, options *DiffOptions, from, t ...@@ -431,7 +431,7 @@ func RunDiff(f cmdutil.Factory, diff *DiffProgram, options *DiffOptions, from, t
if err := info.Get(); err != nil { if err := info.Get(); err != nil {
if !errors.IsNotFound(err) { if !errors.IsNotFound(err) {
return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving current configuration of:\n%v\nfrom server for:", info), info.Source, err) return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving current configuration of:\n%s\nfrom server for:", info.String()), info.Source, err)
} }
info.Object = nil info.Object = nil
} }
......
...@@ -154,6 +154,17 @@ func (i *Info) Refresh(obj runtime.Object, ignoreError bool) error { ...@@ -154,6 +154,17 @@ func (i *Info) Refresh(obj runtime.Object, ignoreError bool) error {
return nil return nil
} }
// String returns the general purpose string representation
func (i *Info) String() string {
basicInfo := fmt.Sprintf("Name: %q, Namespace: %q\nObject: %+q", i.Name, i.Namespace, i.Object)
if i.Mapping != nil {
mappingInfo := fmt.Sprintf("Resource: %q, GroupVersionKind: %q", i.Mapping.Resource,
i.Mapping.GroupVersionKind.String())
return fmt.Sprint(mappingInfo, "\n", basicInfo)
}
return basicInfo
}
// Namespaced returns true if the object belongs to a namespace // Namespaced returns true if the object belongs to a namespace
func (i *Info) Namespaced() bool { func (i *Info) Namespaced() bool {
return i.Mapping != nil && i.Mapping.Scope.Name() == meta.RESTScopeNameNamespace return i.Mapping != nil && i.Mapping.Scope.Name() == meta.RESTScopeNameNamespace
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment