github.com/GoogleContainerTools/skaffold/v2@v2.13.2/pkg/diag/validator/statefulset.go (about)

     1  /*
     2  Copyright 2021 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package validator
    18  
    19  import (
    20  	"context"
    21  
    22  	appsv1 "k8s.io/api/apps/v1"
    23  	v1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/client-go/kubernetes"
    26  )
    27  
    28  type statefulSetPodsSelector struct {
    29  	k      kubernetes.Interface
    30  	setObj appsv1.StatefulSet
    31  }
    32  
    33  func NewStatefulSetPodsSelector(k kubernetes.Interface, d appsv1.StatefulSet) PodSelector {
    34  	return &statefulSetPodsSelector{k, d}
    35  }
    36  
    37  func (s *statefulSetPodsSelector) Select(ctx context.Context, ns string, opts metav1.ListOptions) ([]v1.Pod, error) {
    38  	pods, err := s.k.CoreV1().Pods(ns).List(ctx, opts)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	var filtered []v1.Pod
    43  	for _, po := range pods.Items {
    44  		if isPodOwnedBy(po, &s.setObj) {
    45  			filtered = append(filtered, po)
    46  		}
    47  	}
    48  	return filtered, nil
    49  }