github.com/kotalco/kotal@v0.3.0/apis/filecoin/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-filecoin-kotal-io-v1alpha1-node,mutating=true,failurePolicy=fail,groups=filecoin.kotal.io,resources=nodes,verbs=create;update,versions=v1alpha1,name=mutate-filecoin-v1alpha1-node.kb.io,sideEffects=None,admissionReviewVersions=v1
     8  
     9  var _ webhook.Defaulter = &Node{}
    10  
    11  // Default implements webhook.Defaulter so a webhook will be registered for the type
    12  func (n *Node) Default() {
    13  	nodelog.Info("default", "name", n.Name)
    14  
    15  	mainnet := n.Spec.Network == MainNetwork
    16  	calibration := n.Spec.Network == CalibrationNetwork
    17  
    18  	if n.Spec.Image == "" {
    19  		var image string
    20  
    21  		switch n.Spec.Network {
    22  		case MainNetwork:
    23  			image = DefaultLotusImage
    24  		case CalibrationNetwork:
    25  			image = DefaultLotusCalibrationImage
    26  		}
    27  
    28  		n.Spec.Image = image
    29  	}
    30  
    31  	if n.Spec.Replicas == nil {
    32  		// constants are not addressable
    33  		replicas := DefaltReplicas
    34  		n.Spec.Replicas = &replicas
    35  	}
    36  
    37  	if n.Spec.Logging == "" {
    38  		n.Spec.Logging = DefaultLogging
    39  	}
    40  
    41  	if n.Spec.APIPort == 0 {
    42  		n.Spec.APIPort = DefaultAPIPort
    43  	}
    44  	if n.Spec.APIRequestTimeout == 0 {
    45  		n.Spec.APIRequestTimeout = DefaultAPIRequestTimeout
    46  	}
    47  
    48  	if n.Spec.P2PPort == 0 {
    49  		n.Spec.P2PPort = DefaultP2PPort
    50  	}
    51  
    52  	if n.Spec.Resources.CPU == "" {
    53  		if mainnet {
    54  			n.Spec.CPU = DefaultMainnetNodeCPURequest
    55  		}
    56  		if calibration {
    57  			n.Spec.CPU = DefaultCalibrationNodeCPURequest
    58  		}
    59  	}
    60  
    61  	if n.Spec.CPULimit == "" {
    62  		if mainnet {
    63  			n.Spec.CPULimit = DefaultMainnetNodeCPULimit
    64  		}
    65  		if calibration {
    66  			n.Spec.CPULimit = DefaultCalibrationNodeCPULimit
    67  		}
    68  	}
    69  
    70  	if n.Spec.Memory == "" {
    71  		if mainnet {
    72  			n.Spec.Memory = DefaultMainnetNodeMemoryRequest
    73  		}
    74  		if calibration {
    75  			n.Spec.Memory = DefaultCalibrationNodeMemoryRequest
    76  		}
    77  	}
    78  
    79  	if n.Spec.MemoryLimit == "" {
    80  		if mainnet {
    81  			n.Spec.MemoryLimit = DefaultMainnetNodeMemoryLimit
    82  		}
    83  		if calibration {
    84  			n.Spec.MemoryLimit = DefaultCalibrationNodeMemoryLimit
    85  		}
    86  	}
    87  
    88  	if n.Spec.Storage == "" {
    89  		if mainnet {
    90  			n.Spec.Storage = DefaultMainnetNodeStorageRequest
    91  		}
    92  		if calibration {
    93  			n.Spec.Storage = DefaultCalibrationNodeStorageRequest
    94  		}
    95  	}
    96  
    97  }