k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/util/pods_metadata.go (about)

     1  /*
     2  Copyright 2020 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 util
    18  
    19  import (
    20  	"sync"
    21  )
    22  
    23  // podMetadata contains metadata about a single pod
    24  type podMetadata struct {
    25  	stateless bool
    26  }
    27  
    28  // PodsMetadata store metadata about pods.
    29  type PodsMetadata struct {
    30  	name     string
    31  	lock     sync.Mutex
    32  	metadata map[string]*podMetadata
    33  }
    34  
    35  // NewPodsMetadata created new PodsMetadata instance.
    36  func NewPodsMetadata(name string) *PodsMetadata {
    37  	return &PodsMetadata{
    38  		name:     name,
    39  		metadata: make(map[string]*podMetadata),
    40  	}
    41  }
    42  
    43  // SetStateless marks a given pod as stateless.
    44  func (o *PodsMetadata) SetStateless(key string, stateless bool) {
    45  	o.lock.Lock()
    46  	defer o.lock.Unlock()
    47  	if meta, ok := o.metadata[key]; ok {
    48  		meta.stateless = stateless
    49  		return
    50  	}
    51  	o.metadata[key] = &podMetadata{
    52  		stateless: stateless,
    53  	}
    54  }
    55  
    56  // FilterStateless returns true iff a pod associated with a given key is
    57  // marked as stateless.
    58  func (o *PodsMetadata) FilterStateless(key string) bool {
    59  	o.lock.Lock()
    60  	defer o.lock.Unlock()
    61  	meta, ok := o.metadata[key]
    62  	return ok && meta.stateless
    63  }
    64  
    65  // FilterStateful returns true iff a pod associated with a given key is
    66  // not marked as stateless.
    67  func (o *PodsMetadata) FilterStateful(key string) bool {
    68  	return !o.FilterStateless(key)
    69  }