...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/recover/select.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/recover

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package recover
    17  
    18  import (
    19  	"context"
    20  
    21  	"github.com/pkg/errors"
    22  
    23  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    24  	ctrlclient "github.com/chaos-mesh/chaos-mesh/pkg/ctrl/client"
    25  	"github.com/chaos-mesh/chaos-mesh/pkg/ctrl/server/model"
    26  )
    27  
    28  func SelectPods(ctx context.Context, client *ctrlclient.CtrlClient, selector v1alpha1.PodSelectorSpec) ([]*PartialPod, error) {
    29  	selectInput := model.PodSelectorInput{
    30  		Pods:                map[string]interface{}{},
    31  		NodeSelectors:       map[string]interface{}{},
    32  		Nodes:               selector.Nodes,
    33  		PodPhaseSelectors:   selector.PodPhaseSelectors,
    34  		Namespaces:          selector.Namespaces,
    35  		FieldSelectors:      map[string]interface{}{},
    36  		LabelSelectors:      map[string]interface{}{},
    37  		AnnotationSelectors: map[string]interface{}{},
    38  	}
    39  
    40  	for k, v := range selector.Pods {
    41  		selectInput.Pods[k] = v
    42  	}
    43  
    44  	for k, v := range selector.NodeSelectors {
    45  		selectInput.NodeSelectors[k] = v
    46  	}
    47  
    48  	for k, v := range selector.FieldSelectors {
    49  		selectInput.FieldSelectors[k] = v
    50  	}
    51  
    52  	for k, v := range selector.LabelSelectors {
    53  		selectInput.LabelSelectors[k] = v
    54  	}
    55  
    56  	for k, v := range selector.AnnotationSelectors {
    57  		selectInput.AnnotationSelectors[k] = v
    58  	}
    59  
    60  	podsQuery := new(struct {
    61  		Pods []*PartialPod `graphql:"pods(selector: $selector)"`
    62  	})
    63  
    64  	err := client.QueryClient.Query(ctx, podsQuery, map[string]interface{}{"selector": selectInput})
    65  	if err != nil {
    66  		return nil, errors.Wrapf(err, "select pods with selector: %+v", selector)
    67  	}
    68  
    69  	return podsQuery.Pods, nil
    70  }
    71