Skip to content

Commit

Permalink
Add image field to EtcdCluster spec (#63)
Browse files Browse the repository at this point in the history
Added `image` field to PodSpec of EtcdCluster spec, added tests and implemented
defaulting webhook.
fixes #48
  • Loading branch information
sircthulhu committed Mar 26, 2024
1 parent 82ac0f1 commit 16bc8bc
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 1 deletion.
Empty file removed api/.gitkeep
Empty file.
5 changes: 5 additions & 0 deletions api/v1alpha1/etcdcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const defaultEtcdImage = "quay.io/coreos/etcd:v3.5.12"

// EtcdClusterSpec defines the desired state of EtcdCluster
type EtcdClusterSpec struct {
// Replicas is the count of etcd instances in cluster.
Expand Down Expand Up @@ -103,6 +105,9 @@ type EmbeddedObjectMetadata 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"`
// ImagePullPolicy describes a policy for if/when to pull a container image
// +kubebuilder:default:=IfNotPresent
// +optional
Expand Down
3 changes: 3 additions & 0 deletions api/v1alpha1/etcdcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ 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.PodSpec.Image) == 0 {
r.Spec.PodSpec.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
5 changes: 5 additions & 0 deletions api/v1alpha1/etcdcluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var _ = Describe("EtcdCluster Webhook", func() {
It("Should fill in the default value if a required field is empty", func() {
etcdCluster := &EtcdCluster{}
etcdCluster.Default()
gomega.Expect(etcdCluster.Spec.PodSpec.Image).To(gomega.Equal(defaultEtcdImage))
gomega.Expect(etcdCluster.Spec.Replicas).To(gomega.BeNil(), "User should have an opportunity to create cluster with 0 replicas")
gomega.Expect(etcdCluster.Spec.Storage.EmptyDir).To(gomega.BeNil())
storage := etcdCluster.Spec.Storage.VolumeClaimTemplate.Spec.Resources.Requests.Storage()
Expand All @@ -43,6 +44,9 @@ var _ = Describe("EtcdCluster Webhook", func() {
etcdCluster := &EtcdCluster{
Spec: EtcdClusterSpec{
Replicas: ptr.To(int32(5)),
PodSpec: PodSpec{
Image: "myregistry.local/etcd:v1.1.1",
},
Storage: StorageSpec{
VolumeClaimTemplate: EmbeddedPersistentVolumeClaim{
Spec: corev1.PersistentVolumeClaimSpec{
Expand All @@ -60,6 +64,7 @@ var _ = Describe("EtcdCluster Webhook", func() {
}
etcdCluster.Default()
gomega.Expect(*etcdCluster.Spec.Replicas).To(gomega.Equal(int32(5)))
gomega.Expect(etcdCluster.Spec.PodSpec.Image).To(gomega.Equal("myregistry.local/etcd:v1.1.1"))
gomega.Expect(etcdCluster.Spec.Storage.EmptyDir).To(gomega.BeNil())
storage := etcdCluster.Spec.Storage.VolumeClaimTemplate.Spec.Resources.Requests.Storage()
if gomega.Expect(storage).NotTo(gomega.BeNil()) {
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/etcd.aenix.io_etcdclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,9 @@ spec:
- name
type: object
type: array
image:
description: Image is the etcd container image name
type: string
imagePullPolicy:
default: IfNotPresent
description: ImagePullPolicy describes a policy for if/when to
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/factory/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func CreateOrUpdateStatefulSet(
Containers: []corev1.Container{
{
Name: "etcd",
Image: "quay.io/coreos/etcd:v3.5.12",
Image: cluster.Spec.PodSpec.Image,
ImagePullPolicy: cluster.Spec.PodSpec.ImagePullPolicy,
Command: generateEtcdCommand(cluster),
Ports: []corev1.ContainerPort{
Expand Down

0 comments on commit 16bc8bc

Please sign in to comment.