Skip to content

Commit

Permalink
Calico APIServer configuration refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamas Biro committed Aug 29, 2024
1 parent 5a58dcc commit 7bc4d24
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 465 deletions.
33 changes: 28 additions & 5 deletions api/v1/apiserver_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ type APIServerDeploymentContainer struct {
// If used in conjunction with the deprecated ComponentResources, then this value takes precedence.
// +optional
Resources *v1.ResourceRequirements `json:"resources,omitempty"`

// +optional
Lifecycle *v1.Lifecycle `json:"lifecycle,omitempty" protobuf:"bytes,12,opt,name=lifecycle"`
}

// APIServerDeploymentInitContainer is an API server Deployment init container.
Expand Down Expand Up @@ -177,6 +180,12 @@ type APIServerDeployment struct {

// APIServerDeploymentSpec defines configuration for the API server Deployment.
type APIServerDeploymentSpec struct {
// Replicas defines how many instances of the API server pod will run.
// +optional
// +kubebuilder:validation:Minimum=0
// +kubebuilder:validation:Maximum=2147483647
Replicas *int32 `json:"replicas,omitempty"`

// MinReadySeconds is the minimum number of seconds for which a newly created Deployment pod should
// be ready without any of its container crashing, for it to be considered available.
// If specified, this overrides any minReadySeconds value that may be set on the API server Deployment.
Expand All @@ -189,6 +198,25 @@ type APIServerDeploymentSpec struct {
// Template describes the API server Deployment pod that will be created.
// +optional
Template *APIServerDeploymentPodTemplateSpec `json:"template,omitempty"`

// The deployment strategy to use to replace existing pods with new ones.
// +optional
// +patchStrategy=retainKeys
Strategy *APIServerDeploymentStrategy `json:"strategy,omitempty" patchStrategy:"retainKeys" protobuf:"bytes,4,opt,name=strategy"`
}

// TyphaDeploymentStrategy describes how to replace existing pods with new ones. Only RollingUpdate is supported
// at this time so the Type field is not exposed.
type APIServerDeploymentStrategy struct {
// Rolling update config params. Present only if DeploymentStrategyType =
// RollingUpdate.
// to be.
// +optional
RollingUpdate *appsv1.RollingUpdateDeployment `json:"rollingUpdate,omitempty" protobuf:"bytes,2,opt,name=rollingUpdate"`

// Type of deployment. Can be "Recreate" or "RollingUpdate". Default is RollingUpdate.
// +optional
Type *appsv1.DeploymentStrategyType `json:"type,omitempty" protobuf:"bytes,1,opt,name=type,casttype=DeploymentStrategyType"`
}

func (c *APIServerDeployment) GetMetadata() *Metadata {
Expand Down Expand Up @@ -308,10 +336,5 @@ func (c *APIServerDeployment) GetDeploymentStrategy() *appsv1.DeploymentStrategy
}

func (c *APIServerDeployment) GetPriorityClassName() string {
if c.Spec.Template != nil {
if c.Spec.Template.Spec != nil {
return c.Spec.Template.Spec.PriorityClassName
}
}
return ""
}
3 changes: 0 additions & 3 deletions api/v1/installation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@ type InstallationSpec struct {
// ComponentResources or TyphaAffinity, then these overrides take precedence.
TyphaDeployment *TyphaDeployment `json:"typhaDeployment,omitempty"`

// CalicoApiserverDeployment configures the apiserver Deployment.
APIServerDeployment *APIServerDeployment `json:"apiserverDeployment,omitempty"`

// Deprecated. The CalicoWindowsUpgradeDaemonSet is deprecated and will be removed from the API in the future.
// CalicoWindowsUpgradeDaemonSet configures the calico-windows-upgrade DaemonSet.
CalicoWindowsUpgradeDaemonSet *CalicoWindowsUpgradeDaemonSet `json:"calicoWindowsUpgradeDaemonSet,omitempty"`
Expand Down
9 changes: 0 additions & 9 deletions pkg/controller/installation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
operatorv1 "github.com/tigera/operator/api/v1"
"github.com/tigera/operator/pkg/common"
"github.com/tigera/operator/pkg/common/validation"
apiserver "github.com/tigera/operator/pkg/common/validation/apiserver"
node "github.com/tigera/operator/pkg/common/validation/calico-node"
csinodedriver "github.com/tigera/operator/pkg/common/validation/csi-node-driver"
kubecontrollers "github.com/tigera/operator/pkg/common/validation/kube-controllers"
Expand Down Expand Up @@ -375,14 +374,6 @@ func validateCustomResource(instance *operatorv1.Installation) error {
}
}

// Verify the APIServerDeployment overrides, if specified, is valid.
if deploy := instance.Spec.APIServerDeployment; deploy != nil {
err := validation.ValidateReplicatedPodResourceOverrides(deploy, apiserver.ValidateAPIServerDeploymentContainer, apiserver.ValidateAPIServerDeploymentInitContainer)
if err != nil {
return fmt.Errorf("Installation spec.APIServerDeployment is not valid: %w", err)
}
}

// Verify the CSINodeDriverDaemonSet overrides, if specified, is valid.
if ds := instance.Spec.CSINodeDriverDaemonSet; ds != nil {
err := validation.ValidateReplicatedPodResourceOverrides(ds, csinodedriver.ValidateCSINodeDriverDaemonSetContainer, validation.NoContainersDefined)
Expand Down
33 changes: 0 additions & 33 deletions pkg/controller/installation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -959,39 +959,6 @@ var _ = Describe("Installation validation tests", func() {
Expect(err).To(HaveOccurred())
})
})

Describe("validate APIServerDeployment", func() {
It("should return nil when it is empty", func() {
instance.Spec.APIServerDeployment = &operator.APIServerDeployment{}
err := validateCustomResource(instance)
Expect(err).NotTo(HaveOccurred())
})

It("should return an error if it is invalid", func() {
instance.Spec.APIServerDeployment = &operator.APIServerDeployment{
Metadata: &operator.Metadata{
Labels: map[string]string{
"NoUppercaseOrSpecialCharsLike=Equals": "b",
"WowNoUppercaseOrSpecialCharsLike=Equals": "b",
},
Annotations: map[string]string{
"AnnotNoUppercaseOrSpecialCharsLike=Equals": "bar",
},
},
}
err := validateCustomResource(instance)
Expect(err).To(HaveOccurred())

var invalidMinReadySeconds int32 = -1
instance.Spec.APIServerDeployment = &operator.APIServerDeployment{
Spec: &operator.APIServerDeploymentSpec{
MinReadySeconds: &invalidMinReadySeconds,
},
}
err = validateCustomResource(instance)
Expect(err).To(HaveOccurred())
})
})
Describe("validate Windows configuration", func() {
BeforeEach(func() {
winDpHNS := operator.WindowsDataplaneHNS
Expand Down
98 changes: 0 additions & 98 deletions pkg/controller/utils/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,6 @@ func OverrideInstallationSpec(cfg, override operatorv1.InstallationSpec) operato
inst.TyphaDeployment = mergeTyphaDeployment(inst.TyphaDeployment, override.TyphaDeployment)
}

switch compareFields(inst.APIServerDeployment, override.APIServerDeployment) {
case BOnlySet:
inst.APIServerDeployment = override.APIServerDeployment.DeepCopy()
case Different:
inst.APIServerDeployment = mergeAPIServerDeployment(inst.APIServerDeployment, override.APIServerDeployment)
}

switch compareFields(inst.CalicoWindowsUpgradeDaemonSet, override.CalicoWindowsUpgradeDaemonSet) {
case BOnlySet:
inst.CalicoWindowsUpgradeDaemonSet = override.CalicoWindowsUpgradeDaemonSet.DeepCopy()
Expand Down Expand Up @@ -777,97 +770,6 @@ func mergeTyphaDeployment(cfg, override *operatorv1.TyphaDeployment) *operatorv1
return out
}

func mergeAPIServerDeployment(cfg, override *operatorv1.APIServerDeployment) *operatorv1.APIServerDeployment {
out := cfg.DeepCopy()

switch compareFields(out.Metadata, override.Metadata) {
case BOnlySet:
out.Metadata = override.Metadata.DeepCopy()
case Different:
out.Metadata = mergeMetadata(out.Metadata, override.Metadata)
}

mergePodSpec := func(cfg, override *operatorv1.APIServerDeploymentPodSpec) *operatorv1.APIServerDeploymentPodSpec {
out := cfg.DeepCopy()

switch compareFields(out.InitContainers, override.InitContainers) {
case BOnlySet, Different:
out.InitContainers = make([]operatorv1.APIServerDeploymentInitContainer, len(override.Containers))
copy(out.InitContainers, override.InitContainers)
}

switch compareFields(out.Containers, override.Containers) {
case BOnlySet, Different:
out.Containers = make([]operatorv1.APIServerDeploymentContainer, len(override.Containers))
copy(out.Containers, override.Containers)
}

switch compareFields(out.Affinity, override.Affinity) {
case BOnlySet, Different:
out.Affinity = override.Affinity
}

switch compareFields(out.NodeSelector, override.NodeSelector) {
case BOnlySet, Different:
out.NodeSelector = override.NodeSelector
}

switch compareFields(out.TopologySpreadConstraints, override.TopologySpreadConstraints) {
case BOnlySet, Different:
out.TopologySpreadConstraints = override.TopologySpreadConstraints
}

switch compareFields(out.Tolerations, override.Tolerations) {
case BOnlySet, Different:
out.Tolerations = override.Tolerations
}
return out
}
mergeTemplateSpec := func(cfg, override *operatorv1.APIServerDeploymentPodTemplateSpec) *operatorv1.APIServerDeploymentPodTemplateSpec {
out := cfg.DeepCopy()

switch compareFields(out.Metadata, override.Metadata) {
case BOnlySet:
out.Metadata = override.Metadata.DeepCopy()
case Different:
out.Metadata = mergeMetadata(out.Metadata, override.Metadata)
}

switch compareFields(out.Spec, override.Spec) {
case BOnlySet:
out.Spec = override.Spec.DeepCopy()
case Different:
out.Spec = mergePodSpec(out.Spec, override.Spec)
}

return out
}
mergeSpec := func(cfg, override *operatorv1.APIServerDeploymentSpec) *operatorv1.APIServerDeploymentSpec {
out := cfg.DeepCopy()

switch compareFields(out.MinReadySeconds, override.MinReadySeconds) {
case BOnlySet, Different:
out.MinReadySeconds = override.MinReadySeconds
}

switch compareFields(out.Template, override.Template) {
case BOnlySet:
out.Template = override.Template.DeepCopy()
case Different:
out.Template = mergeTemplateSpec(out.Template, override.Template)
}
return out
}

switch compareFields(out.Spec, override.Spec) {
case BOnlySet:
out.Spec = override.Spec.DeepCopy()
case Different:
out.Spec = mergeSpec(out.Spec, override.Spec)
}
return out
}

func mergeCalicoWindowsUpgradeDaemonSet(cfg, override *operatorv1.CalicoWindowsUpgradeDaemonSet) *operatorv1.CalicoWindowsUpgradeDaemonSet {
out := cfg.DeepCopy()

Expand Down
Loading

0 comments on commit 7bc4d24

Please sign in to comment.