Merge pull request #156 from mrunalp/pod_meta_filter
Pod filtering support
This commit is contained in:
commit
b3f0920fe8
3 changed files with 215 additions and 12 deletions
|
@ -2,6 +2,8 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
|
@ -34,6 +36,10 @@ var runPodSandboxCommand = cli.Command{
|
|||
Value: "",
|
||||
Usage: "the name of the pod sandbox",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "label",
|
||||
Usage: "add key=value labels to the container",
|
||||
},
|
||||
},
|
||||
Action: func(context *cli.Context) error {
|
||||
// Set up a connection to the server.
|
||||
|
@ -44,8 +50,22 @@ var runPodSandboxCommand = cli.Command{
|
|||
defer conn.Close()
|
||||
client := pb.NewRuntimeServiceClient(conn)
|
||||
|
||||
opts := createOptions{
|
||||
configPath: context.String("config"),
|
||||
name: context.String("name"),
|
||||
labels: make(map[string]string),
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
|
||||
// Test RuntimeServiceClient.RunPodSandbox
|
||||
err = RunPodSandbox(client, context.String("config"), context.String("name"))
|
||||
err = RunPodSandbox(client, opts)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Creating the pod sandbox failed: %v", err)
|
||||
}
|
||||
|
@ -138,6 +158,20 @@ var listPodSandboxCommand = cli.Command{
|
|||
Name: "list",
|
||||
Usage: "list pod sandboxes",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "id",
|
||||
Value: "",
|
||||
Usage: "filter by pod sandbox id",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "state",
|
||||
Value: "",
|
||||
Usage: "filter by pod sandbox state",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "label",
|
||||
Usage: "filter by key=value label",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "quiet",
|
||||
Usage: "list only pod IDs",
|
||||
|
@ -152,7 +186,22 @@ var listPodSandboxCommand = cli.Command{
|
|||
defer conn.Close()
|
||||
client := pb.NewRuntimeServiceClient(conn)
|
||||
|
||||
err = ListPodSandboxes(client, context.Bool("quiet"))
|
||||
opts := listOptions{
|
||||
id: context.String("id"),
|
||||
state: context.String("state"),
|
||||
quiet: context.Bool("quiet"),
|
||||
labels: make(map[string]string),
|
||||
}
|
||||
|
||||
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 = ListPodSandboxes(client, opts)
|
||||
if err != nil {
|
||||
return fmt.Errorf("listing pod sandboxes failed: %v", err)
|
||||
}
|
||||
|
@ -162,15 +211,19 @@ var listPodSandboxCommand = cli.Command{
|
|||
|
||||
// RunPodSandbox sends a RunPodSandboxRequest to the server, and parses
|
||||
// the returned RunPodSandboxResponse.
|
||||
func RunPodSandbox(client pb.RuntimeServiceClient, path string, name string) error {
|
||||
config, err := loadPodSandboxConfig(path)
|
||||
func RunPodSandbox(client pb.RuntimeServiceClient, opts createOptions) error {
|
||||
config, err := loadPodSandboxConfig(opts.configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Override the name by the one specified through CLI
|
||||
if name != "" {
|
||||
config.Metadata.Name = &name
|
||||
if opts.name != "" {
|
||||
config.Metadata.Name = &opts.name
|
||||
}
|
||||
|
||||
for k, v := range opts.labels {
|
||||
config.Labels[k] = v
|
||||
}
|
||||
|
||||
r, err := client.RunPodSandbox(context.Background(), &pb.RunPodSandboxRequest{Config: config})
|
||||
|
@ -264,13 +317,35 @@ func PodSandboxStatus(client pb.RuntimeServiceClient, ID string) error {
|
|||
|
||||
// ListPodSandboxes sends a ListPodSandboxRequest to the server, and parses
|
||||
// the returned ListPodSandboxResponse.
|
||||
func ListPodSandboxes(client pb.RuntimeServiceClient, quiet bool) error {
|
||||
r, err := client.ListPodSandbox(context.Background(), &pb.ListPodSandboxRequest{})
|
||||
func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
|
||||
filter := &pb.PodSandboxFilter{}
|
||||
if opts.id != "" {
|
||||
filter.Id = &opts.id
|
||||
}
|
||||
if opts.state != "" {
|
||||
st := pb.PodSandBoxState_NOTREADY
|
||||
switch opts.state {
|
||||
case "ready":
|
||||
st = pb.PodSandBoxState_READY
|
||||
filter.State = &st
|
||||
case "notready":
|
||||
st = pb.PodSandBoxState_NOTREADY
|
||||
filter.State = &st
|
||||
default:
|
||||
log.Fatalf("--state should be ready or notready")
|
||||
}
|
||||
}
|
||||
if opts.labels != nil {
|
||||
filter.LabelSelector = opts.labels
|
||||
}
|
||||
r, err := client.ListPodSandbox(context.Background(), &pb.ListPodSandboxRequest{
|
||||
Filter: filter,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, pod := range r.Items {
|
||||
if quiet {
|
||||
if opts.quiet {
|
||||
fmt.Println(*pod.Id)
|
||||
continue
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ import (
|
|||
"github.com/opencontainers/runc/libcontainer/label"
|
||||
"github.com/opencontainers/runtime-tools/generate"
|
||||
"golang.org/x/net/context"
|
||||
"k8s.io/kubernetes/pkg/fields"
|
||||
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
||||
)
|
||||
|
||||
|
@ -20,7 +21,7 @@ type sandbox struct {
|
|||
id string
|
||||
name string
|
||||
logDir string
|
||||
labels map[string]string
|
||||
labels fields.Set
|
||||
annotations map[string]string
|
||||
containers oci.Store
|
||||
processLabel string
|
||||
|
@ -462,10 +463,46 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR
|
|||
}, nil
|
||||
}
|
||||
|
||||
// filterSandbox returns whether passed container matches filtering criteria
|
||||
func filterSandbox(p *pb.PodSandbox, filter *pb.PodSandboxFilter) bool {
|
||||
if filter != nil {
|
||||
if filter.State != nil {
|
||||
if *p.State != *filter.State {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if filter.LabelSelector != nil {
|
||||
sel := fields.SelectorFromSet(filter.LabelSelector)
|
||||
if !sel.Matches(fields.Set(p.Labels)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// ListPodSandbox returns a list of SandBoxes.
|
||||
func (s *Server) ListPodSandbox(context.Context, *pb.ListPodSandboxRequest) (*pb.ListPodSandboxResponse, error) {
|
||||
func (s *Server) ListPodSandbox(ctx context.Context, req *pb.ListPodSandboxRequest) (*pb.ListPodSandboxResponse, error) {
|
||||
var pods []*pb.PodSandbox
|
||||
var podList []*sandbox
|
||||
for _, sb := range s.state.sandboxes {
|
||||
podList = append(podList, sb)
|
||||
}
|
||||
|
||||
filter := req.Filter
|
||||
// Filter by pod id first.
|
||||
if filter != nil {
|
||||
if filter.Id != nil {
|
||||
sb := s.getSandbox(*filter.Id)
|
||||
if sb == nil {
|
||||
podList = []*sandbox{}
|
||||
} else {
|
||||
podList = []*sandbox{sb}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, sb := range podList {
|
||||
podInfraContainerName := sb.name + "-infra"
|
||||
podInfraContainer := sb.getContainer(podInfraContainerName)
|
||||
if podInfraContainer == nil {
|
||||
|
@ -492,8 +529,11 @@ func (s *Server) ListPodSandbox(context.Context, *pb.ListPodSandboxRequest) (*pb
|
|||
Metadata: sb.metadata,
|
||||
}
|
||||
|
||||
// Filter by other criteria such as state and labels.
|
||||
if filterSandbox(pod, req.Filter) {
|
||||
pods = append(pods, pod)
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.ListPodSandboxResponse{
|
||||
Items: pods,
|
||||
|
|
|
@ -65,3 +65,91 @@ function teardown() {
|
|||
cleanup_pods
|
||||
stop_ocid
|
||||
}
|
||||
|
||||
@test "pod list 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 -name pod1 --label "a=b" --label "c=d" --label "e=f"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
pod1_id="$output"
|
||||
run ocic pod create --config "$TESTDATA"/sandbox_config.json -name pod2 --label "a=b" --label "c=d"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
pod2_id="$output"
|
||||
run ocic pod create --config "$TESTDATA"/sandbox_config.json -name pod3 --label "a=b"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
pod3_id="$output"
|
||||
run ocic pod list --label "a=b" --label "c=d" --label "e=f" --quiet
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" != "" ]]
|
||||
[[ "$output" =~ "$pod1_id" ]]
|
||||
run ocic pod list --label "g=h" --quiet
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == "" ]]
|
||||
run ocic pod list --label "a=b" --label "c=d" --quiet
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" != "" ]]
|
||||
[[ "$output" =~ "$pod1_id" ]]
|
||||
[[ "$output" =~ "$pod2_id" ]]
|
||||
run ocic pod list --label "a=b" --quiet
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" != "" ]]
|
||||
[[ "$output" =~ "$pod1_id" ]]
|
||||
[[ "$output" =~ "$pod2_id" ]]
|
||||
[[ "$output" =~ "$pod3_id" ]]
|
||||
run ocic pod list --id "$pod1_id"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" != "" ]]
|
||||
[[ "$output" =~ "$pod1_id" ]]
|
||||
run ocic pod list --id "$pod2_id"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" != "" ]]
|
||||
[[ "$output" =~ "$pod2_id" ]]
|
||||
run ocic pod list --id "$pod3_id"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" != "" ]]
|
||||
[[ "$output" =~ "$pod3_id" ]]
|
||||
run ocic pod list --id "$pod1_id" --label "a=b"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" != "" ]]
|
||||
[[ "$output" =~ "$pod1_id" ]]
|
||||
run ocic pod list --id "$pod2_id" --label "a=b"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" != "" ]]
|
||||
[[ "$output" =~ "$pod2_id" ]]
|
||||
run ocic pod list --id "$pod3_id" --label "a=b"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" != "" ]]
|
||||
[[ "$output" =~ "$pod3_id" ]]
|
||||
run ocic pod list --id "$pod3_id" --label "c=d"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == "" ]]
|
||||
run ocic pod remove --id "$pod1_id"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
run ocic pod remove --id "$pod2_id"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
run ocic pod remove --id "$pod3_id"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
cleanup_pods
|
||||
stop_ocid
|
||||
}
|
Loading…
Reference in a new issue