agones.dev/agones@v1.53.0/pkg/cloudproduct/eviction/eviction.go (about)

     1  // Copyright 2023 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package eviction implements a generic SetEviction interface for cloud products
    16  package eviction
    17  
    18  import (
    19  	agonesv1 "agones.dev/agones/pkg/apis/agones/v1"
    20  	"github.com/pkg/errors"
    21  	corev1 "k8s.io/api/core/v1"
    22  )
    23  
    24  // SetEviction sets disruptions controls on a Pod based on GameServer.Status.Eviction.
    25  func SetEviction(eviction *agonesv1.Eviction, pod *corev1.Pod) error {
    26  	if eviction == nil {
    27  		return errors.New("No eviction value set. Should be the default value")
    28  	}
    29  	if _, exists := pod.ObjectMeta.Annotations[agonesv1.PodSafeToEvictAnnotation]; !exists {
    30  		switch eviction.Safe {
    31  		case agonesv1.EvictionSafeAlways:
    32  			pod.ObjectMeta.Annotations[agonesv1.PodSafeToEvictAnnotation] = agonesv1.True
    33  		case agonesv1.EvictionSafeOnUpgrade, agonesv1.EvictionSafeNever:
    34  			// For EvictionSafeOnUpgrade and EvictionSafeNever, we block Cluster Autoscaler
    35  			// (on Autopilot, this enables Extended Duration pods, which is equivalent).
    36  			pod.ObjectMeta.Annotations[agonesv1.PodSafeToEvictAnnotation] = agonesv1.False
    37  		default:
    38  			return errors.Errorf("unknown eviction.safe value %q", string(eviction.Safe))
    39  		}
    40  	}
    41  	if _, exists := pod.ObjectMeta.Labels[agonesv1.SafeToEvictLabel]; !exists {
    42  		switch eviction.Safe {
    43  		case agonesv1.EvictionSafeAlways, agonesv1.EvictionSafeOnUpgrade:
    44  			// For EvictionSafeAlways and EvictionSafeOnUpgrade, we use a label value
    45  			// that does not match the agones-gameserver-safe-to-evict-false PDB. But
    46  			// we go ahead and label it, in case someone wants to adopt custom logic
    47  			// for this group of game servers.
    48  			pod.ObjectMeta.Labels[agonesv1.SafeToEvictLabel] = agonesv1.True
    49  		case agonesv1.EvictionSafeNever:
    50  			// For EvictionSafeNever, match gones-gameserver-safe-to-evict-false PDB.
    51  			pod.ObjectMeta.Labels[agonesv1.SafeToEvictLabel] = agonesv1.False
    52  		default:
    53  			return errors.Errorf("unknown eviction.safe value %q", string(eviction.Safe))
    54  		}
    55  	}
    56  	return nil
    57  }