k8s.io/kubernetes@v1.29.3/test/integration/daemonset/util.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes 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 daemonset
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"sync"
    23  
    24  	"k8s.io/apiserver/pkg/admission"
    25  	api "k8s.io/kubernetes/pkg/apis/core"
    26  )
    27  
    28  var _ admission.ValidationInterface = &fakePodFailAdmission{}
    29  
    30  type fakePodFailAdmission struct {
    31  	lock             sync.Mutex
    32  	limitedPodNumber int
    33  	succeedPodsCount int
    34  }
    35  
    36  func (f *fakePodFailAdmission) Handles(operation admission.Operation) bool {
    37  	return operation == admission.Create
    38  }
    39  
    40  func (f *fakePodFailAdmission) Validate(ctx context.Context, attr admission.Attributes, o admission.ObjectInterfaces) (err error) {
    41  	if attr.GetKind().GroupKind() != api.Kind("Pod") {
    42  		return nil
    43  	}
    44  
    45  	f.lock.Lock()
    46  	defer f.lock.Unlock()
    47  
    48  	if f.succeedPodsCount >= f.limitedPodNumber {
    49  		return fmt.Errorf("fakePodFailAdmission error")
    50  	}
    51  	f.succeedPodsCount++
    52  	return nil
    53  }