github.com/GoogleContainerTools/skaffold/v2@v2.13.2/pkg/diag/validator/pod.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  	v1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/client-go/kubernetes"
    25  )
    26  
    27  type standalonePodsSelector struct {
    28  	k kubernetes.Interface
    29  }
    30  
    31  func NewStandalonePodsSelector(k kubernetes.Interface) PodSelector {
    32  	return &standalonePodsSelector{k}
    33  }
    34  
    35  func (s *standalonePodsSelector) Select(ctx context.Context, ns string, opts metav1.ListOptions) ([]v1.Pod, error) {
    36  	pods, err := s.k.CoreV1().Pods(ns).List(ctx, opts)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	var filtered []v1.Pod
    41  	for _, po := range pods.Items {
    42  		// deployments defining pods directly don't have owner references
    43  		if metav1.GetControllerOfNoCopy(&po) == nil {
    44  			filtered = append(filtered, po)
    45  		}
    46  	}
    47  	return filtered, nil
    48  }