Skip to content

Commit

Permalink
Update deployment annotation handling to match devfile/api
Browse files Browse the repository at this point in the history
Adapt handling of additional annotations on the workspace Deployment to
match what is expected from the devfile API (Annotations field on
container components).

As part of supporting this, removes support for specifying additional
annotations/labels via attributes.

Signed-off-by: Angel Misevski <amisevsk@redhat.com>
  • Loading branch information
amisevsk committed Mar 21, 2022
1 parent 118252e commit 3f437aa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 49 deletions.
18 changes: 0 additions & 18 deletions docs/additional-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,6 @@ 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: 0 additions & 8 deletions pkg/constants/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ 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
44 changes: 21 additions & 23 deletions pkg/provision/workspace/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,12 @@ 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 := map[string]string{}
labels[constants.DevWorkspaceIDLabel] = workspace.Status.DevWorkspaceId
labels[constants.DevWorkspaceNameLabel] = workspace.Name

annotations, err := getAdditionalAnnotations(workspace)

deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: common.DeploymentName(workspace.Status.DevWorkspaceId),
Expand Down Expand Up @@ -460,6 +459,24 @@ func needsPVCWorkaround(podAdditions *v1alpha1.PodAdditions) (needs bool, pvcNam
return false, ""
}

func getAdditionalAnnotations(workspace *dw.DevWorkspace) (map[string]string, error) {
annotations := map[string]string{}

for _, component := range workspace.Spec.Template.Components {
if component.Container == nil || component.Container.Annotation == nil || component.Container.Annotation.Deployment == nil {
continue
}
for k, v := range component.Container.Annotation.Deployment {
if currValue, exists := annotations[k]; exists && v != currValue {
return nil, fmt.Errorf("conflicting annotations found on container components for key %s", k)
}
annotations[k] = v
}
}

return annotations, nil
}

func checkPodEvents(pod *corev1.Pod, workspaceID string, clusterAPI sync.ClusterAPI) (msg string, err error) {
evs := &corev1.EventList{}
selector, err := fields.ParseSelector(fmt.Sprintf("involvedObject.name=%s", pod.Name))
Expand Down Expand Up @@ -516,22 +533,3 @@ 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 3f437aa

Please sign in to comment.