Skip to content

Commit

Permalink
Changes in response to code-review.
Browse files Browse the repository at this point in the history
- Make k8sapi.CanI a variadic function (and use where applicable).
- Sort AgentEnv.Excluded before checking slices for equality.

Signed-off-by: Thomas Hallgren <[email protected]>
  • Loading branch information
thallgren committed Jan 2, 2025
1 parent 04e44ca commit 3889a5a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
19 changes: 8 additions & 11 deletions cmd/traffic/cmd/manager/cluster/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,14 @@ func clusterDomainFromResolvConf(confFile, namespace string) (string, error) {
}

func (oi *info) watchNodeSubnets(ctx context.Context, mustSucceed bool) bool {
ok, err := k8sapi.CanI(ctx, &auth.ResourceAttributes{
Verb: "list",
Resource: "nodes",
})
if err != nil || !ok {
return false
}
ok, err = k8sapi.CanI(ctx, &auth.ResourceAttributes{
Verb: "watch",
Resource: "nodes",
})
ok, err := k8sapi.CanI(ctx,
&auth.ResourceAttributes{
Verb: "list",
Resource: "nodes",
}, &auth.ResourceAttributes{
Verb: "watch",
Resource: "nodes",
})
if err != nil || !ok {
return false
}
Expand Down
10 changes: 7 additions & 3 deletions cmd/traffic/cmd/manager/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"slices"
"sort"
"sync"

core "k8s.io/api/core/v1"
Expand Down Expand Up @@ -164,9 +165,12 @@ func (c *config) refreshFile(ctx context.Context, mapData map[string]string) {
}
if err != nil {
dlog.Errorf(ctx, "failed to unmarshal YAML from %s: %v", agentEnvConfigFileName, err)
} else if !ae.Equal(c.agentEnv) {
c.agentEnv = ae
dlog.Debugf(ctx, "Refreshed agent-env:\n%s", yml)
} else {
sort.Strings(ae.Excluded)
if !ae.Equal(c.agentEnv) {
c.agentEnv = ae
dlog.Debugf(ctx, "Refreshed agent-env:\n%s", yml)
}
}
} else if !c.agentEnv.Equal(ae) {
c.agentEnv = ae
Expand Down
37 changes: 20 additions & 17 deletions pkg/k8sapi/cani.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,29 @@ import (
"github.com/datawire/dlib/dlog"
)

func CanI(ctx context.Context, ra *auth.ResourceAttributes) (bool, error) {
func CanI(ctx context.Context, ras ...*auth.ResourceAttributes) (bool, error) {
authHandler := GetK8sInterface(ctx).AuthorizationV1().SelfSubjectAccessReviews()
review := auth.SelfSubjectAccessReview{Spec: auth.SelfSubjectAccessReviewSpec{ResourceAttributes: ra}}
ar, err := authHandler.Create(ctx, &review, meta.CreateOptions{})
if err == nil && ar.Status.Allowed {
return true, nil
}
where := ""
if ra.Namespace != "" {
where = " in namespace " + ra.Namespace
}
if err != nil {
err = fmt.Errorf(`unable to do "can-i %s %s%s": %v`, ra.Verb, ra.Resource, where, err)
if ctx.Err() == nil {
dlog.Error(ctx, err)
for _, ra := range ras {
review := auth.SelfSubjectAccessReview{Spec: auth.SelfSubjectAccessReviewSpec{ResourceAttributes: ra}}
ar, err := authHandler.Create(ctx, &review, meta.CreateOptions{})
if err == nil && ar.Status.Allowed {
continue
}
where := ""
if ra.Namespace != "" {
where = " in namespace " + ra.Namespace
}
if err != nil {
err = fmt.Errorf(`unable to do "can-i %s %s%s": %v`, ra.Verb, ra.Resource, where, err)
if ctx.Err() == nil {
dlog.Error(ctx, err)
}
} else {
dlog.Infof(ctx, `"can-i %s %s%s" is not allowed`, ra.Verb, ra.Resource, where)
}
} else {
dlog.Infof(ctx, `"can-i %s %s%s" is not allowed`, ra.Verb, ra.Resource, where)
return false, err
}
return false, err
return true, nil
}

func CanWatch(ctx context.Context, group, resource, name, ns string) bool {
Expand Down

0 comments on commit 3889a5a

Please sign in to comment.