Merge pull request #138 from mrunalp/labels
Add support for label filtering
This commit is contained in:
commit
24bb1523cf
9 changed files with 521 additions and 11 deletions
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
|
@ -171,6 +172,19 @@ var containerStatusCommand = cli.Command{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type listOptions struct {
|
||||||
|
// id of the container
|
||||||
|
id string
|
||||||
|
// podID of the container
|
||||||
|
podID string
|
||||||
|
// state of the container
|
||||||
|
state string
|
||||||
|
// quiet is for listing just container IDs
|
||||||
|
quiet bool
|
||||||
|
// labels are selectors for the container
|
||||||
|
labels map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
var listContainersCommand = cli.Command{
|
var listContainersCommand = cli.Command{
|
||||||
Name: "list",
|
Name: "list",
|
||||||
Usage: "list containers",
|
Usage: "list containers",
|
||||||
|
@ -194,6 +208,10 @@ var listContainersCommand = cli.Command{
|
||||||
Value: "",
|
Value: "",
|
||||||
Usage: "filter by container state",
|
Usage: "filter by container state",
|
||||||
},
|
},
|
||||||
|
cli.StringSliceFlag{
|
||||||
|
Name: "label",
|
||||||
|
Usage: "filter by key=value label",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Action: func(context *cli.Context) error {
|
Action: func(context *cli.Context) error {
|
||||||
// Set up a connection to the server.
|
// Set up a connection to the server.
|
||||||
|
@ -203,8 +221,23 @@ var listContainersCommand = cli.Command{
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
client := pb.NewRuntimeServiceClient(conn)
|
client := pb.NewRuntimeServiceClient(conn)
|
||||||
|
opts := listOptions{
|
||||||
|
id: context.String("id"),
|
||||||
|
podID: context.String("pod"),
|
||||||
|
state: context.String("state"),
|
||||||
|
quiet: context.Bool("quiet"),
|
||||||
|
labels: make(map[string]string),
|
||||||
|
}
|
||||||
|
|
||||||
err = ListContainers(client, context.Bool("quiet"), context.String("id"), context.String("pod"), context.String("state"))
|
for _, l := range context.StringSlice("label") {
|
||||||
|
pair := strings.Split(l, "=")
|
||||||
|
if len(pair) != 2 {
|
||||||
|
return fmt.Errorf("incorrectly specified label: %v", l)
|
||||||
|
}
|
||||||
|
opts.labels[pair[0]] = pair[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ListContainers(client, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("listing containers failed: %v", err)
|
return fmt.Errorf("listing containers failed: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -320,17 +353,17 @@ func ContainerStatus(client pb.RuntimeServiceClient, ID string) error {
|
||||||
|
|
||||||
// ListContainers sends a ListContainerRequest to the server, and parses
|
// ListContainers sends a ListContainerRequest to the server, and parses
|
||||||
// the returned ListContainerResponse.
|
// the returned ListContainerResponse.
|
||||||
func ListContainers(client pb.RuntimeServiceClient, quiet bool, id string, podID string, state string) error {
|
func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
|
||||||
filter := &pb.ContainerFilter{}
|
filter := &pb.ContainerFilter{}
|
||||||
if id != "" {
|
if opts.id != "" {
|
||||||
filter.Id = &id
|
filter.Id = &opts.id
|
||||||
}
|
}
|
||||||
if podID != "" {
|
if opts.podID != "" {
|
||||||
filter.PodSandboxId = &podID
|
filter.PodSandboxId = &opts.podID
|
||||||
}
|
}
|
||||||
if state != "" {
|
if opts.state != "" {
|
||||||
st := pb.ContainerState_UNKNOWN
|
st := pb.ContainerState_UNKNOWN
|
||||||
switch state {
|
switch opts.state {
|
||||||
case "created":
|
case "created":
|
||||||
st = pb.ContainerState_CREATED
|
st = pb.ContainerState_CREATED
|
||||||
filter.State = &st
|
filter.State = &st
|
||||||
|
@ -344,6 +377,9 @@ func ListContainers(client pb.RuntimeServiceClient, quiet bool, id string, podID
|
||||||
log.Fatalf("--state should be one of created, running or stopped")
|
log.Fatalf("--state should be one of created, running or stopped")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if opts.labels != nil {
|
||||||
|
filter.LabelSelector = opts.labels
|
||||||
|
}
|
||||||
r, err := client.ListContainers(context.Background(), &pb.ListContainersRequest{
|
r, err := client.ListContainers(context.Background(), &pb.ListContainersRequest{
|
||||||
Filter: filter,
|
Filter: filter,
|
||||||
})
|
})
|
||||||
|
@ -351,7 +387,7 @@ func ListContainers(client pb.RuntimeServiceClient, quiet bool, id string, podID
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, c := range r.GetContainers() {
|
for _, c := range r.GetContainers() {
|
||||||
if quiet {
|
if opts.quiet {
|
||||||
fmt.Println(*c.Id)
|
fmt.Println(*c.Id)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -364,6 +400,18 @@ func ListContainers(client pb.RuntimeServiceClient, quiet bool, id string, podID
|
||||||
ctm := time.Unix(*c.CreatedAt, 0)
|
ctm := time.Unix(*c.CreatedAt, 0)
|
||||||
fmt.Printf("Created: %v\n", ctm)
|
fmt.Printf("Created: %v\n", ctm)
|
||||||
}
|
}
|
||||||
|
if c.Labels != nil {
|
||||||
|
fmt.Println("Labels:")
|
||||||
|
for k, v := range c.Labels {
|
||||||
|
fmt.Printf("\t%s -> %s\n", k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if c.Annotations != nil {
|
||||||
|
fmt.Println("Annotations:")
|
||||||
|
for k, v := range c.Annotations {
|
||||||
|
fmt.Printf("\t%s -> %s\n", k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"github.com/kubernetes-incubator/cri-o/utils"
|
"github.com/kubernetes-incubator/cri-o/utils"
|
||||||
"github.com/opencontainers/runtime-spec/specs-go"
|
"github.com/opencontainers/runtime-spec/specs-go"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
"k8s.io/kubernetes/pkg/fields"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -221,7 +222,7 @@ type Container struct {
|
||||||
name string
|
name string
|
||||||
bundlePath string
|
bundlePath string
|
||||||
logPath string
|
logPath string
|
||||||
labels map[string]string
|
labels fields.Set
|
||||||
sandbox string
|
sandbox string
|
||||||
terminal bool
|
terminal bool
|
||||||
state *ContainerState
|
state *ContainerState
|
||||||
|
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"github.com/opencontainers/runc/libcontainer/label"
|
"github.com/opencontainers/runc/libcontainer/label"
|
||||||
"github.com/opencontainers/runtime-tools/generate"
|
"github.com/opencontainers/runtime-tools/generate"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
"k8s.io/kubernetes/pkg/fields"
|
||||||
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -419,7 +420,12 @@ func filterContainer(c *pb.Container, filter *pb.ContainerFilter) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO(mrunalp): Add support for label filtering
|
if filter.LabelSelector != nil {
|
||||||
|
sel := fields.SelectorFromSet(filter.LabelSelector)
|
||||||
|
if !sel.Matches(fields.Set(c.Labels)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -473,6 +479,7 @@ func (s *Server) ListContainers(ctx context.Context, req *pb.ListContainersReque
|
||||||
Id: &cID,
|
Id: &cID,
|
||||||
PodSandboxId: &podSandboxID,
|
PodSandboxId: &podSandboxID,
|
||||||
CreatedAt: int64Ptr(created),
|
CreatedAt: int64Ptr(created),
|
||||||
|
Labels: ctr.Labels(),
|
||||||
}
|
}
|
||||||
|
|
||||||
switch cState.Status {
|
switch cState.Status {
|
||||||
|
|
|
@ -239,3 +239,35 @@ function teardown() {
|
||||||
cleanup_pods
|
cleanup_pods
|
||||||
stop_ocid
|
stop_ocid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "ctr list label filtering" {
|
||||||
|
# this test requires docker, thus it can't yet be run in a container
|
||||||
|
if [ "$TRAVIS" = "true" ]; then # instead of $TRAVIS, add a function is_containerized to skip here
|
||||||
|
skip "cannot yet run this test in a container, use sudo make localintegration"
|
||||||
|
fi
|
||||||
|
|
||||||
|
start_ocid
|
||||||
|
run ocic pod create --config "$TESTDATA"/sandbox_config.json
|
||||||
|
echo "$output"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
pod_id="$output"
|
||||||
|
run ocic ctr create --config "$TESTDATA"/container_redis.json --pod "$pod_id"
|
||||||
|
echo "$output"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
ctr1_id="$output"
|
||||||
|
run ocic ctr list --label "tier=backend" --quiet
|
||||||
|
echo "$output"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[[ "$output" != "" ]]
|
||||||
|
[[ "$output" =~ "$ctr1_id" ]]
|
||||||
|
run ocic ctr list --label "tier=frontend" --quiet
|
||||||
|
echo "$output"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[[ "$output" == "" ]]
|
||||||
|
run ocic pod remove --id "$pod_id"
|
||||||
|
echo "$output"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
cleanup_ctrs
|
||||||
|
cleanup_pods
|
||||||
|
stop_ocid
|
||||||
|
}
|
||||||
|
|
19
vendor/src/k8s.io/kubernetes/pkg/fields/doc.go
vendored
Normal file
19
vendor/src/k8s.io/kubernetes/pkg/fields/doc.go
vendored
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
/*
|
||||||
|
Copyright 2015 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Package fields implements a simple field system, parsing and matching
|
||||||
|
// selectors with sets of fields.
|
||||||
|
package fields // import "k8s.io/kubernetes/pkg/fields"
|
62
vendor/src/k8s.io/kubernetes/pkg/fields/fields.go
vendored
Normal file
62
vendor/src/k8s.io/kubernetes/pkg/fields/fields.go
vendored
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
/*
|
||||||
|
Copyright 2015 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package fields
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Fields allows you to present fields independently from their storage.
|
||||||
|
type Fields interface {
|
||||||
|
// Has returns whether the provided field exists.
|
||||||
|
Has(field string) (exists bool)
|
||||||
|
|
||||||
|
// Get returns the value for the provided field.
|
||||||
|
Get(field string) (value string)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set is a map of field:value. It implements Fields.
|
||||||
|
type Set map[string]string
|
||||||
|
|
||||||
|
// String returns all fields listed as a human readable string.
|
||||||
|
// Conveniently, exactly the format that ParseSelector takes.
|
||||||
|
func (ls Set) String() string {
|
||||||
|
selector := make([]string, 0, len(ls))
|
||||||
|
for key, value := range ls {
|
||||||
|
selector = append(selector, key+"="+value)
|
||||||
|
}
|
||||||
|
// Sort for determinism.
|
||||||
|
sort.StringSlice(selector).Sort()
|
||||||
|
return strings.Join(selector, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Has returns whether the provided field exists in the map.
|
||||||
|
func (ls Set) Has(field string) bool {
|
||||||
|
_, exists := ls[field]
|
||||||
|
return exists
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns the value in the map for the provided field.
|
||||||
|
func (ls Set) Get(field string) string {
|
||||||
|
return ls[field]
|
||||||
|
}
|
||||||
|
|
||||||
|
// AsSelector converts fields into a selectors.
|
||||||
|
func (ls Set) AsSelector() Selector {
|
||||||
|
return SelectorFromSet(ls)
|
||||||
|
}
|
30
vendor/src/k8s.io/kubernetes/pkg/fields/requirements.go
vendored
Normal file
30
vendor/src/k8s.io/kubernetes/pkg/fields/requirements.go
vendored
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
/*
|
||||||
|
Copyright 2016 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package fields
|
||||||
|
|
||||||
|
import "k8s.io/kubernetes/pkg/selection"
|
||||||
|
|
||||||
|
// Requirements is AND of all requirements.
|
||||||
|
type Requirements []Requirement
|
||||||
|
|
||||||
|
// Requirement contains a field, a value, and an operator that relates the field and value.
|
||||||
|
// This is currently for reading internal selection information of field selector.
|
||||||
|
type Requirement struct {
|
||||||
|
Operator selection.Operator
|
||||||
|
Field string
|
||||||
|
Value string
|
||||||
|
}
|
278
vendor/src/k8s.io/kubernetes/pkg/fields/selector.go
vendored
Normal file
278
vendor/src/k8s.io/kubernetes/pkg/fields/selector.go
vendored
Normal file
|
@ -0,0 +1,278 @@
|
||||||
|
/*
|
||||||
|
Copyright 2015 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package fields
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"k8s.io/kubernetes/pkg/selection"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Selector represents a field selector.
|
||||||
|
type Selector interface {
|
||||||
|
// Matches returns true if this selector matches the given set of fields.
|
||||||
|
Matches(Fields) bool
|
||||||
|
|
||||||
|
// Empty returns true if this selector does not restrict the selection space.
|
||||||
|
Empty() bool
|
||||||
|
|
||||||
|
// RequiresExactMatch allows a caller to introspect whether a given selector
|
||||||
|
// requires a single specific field to be set, and if so returns the value it
|
||||||
|
// requires.
|
||||||
|
RequiresExactMatch(field string) (value string, found bool)
|
||||||
|
|
||||||
|
// Transform returns a new copy of the selector after TransformFunc has been
|
||||||
|
// applied to the entire selector, or an error if fn returns an error.
|
||||||
|
Transform(fn TransformFunc) (Selector, error)
|
||||||
|
|
||||||
|
// Requirements converts this interface to Requirements to expose
|
||||||
|
// more detailed selection information.
|
||||||
|
Requirements() Requirements
|
||||||
|
|
||||||
|
// String returns a human readable string that represents this selector.
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Everything returns a selector that matches all fields.
|
||||||
|
func Everything() Selector {
|
||||||
|
return andTerm{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type hasTerm struct {
|
||||||
|
field, value string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *hasTerm) Matches(ls Fields) bool {
|
||||||
|
return ls.Get(t.field) == t.value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *hasTerm) Empty() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *hasTerm) RequiresExactMatch(field string) (value string, found bool) {
|
||||||
|
if t.field == field {
|
||||||
|
return t.value, true
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *hasTerm) Transform(fn TransformFunc) (Selector, error) {
|
||||||
|
field, value, err := fn(t.field, t.value)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &hasTerm{field, value}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *hasTerm) Requirements() Requirements {
|
||||||
|
return []Requirement{{
|
||||||
|
Field: t.field,
|
||||||
|
Operator: selection.Equals,
|
||||||
|
Value: t.value,
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *hasTerm) String() string {
|
||||||
|
return fmt.Sprintf("%v=%v", t.field, t.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
type notHasTerm struct {
|
||||||
|
field, value string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *notHasTerm) Matches(ls Fields) bool {
|
||||||
|
return ls.Get(t.field) != t.value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *notHasTerm) Empty() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *notHasTerm) RequiresExactMatch(field string) (value string, found bool) {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *notHasTerm) Transform(fn TransformFunc) (Selector, error) {
|
||||||
|
field, value, err := fn(t.field, t.value)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return ¬HasTerm{field, value}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *notHasTerm) Requirements() Requirements {
|
||||||
|
return []Requirement{{
|
||||||
|
Field: t.field,
|
||||||
|
Operator: selection.NotEquals,
|
||||||
|
Value: t.value,
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *notHasTerm) String() string {
|
||||||
|
return fmt.Sprintf("%v!=%v", t.field, t.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
type andTerm []Selector
|
||||||
|
|
||||||
|
func (t andTerm) Matches(ls Fields) bool {
|
||||||
|
for _, q := range t {
|
||||||
|
if !q.Matches(ls) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t andTerm) Empty() bool {
|
||||||
|
if t == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if len([]Selector(t)) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
for i := range t {
|
||||||
|
if !t[i].Empty() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t andTerm) RequiresExactMatch(field string) (string, bool) {
|
||||||
|
if t == nil || len([]Selector(t)) == 0 {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
for i := range t {
|
||||||
|
if value, found := t[i].RequiresExactMatch(field); found {
|
||||||
|
return value, found
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t andTerm) Transform(fn TransformFunc) (Selector, error) {
|
||||||
|
next := make([]Selector, len([]Selector(t)))
|
||||||
|
for i, s := range []Selector(t) {
|
||||||
|
n, err := s.Transform(fn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
next[i] = n
|
||||||
|
}
|
||||||
|
return andTerm(next), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t andTerm) Requirements() Requirements {
|
||||||
|
reqs := make([]Requirement, 0, len(t))
|
||||||
|
for _, s := range []Selector(t) {
|
||||||
|
rs := s.Requirements()
|
||||||
|
reqs = append(reqs, rs...)
|
||||||
|
}
|
||||||
|
return reqs
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t andTerm) String() string {
|
||||||
|
var terms []string
|
||||||
|
for _, q := range t {
|
||||||
|
terms = append(terms, q.String())
|
||||||
|
}
|
||||||
|
return strings.Join(terms, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
// SelectorFromSet returns a Selector which will match exactly the given Set. A
|
||||||
|
// nil Set is considered equivalent to Everything().
|
||||||
|
func SelectorFromSet(ls Set) Selector {
|
||||||
|
if ls == nil {
|
||||||
|
return Everything()
|
||||||
|
}
|
||||||
|
items := make([]Selector, 0, len(ls))
|
||||||
|
for field, value := range ls {
|
||||||
|
items = append(items, &hasTerm{field: field, value: value})
|
||||||
|
}
|
||||||
|
if len(items) == 1 {
|
||||||
|
return items[0]
|
||||||
|
}
|
||||||
|
return andTerm(items)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseSelectorOrDie takes a string representing a selector and returns an
|
||||||
|
// object suitable for matching, or panic when an error occur.
|
||||||
|
func ParseSelectorOrDie(s string) Selector {
|
||||||
|
selector, err := ParseSelector(s)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return selector
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseSelector takes a string representing a selector and returns an
|
||||||
|
// object suitable for matching, or an error.
|
||||||
|
func ParseSelector(selector string) (Selector, error) {
|
||||||
|
return parseSelector(selector,
|
||||||
|
func(lhs, rhs string) (newLhs, newRhs string, err error) {
|
||||||
|
return lhs, rhs, nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parses the selector and runs them through the given TransformFunc.
|
||||||
|
func ParseAndTransformSelector(selector string, fn TransformFunc) (Selector, error) {
|
||||||
|
return parseSelector(selector, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to transform selectors.
|
||||||
|
type TransformFunc func(field, value string) (newField, newValue string, err error)
|
||||||
|
|
||||||
|
func try(selectorPiece, op string) (lhs, rhs string, ok bool) {
|
||||||
|
pieces := strings.Split(selectorPiece, op)
|
||||||
|
if len(pieces) == 2 {
|
||||||
|
return pieces[0], pieces[1], true
|
||||||
|
}
|
||||||
|
return "", "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseSelector(selector string, fn TransformFunc) (Selector, error) {
|
||||||
|
parts := strings.Split(selector, ",")
|
||||||
|
sort.StringSlice(parts).Sort()
|
||||||
|
var items []Selector
|
||||||
|
for _, part := range parts {
|
||||||
|
if part == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if lhs, rhs, ok := try(part, "!="); ok {
|
||||||
|
items = append(items, ¬HasTerm{field: lhs, value: rhs})
|
||||||
|
} else if lhs, rhs, ok := try(part, "=="); ok {
|
||||||
|
items = append(items, &hasTerm{field: lhs, value: rhs})
|
||||||
|
} else if lhs, rhs, ok := try(part, "="); ok {
|
||||||
|
items = append(items, &hasTerm{field: lhs, value: rhs})
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("invalid selector: '%s'; can't understand '%s'", selector, part)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(items) == 1 {
|
||||||
|
return items[0].Transform(fn)
|
||||||
|
}
|
||||||
|
return andTerm(items).Transform(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OneTermEqualSelector returns an object that matches objects where one field/field equals one value.
|
||||||
|
// Cannot return an error.
|
||||||
|
func OneTermEqualSelector(k, v string) Selector {
|
||||||
|
return &hasTerm{field: k, value: v}
|
||||||
|
}
|
33
vendor/src/k8s.io/kubernetes/pkg/selection/operator.go
vendored
Normal file
33
vendor/src/k8s.io/kubernetes/pkg/selection/operator.go
vendored
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
/*
|
||||||
|
Copyright 2016 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package selection
|
||||||
|
|
||||||
|
// Operator represents a key/field's relationship to value(s).
|
||||||
|
// See labels.Requirement and fields.Requirement for more details.
|
||||||
|
type Operator string
|
||||||
|
|
||||||
|
const (
|
||||||
|
DoesNotExist Operator = "!"
|
||||||
|
Equals Operator = "="
|
||||||
|
DoubleEquals Operator = "=="
|
||||||
|
In Operator = "in"
|
||||||
|
NotEquals Operator = "!="
|
||||||
|
NotIn Operator = "notin"
|
||||||
|
Exists Operator = "exists"
|
||||||
|
GreaterThan Operator = "gt"
|
||||||
|
LessThan Operator = "lt"
|
||||||
|
)
|
Loading…
Reference in a new issue