Skip to content

Commit

Permalink
Add dev annotations both, to the pod and the deployment metadata (#892)
Browse files Browse the repository at this point in the history
* Add dev annotations both, to the pod and the deployment metadata

Signed-off-by: Pablo Chico de Guzman <pchico83@gmail.com>

* Add unit tests

Signed-off-by: Pablo Chico de Guzman <pchico83@gmail.com>
  • Loading branch information
pchico83 committed Jun 4, 2020
1 parent 1944ebf commit 961a294
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions pkg/k8s/deployments/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ func TranslateDevModeOff(d *appsv1.Deployment) (*appsv1.Deployment, error) {
if err := deleteUserAnnotations(annotations); err != nil {
return nil, err
}
annotations = d.GetObjectMeta().GetAnnotations()
if err := deleteUserAnnotations(annotations); err != nil {
return nil, err
}
delete(annotations, okLabels.TranslationAnnotation)
delete(annotations, model.OktetoRestartAnnotation)
d.Spec.Template.GetObjectMeta().SetAnnotations(annotations)
Expand Down
7 changes: 4 additions & 3 deletions pkg/k8s/deployments/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ func translate(t *model.Translation, ns *apiv1.Namespace, c *kubernetes.Clientse
}

func commonTranslation(t *model.Translation) {
TranslatePodUserAnnotations(t.Deployment.Spec.Template.GetObjectMeta(), t.Annotations)
TranslateUserAnnotations(t.Deployment.GetObjectMeta(), t.Annotations)
TranslateUserAnnotations(t.Deployment.Spec.Template.GetObjectMeta(), t.Annotations)
setAnnotation(t.Deployment.GetObjectMeta(), oktetoVersionAnnotation, okLabels.Version)
setLabel(t.Deployment.GetObjectMeta(), okLabels.DevLabel, "true")

Expand Down Expand Up @@ -145,8 +146,8 @@ func GetDevContainer(spec *apiv1.PodSpec, name string) *apiv1.Container {
return nil
}

//TranslatePodUserAnnotations translates the user provided annotations of pod
func TranslatePodUserAnnotations(o metav1.Object, annotations map[string]string) {
//TranslateUserAnnotations sets the user provided annotations
func TranslateUserAnnotations(o metav1.Object, annotations map[string]string) {
for key, value := range annotations {
setAnnotation(o, key, value)
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/k8s/deployments/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ secrets:
- %s:/remote
persistentVolume:
enabled: true
annotations:
key: value
resources:
limits:
cpu: 2
Expand All @@ -70,6 +72,8 @@ resources:
amd.com/gpu: 1
services:
- name: worker
annotations:
key: value
container: dev
image: worker:latest
command: ["./run_worker.sh"]
Expand All @@ -89,6 +93,7 @@ services:
Version: model.TranslationVersion,
Deployment: d1,
Rules: []*model.TranslationRule{rule1},
Annotations: map[string]string{"key": "value"},
}
err = translate(tr1, nil, nil)
if err != nil {
Expand Down Expand Up @@ -264,6 +269,12 @@ services:
if string(marshalled1) != string(marshalled1OK) {
t.Fatalf("Wrong d1 generation.\nActual %+v, \nExpected %+v", string(marshalled1), string(marshalled1OK))
}
if d1.Annotations["key"] != "value" {
t.Fatalf("Wrong d1 annotations: '%s'", d1.Annotations["key"])
}
if d1.Spec.Template.Annotations["key"] != "value" {
t.Fatalf("Wrong d1 pod annotations: '%s'", d1.Spec.Template.Annotations["key"])
}

d1Down, err := TranslateDevModeOff(d1)
if err != nil {
Expand All @@ -275,6 +286,12 @@ services:
if string(marshalled1Down) != string(marshalled1Orig) {
t.Fatalf("Wrong d1 down.\nActual %+v, \nExpected %+v", string(marshalled1Down), string(marshalled1Orig))
}
if d1Down.Annotations["key"] != "" {
t.Fatalf("Wrong d1 annotations after down: '%s'", d1.Annotations["key"])
}
if d1Down.Spec.Template.Annotations["key"] != "" {
t.Fatalf("Wrong d1 pod annotations after down: '%s'", d1.Spec.Template.Annotations["key"])
}

dev2 := dev.Services[0]
d2 := dev2.GevSandbox()
Expand All @@ -285,6 +302,7 @@ services:
Version: model.TranslationVersion,
Deployment: d2,
Rules: []*model.TranslationRule{rule2},
Annotations: map[string]string{"key": "value"},
}
err = translate(tr2, nil, nil)
if err != nil {
Expand Down Expand Up @@ -353,6 +371,12 @@ services:
if string(marshalled2) != string(marshalled2OK) {
t.Fatalf("Wrong d2 generation.\nActual %s, \nExpected %s", string(marshalled2), string(marshalled2OK))
}
if d2.Annotations["key"] != "value" {
t.Fatalf("Wrong d2 annotations: '%s'", d2.Annotations["key"])
}
if d2.Spec.Template.Annotations["key"] != "value" {
t.Fatalf("Wrong d2 pod annotations: '%s'", d2.Spec.Template.Annotations["key"])
}

d2Down, err := TranslateDevModeOff(d2)
if err != nil {
Expand All @@ -364,6 +388,12 @@ services:
if string(marshalled2Down) != string(marshalled2Orig) {
t.Fatalf("Wrong d2 down.\nActual %+v, \nExpected %+v", string(marshalled2Down), string(marshalled2Orig))
}
if d2Down.Annotations["key"] != "" {
t.Fatalf("Wrong d2 annotations after down: '%s'", d2.Annotations["key"])
}
if d2Down.Spec.Template.Annotations["key"] != "" {
t.Fatalf("Wrong d2 pod annotations after down: '%s'", d2.Spec.Template.Annotations["key"])
}
}

func Test_translateWithoutVolumes(t *testing.T) {
Expand Down

0 comments on commit 961a294

Please sign in to comment.