Skip to content

Commit

Permalink
Allow adding custom labels and annotations to the pod created by PodR…
Browse files Browse the repository at this point in the history
…unner (#729)

* Allow adding custom annotations and labels

* General cleanup
  • Loading branch information
pavannd1 committed Aug 6, 2020
1 parent 86e7c52 commit f21b7f0
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 32 deletions.
2 changes: 1 addition & 1 deletion examples/stable/couchbase/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ $ kubectl create -f couchbase-blueprint.yaml -n kasten-io
blueprint.cr.kanister.io/couchbase-blueprint created
```

Once Couchbase is running, you can populate it with some data. Let's add some dummy documents to the default bucket:
Once Couchbase is running, you can populate it with some data. Let's add some documents to the default bucket:

```bash
# Connect to couchbase cluster pod
Expand Down
2 changes: 1 addition & 1 deletion examples/stable/mongodb-deploymentconfig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Create Blueprint in the same namespace as the controller (`kanister`)
blueprint.cr.kanister.io/mongodb-blueprint created
```

Now that we have created the Profile and Blueprint Kanister resources we will insert some dummy data into
Now that we have created the Profile and Blueprint Kanister resources we will insert some data into
MongoDB database that we will take backup of.
To insert the data into the MongoDB database we will `exec` into the MongoDB pod, please follow below commands
to do so
Expand Down
2 changes: 1 addition & 1 deletion examples/stable/mysql-deploymentconfig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Create Blueprint in the same namespace as the controller
blueprint.cr.kanister.io/mysql-dep-config-blueprint created
```

Now that we have created the Profile and Blueprint Kanister resources we will insert some dummy data into
Now that we have created the Profile and Blueprint Kanister resources we will insert some data into
MySQL database that we will take backup of.
To insert the data into the MySQL database we will `exec` into the MySQL pod, please follow below commands
to do so
Expand Down
2 changes: 1 addition & 1 deletion examples/stable/postgresql-deploymentconfig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Create Blueprint in the same namespace as the controller (`kanister`)
blueprint.cr.kanister.io/postgres-bp created
```

Now that we have created the Profile and Blueprint Kanister resources we will insert some dummy data into
Now that we have created the Profile and Blueprint Kanister resources we will insert some data into
PostgreSQL database that we will take backup of.
To insert the data into the PostgreSQL database we will `exec` into the PostgreSQL pod, please follow below commands
to do so
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/mongodb-deploymentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (mongo *MongoDBDepConfig) Count(ctx context.Context) (int, error) {

func (mongo *MongoDBDepConfig) Reset(ctx context.Context) error {
log.Print("Resetting the application.", field.M{"app": mongo.name})
// delete all the entries from the restaurants dummy collection
// delete all the entries from the restaurants collection
// we are not deleting the database because we are dealing with admin database here
// and deletion admin database is prohibited
deleteDBCMD := []string{"bash", "-c", fmt.Sprintf("mongo admin --authenticationDatabase admin -u %s -p $MONGODB_ADMIN_PASSWORD --quiet --eval \"db.restaurants.drop()\"", mongo.user)}
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (mongo *MongoDB) Count(ctx context.Context) (int, error) {
}
func (mongo *MongoDB) Reset(ctx context.Context) error {
log.Print("Resetting the application.", field.M{"app": mongo.name})
// delete all the entries from the restaurants dummy collection
// delete all the entries from the restaurants collection
// we are not deleting the database because we are dealing with admin database here
// and deletion admin database is prohibited
deleteDBCMD := []string{"sh", "-c", fmt.Sprintf("mongo admin --authenticationDatabase admin -u %s -p $MONGODB_ROOT_PASSWORD --quiet --eval \"db.restaurants.drop()\"", mongo.username)}
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/mysql-deploymentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (mdep *MysqlDepConfig) Reset(ctx context.Context) error {
return errors.Wrapf(err, "Error while dropping the mysql table: %s", stderr)
}

// create the database and a pets dummy table
// create the database and a pets table
createCMD := []string{"bash", "-c", fmt.Sprintf("mysql -u root -e 'create database testdb; use testdb; CREATE TABLE pets (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);'")}
_, stderr, err = mdep.execCommand(ctx, createCMD)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (mdb *MysqlDB) Reset(ctx context.Context) error {
return errors.Wrapf(err, "Error while dropping the mysql table: %s", stderr)
}

// create the database and a pets dummy table
// create the database and a pets table
createTableCMD := []string{"sh", "-c", "mysql -u root --password=$MYSQL_ROOT_PASSWORD -e 'create database testdb; use testdb; CREATE TABLE pets (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);'"}
_, stderr, err = mdb.execCommand(ctx, createTableCMD)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkg/blockstorage/awsefs/awsefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const (

efsType = "EFS"
k10BackupVaultName = "k10vault"
dummyMarker = ""
testMarker = ""

maxRetries = 10
)
Expand Down Expand Up @@ -536,25 +536,25 @@ func (e *efs) snapshotsFromRecoveryPoints(ctx context.Context, rps []*backup.Rec
}

func emptyResponseRequestForBackups() (*backup.ListRecoveryPointsByBackupVaultOutput, *backup.ListRecoveryPointsByBackupVaultInput) {
resp := (&backup.ListRecoveryPointsByBackupVaultOutput{}).SetNextToken(dummyMarker)
resp := (&backup.ListRecoveryPointsByBackupVaultOutput{}).SetNextToken(testMarker)
req := &backup.ListRecoveryPointsByBackupVaultInput{}
return resp, req
}

func emptyResponseRequestForFilesystems() (*awsefs.DescribeFileSystemsOutput, *awsefs.DescribeFileSystemsInput) {
resp := (&awsefs.DescribeFileSystemsOutput{}).SetNextMarker(dummyMarker)
resp := (&awsefs.DescribeFileSystemsOutput{}).SetNextMarker(testMarker)
req := &awsefs.DescribeFileSystemsInput{}
return resp, req
}

func emptyResponseRequestForListTags() (*backup.ListTagsOutput, *backup.ListTagsInput) {
resp := (&backup.ListTagsOutput{}).SetNextToken(dummyMarker)
resp := (&backup.ListTagsOutput{}).SetNextToken(testMarker)
req := &backup.ListTagsInput{}
return resp, req
}

func emptyResponseRequestForMountTargets() (*awsefs.DescribeMountTargetsOutput, *awsefs.DescribeMountTargetsInput) {
resp := (&awsefs.DescribeMountTargetsOutput{}).SetNextMarker(dummyMarker)
resp := (&awsefs.DescribeMountTargetsOutput{}).SetNextMarker(testMarker)
req := &awsefs.DescribeMountTargetsInput{}
return resp, req
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/blockstorage/zone/levenshtein.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func computeDistance(a, b string) int {
x[i] = uint16(i)
}

// make a dummy bounds check to prevent the 2 bounds check down below.
// make a bounds check to prevent the 2 bounds check down below.
// The one inside the loop is particularly costly.
_ = x[lenS1]
// fill in the rest
Expand Down
6 changes: 3 additions & 3 deletions pkg/function/rds_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s *RDSFunctionsTest) TestPrepareCommand(c *C) {
dbEngine: PostgrSQLEngine,
action: RestoreAction,
dbEndpoint: "db-endpoint",
username: "dummy-user",
username: "test-user",
password: "secret-pass",
backupPrefix: "/backup/postgres-backup",
backupID: "backup-id",
Expand All @@ -65,7 +65,7 @@ func (s *RDSFunctionsTest) TestPrepareCommand(c *C) {
dbEngine: PostgrSQLEngine,
action: BackupAction,
dbEndpoint: "db-endpoint",
username: "dummy-user",
username: "test-user",
password: "secret-pass",
backupPrefix: "/backup/postgres-backup",
backupID: "backup-id",
Expand All @@ -92,7 +92,7 @@ func (s *RDSFunctionsTest) TestPrepareCommand(c *C) {
dbEngine: "MySQLDBEngine",
action: BackupAction,
dbEndpoint: "db-endpoint",
username: "dummy-user",
username: "test-user",
password: "secret-pass",
backupPrefix: "/backup/postgres-backup",
backupID: "backup-id",
Expand Down
19 changes: 15 additions & 4 deletions pkg/kube/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

json "github.com/json-iterator/go"
"github.com/pkg/errors"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
sp "k8s.io/apimachinery/pkg/util/strategicpatch"
Expand All @@ -42,12 +42,14 @@ const (

// PodOptions specifies options for `CreatePod`
type PodOptions struct {
Namespace string
Annotations map[string]string
Command []string
GenerateName string
Image string
Command []string
Volumes map[string]string
Labels map[string]string
Namespace string
ServiceAccountName string
Volumes map[string]string
PodOverride crv1alpha1.JSONMap
}

Expand Down Expand Up @@ -105,6 +107,15 @@ func CreatePod(ctx context.Context, cli kubernetes.Interface, opts *PodOptions)
},
Spec: patchedSpecs,
}

// Add Annotations and Labels, if specified
if opts.Annotations != nil {
pod.ObjectMeta.Annotations = opts.Annotations
}
if opts.Labels != nil {
pod.ObjectMeta.Labels = opts.Labels
}

pod, err = cli.CoreV1().Pods(ns).Create(pod)
if err != nil {
return nil, errors.Wrapf(err, "Failed to create pod. Namespace: %s, NameFmt: %s", ns, opts.GenerateName)
Expand Down
38 changes: 33 additions & 5 deletions pkg/kube/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"time"

. "gopkg.in/check.v1"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -41,7 +41,7 @@ type PodSuite struct {
}

const (
dummySAName = "dummy-sa"
testSAName = "test-sa"
controllerSA = "controller-sa"
kanisterToolsImage = "kanisterio/kanister-tools:0.32.0"
)
Expand All @@ -64,7 +64,7 @@ func (s *PodSuite) SetUpSuite(c *C) {
os.Setenv("POD_NAMESPACE", ns.Name)
os.Setenv("POD_SERVICE_ACCOUNT", controllerSA)

err = s.createServiceAccount(dummySAName, s.namespace)
err = s.createServiceAccount(testSAName, s.namespace)
c.Assert(err, IsNil)

err = s.createServiceAccount(controllerSA, s.namespace)
Expand Down Expand Up @@ -100,7 +100,7 @@ func (s *PodSuite) TestPod(c *C) {
GenerateName: "test-",
Image: kanisterToolsImage,
Command: []string{"sh", "-c", "tail -f /dev/null"},
ServiceAccountName: dummySAName,
ServiceAccountName: testSAName,
},
{
Namespace: cns,
Expand All @@ -113,7 +113,25 @@ func (s *PodSuite) TestPod(c *C) {
GenerateName: "test-",
Image: kanisterToolsImage,
Command: []string{"sh", "-c", "tail -f /dev/null"},
ServiceAccountName: dummySAName,
ServiceAccountName: testSAName,
},
{
Namespace: s.namespace,
GenerateName: "test-",
Image: kanisterToolsImage,
Command: []string{"sh", "-c", "tail -f /dev/null"},
Annotations: map[string]string{
"test-annotation": "true",
},
},
{
Namespace: s.namespace,
GenerateName: "test-",
Image: kanisterToolsImage,
Command: []string{"sh", "-c", "tail -f /dev/null"},
Labels: map[string]string{
"run": "pod",
},
},
}

Expand All @@ -134,6 +152,16 @@ func (s *PodSuite) TestPod(c *C) {
c.Assert(pod.Spec.ServiceAccountName, Equals, expectedSA)
}

if po.Annotations != nil {
c.Check(pod.ObjectMeta.Annotations, NotNil)
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.Assert(err, IsNil)
c.Assert(WaitForPodReady(ctx, s.cli, po.Namespace, pod.Name), IsNil)
c.Assert(DeletePod(context.Background(), s.cli, pod), IsNil)
Expand Down
4 changes: 2 additions & 2 deletions pkg/kube/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,15 @@ func DeletePVC(cli kubernetes.Interface, namespace, pvcName string) error {
})
}

var labelBlackList = map[string]struct{}{
var labelDenyList = map[string]struct{}{
"chart": {},
"heritage": {},
}

func labelSelector(labels map[string]string) string {
ls := make([]string, 0, len(labels))
for k, v := range labels {
if _, ok := labelBlackList[k]; ok {
if _, ok := labelDenyList[k]; ok {
continue
}
ls = append(ls, fmt.Sprintf("%s=%s", k, v))
Expand Down
4 changes: 2 additions & 2 deletions pkg/testing/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (s *E2ESuite) TestKubeExec(c *C) {
err = kube.WaitOnDeploymentReady(ctx, s.cli, s.namespace, d.GetName())
c.Assert(err, IsNil)

// Create a dummy Profile and secret
// Create test Profile and secret
sec := testutil.NewTestProfileSecret()
sec, err = s.cli.CoreV1().Secrets(s.namespace).Create(sec)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -179,7 +179,7 @@ func (s *E2ESuite) TestKubeTask(c *C) {
err = kube.WaitOnDeploymentReady(ctx, s.cli, s.namespace, d.GetName())
c.Assert(err, IsNil)

// Create a dummy Profile and secret
// Create test Profile and secret
sec := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-secret-",
Expand Down
4 changes: 2 additions & 2 deletions pkg/virtualfs/file_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type ReaderSeekerCloser interface {
io.Closer
}

// readSeekerWrapper adds a dummy Close method to a ReadSeeker
// readSeekerWrapper adds a no-op Close method to a ReadSeeker
type readSeekerWrapper struct {
io.ReadSeeker
}
Expand All @@ -39,7 +39,7 @@ func (rs readSeekerWrapper) Close() error {
return nil
}

// readCloserWrapper adds a dummy Seek method to a ReadCloser
// readCloserWrapper adds a no-op Seek method to a ReadCloser
type readCloserWrapper struct {
io.ReadCloser
}
Expand Down

0 comments on commit f21b7f0

Please sign in to comment.