github.com/GoogleContainerTools/skaffold/v2@v2.13.2/pkg/diag/validator/deployment.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  	"fmt"
    22  	"strings"
    23  
    24  	appsv1 "k8s.io/api/apps/v1"
    25  	v1 "k8s.io/api/core/v1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/client-go/kubernetes"
    28  
    29  	"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output/log"
    30  )
    31  
    32  type deploymentPodsSelector struct {
    33  	k      kubernetes.Interface
    34  	depObj appsv1.Deployment
    35  }
    36  
    37  func NewDeploymentPodsSelector(k kubernetes.Interface, d appsv1.Deployment) PodSelector {
    38  	return &deploymentPodsSelector{k, d}
    39  }
    40  
    41  func (s *deploymentPodsSelector) Select(ctx context.Context, ns string, opts metav1.ListOptions) ([]v1.Pod, error) {
    42  	_, _, controller, err := getReplicaSet(&s.depObj, s.k.AppsV1())
    43  	if err != nil {
    44  		log.Entry(ctx).Debugf("could not fetch deployment replica set %s", err)
    45  		return nil, err
    46  	} else if controller == nil {
    47  		log.Entry(ctx).Debugf("deployment replica set not created yet.")
    48  		return nil, nil
    49  	}
    50  	for _, c := range controller.Status.Conditions {
    51  		if c.Type == "ReplicaFailure" && c.Reason == "FailedCreate" && c.Status == "True" && strings.Contains(c.Message, "admission webhook") {
    52  			return nil, fmt.Errorf("%s: %s", ReplicaFailureAdmissionErr, c.Message)
    53  		}
    54  	}
    55  
    56  	pods, err := s.k.CoreV1().Pods(ns).List(ctx, opts)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	var filtered []v1.Pod
    61  	for _, po := range pods.Items {
    62  		if isPodOwnedBy(po, controller) {
    63  			filtered = append(filtered, po)
    64  		}
    65  	}
    66  	return filtered, nil
    67  }