Merge pull request #108777 from pjo256/recursive-rollout-status

feat(kubectl rollout): support multiple resources for rollout status

Kubernetes-commit: 47bb8c6d0cb088016e62b6de172ff0594c596d23
This commit is contained in:
Kubernetes Publisher 2022-05-13 13:15:55 -07:00
commit 0dde236657
3 changed files with 67 additions and 73 deletions

18
go.mod
View File

@ -29,11 +29,11 @@ require (
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20220209214540-3681064d5158
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.0.0-20220510154143-ae35a85329f1
k8s.io/apimachinery v0.0.0-20220511034417-1c398d5dd6e1
k8s.io/api v0.0.0-20220512153301-be84346886a4
k8s.io/apimachinery v0.0.0-20220511125320-f3b1305f4010
k8s.io/cli-runtime v0.0.0-20220509192006-eb61732d7560
k8s.io/client-go v0.0.0-20220511040600-b4b8e5e1e565
k8s.io/component-base v0.0.0-20220510143133-ee36a6a1367e
k8s.io/client-go v0.0.0-20220512233552-ef63e495baf6
k8s.io/component-base v0.0.0-20220512234209-c37f6817941c
k8s.io/component-helpers v0.0.0-20220509184522-76ad8716b7b9
k8s.io/klog/v2 v2.60.1
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42
@ -98,12 +98,12 @@ require (
)
replace (
k8s.io/api => k8s.io/api v0.0.0-20220510154143-ae35a85329f1
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20220511034417-1c398d5dd6e1
k8s.io/api => k8s.io/api v0.0.0-20220512153301-be84346886a4
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20220511125320-f3b1305f4010
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20220509192006-eb61732d7560
k8s.io/client-go => k8s.io/client-go v0.0.0-20220511040600-b4b8e5e1e565
k8s.io/code-generator => k8s.io/code-generator v0.0.0-20220509181537-9f887a7281d8
k8s.io/component-base => k8s.io/component-base v0.0.0-20220510143133-ee36a6a1367e
k8s.io/client-go => k8s.io/client-go v0.0.0-20220512233552-ef63e495baf6
k8s.io/code-generator => k8s.io/code-generator v0.0.0-20220512215755-7d977b3e5454
k8s.io/component-base => k8s.io/component-base v0.0.0-20220512234209-c37f6817941c
k8s.io/component-helpers => k8s.io/component-helpers v0.0.0-20220509184522-76ad8716b7b9
k8s.io/metrics => k8s.io/metrics v0.0.0-20220509191710-5c2ab688925a
)

16
go.sum
View File

@ -759,16 +759,16 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/api v0.0.0-20220510154143-ae35a85329f1 h1:yZN3QEsAnbRVMdS2+kSdD4NY7M23TUKydf8rAkD+FLA=
k8s.io/api v0.0.0-20220510154143-ae35a85329f1/go.mod h1:sZiCpVOT/SE/nrwAAwOLnekwXoB+cNDNfNMmYNckG3k=
k8s.io/apimachinery v0.0.0-20220511034417-1c398d5dd6e1 h1:6AtrxWSMj76OITuOyKalJ3IuOgdhTVedlPkkXW4QDA4=
k8s.io/apimachinery v0.0.0-20220511034417-1c398d5dd6e1/go.mod h1:1oBVxgNUfLl978lJAlywA+H45m2ctSuqJU2stpbcjT4=
k8s.io/api v0.0.0-20220512153301-be84346886a4 h1:ATL01GmGTBjwQjXxfD0UGj3une2C8WyzTOXXzNFI790=
k8s.io/api v0.0.0-20220512153301-be84346886a4/go.mod h1:swy7cXqCDjYcBv6ylU8ErrJ2ALa+9PSVEdM4zw65jFQ=
k8s.io/apimachinery v0.0.0-20220511125320-f3b1305f4010 h1:UDqeN/6SuN6isZIYNS8S4p1Dhd4uxH0eR18ntskjI4s=
k8s.io/apimachinery v0.0.0-20220511125320-f3b1305f4010/go.mod h1:1oBVxgNUfLl978lJAlywA+H45m2ctSuqJU2stpbcjT4=
k8s.io/cli-runtime v0.0.0-20220509192006-eb61732d7560 h1:j8BTlnEUZ+yqwbtEayJS9qWGEhbiZQMuU46/JBunrxg=
k8s.io/cli-runtime v0.0.0-20220509192006-eb61732d7560/go.mod h1:CkzQi01IKV+8rXQDV0jpfK1j+u88qd3tHVhzoPV8meg=
k8s.io/client-go v0.0.0-20220511040600-b4b8e5e1e565 h1:rko5TUeMOqcPvrf2+ehzdiLf9OO/Oz9EGjhJMzOaS98=
k8s.io/client-go v0.0.0-20220511040600-b4b8e5e1e565/go.mod h1:KJevFxWYxWsKVLspsuElC2dKpk+ZnBc31WQaOe+CdAk=
k8s.io/component-base v0.0.0-20220510143133-ee36a6a1367e h1:vkKEhffaC9iR/Ql71tIJz7IqPlSG2nLxDA2IdFgjtZc=
k8s.io/component-base v0.0.0-20220510143133-ee36a6a1367e/go.mod h1:rgm2aW3vatuxl3Drh0QYdQbmlSaOsj9RdFa+GeDIZyg=
k8s.io/client-go v0.0.0-20220512233552-ef63e495baf6 h1:zC9KDs1Sicu9nPmPA4PBQhDx0LMZM3gJ/9Fn+JQwEo0=
k8s.io/client-go v0.0.0-20220512233552-ef63e495baf6/go.mod h1:N3TQYUhBak5FD9FvLLjpRRpdT2vjtNTvsrCwNZP/Qzg=
k8s.io/component-base v0.0.0-20220512234209-c37f6817941c h1:mXjI7vadkvEb5V3Jpsfx2/DA+bmFF7khwhVrp8F8fHI=
k8s.io/component-base v0.0.0-20220512234209-c37f6817941c/go.mod h1:NjTZfHd0dH/TjoaGOrLmvEbCzyIvRL8u2qtbyd7zItk=
k8s.io/component-helpers v0.0.0-20220509184522-76ad8716b7b9 h1:Q9MviBOPQrPjw1uC5Yh7Al4tSGB4EgC+8zT7jR4CQDI=
k8s.io/component-helpers v0.0.0-20220509184522-76ad8716b7b9/go.mod h1:asOxRy2Bj6VxYy901OzS59rnMbBANJnYIa8cyZ1ybbk=
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=

View File

@ -168,73 +168,67 @@ func (o *RolloutStatusOptions) Run() error {
LabelSelectorParam(o.LabelSelector).
FilenameParam(o.EnforceNamespace, o.FilenameOptions).
ResourceTypeOrNameArgs(true, o.BuilderArgs...).
SingleResourceType().
ContinueOnError().
Latest().
Do()
err := r.Err()
if err != nil {
return err
}
infos, err := r.Infos()
if err != nil {
return err
}
if len(infos) != 1 {
return fmt.Errorf("rollout status is only supported on individual resources and resource collections - %d resources were found", len(infos))
}
info := infos[0]
mapping := info.ResourceMapping()
return r.Visit(func(info *resource.Info, err error) error {
mapping := info.ResourceMapping()
statusViewer, err := o.StatusViewerFn(mapping)
if err != nil {
return err
}
statusViewer, err := o.StatusViewerFn(mapping)
if err != nil {
return err
}
fieldSelector := fields.OneTermEqualSelector("metadata.name", info.Name).String()
lw := &cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
options.FieldSelector = fieldSelector
return o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).List(context.TODO(), options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
options.FieldSelector = fieldSelector
return o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).Watch(context.TODO(), options)
},
}
fieldSelector := fields.OneTermEqualSelector("metadata.name", info.Name).String()
lw := &cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
options.FieldSelector = fieldSelector
return o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).List(context.TODO(), options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
options.FieldSelector = fieldSelector
return o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).Watch(context.TODO(), options)
},
}
// if the rollout isn't done yet, keep watching deployment status
ctx, cancel := watchtools.ContextWithOptionalTimeout(context.Background(), o.Timeout)
intr := interrupt.New(nil, cancel)
return intr.Run(func() error {
_, err = watchtools.UntilWithSync(ctx, lw, &unstructured.Unstructured{}, nil, func(e watch.Event) (bool, error) {
switch t := e.Type; t {
case watch.Added, watch.Modified:
status, done, err := statusViewer.Status(e.Object.(runtime.Unstructured), o.Revision)
if err != nil {
return false, err
}
fmt.Fprintf(o.Out, "%s", status)
// Quit waiting if the rollout is done
if done {
return true, nil
}
// if the rollout isn't done yet, keep watching deployment status
ctx, cancel := watchtools.ContextWithOptionalTimeout(context.Background(), o.Timeout)
intr := interrupt.New(nil, cancel)
return intr.Run(func() error {
_, err = watchtools.UntilWithSync(ctx, lw, &unstructured.Unstructured{}, nil, func(e watch.Event) (bool, error) {
switch t := e.Type; t {
case watch.Added, watch.Modified:
status, done, err := statusViewer.Status(e.Object.(runtime.Unstructured), o.Revision)
if err != nil {
return false, err
shouldWatch := o.Watch
if !shouldWatch {
return true, nil
}
return false, nil
case watch.Deleted:
// We need to abort to avoid cases of recreation and not to silently watch the wrong (new) object
return true, fmt.Errorf("object has been deleted")
default:
return true, fmt.Errorf("internal error: unexpected event %#v", e)
}
fmt.Fprintf(o.Out, "%s", status)
// Quit waiting if the rollout is done
if done {
return true, nil
}
shouldWatch := o.Watch
if !shouldWatch {
return true, nil
}
return false, nil
case watch.Deleted:
// We need to abort to avoid cases of recreation and not to silently watch the wrong (new) object
return true, fmt.Errorf("object has been deleted")
default:
return true, fmt.Errorf("internal error: unexpected event %#v", e)
}
})
return err
})
return err
})
}