Commit c7bdb4fa authored by Edmund Ochieng's avatar Edmund Ochieng

Merge branch '15-more-selectors-editors' into 'master'

Add more Editors and Selectors for Helm facility

Closes #15

See merge request !15
parents 9d14009e 39524b60
Pipeline #193529443 passed with stages
in 20 minutes and 7 seconds
This diff is collapsed.
package helm_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"gitlab.com/gitlab-org/gl-openshift/gitlab-operator/controllers/helm"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
)
var _ = Describe("Template", func() {
It("Must return all objects when the selector matches all", func() {
template, _, err := loadTemplate()
Expect(err).To(BeNil())
selectedObjects, err := template.GetObjects(helm.TrueSelector)
Expect(err).To(BeNil())
Expect(selectedObjects).To(Equal(template.Objects()))
})
It("Must only select ConfigMaps that match the expected label", func() {
template, _, err := loadTemplate()
Expect(err).To(BeNil())
selector := func(configMap *corev1.ConfigMap) bool {
return configMap.ObjectMeta.Labels["app.kubernetes.io/managed-by"] == "Helm"
}
selectedObjects, err := template.GetObjects(helm.NewConfigMapSelector(selector))
Expect(err).To(BeNil())
Expect(selectedObjects).To(HaveLen(1))
})
It("Must delete no object when the selector does not match any", func() {
template, _, err := loadTemplate()
Expect(err).To(BeNil())
deletedCount, err := template.DeleteObjects(helm.FalseSelector)
Expect(err).To(BeNil())
Expect(deletedCount).To(BeZero())
})
It("Must delete the Ingress objects", func() {
template, _, err := loadTemplate()
Expect(err).To(BeNil())
initialLength := len(template.Objects())
Expect(initialLength).NotTo(BeZero())
ingresses, err := template.GetObjects(helm.IngressSelector)
Expect(err).To(BeNil())
Expect(ingresses).ToNot(BeEmpty())
deletedCount, err := template.DeleteObjects(helm.IngressSelector)
Expect(err).To(BeNil())
Expect(deletedCount).ToNot(BeZero())
Expect(len(template.Objects())).To(Equal(initialLength - deletedCount))
})
It("Must edit Deployment objects", func() {
template, _, err := loadTemplate()
Expect(err).To(BeNil())
initialLength := len(template.Objects())
Expect(initialLength).NotTo(BeZero())
deployments, err := template.GetObjects(helm.DeploymentSelector)
Expect(err).To(BeNil())
Expect(deployments).ToNot(BeEmpty())
editedCount, err := template.EditObjects(helm.NewDeploymentEditor(
func(d *appsv1.Deployment) error {
d.Spec.Paused = true
if d.ObjectMeta.Annotations == nil {
d.ObjectMeta.Annotations = map[string]string{}
}
d.ObjectMeta.Annotations["gitlab.com/foo"] = "bar"
return nil
}),
)
Expect(err).To(BeNil())
Expect(editedCount).NotTo(BeZero())
for _, o := range deployments {
deployment, ok := (*o).(*appsv1.Deployment)
Expect(ok).To(BeTrue())
Expect(deployment.Spec.Paused).To(BeTrue())
foo, ok := deployment.ObjectMeta.Annotations["gitlab.com/foo"]
Expect(ok).To(BeTrue())
Expect(foo).To(Equal("bar"))
}
})
})
......@@ -199,16 +199,18 @@ func (t *Template) ReplaceObject(selector ObjectSelector, object runtime.Object)
return nil, nil
}
// EditObjects selects all objects that match the selector and pass them to the editor.
func (t *Template) EditObjects(selector ObjectSelector, editor ObjectEditor) (int, error) {
// EditObjects edits all objects that the editor can handle.
func (t *Template) EditObjects(editor ObjectEditor) (int, error) {
count := 0
for i := 0; i < len(t.objects); i++ {
if selector(*t.objects[i]) {
if err := editor(t.objects[i]); err != nil {
return count, err
err := editor(t.objects[i])
if err != nil {
if IsTypeMistmatchError(err) {
continue
}
count++
return count, err
}
count++
}
return count, nil
}
......@@ -7,18 +7,42 @@ import (
. "github.com/onsi/gomega"
"gitlab.com/gitlab-org/gl-openshift/gitlab-operator/controllers/helm"
apps "k8s.io/api/apps/v1"
)
func loadTemplate() (*helm.Template, []error, error) {
chartPath := os.Getenv("HELM_CHART")
if chartPath == "" {
chartPath = "testdata/chart/test"
}
valuesPath := os.Getenv("HELM_CHART_VALUES")
if valuesPath == "" {
valuesPath = "testdata/chart/values.yaml"
}
template := helm.NewTemplate(chartPath)
values := helm.EmptyValues()
values.AddFromFile(valuesPath)
warnings, err := template.Load(values)
return template, warnings, err
}
var _ = Describe("Template", func() {
When("Initialized", func() {
template := helm.NewTemplate("foo")
helmNamespace := os.Getenv("HELM_NAMESPACE")
if helmNamespace == "" {
helmNamespace = "default"
}
It("Must be empty and use default settings", func() {
Expect(template.ChartName()).To(Equal("foo"))
// Expect(template.Namespace()).To(Equal("default"))
Expect(template.Namespace()).To(Equal(helmNamespace))
Expect(template.ReleaseName()).To(Equal(helm.DefaultReleaseName))
Expect(template.HooksDisabled()).To(BeFalse())
Expect(template.Objects()).To(BeEmpty())
......@@ -28,27 +52,6 @@ var _ = Describe("Template", func() {
When("Uses a chart", func() {
loadTemplate := func() (*helm.Template, []error, error) {
chartPath := os.Getenv("HELM_CHART")
if chartPath == "" {
chartPath = "testdata/chart/test"
}
valuesPath := os.Getenv("HELM_CHART_VALUES")
if valuesPath == "" {
valuesPath = "testdata/chart/values.yaml"
}
template := helm.NewTemplate(chartPath)
values := helm.EmptyValues()
values.AddFromFile(valuesPath)
warnings, err := template.Load(values)
return template, warnings, err
}
It("Must render the template and parse objects", func() {
template, warnings, err := loadTemplate()
......@@ -57,74 +60,5 @@ var _ = Describe("Template", func() {
Expect(template.Objects()).NotTo(BeEmpty())
})
It("Must return all objects when the selector matches all", func() {
template, _, err := loadTemplate()
Expect(err).To(BeNil())
selectedObjects, err := template.GetObjects(helm.AnySelector)
Expect(err).To(BeNil())
Expect(selectedObjects).To(Equal(template.Objects()))
})
It("Must delete no object when the selector does not match any", func() {
template, _, err := loadTemplate()
Expect(err).To(BeNil())
deletedCount, err := template.DeleteObjects(helm.NoneSelector)
Expect(err).To(BeNil())
Expect(deletedCount).To(BeZero())
})
It("Must delete the Ingress objects", func() {
template, _, err := loadTemplate()
Expect(err).To(BeNil())
initialLength := len(template.Objects())
Expect(initialLength).NotTo(BeZero())
ingresses, err := template.GetObjects(helm.IngressSelector)
Expect(err).To(BeNil())
Expect(ingresses).ToNot(BeEmpty())
deletedCount, err := template.DeleteObjects(helm.IngressSelector)
Expect(err).To(BeNil())
Expect(deletedCount).ToNot(BeZero())
Expect(len(template.Objects())).To(Equal(initialLength - deletedCount))
})
It("Must edit Deployment objects", func() {
template, _, err := loadTemplate()
Expect(err).To(BeNil())
initialLength := len(template.Objects())
Expect(initialLength).NotTo(BeZero())
deployments, err := template.GetObjects(helm.DeploymentSelector)
Expect(err).To(BeNil())
Expect(deployments).ToNot(BeEmpty())
editedCount, err := template.EditObjects(helm.DeploymentSelector,
helm.NewDeploymentEditor(func(d *apps.Deployment) error {
d.Spec.Paused = true
if d.ObjectMeta.Annotations == nil {
d.ObjectMeta.Annotations = map[string]string{}
}
d.ObjectMeta.Annotations["gitlab.com/foo"] = "bar"
return nil
}))
Expect(err).To(BeNil())
Expect(editedCount).NotTo(BeZero())
for _, o := range deployments {
deployment, ok := (*o).(*apps.Deployment)
Expect(ok).To(BeTrue())
Expect(deployment.Spec.Paused).To(BeTrue())
foo, ok := deployment.ObjectMeta.Annotations["gitlab.com/foo"]
Expect(ok).To(BeTrue())
Expect(foo).To(Equal("bar"))
}
})
})
})
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "test.fullname" . }}
labels:
{{- include "test.labels" . | nindent 4 }}
data:
foo: |
FOO BAR
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment