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

Improve reconcile output to explain what changes are being made #71564

Merged
merged 1 commit into from
Feb 13, 2019
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
1 change: 1 addition & 0 deletions pkg/kubectl/cmd/auth/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ go_library(
"//staging/src/k8s.io/api/rbac/v1alpha1:go_default_library",
"//staging/src/k8s.io/api/rbac/v1beta1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/meta:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library",
"//staging/src/k8s.io/cli-runtime/pkg/genericclioptions/printers:go_default_library",
Expand Down
62 changes: 58 additions & 4 deletions pkg/kubectl/cmd/auth/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog"

rbacv1 "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -192,7 +193,7 @@ func (o *ReconcileOptions) RunReconcile() error {
if err != nil {
return err
}
o.PrintObject(result.Role.GetObject(), o.Out)
o.printResults(result.Role.GetObject(), nil, nil, result.MissingRules, result.ExtraRules, result.Operation, result.Protected)
liggitt marked this conversation as resolved.
Show resolved Hide resolved

case *rbacv1.ClusterRole:
reconcileOptions := reconciliation.ReconcileRoleOptions{
Expand All @@ -207,7 +208,7 @@ func (o *ReconcileOptions) RunReconcile() error {
if err != nil {
return err
}
o.PrintObject(result.Role.GetObject(), o.Out)
o.printResults(result.Role.GetObject(), nil, nil, result.MissingRules, result.ExtraRules, result.Operation, result.Protected)

case *rbacv1.RoleBinding:
reconcileOptions := reconciliation.ReconcileRoleBindingOptions{
Expand All @@ -223,7 +224,7 @@ func (o *ReconcileOptions) RunReconcile() error {
if err != nil {
return err
}
o.PrintObject(result.RoleBinding.GetObject(), o.Out)
o.printResults(result.RoleBinding.GetObject(), result.MissingSubjects, result.ExtraSubjects, nil, nil, result.Operation, result.Protected)

case *rbacv1.ClusterRoleBinding:
reconcileOptions := reconciliation.ReconcileRoleBindingOptions{
Expand All @@ -238,7 +239,7 @@ func (o *ReconcileOptions) RunReconcile() error {
if err != nil {
return err
}
o.PrintObject(result.RoleBinding.GetObject(), o.Out)
o.printResults(result.RoleBinding.GetObject(), result.MissingSubjects, result.ExtraSubjects, nil, nil, result.Operation, result.Protected)

case *rbacv1beta1.Role,
*rbacv1beta1.RoleBinding,
Expand All @@ -258,3 +259,56 @@ func (o *ReconcileOptions) RunReconcile() error {
return nil
})
}

func (o *ReconcileOptions) printResults(object runtime.Object,
missingSubjects, extraSubjects []rbacv1.Subject,
missingRules, extraRules []rbacv1.PolicyRule,
operation reconciliation.ReconcileOperation,
protected bool) {

o.PrintObject(object, o.Out)

caveat := ""
if protected {
caveat = ", but object opted out (rbac.authorization.kubernetes.io/autoupdate: false)"
}
switch operation {
case reconciliation.ReconcileNone:
return
case reconciliation.ReconcileCreate:
fmt.Fprintf(o.ErrOut, "\treconciliation required create%s\n", caveat)
case reconciliation.ReconcileUpdate:
fmt.Fprintf(o.ErrOut, "\treconciliation required update%s\n", caveat)
case reconciliation.ReconcileRecreate:
fmt.Fprintf(o.ErrOut, "\treconciliation required recreate%s\n", caveat)
}

if len(missingSubjects) > 0 {
fmt.Fprintf(o.ErrOut, "\tmissing subjects added:\n")
for _, s := range missingSubjects {
fmt.Fprintf(o.ErrOut, "\t\t%+v\n", s)
}
}
if o.RemoveExtraSubjects {
if len(extraSubjects) > 0 {
fmt.Fprintf(o.ErrOut, "\textra subjects removed:\n")
for _, s := range extraSubjects {
fmt.Fprintf(o.ErrOut, "\t\t%+v\n", s)
}
}
}
liggitt marked this conversation as resolved.
Show resolved Hide resolved
if len(missingRules) > 0 {
fmt.Fprintf(o.ErrOut, "\tmissing rules added:\n")
for _, r := range missingRules {
fmt.Fprintf(o.ErrOut, "\t\t%+v\n", r)
}
}
if o.RemoveExtraPermissions {
if len(extraRules) > 0 {
fmt.Fprintf(o.ErrOut, "\textra rules removed:\n")
for _, r := range extraRules {
fmt.Fprintf(o.ErrOut, "\t\t%+v\n", r)
}
}
}
liggitt marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 4 additions & 1 deletion pkg/registry/rbac/reconciliation/reconcile_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ func computeReconciledRole(existing, expected RuleOwner, removeExtraPermissions
}

// Compute extra and missing rules
_, result.ExtraRules = validation.Covers(expected.GetRules(), existing.GetRules())
// Don't compute extra permissions if expected and existing roles are both aggregated
if expected.GetAggregationRule() == nil || existing.GetAggregationRule() == nil {
liggitt marked this conversation as resolved.
Show resolved Hide resolved
_, result.ExtraRules = validation.Covers(expected.GetRules(), existing.GetRules())
}
_, result.MissingRules = validation.Covers(existing.GetRules(), expected.GetRules())

switch {
Expand Down