Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix --local panic in set commands #65636

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/kubectl/cmd/set/set_env.go
Expand Up @@ -474,7 +474,8 @@ func (o *EnvOptions) RunEnv() error {
for _, patch := range patches {
info := patch.Info
if patch.Err != nil {
allErrs = append(allErrs, fmt.Errorf("error: %s/%s %v", info.Mapping.Resource, info.Name, patch.Err))
name := info.ObjectName()
allErrs = append(allErrs, fmt.Errorf("error: %s %v\n", name, patch.Err))
continue
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/kubectl/cmd/set/set_image.go
Expand Up @@ -247,7 +247,8 @@ func (o *SetImageOptions) Run() error {
for _, patch := range patches {
info := patch.Info
if patch.Err != nil {
allErrs = append(allErrs, fmt.Errorf("error: %s/%s %v", info.Mapping.Resource, info.Name, patch.Err))
name := info.ObjectName()
allErrs = append(allErrs, fmt.Errorf("error: %s %v\n", name, patch.Err))
continue
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/kubectl/cmd/set/set_resources.go
Expand Up @@ -260,14 +260,15 @@ func (o *SetResourcesOptions) Run() error {

for _, patch := range patches {
info := patch.Info
name := info.ObjectName()
if patch.Err != nil {
allErrs = append(allErrs, fmt.Errorf("error: %s/%s %v", info.Mapping.Resource, info.Name, patch.Err))
allErrs = append(allErrs, fmt.Errorf("error: %s %v\n", name, patch.Err))
continue
}

//no changes
if string(patch.Patch) == "{}" || len(patch.Patch) == 0 {
allErrs = append(allErrs, fmt.Errorf("info: %s %q was not changed", info.Mapping.Resource, info.Name))
allErrs = append(allErrs, fmt.Errorf("info: %s was not changed\n", name))
continue
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/kubectl/cmd/set/set_serviceaccount.go
Expand Up @@ -191,8 +191,9 @@ func (o *SetServiceAccountOptions) Run() error {
patches := CalculatePatches(o.infos, scheme.DefaultJSONEncoder(), patchFn)
for _, patch := range patches {
info := patch.Info
name := info.ObjectName()
if patch.Err != nil {
patchErrs = append(patchErrs, fmt.Errorf("error: %s/%s %v", info.Mapping.Resource, info.Name, patch.Err))
patchErrs = append(patchErrs, fmt.Errorf("error: %s %v\n", name, patch.Err))
continue
}
if o.local || o.dryRun {
Expand Down
5 changes: 3 additions & 2 deletions pkg/kubectl/cmd/set/set_subject.go
Expand Up @@ -240,14 +240,15 @@ func (o *SubjectOptions) Run(fn updateSubjects) error {
allErrs := []error{}
for _, patch := range patches {
info := patch.Info
name := info.ObjectName()
if patch.Err != nil {
allErrs = append(allErrs, fmt.Errorf("error: %s/%s %v", info.Mapping.Resource, info.Name, patch.Err))
allErrs = append(allErrs, fmt.Errorf("error: %s %v\n", name, patch.Err))
continue
}

//no changes
if string(patch.Patch) == "{}" || len(patch.Patch) == 0 {
allErrs = append(allErrs, fmt.Errorf("info: %s %q was not changed", info.Mapping.Resource, info.Name))
allErrs = append(allErrs, fmt.Errorf("info: %s was not changed\n", name))
continue
}

Expand Down
Expand Up @@ -24,6 +24,7 @@ import (
"net/url"
"os"
"path/filepath"
"strings"
"time"

"golang.org/x/text/encoding/unicode"
Expand Down Expand Up @@ -141,6 +142,18 @@ func (i *Info) Refresh(obj runtime.Object, ignoreError bool) error {
return nil
}

// ObjectName returns an approximate form of the resource's kind/name.
func (i *Info) ObjectName() string {
if i.Mapping != nil {
return fmt.Sprintf("%s/%s", i.Mapping.Resource.Resource, i.Name)
}
gvk := i.Object.GetObjectKind().GroupVersionKind()
if len(gvk.Group) == 0 {
return fmt.Sprintf("%s/%s", strings.ToLower(gvk.Kind), i.Name)
}
return fmt.Sprintf("%s.%s/%s\n", strings.ToLower(gvk.Kind), gvk.Group, i.Name)
}

// 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)
Expand Down