Skip to content

Commit

Permalink
feat: Add missing pod spec (#123)
Browse files Browse the repository at this point in the history
Fixes: #107
  • Loading branch information
sergeyshevch authored Apr 6, 2024
1 parent 165e53e commit fdca0b9
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 20 deletions.
15 changes: 9 additions & 6 deletions api/v1alpha1/etcdcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type EtcdClusterSpec struct {
// PodTemplate defines the desired state of PodSpec for etcd members. If not specified, default values will be used.
PodTemplate PodTemplate `json:"podTemplate,omitempty"`
// PodDisruptionBudgetTemplate describes PDB resource to create for etcd cluster members. Nil to disable.
//+optional
// +optional
PodDisruptionBudgetTemplate *EmbeddedPodDisruptionBudget `json:"podDisruptionBudgetTemplate,omitempty"`
Storage StorageSpec `json:"storage"`
}
Expand Down Expand Up @@ -138,10 +138,6 @@ type PodTemplate struct {
// PodSpec defines the desired state of PodSpec for etcd members.
// +k8s:openapi-gen=true
type PodSpec struct {
// Image is the etcd container image name
// +optional
Image string `json:"image,omitempty"`

// Containers allows the user to add containers to the pod and change "etcd" container if such options are not
// available in the EtcdCluster custom resource.
// +optional
Expand All @@ -152,6 +148,13 @@ type PodSpec struct {
// see https://kubernetes.io/docs/concepts/containers/images/#referring-to-an-imagepullsecrets-on-a-pod
// +optional
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
// ServiceAccountName is the name of the ServiceAccount to use to run the etcd pods.
// +optional
ServiceAccountName string `json:"serviceAccountName,omitempty"`
// ReadinessGates is an optional list of conditions that must be true for the pod to be considered ready for
// traffic. A pod is considered ready when all of its containers are ready.
// +optional
ReadinessGates []corev1.PodReadinessGate `json:"readinessGates,omitempty"`
// Affinity sets the scheduling constraints for the pod.
// +optional
Affinity *corev1.Affinity `json:"affinity,omitempty"`
Expand Down Expand Up @@ -225,7 +228,7 @@ type EmbeddedPodDisruptionBudget struct {
EmbeddedObjectMetadata `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
// Spec defines the desired characteristics of a PDB.
// More info: https://kubernetes.io/docs/concepts/workloads/pods/disruptions/#pod-disruption-budgets
//+optional
// +optional
Spec PodDisruptionBudgetSpec `json:"spec"`
}

Expand Down
3 changes: 0 additions & 3 deletions api/v1alpha1/etcdcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ var _ webhook.Defaulter = &EtcdCluster{}
// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *EtcdCluster) Default() {
etcdclusterlog.Info("default", "name", r.Name)
if len(r.Spec.PodTemplate.Spec.Image) == 0 {
r.Spec.PodTemplate.Spec.Image = defaultEtcdImage
}
if r.Spec.Storage.EmptyDir == nil {
if len(r.Spec.Storage.VolumeClaimTemplate.Spec.AccessModes) == 0 {
r.Spec.Storage.VolumeClaimTemplate.Spec.AccessModes = []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce}
Expand Down
8 changes: 1 addition & 7 deletions api/v1alpha1/etcdcluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var _ = Describe("EtcdCluster Webhook", func() {
It("Should fill in the default value if a required field is empty", func() {
etcdCluster := &EtcdCluster{}
etcdCluster.Default()
Expect(etcdCluster.Spec.PodTemplate.Spec.Image).To(Equal(defaultEtcdImage))
Expect(etcdCluster.Spec.PodTemplate.Spec.Containers[0].Image).To(Equal(defaultEtcdImage))
Expect(etcdCluster.Spec.Replicas).To(BeNil(), "User should have an opportunity to create cluster with 0 replicas")
Expect(etcdCluster.Spec.Storage.EmptyDir).To(BeNil())
storage := etcdCluster.Spec.Storage.VolumeClaimTemplate.Spec.Resources.Requests.Storage()
Expand All @@ -46,11 +46,6 @@ var _ = Describe("EtcdCluster Webhook", func() {
etcdCluster := &EtcdCluster{
Spec: EtcdClusterSpec{
Replicas: ptr.To(int32(5)),
PodTemplate: PodTemplate{
Spec: PodSpec{
Image: "myregistry.local/etcd:v1.1.1",
},
},
PodDisruptionBudgetTemplate: &EmbeddedPodDisruptionBudget{
Spec: PodDisruptionBudgetSpec{
MaxUnavailable: ptr.To(intstr.FromInt32(int32(2))),
Expand All @@ -75,7 +70,6 @@ var _ = Describe("EtcdCluster Webhook", func() {
Expect(*etcdCluster.Spec.Replicas).To(Equal(int32(5)))
Expect(etcdCluster.Spec.PodDisruptionBudgetTemplate).NotTo(BeNil())
Expect(etcdCluster.Spec.PodDisruptionBudgetTemplate.Spec.MaxUnavailable.IntValue()).To(Equal(2))
Expect(etcdCluster.Spec.PodTemplate.Spec.Image).To(Equal("myregistry.local/etcd:v1.1.1"))
Expect(etcdCluster.Spec.Storage.EmptyDir).To(BeNil())
storage := etcdCluster.Spec.Storage.VolumeClaimTemplate.Spec.Resources.Requests.Storage()
if Expect(storage).NotTo(BeNil()) {
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions config/crd/bases/etcd.aenix.io_etcdclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2401,9 +2401,6 @@ spec:
- name
type: object
type: array
image:
description: Image is the etcd container image name
type: string
imagePullSecrets:
description: |-
ImagePullSecrets An optional list of references to secrets in the same namespace
Expand Down Expand Up @@ -2433,6 +2430,22 @@ spec:
description: PriorityClassName is the name of the PriorityClass
for this pod.
type: string
readinessGates:
description: |-
ReadinessGates is an optional list of conditions that must be true for the pod to be considered ready for
traffic. A pod is considered ready when all of its containers are ready.
items:
description: PodReadinessGate contains the reference to
a pod condition
properties:
conditionType:
description: ConditionType refers to a condition in
the pod's condition list with matching type.
type: string
required:
- conditionType
type: object
type: array
runtimeClassName:
description: RuntimeClassName refers to a RuntimeClass object
in the node.k8s.io group, which should be used to run this
Expand Down Expand Up @@ -2618,6 +2631,10 @@ spec:
type: string
type: object
type: object
serviceAccountName:
description: ServiceAccountName is the name of the ServiceAccount
to use to run the etcd pods.
type: string
terminationGracePeriodSeconds:
description: TerminationGracePeriodSeconds is the time to
wait before forceful pod shutdown.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
k8s.io/api v0.29.0
k8s.io/apimachinery v0.29.0
k8s.io/client-go v0.29.0
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/controller-runtime v0.17.0
)

Expand Down Expand Up @@ -66,7 +67,6 @@ require (
k8s.io/component-base v0.29.0 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions internal/controller/factory/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func CreateOrUpdateStatefulSet(
PriorityClassName: cluster.Spec.PodTemplate.Spec.PriorityClassName,
TerminationGracePeriodSeconds: cluster.Spec.PodTemplate.Spec.TerminationGracePeriodSeconds,
SchedulerName: cluster.Spec.PodTemplate.Spec.SchedulerName,
ServiceAccountName: cluster.Spec.PodTemplate.Spec.ServiceAccountName,
ReadinessGates: cluster.Spec.PodTemplate.Spec.ReadinessGates,
RuntimeClassName: cluster.Spec.PodTemplate.Spec.RuntimeClassName,
},
},
Expand Down
15 changes: 15 additions & 0 deletions internal/controller/factory/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ var _ = Describe("CreateOrUpdateStatefulSet handler", func() {
},
},
Spec: etcdaenixiov1alpha1.PodSpec{
ServiceAccountName: "etcd-operator",
ReadinessGates: []v1.PodReadinessGate{
{
// Some custom readiness gate
ConditionType: "target-health.elbv2.k8s.aws",
},
},
Containers: []v1.Container{
{
Name: "etcd",
Expand Down Expand Up @@ -137,6 +144,14 @@ var _ = Describe("CreateOrUpdateStatefulSet handler", func() {
By("Checking the extraArgs")
Expect(sts.Spec.Template.Spec.Containers[0].Command).To(Equal(generateEtcdCommand()))

By("Checking the readinessGates", func() {
Expect(sts.Spec.Template.Spec.ReadinessGates).To(Equal(etcdcluster.Spec.PodTemplate.Spec.ReadinessGates))
})

By("Checking the serviceAccountName", func() {
Expect(sts.Spec.Template.Spec.ServiceAccountName).To(Equal(etcdcluster.Spec.PodTemplate.Spec.ServiceAccountName))
})

By("Checking the default startup probe", func() {
Expect(sts.Spec.Template.Spec.Containers[0].StartupProbe).To(Equal(&v1.Probe{
ProbeHandler: v1.ProbeHandler{
Expand Down

0 comments on commit fdca0b9

Please sign in to comment.