Skip to content
Draft
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
8 changes: 3 additions & 5 deletions docs/additional-configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,10 @@ This will mount the files in the `git-ssh-key` secret to `/etc/ssh/`, creating f

## Setting an alternate configuration for a workspace
It is possible to configure a workspace to use an alternate DevWorkspaceOperatorConfig.
In order to do so, the alternate DevWorkspaceOperatorConfig must exist on the cluster, and the `controller.devfile.io/devworkspace-config` workspace attribute must be set.
The `controller.devfile.io/devworkspace-config` attribute takes two string fields: `name` and `namespace`.
The alternate DevWorkspaceOperatorConfig must exist in the namespace where the DevWorkspace Operator is installed, and the `controller.devfile.io/devworkspace-config` workspace attribute must be set.
The `controller.devfile.io/devworkspace-config` attribute takes one string field: `name`.

* `name`: the `metadata.name` of the alternate DevWorkspaceOperatorConfig.
* `namespace`: the `metadata.namespace` of the alternate DevWorkspaceOperatorConfig.
* `name`: the `metadata.name` of the alternate DevWorkspaceOperatorConfig. The operator reads that object from its own installation namespace. A DevWorkspaceOperatorConfig in any other namespace, including the workspace namespace, is not applied.

[source,yaml]
----
Expand All @@ -353,7 +352,6 @@ spec:
attributes:
controller.devfile.io/devworkspace-config:
name: <string>
namespace: <string>
----

*Note:* the alternate DevWorkspaceOperatorConfig will be
Expand Down
6 changes: 3 additions & 3 deletions docs/dwo-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ config:
### DevWorkspace specific configuration

