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

Add support for field selectors #2892

Merged
merged 3 commits into from
Apr 25, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Versioning](http://semver.org/spec/v2.0.0.html).

## [5.6.0] - TBD

### Added
- Added fields getter functions for resources available via the REST API.

### Fixed
- Fixed the agent `--annotations` and `--labels` flags.

Expand Down
11 changes: 11 additions & 0 deletions api/core/v2/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/url"
"path"
"regexp"
"strings"

"github.com/sensu/sensu-go/js"
)
Expand Down Expand Up @@ -101,3 +102,13 @@ func (a *Asset) URIPath() string {
func NewAsset(meta ObjectMeta) *Asset {
return &Asset{ObjectMeta: meta}
}

// AssetFields returns a set of fields that represent that resource
func AssetFields(r Resource) map[string]string {
resource := r.(*Asset)
return map[string]string{
"asset.name": resource.ObjectMeta.Name,
"asset.namespace": resource.ObjectMeta.Namespace,
"asset.filters": strings.Join(resource.Filters, ","),
}
}
16 changes: 16 additions & 0 deletions api/core/v2/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"net/url"
"sort"
"strconv"
"strings"
"time"

jsoniter "github.com/json-iterator/go"
Expand Down Expand Up @@ -452,3 +454,17 @@ func (c *CheckConfig) IsSubdued() bool {
}
return subdued
}

// CheckConfigFields returns a set of fields that represent that resource
func CheckConfigFields(r Resource) map[string]string {
resource := r.(*CheckConfig)
return map[string]string{
"check.name": resource.ObjectMeta.Name,
"check.namespace": resource.ObjectMeta.Namespace,
"check.handlers": strings.Join(resource.Handlers, ","),
"check.publish": strconv.FormatBool(resource.Publish),
"check.round_robin": strconv.FormatBool(resource.RoundRobin),
"check.runtime_assets": strings.Join(resource.RuntimeAssets, ","),
"check.subscriptions": strings.Join(resource.Subscriptions, ","),
}
}
14 changes: 14 additions & 0 deletions api/core/v2/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"net/url"
"sort"
"strconv"
"strings"

