Skip to content

Commit 662589c

Browse files
committed
[ws-daemon] Use token secret
1 parent 2b2826c commit 662589c

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

components/ws-daemon/pkg/controller/workspace_controller.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import (
2222
"github.com/prometheus/client_golang/prometheus"
2323

2424
"google.golang.org/protobuf/proto"
25+
corev1 "k8s.io/api/core/v1"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/types"
2628
"k8s.io/apimachinery/pkg/util/wait"
2729
"k8s.io/client-go/util/retry"
2830
ctrl "sigs.k8s.io/controller-runtime"
@@ -55,9 +57,10 @@ type WorkspaceController struct {
5557
maxConcurrentReconciles int
5658
operations *WorkspaceOperations
5759
metrics *workspaceMetrics
60+
secretNamespace string
5861
}
5962

60-
func NewWorkspaceController(c client.Client, nodeName string, maxConcurrentReconciles int, ops *WorkspaceOperations, reg prometheus.Registerer) (*WorkspaceController, error) {
63+
func NewWorkspaceController(c client.Client, nodeName, secretNamespace string, maxConcurrentReconciles int, ops *WorkspaceOperations, reg prometheus.Registerer) (*WorkspaceController, error) {
6164
metrics := newWorkspaceMetrics()
6265
reg.Register(metrics)
6366

@@ -67,6 +70,7 @@ func NewWorkspaceController(c client.Client, nodeName string, maxConcurrentRecon
6770
maxConcurrentReconciles: maxConcurrentReconciles,
6871
operations: ops,
6972
metrics: metrics,
73+
secretNamespace: secretNamespace,
7074
}, nil
7175
}
7276

@@ -139,10 +143,8 @@ func (wsc *WorkspaceController) handleWorkspaceInit(ctx context.Context, ws *wor
139143
defer tracing.FinishSpan(span, &err)
140144

141145
if c := wsk8s.GetCondition(ws.Status.Conditions, string(workspacev1.WorkspaceConditionContentReady)); c == nil {
142-
var init csapi.WorkspaceInitializer
143-
err = proto.Unmarshal(ws.Spec.Initializer, &init)
146+
init, err := wsc.prepareInitializer(ctx, ws)
144147
if err != nil {
145-
err = fmt.Errorf("cannot unmarshal initializer config: %w", err)
146148
return ctrl.Result{}, err
147149
}
148150

@@ -153,7 +155,7 @@ func (wsc *WorkspaceController) handleWorkspaceInit(ctx context.Context, ws *wor
153155
WorkspaceId: ws.Spec.Ownership.WorkspaceID,
154156
InstanceId: ws.Name,
155157
},
156-
Initializer: &init,
158+
Initializer: init,
157159
Headless: ws.IsHeadless(),
158160
})
159161

@@ -300,6 +302,27 @@ func (wsc *WorkspaceController) handleWorkspaceStop(ctx context.Context, ws *wor
300302
return ctrl.Result{}, err
301303
}
302304

305+
func (wsc *WorkspaceController) prepareInitializer(ctx context.Context, ws *workspacev1.Workspace) (*csapi.WorkspaceInitializer, error) {
306+
var init csapi.WorkspaceInitializer
307+
err := proto.Unmarshal(ws.Spec.Initializer, &init)
308+
if err != nil {
309+
err = fmt.Errorf("cannot unmarshal initializer config: %w", err)
310+
return nil, err
311+
}
312+
313+
var tokenSecret corev1.Secret
314+
err = wsc.Get(ctx, types.NamespacedName{Name: fmt.Sprintf("%s-tokens", ws.Name), Namespace: wsc.secretNamespace}, &tokenSecret)
315+
if err != nil {
316+
return nil, fmt.Errorf("could not get token secret for workspace: %w", err)
317+
}
318+
319+
if err = csapi.InjectSecretsToInitializer(&init, tokenSecret.Data); err != nil {
320+
return nil, fmt.Errorf("failed to inject secrets into initializer: %w", err)
321+
}
322+
323+
return &init, nil
324+
}
325+
303326
func toWorkspaceGitStatus(status *csapi.GitStatus) *workspacev1.GitStatus {
304327
if status == nil {
305328
return nil

components/ws-daemon/pkg/daemon/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type RuntimeConfig struct {
4242
Container *container.Config `json:"containerRuntime"`
4343
Kubeconfig string `json:"kubeconfig"`
4444
KubernetesNamespace string `json:"namespace"`
45+
SecretsNamespace string `json:"secretsNamespace"`
4546
}
4647

4748
type IOLimitConfig struct {

components/ws-daemon/pkg/daemon/daemon.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"k8s.io/client-go/rest"
2323
"k8s.io/client-go/tools/clientcmd"
2424
ctrl "sigs.k8s.io/controller-runtime"
25+
"sigs.k8s.io/controller-runtime/pkg/cache"
2526
"sigs.k8s.io/controller-runtime/pkg/manager"
2627
"sigs.k8s.io/controller-runtime/pkg/metrics"
2728

@@ -175,6 +176,7 @@ func NewDaemon(config Config) (*Daemon, error) {
175176
Namespace: config.Runtime.KubernetesNamespace,
176177
HealthProbeBindAddress: "0",
177178
MetricsBindAddress: "0", // Metrics are exposed through baseserver.
179+
NewCache: cache.MultiNamespacedCacheBuilder([]string{config.Runtime.KubernetesNamespace, config.Runtime.SecretsNamespace}),
178180
})
179181
if err != nil {
180182
return nil, err
@@ -207,7 +209,8 @@ func NewDaemon(config Config) (*Daemon, error) {
207209
return nil, err
208210
}
209211

210-
wsctrl, err := controller.NewWorkspaceController(mgr.GetClient(), nodename, config.WorkspaceController.MaxConcurrentReconciles, workspaceOps, wrappedReg)
212+
wsctrl, err := controller.NewWorkspaceController(
213+
mgr.GetClient(), nodename, config.Runtime.SecretsNamespace, config.WorkspaceController.MaxConcurrentReconciles, workspaceOps, wrappedReg)
211214
if err != nil {
212215
return nil, err
213216
}

install/installer/pkg/components/ws-daemon/configmap.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func configmap(ctx *common.RenderContext) ([]runtime.Object, error) {
112112
Daemon: daemon.Config{
113113
Runtime: daemon.RuntimeConfig{
114114
KubernetesNamespace: ctx.Namespace,
115+
SecretsNamespace: common.WorkspaceSecretsNamespace,
115116
Container: &container.Config{
116117
Runtime: container.RuntimeContainerd,
117118
Mapping: runtimeMapping,

0 commit comments

Comments
 (0)