Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add attribute to allow specifyin deployment labels/annotations #793

Merged
merged 2 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/additional-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ data:

Note: As for automatically mounting secrets, it is necessary to apply the `controller.devfile.io/watch-secret` label to git credentials secrets

## Applying labels and annotations to the workspace deployment
In some cases, it is useful to apply additional labels or annotations to the deployment that is created for a DevWorkspace. This is supported by setting attributes `controller.devfile.io/deployment-labels` and `controller.devfile.io/deployment-annotations` in the DevWorkspace's attributes field. The value of these attributes should be specified as a string map, as it is for regular metadata labels and annotations. For example:
```
kind: DevWorkspace
apiVersion: workspace.devfile.io/v1alpha2
metadata:
name: my-workspace
spec:
template:
attributes:
controller.devfile.io/deployment-labels:
my-label: foo
my-other-label: bar
controller.devfile.io/deployment-annotations:
my-attribute: foo
my-other-attribute: bar
```

## Debugging a failing workspace
Normally, when a workspace fails to start, the deployment will be scaled down and the workspace will be stopped in a `Failed` state. This can make it difficult to debug misconfiguration errors, so the annotation `controller.devfile.io/debug-start: "true"` can be applied to DevWorkspaces to leave resources for failed workspaces on the cluster. This allows viewing logs from workspace containers.

Expand Down
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 @@ -235,14 +235,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 @@ -501,3 +506,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
}