Skip to content

Commit

Permalink
Merge pull request #278 from jordigilh/propagate_workload_annotations
Browse files Browse the repository at this point in the history
Propagate edgeworkload annotations
  • Loading branch information
openshift-ci[bot] authored Jul 7, 2022
2 parents 219ebae + 5491fa9 commit b925a42
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 13 deletions.
11 changes: 0 additions & 11 deletions internal/common/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,3 @@ func IsSelectorLabel(label string) bool {
func CreateSelectorLabel(label string) string {
return SelectorLabelPrefix + label
}

// GetPodmanLabels filter all the labels of the EdgeWorkload CR starting with prefix "podman/"
func GetPodmanLabels(workloadLabels map[string]string) map[string]string {
labels := map[string]string{}
for key, value := range workloadLabels {
if strings.HasPrefix(key, "podman/") {
labels[key[7:]] = value
}
}
return labels
}
14 changes: 14 additions & 0 deletions internal/common/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,17 @@ func ExtractInfoFromEnv(env []corev1.EnvVar, configmapMap MapType, ref GetRefEnv
}
}
}

// FilterByPodmanPrefix does two things:
// first it filters the keys that start with prefix "podman/"
// And secondly, of those keys that have the "podman/" prefix, it uses the remaining part of the key as the new key in the returning map with the same value as in the original map
// e.g. 'podman/this-is-my-key:1' is stored in the returning map as 'this-is-my-key:1'
func FilterByPodmanPrefix(keyValues map[string]string) map[string]string {
ret := map[string]string{}
for key, value := range keyValues {
if strings.HasPrefix(key, "podman/") {
ret[key[7:]] = value
}
}
return ret
}
27 changes: 27 additions & 0 deletions internal/common/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,31 @@ var _ = Describe("Utils", func() {
Expect(ok).To(BeTrue())
})
})
Context("FilterByPodmanPrefix", func() {
It("should return empty with an empty map", func() {
// given
aMap := map[string]string{}
// when
ret := utils.FilterByPodmanPrefix(aMap)
// then
Expect(ret).To(BeEmpty())

})
It("should return no keys when none match the 'podman/' prefix", func() {
// given
aMap := map[string]string{"noMatch": "1", "podman-/this-key": "1"}
// when
ret := utils.FilterByPodmanPrefix(aMap)
// then
Expect(ret).To(BeEmpty())
})
It("should return a key value pair with the 'podman/' removed when a match is found", func() {
// given
aMap := map[string]string{"noMatch": "1", "podman/pod_label": "1"}
// when
ret := utils.FilterByPodmanPrefix(aMap)
// then
Expect(ret).To(BeEquivalentTo(map[string]string{"pod_label": "1"}))
})
})
})
5 changes: 3 additions & 2 deletions internal/edgeapi/backend/k8s/configuration-assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"k8s.io/client-go/tools/record"

