Skip to content

Commit

Permalink
Deny non-cluster-wide settings
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Jul 10, 2024
1 parent 723a6ae commit 0d55fbf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
11 changes: 10 additions & 1 deletion pkg/apis/k0s/v1beta1/clusterconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@ const (
)

// ClusterSpec defines the desired state of ClusterConfig
// +kubebuilder:validation:XValidation:rule="!has(self.api.externalAddress) || self.api.externalAddress == '' || !has(self.network.nodeLocalLoadBalancing.enabled) || !self.network.nodeLocalLoadBalancing.enabled",message="node-local load balancing cannot be used in conjunction with an external Kubernetes API server address"
// +kubebuilder:validation:XValidation:rule="!has(self.api)",message="All API settings have to be configured in the local k0s controller configuration"
// +kubebuilder:validation:XValidation:rule="!has(self.storage)",message="All storage settings have to be configured in the local k0s controller configuration"
// +kubebuilder:validation:XValidation:rule="!has(self.install)",message="All install settings have to be configured in the local k0s controller configuration"
type ClusterSpec struct {
// Keep the validation rules in sync with the GetClusterWideConfig method.

// some disabled rules:
// kubebuilder:validation:XValidation:rule="!has(self.api.externalAddress) || size(self.api.externalAddress) == nil || !has(self.network.nodeLocalLoadBalancing.enabled) || !self.network.nodeLocalLoadBalancing.enabled",message="node-local load balancing cannot be used in conjunction with an external Kubernetes API server address"

API *APISpec `json:"api,omitempty"`
ControllerManager *ControllerManagerSpec `json:"controllerManager,omitempty"`
Scheduler *SchedulerSpec `json:"scheduler,omitempty"`
Expand Down Expand Up @@ -382,6 +389,8 @@ func (c *ClusterConfig) Validate() (errs []error) {
// - Network.ControlPlaneLoadBalancing
// - Install
func (c *ClusterConfig) GetClusterWideConfig() *ClusterConfig {
// Keep this method in sync with the XValidation rules.

c = c.DeepCopy()
if c != nil && c.Spec != nil {
c.Spec.API = nil
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/k0s/v1beta1/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ import (
var _ Validateable = (*Network)(nil)

// Network defines the network related config options
// +kubebuilder:validation:XValidation:rule="!has(self.serviceCIDR)",message="The service CIDR has to be configured in the local k0s controller configuration"
// +kubebuilder:validation:XValidation:rule="!has(self.clusterDomain)",message="The cluster domain has to be configured in the local k0s controller configuration"
// +kubebuilder:validation:XValidation:rule="!has(self.controlPlaneLoadBalancing)",message="Control plane load balancing has to be configured in the local k0s controller configuration"
type Network struct {
// Keep the validation rules in sync with the GetClusterWideConfig method.

Calico *Calico `json:"calico"`
DualStack DualStack `json:"dualStack,omitempty"`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,16 @@ spec:
description: Network CIDR to use for cluster VIP services
type: string
type: object
x-kubernetes-validations:
- message: The service CIDR has to be configured in the local k0s
controller configuration
rule: '!has(self.serviceCIDR)'
- message: The cluster domain has to be configured in the local k0s
controller configuration
rule: '!has(self.clusterDomain)'
- message: Control plane load balancing has to be configured in the
local k0s controller configuration
rule: '!has(self.controlPlaneLoadBalancing)'
scheduler:
description: SchedulerSpec defines the fields for the Scheduler
properties:
Expand Down Expand Up @@ -809,10 +819,15 @@ spec:
type: array
type: object
x-kubernetes-validations:
- message: node-local load balancing cannot be used in conjunction with
an external Kubernetes API server address
rule: '!has(self.api.externalAddress) || self.api.externalAddress ==
'''' || !has(self.network.nodeLocalLoadBalancing.enabled) || !self.network.nodeLocalLoadBalancing.enabled'
- message: All API settings have to be configured in the local k0s controller
configuration
rule: '!has(self.api)'
- message: All storage settings have to be configured in the local k0s
controller configuration
rule: '!has(self.storage)'
- message: All install settings have to be configured in the local k0s
controller configuration
rule: '!has(self.install)'
status:
description: ClusterConfigStatus defines the observed state of ClusterConfig
type: object
Expand Down

0 comments on commit 0d55fbf

Please sign in to comment.