Skip to content

Commit

Permalink
Add label to pods created by Kanister (#886)
Browse files Browse the repository at this point in the history
* Add label to pods created by Kanister

* Update label map initialization

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
Steve Joachim and mergify[bot] committed Jan 22, 2021
1 parent 60fc0ff commit de245a7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ const (
ContainerNameKey = "Container"
PhaseNameKey = "Phase"
GoogleCloudCredsFilePath = "/tmp/creds.txt"
LabelKeyCreatedBy = "createdBy"
LabelValueKanister = "kanister"
)
11 changes: 9 additions & 2 deletions pkg/kube/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/client-go/kubernetes"

crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/consts"
"github.com/kanisterio/kanister/pkg/log"
"github.com/kanisterio/kanister/pkg/poll"
)
Expand Down Expand Up @@ -104,6 +105,9 @@ func CreatePod(ctx context.Context, cli kubernetes.Interface, opts *PodOptions)
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: opts.GenerateName,
Labels: map[string]string{
consts.LabelKeyCreatedBy: consts.LabelValueKanister,
},
},
Spec: patchedSpecs,
}
Expand All @@ -112,8 +116,11 @@ func CreatePod(ctx context.Context, cli kubernetes.Interface, opts *PodOptions)
if opts.Annotations != nil {
pod.ObjectMeta.Annotations = opts.Annotations
}
if opts.Labels != nil {
pod.ObjectMeta.Labels = opts.Labels
if pod.ObjectMeta.Labels == nil {
pod.ObjectMeta.Labels = map[string]string{}
}
for key, value := range opts.Labels {
pod.ObjectMeta.Labels[key] = value
}

pod, err = cli.CoreV1().Pods(ns).Create(ctx, pod, metav1.CreateOptions{})
Expand Down
8 changes: 5 additions & 3 deletions pkg/kube/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"k8s.io/client-go/testing"

crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/consts"
)

type PodSuite struct {
Expand Down Expand Up @@ -157,9 +158,10 @@ func (s *PodSuite) TestPod(c *C) {
c.Check(pod.ObjectMeta.Annotations, DeepEquals, po.Annotations)
}

if po.Labels != nil {
c.Check(pod.ObjectMeta.Labels, NotNil)
c.Check(pod.ObjectMeta.Labels, DeepEquals, po.Labels)
c.Check(len(pod.ObjectMeta.Labels), Equals, len(po.Labels)+1)
c.Check(pod.ObjectMeta.Labels[consts.LabelKeyCreatedBy], Equals, consts.LabelValueKanister)
for key, value := range po.Labels {
c.Check(pod.ObjectMeta.Labels[key], Equals, value)
}

c.Assert(err, IsNil)
Expand Down

0 comments on commit de245a7

Please sign in to comment.