github.com/maxgio92/test-infra@v0.1.0/kubetest/boskos/common/mason_config.go (about)

     1  /*
     2  Copyright 2017 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 common
    18  
    19  import (
    20  	"time"
    21  
    22  	"github.com/google/uuid"
    23  )
    24  
    25  // ResourceNeeds maps the type to count of resources types needed
    26  type ResourceNeeds map[string]int
    27  
    28  // TypeToResources stores all the leased resources with the same type f
    29  type TypeToResources map[string][]Resource
    30  
    31  // ConfigType gather the type of config to be applied by Mason in order to construct the resource
    32  type ConfigType struct {
    33  	// Identifier of the struct this maps back to
    34  	Type string `json:"type,omitempty"`
    35  	// Marshaled JSON content
    36  	Content string `json:"content,omitempty"`
    37  }
    38  
    39  // DynamicResourceLifeCycle defines the life cycle of a dynamic resource.
    40  // All Resource of a given type will be constructed using the same configuration
    41  type DynamicResourceLifeCycle struct {
    42  	Type string `json:"type"`
    43  	// Initial state to be created as
    44  	InitialState string `json:"state"`
    45  	// Minimum number of resources to be use as a buffer.
    46  	// Resources in the process of being deleted and cleaned up are included in this count.
    47  	MinCount int `json:"min-count"`
    48  	// Maximum number of resources expected. This maximum may be temporarily
    49  	// exceeded while resources are in the process of being deleted, though this
    50  	// is only expected when MaxCount is lowered.
    51  	MaxCount int `json:"max-count"`
    52  	// Lifespan of a resource, time after which the resource should be reset.
    53  	LifeSpan *time.Duration `json:"lifespan,omitempty"`
    54  	// Config information about how to create the object
    55  	Config ConfigType `json:"config,omitempty"`
    56  	// Needs define the resource needs to create the object
    57  	Needs ResourceNeeds `json:"needs,omitempty"`
    58  }
    59  
    60  // DRLCByName helps sorting ResourcesConfig by name
    61  type DRLCByName []DynamicResourceLifeCycle
    62  
    63  func (ut DRLCByName) Len() int           { return len(ut) }
    64  func (ut DRLCByName) Swap(i, j int)      { ut[i], ut[j] = ut[j], ut[i] }
    65  func (ut DRLCByName) Less(i, j int) bool { return ut[i].Type < ut[j].Type }
    66  
    67  // NewDynamicResourceLifeCycleFromConfig parse the a ResourceEntry into a DynamicResourceLifeCycle
    68  func NewDynamicResourceLifeCycleFromConfig(e ResourceEntry) DynamicResourceLifeCycle {
    69  	var dur *time.Duration
    70  	if e.LifeSpan != nil {
    71  		dur = e.LifeSpan.Duration
    72  	}
    73  	return DynamicResourceLifeCycle{
    74  		Type:         e.Type,
    75  		MaxCount:     e.MaxCount,
    76  		MinCount:     e.MinCount,
    77  		LifeSpan:     dur,
    78  		InitialState: e.State,
    79  		Config:       e.Config,
    80  		Needs:        e.Needs,
    81  	}
    82  }
    83  
    84  // Copy returns a copy of the TypeToResources
    85  func (t TypeToResources) Copy() TypeToResources {
    86  	n := TypeToResources{}
    87  	for k, v := range t {
    88  		n[k] = v
    89  	}
    90  	return n
    91  }
    92  
    93  // GenerateDynamicResourceName generates a unique name for dynamic resources
    94  func GenerateDynamicResourceName() string {
    95  	return uuid.New().String()
    96  }