Skip to content

Commit

Permalink
Use PodTemplate metadata in managed Pods (flyteorg#411)
Browse files Browse the repository at this point in the history
* overriding base PodTemplate with manager-specific updates

Signed-off-by: Daniel Rammer <daniel@union.ai>

* fixed lint issues

Signed-off-by: Daniel Rammer <daniel@union.ai>

* using the ObjectMeta from the PodTemplateSpec instead of the PodTemplate resource

Signed-off-by: Daniel Rammer <daniel@union.ai>
  • Loading branch information
hamersaw committed Mar 8, 2022
1 parent 50f0fd5 commit bd1f108
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions flytepropeller/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/golang/protobuf v1.4.3
github.com/google/uuid v1.2.0
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0
github.com/imdario/mergo v0.3.11 // indirect
github.com/magiconair/properties v1.8.4
github.com/mitchellh/mapstructure v1.4.1
github.com/pkg/errors v0.9.1
Expand Down
27 changes: 19 additions & 8 deletions flytepropeller/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flytestdlib/promutils"

"github.com/imdario/mergo"

"github.com/prometheus/client_golang/prometheus"

v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -163,15 +165,24 @@ func (m *Manager) createPods(ctx context.Context) error {
errs := stderrors.ErrorCollection{}
for i, podName := range podNames {
if exists := podExists[podName]; !exists {
baseObjectMeta := podTemplate.Template.ObjectMeta.DeepCopy()
objectMeta := metav1.ObjectMeta{
Annotations: podAnnotations,
Name: podName,
Namespace: m.podNamespace,
Labels: podLabels,
OwnerReferences: m.ownerReferences,
}

err = mergo.Merge(baseObjectMeta, objectMeta, mergo.WithOverride, mergo.WithAppendSlice)
if err != nil {
errs.Append(fmt.Errorf("failed to initialize pod ObjectMeta for '%s' [%v]", podName, err))
continue
}

pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: podAnnotations,
Name: podName,
Namespace: m.podNamespace,
Labels: podLabels,
OwnerReferences: m.ownerReferences,
},
Spec: *podTemplate.Template.Spec.DeepCopy(),
ObjectMeta: *baseObjectMeta,
Spec: *podTemplate.Template.Spec.DeepCopy(),
}

err := m.shardStrategy.UpdatePodSpec(&pod.Spec, m.podTemplateContainerName, i)
Expand Down
15 changes: 15 additions & 0 deletions flytepropeller/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ var (
ResourceVersion: "0",
},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"foo": "bar",
},
Labels: map[string]string{
"app": "foo",
"bar": "baz",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Expand Down Expand Up @@ -84,6 +93,12 @@ func TestCreatePods(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, tt.shardStrategy.GetPodCount(), len(pods.Items))

for _, pod := range pods.Items {
assert.Equal(t, pod.Annotations["foo"], "bar")
assert.Equal(t, pod.Labels["app"], "flytepropeller")
assert.Equal(t, pod.Labels["bar"], "baz")
}

// execute again to ensure no new pods are created
err = manager.createPods(ctx)
assert.NoError(t, err)
Expand Down

0 comments on commit bd1f108

Please sign in to comment.