Skip to content

Commit

Permalink
Add attributes to allow specifying deployment labels/annotations
Browse files Browse the repository at this point in the history
Add attributes
  * controller.devfile.io/deployment-labels
  * controller.devfile.io/deployment-annotations

to allow specifying labels and annotations that should be applied to the
DevWorkspace deployment.

Signed-off-by: Angel Misevski <amisevsk@redhat.com>
  • Loading branch information
amisevsk committed Mar 17, 2022
1 parent 21fe7f2 commit 9420d6b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
8 changes: 8 additions & 0 deletions pkg/constants/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ const (
// will not be cloned into the workspace on start.
ProjectCloneAttribute = "controller.devfile.io/project-clone"

// DeployLabelsAttribute is an DevWorkspace attribute used in .spec.attributes that defines additional labels
// that should be applied to the workspace deployment. Value should be a map[string]string
DeployLabelsAttribute = "controller.devfile.io/deployment-labels"

// DeployAnnotationsAttribute is an DevWorkspace attribute used in .spec.attributes that defines additional annotations
// that should be applied to the workspace deployment. Value should be a map[string]string
DeployAnnotationsAttribute = "controller.devfile.io/deployment-annotations"

// PluginSourceAttribute is an attribute added to components, commands, and projects in a flattened
// DevWorkspace representation to signify where the respective component came from (i.e. which plugin
// or parent imported it)
Expand Down
36 changes: 30 additions & 6 deletions pkg/provision/workspace/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,19 @@ func getSpecDeployment(
podAdditions.InitContainers[idx].VolumeMounts = append(podAdditions.InitContainers[idx].VolumeMounts, podAdditions.VolumeMounts...)
}

labels, annotations, err := getAdditionalLabelsAndAttributes(workspace)
if err != nil {
return nil, err
}
labels[constants.DevWorkspaceIDLabel] = workspace.Status.DevWorkspaceId
labels[constants.DevWorkspaceNameLabel] = workspace.Name

deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: common.DeploymentName(workspace.Status.DevWorkspaceId),
Namespace: workspace.Namespace,
Labels: map[string]string{
constants.DevWorkspaceIDLabel: workspace.Status.DevWorkspaceId,
constants.DevWorkspaceNameLabel: workspace.Name,
},
Name: common.DeploymentName(workspace.Status.DevWorkspaceId),
Namespace: workspace.Namespace,
Labels: labels,
Annotations: annotations,
},
Spec: appsv1.DeploymentSpec{
Replicas: &replicas,
Expand Down Expand Up @@ -511,3 +516,22 @@ func checkIfUnrecoverableEventIgnored(reason string) (ignored bool) {
}
return false
}

// getAdditionalLabelsAndAttributes reads attributes on the DevWorkspace and returns the additional labels and
// attributes that should be applied to the DevWorkspace. Returns an error if attributes cannot be deserialized
// into a map[string]string. If attributes are not defined, returns an empty map.
func getAdditionalLabelsAndAttributes(workspace *dw.DevWorkspace) (labels, annotations map[string]string, err error) {
labels = map[string]string{}
annotations = map[string]string{}
if workspace.Spec.Template.Attributes.Exists(constants.DeployLabelsAttribute) {
if err := workspace.Spec.Template.Attributes.GetInto(constants.DeployLabelsAttribute, &labels); err != nil {
return nil, nil, fmt.Errorf("failed to process %s attribute: %w", constants.DeployLabelsAttribute, err)
}
}
if workspace.Spec.Template.Attributes.Exists(constants.DeployAnnotationsAttribute) {
if err := workspace.Spec.Template.Attributes.GetInto(constants.DeployAnnotationsAttribute, &annotations); err != nil {
return nil, nil, fmt.Errorf("failed to process %s attribute: %w", constants.DeployAnnotationsAttribute, err)
}
}
return labels, annotations, nil
}

0 comments on commit 9420d6b

Please sign in to comment.