Skip to content

Commit

Permalink
enh: use std slice index func (#121)
Browse files Browse the repository at this point in the history
Simplify code with using std slices pkg
  • Loading branch information
ilya-hontarau authored Apr 4, 2024
1 parent e3cdb40 commit 770379b
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions internal/controller/factory/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,10 @@ func generateContainers(cluster *etcdaenixiov1alpha1.EtcdCluster) []corev1.Conta
{Name: "client", ContainerPort: 2379},
})
clusterStateConfigMapName := GetClusterStateConfigMapName(cluster)
ok := false
for _, env := range c.EnvFrom {
if env.ConfigMapRef != nil && env.ConfigMapRef.LocalObjectReference.Name == clusterStateConfigMapName {
ok = true
break
}
}
if !ok {
envIdx := slices.IndexFunc(c.EnvFrom, func(env corev1.EnvFromSource) bool {
return env.ConfigMapRef != nil && env.ConfigMapRef.LocalObjectReference.Name == clusterStateConfigMapName
})
if envIdx == -1 {
c.EnvFrom = append(c.EnvFrom, corev1.EnvFromSource{
ConfigMapRef: &corev1.ConfigMapEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Expand All @@ -213,21 +209,17 @@ func generateContainers(cluster *etcdaenixiov1alpha1.EtcdCluster) []corev1.Conta
c.ReadinessProbe = getReadinessProbe(c.ReadinessProbe)
c.Env = mergeEnvs(c.Env, podEnv)

ok = false
for idx, v := range c.VolumeMounts {
if v.Name == "data" {
c.VolumeMounts[idx].ReadOnly = false
c.VolumeMounts[idx].MountPath = "/var/run/etcd"

ok = true
break
}
}
if !ok {
mountIdx := slices.IndexFunc(c.VolumeMounts, func(mount corev1.VolumeMount) bool {
return mount.Name == "data"
})
if mountIdx == -1 {
c.VolumeMounts = append(c.VolumeMounts, corev1.VolumeMount{
Name: "data",
MountPath: "/var/run/etcd",
})
} else {
c.VolumeMounts[mountIdx].ReadOnly = false
c.VolumeMounts[mountIdx].MountPath = "/var/run/etcd"
}
}

Expand Down

0 comments on commit 770379b

Please sign in to comment.