To apply a configuration to a specific `DevWorkspace` instead of globally, an existing `DevWorkspaceOperatorConfig` can
be referenced in a `DevWorkspace`'s attributes:
be referenced in a `DevWorkspace`'s attributes. The named object is always read from the operator installation
namespace.
```yaml
apiVersion: workspace.devfile.io/v1alpha2
kind: DevWorkspace
Expand All @@ -41,8 +42,7 @@ spec:
template:
attributes:
controller.devfile.io/devworkspace-config:
name: <name of DevWorkspaceOperatorConfig CR>
namespace: <namespace of DevWorkspaceOperatorConfig CR>
name: <name of DevWorkspaceOperatorConfig CR in the operator namespace>
```
Configuration specified as above will be merged into the default global configuration, overriding any values present.
Note that fields set in the global configuration cannot be cleared (reset to unset) by a workspace-specific
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func buildExternalConfig(config *v1alpha1.OperatorConfiguration) *v1alpha1.DevWo
return &v1alpha1.DevWorkspaceOperatorConfig{
ObjectMeta: metav1.ObjectMeta{
Name: externalConfigName,
Namespace: externalConfigNamespace,
Namespace: testNamespace,
},
Config: config,
}
Expand Down
14 changes: 10 additions & 4 deletions pkg/config/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ func GetGlobalConfig() *controller.OperatorConfiguration {
// DevWorkspaceOperatorConfig specified by the optional workspace attribute `controller.devfile.io/devworkspace-config`.
// If the `controller.devfile.io/devworkspace-config` is not set, the global DevWorkspaceOperatorConfig is returned.
// If the `controller.devfile.io/devworkspace-config` attribute is incorrectly set, or the specified DevWorkspaceOperatorConfig
// does not exist on the cluster, an error is returned.
// does not exist in the operator namespace, an error is returned.
//
// The attribute's name is honored. Its namespace is optional and ignored: the named config is always read from the
// operator installation namespace.
// See adr/2026-12-02-trusted-devworkspace-config-resolution.md.
func ResolveConfigForWorkspace(workspace *dw.DevWorkspace, client crclient.Client) (*controller.OperatorConfiguration, error) {
if !workspace.Spec.Template.Attributes.Exists(constants.ExternalDevWorkspaceConfiguration) {
return GetGlobalConfig(), nil
Expand All @@ -76,14 +80,16 @@ func ResolveConfigForWorkspace(workspace *dw.DevWorkspace, client crclient.Clien
return nil, fmt.Errorf("'name' must be set for attribute %s in DevWorkspace attributes", constants.ExternalDevWorkspaceConfiguration)
}

if namespacedName.Namespace == "" {
return nil, fmt.Errorf("'namespace' must be set for attribute %s in DevWorkspace attributes", constants.ExternalDevWorkspaceConfiguration)
if configNamespace == "" {
return nil, fmt.Errorf("failed to resolve attribute %s: operator namespace is not configured", constants.ExternalDevWorkspaceConfiguration)
}
attributeNamespace := namespacedName.Namespace
namespacedName.Namespace = configNamespace

externalDWOC := &controller.DevWorkspaceOperatorConfig{}
err = client.Get(context.TODO(), namespacedName, externalDWOC)
if err != nil {
return nil, fmt.Errorf("could not fetch external DWOC with name %s in namespace %s: %w", namespacedName.Name, namespacedName.Namespace, err)
return nil, fmt.Errorf("could not fetch DevWorkspaceOperatorConfig %q from operator namespace %q (attribute namespace %q is ignored): %w", namespacedName.Name, configNamespace, attributeNamespace, err)
}
return getMergedConfig(externalDWOC.Config, internalConfig), nil
}
Expand Down
102 changes: 99 additions & 3 deletions pkg/config/sync_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2019-2025 Red Hat, Inc.
// Copyright (c) 2019-2026 Red Hat, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -29,6 +29,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -155,6 +156,95 @@ func TestCatchesNonExistentExternalDWOC(t *testing.T) {
assert.Equal(t, resolvedConfig, internalConfig, "Internal/Global DWOC should be used as fallback if external DWOC could not be resolved")
}

func TestResolveConfigForWorkspaceIgnoresAttributeNamespace(t *testing.T) {
setupForTest(t)

workspace := &dw.DevWorkspace{}
workspaceAttributes := attributes.Attributes{}
workspaceAttributes.Put(constants.ExternalDevWorkspaceConfiguration, types.NamespacedName{
Name: externalConfigName,
Namespace: externalConfigNamespace,
}, nil)
workspace.Spec.Template.DevWorkspaceTemplateSpecContent = dw.DevWorkspaceTemplateSpecContent{
Attributes: workspaceAttributes,
}

tenantConfig := buildExternalConfig(defaultConfig.DeepCopy())
tenantConfig.Namespace = externalConfigNamespace
tenantConfig.Config.Workspace.ImagePullPolicy = "Never"

client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(tenantConfig).Build()
resolvedConfig, err := ResolveConfigForWorkspace(workspace, client)
if assert.Error(t, err, "A DevWorkspaceOperatorConfig that exists only in the attribute namespace must not be applied") {
assert.True(t, k8sErrors.IsNotFound(err), "Lookup should miss in the operator namespace, got: %s", err)
}
assert.Nil(t, resolvedConfig)

operatorConfig := buildExternalConfig(defaultConfig.DeepCopy())
operatorConfig.Config.Workspace.ImagePullPolicy = "Always"
err = client.Create(context.Background(), operatorConfig)
if !assert.NoError(t, err) {
return
}

SetGlobalConfigForTesting(defaultConfig.DeepCopy())
resolvedConfig, err = ResolveConfigForWorkspace(workspace, client)
if !assert.NoError(t, err, "The same name in the operator namespace should resolve") {
return
}
assert.Equal(t, "Always", resolvedConfig.Workspace.ImagePullPolicy)
}

func TestResolveConfigForWorkspaceWithoutAttributeNamespace(t *testing.T) {
setupForTest(t)

workspace := &dw.DevWorkspace{}
workspaceAttributes := attributes.Attributes{}
workspaceAttributes.Put(constants.ExternalDevWorkspaceConfiguration, map[string]string{
"name": externalConfigName,
}, nil)
workspace.Spec.Template.DevWorkspaceTemplateSpecContent = dw.DevWorkspaceTemplateSpecContent{
Attributes: workspaceAttributes,
}

operatorConfig := buildExternalConfig(defaultConfig.DeepCopy())
operatorConfig.Config.Workspace.ImagePullPolicy = "Always"
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(operatorConfig).Build()

SetGlobalConfigForTesting(defaultConfig.DeepCopy())
resolvedConfig, err := ResolveConfigForWorkspace(workspace, client)
if !assert.NoError(t, err, "The attribute namespace is ignored, so omitting it should not be an error") {
return
}
assert.Equal(t, "Always", resolvedConfig.Workspace.ImagePullPolicy)
}

func TestResolveConfigForWorkspaceFailsWithoutOperatorNamespace(t *testing.T) {
setupForTest(t)
configNamespace = ""

workspace := &dw.DevWorkspace{}
workspaceAttributes := attributes.Attributes{}
workspaceAttributes.Put(constants.ExternalDevWorkspaceConfiguration, types.NamespacedName{
Name: externalConfigName,
Namespace: externalConfigNamespace,
}, nil)
workspace.Spec.Template.DevWorkspaceTemplateSpecContent = dw.DevWorkspaceTemplateSpecContent{
Attributes: workspaceAttributes,
}

// Present in the attribute namespace: must not be reachable when the operator namespace is unknown.
tenantConfig := buildExternalConfig(defaultConfig.DeepCopy())
tenantConfig.Namespace = externalConfigNamespace
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(tenantConfig).Build()

resolvedConfig, err := ResolveConfigForWorkspace(workspace, client)
if assert.Error(t, err, "Resolution must fail when the operator namespace is not configured") {
assert.Contains(t, err.Error(), "operator namespace is not configured")
}
assert.Nil(t, resolvedConfig)
}

func TestMergeExternalConfig(t *testing.T) {
setupForTest(t)

Expand All @@ -181,7 +271,13 @@ func TestMergeExternalConfig(t *testing.T) {
}

clusterConfig := buildConfig(defaultConfig.DeepCopy())
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(clusterConfig, externalConfig).Build()
// Same name in the attribute's namespace. Must not be merged: resolution is forced
// into the operator namespace.
decoyConfig := externalConfig.DeepCopy()
decoyConfig.Namespace = externalConfigNamespace
decoyConfig.Config.Workspace.ImagePullPolicy = "Never"
decoyConfig.Config.Workspace.PVCName = "tenant-controlled-pvc"
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(clusterConfig, externalConfig, decoyConfig).Build()
err := SetupControllerConfig(client)
if !assert.NoError(t, err, "Should not return error") {
return
Expand Down Expand Up @@ -716,7 +812,7 @@ func TestResolveConfigForWorkspaceWithNetworkPolicy(t *testing.T) {
externalDwoc := &v1alpha1.DevWorkspaceOperatorConfig{
ObjectMeta: metav1.ObjectMeta{
Name: externalConfigName,
Namespace: externalConfigNamespace,
Namespace: configNamespace,
},
Config: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
Expand Down
8 changes: 3 additions & 5 deletions pkg/constants/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,16 @@ const (
DevWorkspaceStorageTypeAttribute = "controller.devfile.io/storage-type"

// ExternalDevWorkspaceConfiguration is an attribute that allows for specifying an (optional) external DevWorkspaceOperatorConfig
// which will merged with the internal/global DevWorkspaceOperatorConfig. The DevWorkspaceOperatorConfig resulting from the merge will be used for the workspace.
// which will be merged with the internal/global DevWorkspaceOperatorConfig. The DevWorkspaceOperatorConfig resulting from the merge will be used for the workspace.
// The fields which are set in the external DevWorkspaceOperatorConfig will overwrite those existing in the
// internal/global DevWorkspaceOperatorConfig during the merge.
// The structure of the attribute value should contain two strings: name and namespace.
// 'name' specifies the metadata.name of the external operator configuration.
// 'namespace' specifies the metadata.namespace of the external operator configuration .
// 'name' specifies the metadata.name of the external operator configuration, which is always read from the
// operator installation namespace. A 'namespace' field may be present in existing manifests; it is ignored.
// For example:
//
// attributes:
// controller.devfile.io/devworkspace-config:
// name: external-dwoc-name
// namespace: some-namespace
ExternalDevWorkspaceConfiguration = "controller.devfile.io/devworkspace-config"

// RuntimeClassNameAttribute is an attribute added to a DevWorkspace to specify a runtimeClassName for container
Expand Down
Loading