Skip to content

Commit

Permalink
Improve actionset creation logic to support configuring labels/annota…
Browse files Browse the repository at this point in the history
…tions for kanister function pods (#3049)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
viveksinghggits and mergify[bot] authored Aug 26, 2024
1 parent 07b3b03 commit e3b8b72
Show file tree
Hide file tree
Showing 26 changed files with 628 additions and 52 deletions.
6 changes: 6 additions & 0 deletions docs/architecture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ as follows:
Options map[string]string `json:"options"`
Profile *ObjectReference `json:"profile"`
PodOverride map[string]interface{} `json:"podOverride,omitempty"`
PodLabels map[string]string `json:"podLabels"`
PodAnnotations map[string]string `json:"podAnnotations"`
}
- ``Name`` is required and specifies the action in the Blueprint.
Expand All @@ -177,6 +179,10 @@ as follows:
- ``Options`` is used to specify additional values to be used in the Blueprint
- ``PodOverride`` is used to specify pod specs that will override default specs
of the Pod created while executing functions like KubeTask, PrepareData, etc.
- ``PodLabels`` is used to configure the labels of the pods that are created
by Kanister functions run by this ActionSet.
- ``PodAnnotations`` is used to configure the annotations of the pods that created
by Kanister functions run by this ActionSet.

As a reference, below is an example of a ActionSpec.

Expand Down
39 changes: 39 additions & 0 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,45 @@ Create a new ActionSet that has the name-to-Secret reference in its action's
namespace: kanister
EOF
Configuring Labels and Annotations of Kanister function pods using ActionSet
============================================================================

We create an ActionSet each time we want to execute a
Kanister action. This action is going to be defined in Kanister blueprints
using Kanister functions.

If the specified Kanister function creates a pod, labels and annotations of
that pod can be configured via ``podLabels`` and ``podAnnotations`` fields
of the ActionSet resource.

Once these fields are configured in the ActionSet resource, all the pods that
are created by Kanister functions that is run by this ActionSet would have these
labels and annotations.

.. code-block:: yaml
$ cat <<EOF | kubectl create -f -
apiVersion: cr.kanister.io/v1alpha1
kind: ActionSet
metadata:
generateName: s3backup-
namespace: kanister
spec:
actions:
- name: backup
blueprint: time-log-bp
podLabels:
labelKeyZero: labelValueZero
labelKeyOne: labelValueone
podAnnotations:
annotationKey: annotationValue
object:
kind: Deployment
name: time-logger
namespace: default
EOF
Artifacts
=========

Expand Down
6 changes: 6 additions & 0 deletions docs_new/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ type ActionSpec struct {
Options map[string]string `json:"options"`
Profile *ObjectReference `json:"profile"`
PodOverride map[string]interface{} `json:"podOverride,omitempty"`
PodLabels map[string]string `json:"podLabels"`
PodAnnotations map[string]string `json:"podAnnotations"`
}
```

Expand All @@ -165,6 +167,10 @@ type ActionSpec struct {
- `PodOverride` is used to specify pod specs that will override
default specs of the Pod created while executing functions like
KubeTask, PrepareData, etc.
- ``PodLabels`` is used to configure the labels of the pods that are created
by Kanister functions run by this ActionSet.
- ``PodAnnotations`` is used to configure the annotations of the pods that created
by Kanister functions run by this ActionSet.

As a reference, below is an example of a ActionSpec.

Expand Down
35 changes: 35 additions & 0 deletions docs_new/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,41 @@ spec:
EOF
```

## Configuring Labels and Annotations of Kanister function pods using ActionSet

We create an ActionSet each time we want to execute a
Kanister action. This action is going to be defined in Kanister blueprints
using Kanister functions.

If the specified Kanister function creates a pod, labels and annotations of
that pod can be configured via ``podLabels`` and ``podAnnotations`` fields
of the ActionSet resource.

Once these fields are configured in the ActionSet resource, all the pods that
are created by Kanister functions that is run by this ActionSet would have these
labels and annotations.

