github.com/kotalco/kotal@v0.3.0/apis/aptos/v1alpha1/node_defaulting_webhook.go (about)

     1  package v1alpha1
     2  
     3  import (
     4  	"sigs.k8s.io/controller-runtime/pkg/webhook"
     5  )
     6  
     7  // +kubebuilder:webhook:path=/mutate-aptos-kotal-io-v1alpha1-node,mutating=true,failurePolicy=fail,groups=aptos.kotal.io,resources=nodes,verbs=create;update,versions=v1alpha1,name=mutate-aptos-v1alpha1-node.kb.io,sideEffects=None,admissionReviewVersions=v1
     8  
     9  var _ webhook.Defaulter = &Node{}
    10  
    11  func (r *Node) DefaultNodeResources() {
    12  
    13  	if r.Spec.Resources.CPU == "" {
    14  		r.Spec.Resources.CPU = DefaultNodeCPURequest
    15  	}
    16  
    17  	if r.Spec.Resources.CPULimit == "" {
    18  		r.Spec.Resources.CPULimit = DefaultNodeCPULimit
    19  	}
    20  
    21  	if r.Spec.Resources.Memory == "" {
    22  		r.Spec.Resources.Memory = DefaultNodeMemoryRequest
    23  	}
    24  
    25  	if r.Spec.Resources.MemoryLimit == "" {
    26  		r.Spec.Resources.MemoryLimit = DefaultNodeMemoryLimit
    27  	}
    28  
    29  	if r.Spec.Resources.Storage == "" {
    30  		r.Spec.Resources.Storage = DefaultNodeStorageRequest
    31  	}
    32  
    33  }
    34  
    35  // Default implements webhook.Defaulter so a webhook will be registered for the type
    36  func (r *Node) Default() {
    37  	nodelog.Info("default", "name", r.Name)
    38  
    39  	r.DefaultNodeResources()
    40  
    41  	if r.Spec.Image == "" {
    42  		r.Spec.Image = DefaultAptosCoreImage
    43  	}
    44  
    45  	if r.Spec.Replicas == nil {
    46  		// constants are not addressable
    47  		replicas := DefaltReplicas
    48  		r.Spec.Replicas = &replicas
    49  	}
    50  
    51  	if r.Spec.MetricsPort == 0 {
    52  		r.Spec.MetricsPort = DefaultMetricsPort
    53  	}
    54  
    55  	if r.Spec.APIPort == 0 {
    56  		r.Spec.APIPort = DefaultAPIPort
    57  	}
    58  
    59  	if r.Spec.P2PPort == 0 {
    60  		if r.Spec.Validator {
    61  			r.Spec.P2PPort = DefaultValidatorP2PPort
    62  		} else {
    63  			r.Spec.P2PPort = DefaultFullnodeP2PPort
    64  		}
    65  	}
    66  
    67  }