"github.com/project-flotta/flotta-operator/api/v1alpha1"
"github.com/project-flotta/flotta-operator/internal/common/labels"
"github.com/project-flotta/flotta-operator/internal/common/storage"
"github.com/project-flotta/flotta-operator/internal/common/utils"
"github.com/project-flotta/flotta-operator/internal/edgeapi/configmaps"
"github.com/project-flotta/flotta-operator/internal/edgeapi/devicemetrics"
"github.com/project-flotta/flotta-operator/internal/edgeapi/images"
Expand Down Expand Up @@ -302,7 +302,8 @@ func (a *ConfigurationAssembler) toWorkloadList(ctx context.Context, logger *zap
workload := models.Workload{
Name: edgeworkload.Name,
Namespace: edgeworkload.Namespace,
Labels: labels.GetPodmanLabels(edgeworkload.Labels),
Annotations: utils.FilterByPodmanPrefix(edgeworkload.Annotations),
Labels: utils.FilterByPodmanPrefix(edgeworkload.Labels),
Specification: string(podSpec),
Data: data,
LogCollection: spec.LogCollection,
Expand Down
79 changes: 79 additions & 0 deletions internal/edgeapi/yggdrasil/yggdrasil_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2051,6 +2051,85 @@ var _ = Describe("Yggdrasil", func() {
Expect(config.Configuration.Storage).To(BeNil())
})
})
Context("workload annotations and labels", func() {
It("should propagate Labels with prefix 'podman/'", func() {
// given
deviceName := "foo"
device := getDevice(deviceName)
device.Status.Workloads = []v1alpha1.Workload{{Name: "workload1"}}

repositoryMock.EXPECT().
GetEdgeDevice(gomock.Any(), deviceName, testNamespace).
Return(device, nil).
Times(1)

workloadData := &v1alpha1.EdgeWorkload{
ObjectMeta: v1.ObjectMeta{
Name: "workload1",
Namespace: "default",
Labels: map[string]string{"podman/label1": "1"},
},
Spec: v1alpha1.EdgeWorkloadSpec{
Type: "pod",
Pod: v1alpha1.Pod{},
}}
configMap.EXPECT().Fetch(gomock.Any(), gomock.Any(), gomock.Any()).Return(models.ConfigmapList{}, nil)
repositoryMock.EXPECT().
GetEdgeWorkload(gomock.Any(), "workload1", testNamespace).
Return(workloadData, nil)
// when
res := handler.GetDataMessageForDevice(deviceCtx, params)

// then
Expect(res).To(BeAssignableToTypeOf(&operations.GetDataMessageForDeviceOK{}))
config := validateAndGetDeviceConfig(res)

Expect(config.DeviceID).To(Equal(deviceName))
Expect(config.Workloads).To(HaveLen(1))
workload := config.Workloads[0]
Expect(workload.Name).To(Equal("workload1"))
Expect(workload.Labels).To(Equal(map[string]string{"label1": "1"}))
})

It("should propagate Annotations with prefix 'podman/'", func() {
// given
deviceName := "foo"
device := getDevice(deviceName)
device.Status.Workloads = []v1alpha1.Workload{{Name: "workload1"}}

repositoryMock.EXPECT().
GetEdgeDevice(gomock.Any(), deviceName, testNamespace).
Return(device, nil).
Times(1)

workloadData := &v1alpha1.EdgeWorkload{
ObjectMeta: v1.ObjectMeta{
Name: "workload1",
Namespace: "default",
Annotations: map[string]string{"podman/annotate1": "1"},
},
Spec: v1alpha1.EdgeWorkloadSpec{
Type: "pod",
Pod: v1alpha1.Pod{},
}}
configMap.EXPECT().Fetch(gomock.Any(), gomock.Any(), gomock.Any()).Return(models.ConfigmapList{}, nil)
repositoryMock.EXPECT().
GetEdgeWorkload(gomock.Any(), "workload1", testNamespace).
Return(workloadData, nil)
// when
res := handler.GetDataMessageForDevice(deviceCtx, params)

// then
Expect(res).To(BeAssignableToTypeOf(&operations.GetDataMessageForDeviceOK{}))
config := validateAndGetDeviceConfig(res)

Expect(config.DeviceID).To(Equal(deviceName))
Expect(config.Workloads).To(HaveLen(1))
workload := config.Workloads[0]
Expect(workload.Name).To(Equal("workload1"))
Expect(workload.Annotations).To(Equal(map[string]string{"annotate1": "1"}))
})
})
})

Context("PostDataMessageForDevice", func() {
Expand Down
3 changes: 3 additions & 0 deletions models/workload.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions restapi/embedded_spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ definitions:
log_collection:
description: Log collection target for this workload
type: string
annotations:
type: object
description: Workload Annotations
additionalProperties:
type: string
secret-list:
description: List of secrets used by the workloads
type: array
Expand Down

0 comments on commit b925a42

Please sign in to comment.