```
$ cat <<EOF | kubectl create -f -
apiVersion: cr.kanister.io/v1alpha1
kind: ActionSet
metadata:
generateName: s3backup-
namespace: kanister
spec:
actions:
- name: backup
blueprint: time-log-bp
podLabels:
labelKeyZero: labelValueZero
labelKeyOne: labelValueone
podAnnotations:
annotationKey: annotationValue
object:
kind: Deployment
EOF
```
## Artifacts
At this point, we have successfully backed up our application\'s data to
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/cr/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ type ActionSpec struct {
// PreferredVersion will be used to select the preferred version of Kanister functions
// to be executed for this action
PreferredVersion string `json:"preferredVersion"`
// PodLabels will be used to configure the labels of the pods that are created
// by Kanister functions run by this ActionSet
PodLabels map[string]string `json:"podLabels"`
// PodAnnotations will be used to configure the annotations of the pods that created
// by Kanister functions run by this ActionSet
PodAnnotations map[string]string `json:"podAnnotations"`
}

// ActionSetStatus is the status for the actionset. This should only be updated by the controller.
Expand Down
14 changes: 14 additions & 0 deletions pkg/apis/cr/v1alpha1/zz_generated.deepcopy.go

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

30 changes: 30 additions & 0 deletions pkg/client/applyconfiguration/cr/v1alpha1/actionspec.go

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

