etcd-io / etcd-io/etcd-operator
Refactor podSpec and containers
- Dominant language
- Go
- Stars
- 164
- Forks
- 72
- Avg merge
- 22h 46m
- Merged PRs (30d)
- 34
Description
Currently containers are defined within the `podSpec` which is is functional at the moment but there are some advantages to definition container specs outside of the podSpec.
- Faster and more targeted unit tests for code specific to container unit spec.
- Easier to write code if there are multiple containers required
- Use of custom type to allow adding and testing functions in an isolated manner, particularly when expanding the configuration options available through the CRD
- Reduce need for loop based lookups to manage container configuration
Below is a rough snippet from a scratch file to demonstrate how containers might be managed using the proposed approach.
```go
const EtcdContainerName = "etcd"
type EtcdPodSpec corev1.PodSpec
func (ps *EtcdPodSpec) createOrUpdateContainer(c corev1.Container) {
containerExists := false
if len(ps.Containers) == 0 {
ps.Containers = append(ps.Containers, c)
} else {
for i := range ps.Containers {
if ps.Containers[i].Name == c.Name {
ps.Containers[i] = c
containerExists = true
break
}
}
if !containerExists {
ps.Containers = append(ps.Containers, c)
}
}
}
podSpec := EtcdPodSpec{
Containers: []corev1.Container{},
}
etcdContainer := corev1.Container{
Name: EtcdContainerName,
}
podSpec.createOrUpdateContainer(etcdContainer)
```
Contributor guide
Assessment
This issue has not been assessed yet.