utilstrings "github.com/sensu/sensu-go/util/strings"
)
Expand Down Expand Up @@ -169,3 +171,15 @@ func (s *entitySorter) Swap(i, j int) {
func (s *entitySorter) Less(i, j int) bool {
return s.byFn(s.entities[i], s.entities[j])
}

// EntityFields returns a set of fields that represent that resource
func EntityFields(r Resource) map[string]string {
resource := r.(*Entity)
return map[string]string{
"entity.name": resource.ObjectMeta.Name,
"entity.namespace": resource.ObjectMeta.Namespace,
"entity.deregister": strconv.FormatBool(resource.Deregister),
"entity.entity_class": resource.EntityClass,
"entity.subscriptions": strings.Join(resource.Subscriptions, ","),
}
}
22 changes: 21 additions & 1 deletion api/core/v2/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
fmt "fmt"
"net/url"
"sort"
"strconv"
"strings"
"time"

stringsutil "github.com/sensu/sensu-go/util/strings"
Expand Down Expand Up @@ -106,7 +108,7 @@ func (e *Event) IsSilenced() bool {
return len(e.Check.Silenced) > 0
}

// Implements dynamic.SynthesizeExtras
// SynthesizeExtras implements dynamic.SynthesizeExtras
func (e *Event) SynthesizeExtras() map[string]interface{} {
return map[string]interface{}{
"has_check": e.HasCheck(),
Expand Down Expand Up @@ -349,3 +351,21 @@ func (e *Event) IsSilencedBy(entry *Silenced) bool {

return false
}

// EventFields returns a set of fields that represent that resource
func EventFields(r Resource) map[string]string {
resource := r.(*Event)
return map[string]string{
"event.name": resource.ObjectMeta.Name,
"event.namespace": resource.ObjectMeta.Namespace,
"event.check.handlers": strings.Join(resource.Check.Handlers, ","),
"event.check.publish": strconv.FormatBool(resource.Check.Publish),
"event.check.round_robin": strconv.FormatBool(resource.Check.RoundRobin),
"event.check.runtime_assets": strings.Join(resource.Check.RuntimeAssets, ","),
"event.check.status": strconv.Itoa(int(resource.Check.Status)),
"event.check.subscriptions": strings.Join(resource.Check.Subscriptions, ","),
"event.entity.deregister": strconv.FormatBool(resource.Entity.Deregister),
"event.entity.entity_class": resource.Entity.EntityClass,
"event.entity.subscriptions": strings.Join(resource.Entity.Subscriptions, ","),
}
}
10 changes: 10 additions & 0 deletions api/core/v2/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ func FixtureExtension(name string) *Extension {
}
}

// NewExtension intializes an extension with the given object meta
func NewExtension(meta ObjectMeta) *Extension {
return &Extension{ObjectMeta: meta}
}

// ExtensionFields returns a set of fields that represent that resource
func ExtensionFields(r Resource) map[string]string {
resource := r.(*Extension)
return map[string]string{
"extension.name": resource.ObjectMeta.Name,
"extension.namespace": resource.ObjectMeta.Namespace,
}
}
12 changes: 12 additions & 0 deletions api/core/v2/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/url"
"strings"

"github.com/sensu/sensu-go/js"
utilstrings "github.com/sensu/sensu-go/util/strings"
Expand Down Expand Up @@ -103,3 +104,14 @@ func FixtureDenyEventFilter(name string) *EventFilter {
func (f *EventFilter) URIPath() string {
return fmt.Sprintf("/api/core/v2/namespaces/%s/filters/%s", url.PathEscape(f.Namespace), url.PathEscape(f.Name))
}

// EventFilterFields returns a set of fields that represent that resource
func EventFilterFields(r Resource) map[string]string {
resource := r.(*EventFilter)
return map[string]string{
"filter.name": resource.ObjectMeta.Name,
"filter.namespace": resource.ObjectMeta.Namespace,
"filter.action": resource.Action,
"filter.runtime_assets": strings.Join(resource.RuntimeAssets, ","),
}
}
14 changes: 14 additions & 0 deletions api/core/v2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
fmt "fmt"
"net/url"
"strings"
)

const (
Expand Down Expand Up @@ -110,3 +111,16 @@ func FixtureSetHandler(name string, handlers ...string) *Handler {
func (h *Handler) URIPath() string {
return fmt.Sprintf("/api/core/v2/namespaces/%s/handlers/%s", url.PathEscape(h.Namespace), url.PathEscape(h.Name))
}

// HandlerFields returns a set of fields that represent that resource
func HandlerFields(r Resource) map[string]string {
resource := r.(*Handler)
return map[string]string{
"handler.name": resource.ObjectMeta.Name,
"handler.namespace": resource.ObjectMeta.Namespace,
"handler.filters": strings.Join(resource.Filters, ","),
"handler.handlers": strings.Join(resource.Handlers, ","),
"handler.mutator": resource.Mutator,
"handler.type": resource.Type,
}
}
9 changes: 9 additions & 0 deletions api/core/v2/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,12 @@ func FixtureHookList(hookName string) *HookList {
func (h *Hook) URIPath() string {
return fmt.Sprintf("/api/core/v2/namespaces/%s/hooks/%s", url.PathEscape(h.Namespace), url.PathEscape(h.Name))
}

// HookConfigFields returns a set of fields that represent that resource
func HookConfigFields(r Resource) map[string]string {
resource := r.(*HookConfig)
return map[string]string{
"hook.name": resource.ObjectMeta.Name,
"hook.namespace": resource.ObjectMeta.Namespace,
}
}
11 changes: 11 additions & 0 deletions api/core/v2/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
fmt "fmt"
"net/url"
"strings"
)

// NewMutator creates a new Mutator.
Expand Down Expand Up @@ -59,3 +60,13 @@ func FixtureMutator(name string) *Mutator {
func (m *Mutator) URIPath() string {
return fmt.Sprintf("/api/core/v2/namespaces/%s/mutators/%s", url.PathEscape(m.Namespace), url.PathEscape(m.Name))
}

// MutatorFields returns a set of fields that represent that resource
func MutatorFields(r Resource) map[string]string {
resource := r.(*Mutator)
return map[string]string{
"mutator.name": resource.ObjectMeta.Name,
"mutator.namespace": resource.ObjectMeta.Namespace,
"mutator.runtime_assets": strings.Join(resource.RuntimeAssets, ","),
}
}
8 changes: 8 additions & 0 deletions api/core/v2/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ func (n *Namespace) URIPath() string {
func (n *Namespace) GetObjectMeta() ObjectMeta {
return ObjectMeta{}
}

// NamespaceFields returns a set of fields that represent that resource
func NamespaceFields(r Resource) map[string]string {
resource := r.(*Namespace)
return map[string]string{
"namespace.name": resource.Name,
}
}
38 changes: 38 additions & 0 deletions api/core/v2/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,41 @@ func NewRole(meta ObjectMeta) *Role {
func NewRoleBinding(meta ObjectMeta) *RoleBinding {
return &RoleBinding{ObjectMeta: meta}
}

// ClusterRoleFields returns a set of fields that represent that resource
func ClusterRoleFields(r Resource) map[string]string {
resource := r.(*ClusterRole)
return map[string]string{
"clusterrole.name": resource.ObjectMeta.Name,
}
}

// ClusterRoleBindingFields returns a set of fields that represent that resource
func ClusterRoleBindingFields(r Resource) map[string]string {
resource := r.(*ClusterRoleBinding)
return map[string]string{
"clusterrolebinding.name": resource.ObjectMeta.Name,
"clusterrolebinding.role_ref.name": resource.RoleRef.Name,
"clusterrolebinding.role_ref.type": resource.RoleRef.Type,
}
}

// RoleFields returns a set of fields that represent that resource
func RoleFields(r Resource) map[string]string {
resource := r.(*Role)
return map[string]string{
"role.name": resource.ObjectMeta.Name,
"role.namespace": resource.ObjectMeta.Namespace,
}
}

// RoleBindingFields returns a set of fields that represent that resource
func RoleBindingFields(r Resource) map[string]string {
resource := r.(*RoleBinding)
return map[string]string{
"rolebinding.name": resource.ObjectMeta.Name,
"rolebinding.namespace": resource.ObjectMeta.Namespace,
"rolebinding.role_ref.name": resource.RoleRef.Name,
"rolebinding.role_ref.type": resource.RoleRef.Type,
}
}
14 changes: 14 additions & 0 deletions api/core/v2/silenced.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/url"
"sort"
"strconv"
"strings"
)

Expand Down Expand Up @@ -124,3 +125,16 @@ func (s *silenceSorter) Swap(i, j int) {
func (s *silenceSorter) Less(i, j int) bool {
return s.byFn(s.silences[i], s.silences[j])
}

// SilencedFields returns a set of fields that represent that resource
func SilencedFields(r Resource) map[string]string {
resource := r.(*Silenced)
return map[string]string{
"silenced.name": resource.ObjectMeta.Name,
"silenced.namespace": resource.ObjectMeta.Namespace,
"silenced.check": resource.Check,
"silenced.creator": resource.Creator,
"silenced.expire_on_resolve": strconv.FormatBool(resource.ExpireOnResolve),
"silenced.subscription": resource.Subscription,
}
}
3 changes: 2 additions & 1 deletion api/core/v2/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ var (
// PCI compliance as of Jun 30, 2018: anything under TLS 1.1 must be disabled
// we bump this up to TLS 1.2 so we can support the best possible ciphers
tlsMinVersion = uint16(tls.VersionTLS12)
// disable CBC suites (Lucky13 attack) this means TLS 1.1 can't work (no GCM)
// DefaultCipherSuites overrides the default cipher suites in order to disable
// CBC suites (Lucky13 attack) this means TLS 1.1 can't work (no GCM)
// additionally, we should only use perfect forward secrecy ciphers
DefaultCipherSuites = []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
Expand Down
12 changes: 12 additions & 0 deletions api/core/v2/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
fmt "fmt"
"net/url"
"strconv"
"strings"
)

// FixtureUser returns a testing fixture for an Entity object.
Expand Down Expand Up @@ -46,3 +48,13 @@ func (u *User) URIPath() string {
func (u *User) GetObjectMeta() ObjectMeta {
return ObjectMeta{}
}

// UserFields returns a set of fields that represent that resource
func UserFields(r Resource) map[string]string {
resource := r.(*User)
return map[string]string{
"user.username": resource.Username,
"user.disabled": strconv.FormatBool(resource.Disabled),
"user.groups": strings.Join(resource.Groups, ","),
}
}
5 changes: 3 additions & 2 deletions backend/apid/routers/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"

"github.com/gorilla/mux"
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/backend/apid/actions"
"github.com/sensu/sensu-go/backend/store"
"github.com/sensu/sensu-go/types"
Expand All @@ -30,8 +31,8 @@ func (r *AssetsRouter) Mount(parent *mux.Router) {
}

routes.Get(r.find)
routes.List(r.controller.List)
routes.ListAllNamespaces(r.controller.List, "/{resource:assets}")
routes.List(r.controller.List, corev2.AssetFields)
routes.ListAllNamespaces(r.controller.List, "/{resource:assets}", corev2.AssetFields)
routes.Post(r.create)
routes.Put(r.createOrReplace)
}
Expand Down
4 changes: 2 additions & 2 deletions backend/apid/routers/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (r *ChecksRouter) Mount(parent *mux.Router) {

routes.Del(r.destroy)
routes.Get(r.find)
routes.List(r.controller.List)
routes.ListAllNamespaces(r.controller.List, "/{resource:checks}")
routes.List(r.controller.List, corev2.CheckConfigFields)
routes.ListAllNamespaces(r.controller.List, "/{resource:checks}", corev2.CheckConfigFields)
routes.Post(r.create)
routes.Put(r.createOrReplace)

Expand Down
3 changes: 2 additions & 1 deletion backend/apid/routers/clusterrolebindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"

"github.com/gorilla/mux"
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/backend/apid/actions"
"github.com/sensu/sensu-go/backend/store"
"github.com/sensu/sensu-go/types"
Expand All @@ -28,7 +29,7 @@ func (r *ClusterRoleBindingsRouter) Mount(parent *mux.Router) {
Router: parent,
PathPrefix: "/{resource:clusterrolebindings}",
}
routes.List(r.controller.List)
routes.List(r.controller.List, corev2.ClusterRoleBindingFields)
routes.Get(r.find)
routes.Post(r.create)
routes.Del(r.destroy)
Expand Down
3 changes: 2 additions & 1 deletion backend/apid/routers/clusterroles.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"

"github.com/gorilla/mux"
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/backend/apid/actions"
"github.com/sensu/sensu-go/backend/store"
"github.com/sensu/sensu-go/types"
Expand All @@ -28,7 +29,7 @@ func (r *ClusterRolesRouter) Mount(parent *mux.Router) {
Router: parent,
PathPrefix: "/{resource:clusterroles}",
}
routes.List(r.controller.List)
routes.List(r.controller.List, corev2.ClusterRoleFields)
routes.Get(r.find)
routes.Post(r.create)
routes.Del(r.destroy)
Expand Down
Loading