12 changes: 12 additions & 0 deletions pkg/customresource/actionset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ spec:
description: Options will be used to specify additional values
to be used in the Blueprint.
type: object
podLabels:
additionalProperties:
type: string
description: Custom labels for the pods that are going to get created
by the action targeted by this ActionSet.
type: object
podAnnotations:
additionalProperties:
type: string
description: Custom annotations for the pods that are going to get created
by the action targeted by this ActionSet.
type: object
podOverride:
type: object
x-kubernetes-preserve-unknown-fields: true
Expand Down
19 changes: 16 additions & 3 deletions pkg/function/backup_data_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (b *BackupDataStatsFunc) Exec(ctx context.Context, tp param.TemplateParams,

var namespace, backupArtifactPrefix, backupID, mode, encryptionKey string
var err error
var annotations, labels map[string]string
var bpAnnotations, bpLabels map[string]string
if err = Arg(args, BackupDataStatsNamespaceArg, &namespace); err != nil {
return nil, err
}
Expand All @@ -179,10 +179,10 @@ func (b *BackupDataStatsFunc) Exec(ctx context.Context, tp param.TemplateParams,
if err = OptArg(args, BackupDataStatsEncryptionKeyArg, &encryptionKey, restic.GeneratePassword()); err != nil {
return nil, err
}
if err = OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil {
if err = OptArg(args, PodAnnotationsArg, &bpAnnotations, nil); err != nil {
return nil, err
}
if err = OptArg(args, PodLabelsArg, &labels, nil); err != nil {
if err = OptArg(args, PodLabelsArg, &bpLabels, nil); err != nil {
return nil, err
}

Expand All @@ -191,6 +191,19 @@ func (b *BackupDataStatsFunc) Exec(ctx context.Context, tp param.TemplateParams,
return nil, err
}

var labels, annotations map[string]string
if tp.PodAnnotations != nil {
// merge the actionset annotations with blueprint annotations
var actionSetAnn ActionSetAnnotations = tp.PodAnnotations
annotations = actionSetAnn.MergeBPAnnotations(bpAnnotations)
}

if tp.PodLabels != nil {
// merge the actionset labels with blueprint labels
var actionSetLabels ActionSetLabels = tp.PodLabels
labels = actionSetLabels.MergeBPLabels(bpLabels)
}

if err = ValidateProfile(tp.Profile); err != nil {
return nil, errors.Wrapf(err, "Failed to validate Profile")
}
Expand Down
19 changes: 16 additions & 3 deletions pkg/function/checkRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (c *CheckRepositoryFunc) Exec(ctx context.Context, tp param.TemplateParams,

var checkRepositoryArtifactPrefix, encryptionKey string
var insecureTLS bool
var annotations, labels map[string]string
var bpAnnotations, bpLabels map[string]string
if err := Arg(args, CheckRepositoryArtifactPrefixArg, &checkRepositoryArtifactPrefix); err != nil {
return nil, err
}
Expand All @@ -159,10 +159,10 @@ func (c *CheckRepositoryFunc) Exec(ctx context.Context, tp param.TemplateParams,
if err := OptArg(args, InsecureTLS, &insecureTLS, false); err != nil {
return nil, err
}
if err := OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil {
if err := OptArg(args, PodAnnotationsArg, &bpAnnotations, nil); err != nil {
return nil, err
}
if err := OptArg(args, PodLabelsArg, &labels, nil); err != nil {
if err := OptArg(args, PodLabelsArg, &bpLabels, nil); err != nil {
return nil, err
}

Expand All @@ -171,6 +171,19 @@ func (c *CheckRepositoryFunc) Exec(ctx context.Context, tp param.TemplateParams,
return nil, err
}

var labels, annotations map[string]string
if tp.PodAnnotations != nil {
// merge the actionset annotations with blueprint annotations
var actionSetAnn ActionSetAnnotations = tp.PodAnnotations
annotations = actionSetAnn.MergeBPAnnotations(bpAnnotations)
}

if tp.PodLabels != nil {
// merge the actionset labels with blueprint labels
var actionSetLabels ActionSetLabels = tp.PodLabels
labels = actionSetLabels.MergeBPLabels(bpLabels)
}

if err = ValidateProfile(tp.Profile); err != nil {
return nil, err
}
Expand Down
19 changes: 16 additions & 3 deletions pkg/function/copy_volume_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (c *copyVolumeDataFunc) Exec(ctx context.Context, tp param.TemplateParams,

var namespace, vol, targetPath, encryptionKey string
var err error
var annotations, labels map[string]string
var bpAnnotations, bpLabels map[string]string
var insecureTLS bool
if err = Arg(args, CopyVolumeDataNamespaceArg, &namespace); err != nil {
return nil, err
Expand All @@ -215,17 +215,30 @@ func (c *copyVolumeDataFunc) Exec(ctx context.Context, tp param.TemplateParams,
if err = OptArg(args, InsecureTLS, &insecureTLS, false); err != nil {
return nil, err
}
if err = OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil {
if err = OptArg(args, PodAnnotationsArg, &bpAnnotations, nil); err != nil {
return nil, err
}
if err = OptArg(args, PodLabelsArg, &labels, nil); err != nil {
if err = OptArg(args, PodLabelsArg, &bpLabels, nil); err != nil {
return nil, err
}
podOverride, err := GetPodSpecOverride(tp, args, CopyVolumeDataPodOverrideArg)
if err != nil {
return nil, err
}

var labels, annotations map[string]string
if tp.PodAnnotations != nil {
// merge the actionset annotations with blueprint annotations
var actionSetAnn ActionSetAnnotations = tp.PodAnnotations
annotations = actionSetAnn.MergeBPAnnotations(bpAnnotations)
}

if tp.PodLabels != nil {
// merge the actionset labels with blueprint labels
var actionSetLabels ActionSetLabels = tp.PodLabels
labels = actionSetLabels.MergeBPLabels(bpLabels)
}

if err = ValidateProfile(tp.Profile); err != nil {
return nil, errors.Wrapf(err, "Failed to validate Profile")
}
Expand Down
19 changes: 16 additions & 3 deletions pkg/function/delete_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (d *deleteDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args
var reclaimSpace bool
var err error
var insecureTLS bool
var annotations, labels map[string]string
var bpAnnotations, bpLabels map[string]string
if err = Arg(args, DeleteDataNamespaceArg, &namespace); err != nil {
return nil, err
}
Expand All @@ -245,10 +245,10 @@ func (d *deleteDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args
if err = OptArg(args, InsecureTLS, &insecureTLS, false); err != nil {
return nil, err
}
if err = OptArg(args, PodAnnotationsArg, &annotations, nil); err != nil {
if err = OptArg(args, PodAnnotationsArg, &bpAnnotations, nil); err != nil {
return nil, err
}
if err = OptArg(args, PodLabelsArg, &labels, nil); err != nil {
if err = OptArg(args, PodLabelsArg, &bpLabels, nil); err != nil {
return nil, err
}

Expand All @@ -257,6 +257,19 @@ func (d *deleteDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args
return nil, err
}

var labels, annotations map[string]string
if tp.PodAnnotations != nil {
// merge the actionset annotations with blueprint annotations
var actionSetAnn ActionSetAnnotations = tp.PodAnnotations
annotations = actionSetAnn.MergeBPAnnotations(bpAnnotations)
}

if tp.PodLabels != nil {
// merge the actionset labels with blueprint labels
var actionSetLabels ActionSetLabels = tp.PodLabels
labels = actionSetLabels.MergeBPLabels(bpLabels)
}

if err = ValidateProfile(tp.Profile); err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit e3b8b72

Please sign in to comment.