Skip to content

Commit 6694d8e

Browse files
BH4AWSfurykerry
authored andcommitted
sandboxclaim supoort to do csi volume mount when create a sandbox
Signed-off-by: jicheng.sk <jicheng.sk@alibaba-inc.com>
1 parent 59abb93 commit 6694d8e

File tree

9 files changed

+1708
-53
lines changed

9 files changed

+1708
-53
lines changed

pkg/controller/sandboxclaim/core/common_control.go

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,24 @@ package core
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"fmt"
2223
"sync"
2324
"time"
2425

25-
"github.com/openkruise/agents/pkg/sandbox-manager/config"
26-
"github.com/openkruise/agents/pkg/utils"
27-
stateutils "github.com/openkruise/agents/pkg/utils/sandboxutils"
26+
"github.com/google/uuid"
2827
"golang.org/x/time/rate"
2928
"k8s.io/client-go/tools/record"
29+
"k8s.io/klog/v2"
3030
"sigs.k8s.io/controller-runtime/pkg/client"
3131
logf "sigs.k8s.io/controller-runtime/pkg/log"
3232

33+
"github.com/openkruise/agents/pkg/agent-runtime/storages"
34+
"github.com/openkruise/agents/pkg/sandbox-manager/config"
35+
"github.com/openkruise/agents/pkg/utils"
36+
"github.com/openkruise/agents/pkg/utils/csiutils"
37+
stateutils "github.com/openkruise/agents/pkg/utils/sandboxutils"
38+
3339
agentsv1alpha1 "github.com/openkruise/agents/api/v1alpha1"
3440
"github.com/openkruise/agents/pkg/sandbox-manager/clients"
3541
"github.com/openkruise/agents/pkg/sandbox-manager/infra"
@@ -38,22 +44,24 @@ import (
3844

3945
type commonControl struct {
4046
client.Client
41-
recorder record.EventRecorder
42-
sandboxClient *clients.ClientSet
43-
cache *sandboxcr.Cache
44-
pickCache sync.Map
47+
recorder record.EventRecorder
48+
sandboxClient *clients.ClientSet
49+
cache *sandboxcr.Cache
50+
storageRegistry storages.VolumeMountProviderRegistry
51+
pickCache sync.Map
4552
}
4653

4754
func NewCommonControl(c client.Client, recorder record.EventRecorder, sandboxClient *clients.ClientSet, cache *sandboxcr.Cache) ClaimControl {
4855
// Note: sandboxClient and cache can be nil for unit tests
4956
// In production, SetupWithManager always provides these dependencies
5057

5158
control := &commonControl{
52-
Client: c,
53-
recorder: recorder,
54-
sandboxClient: sandboxClient,
55-
cache: cache,
56-
pickCache: sync.Map{},
59+
Client: c,
60+
recorder: recorder,
61+
sandboxClient: sandboxClient,
62+
cache: cache,
63+
storageRegistry: storages.NewStorageProvider(),
64+
pickCache: sync.Map{},
5765
}
5866

5967
return control
@@ -224,6 +232,7 @@ func (c *commonControl) claimSandboxes(ctx context.Context, claim *agentsv1alpha
224232

225233
// buildClaimOptions constructs ClaimSandboxOptions for TryClaimSandbox
226234
func (c *commonControl) buildClaimOptions(ctx context.Context, claim *agentsv1alpha1.SandboxClaim, sandboxSet *agentsv1alpha1.SandboxSet) (infra.ClaimSandboxOptions, error) {
235+
logger := logf.FromContext(ctx).WithValues("SandboxClaim", klog.KObj(claim))
227236
opts := infra.ClaimSandboxOptions{
228237
User: string(claim.UID), // Use UID to ensure uniqueness across claim recreations
229238
Template: sandboxSet.Name,
@@ -274,6 +283,41 @@ func (c *commonControl) buildClaimOptions(ctx context.Context, claim *agentsv1al
274283
}
275284

276285
// todo support other options (like envvars, inplace update...)
286+
if len(claim.Spec.DynamicVolumesMount) > 0 {
287+
csiMountOptions := make([]config.MountConfig, 0, len(claim.Spec.DynamicVolumesMount))
288+
csiClient := csiutils.NewCSIMountHandler(c.sandboxClient, c.cache, c.storageRegistry, utils.DefaultSandboxDeployNamespace)
289+
for _, mountConfig := range claim.Spec.DynamicVolumesMount {
290+
driverName, csiReqConfigRaw, genErr := csiClient.CSIMountOptionsConfig(ctx, mountConfig)
291+
if genErr != nil {
292+
errMsg := "failed to generate csi mount options config for sandbox"
293+
logger.Error(genErr, errMsg, "mountConfigRequest", mountConfig)
294+
return opts, fmt.Errorf("%s, err: %v", errMsg, genErr)
295+
}
296+
csiMountOptions = append(csiMountOptions, config.MountConfig{
297+
Driver: driverName,
298+
RequestRaw: csiReqConfigRaw,
299+
})
300+
}
301+
opts.CSIMount = &config.CSIMountOptions{
302+
MountOptionList: csiMountOptions,
303+
}
304+
305+
// json marshal csi mount config to raw string
306+
csiMountOptionsRaw, err := json.Marshal(claim.Spec.DynamicVolumesMount)
307+
if err != nil {
308+
logger.Error(err, "failed to marshal csi mount config")
309+
return opts, fmt.Errorf("failed to marshal csi mount config, err: %v", err)
310+
}
311+
opts.CSIMount.MountOptionListRaw = string(csiMountOptionsRaw)
312+
313+
// Init runtime is required when CSI mount is specified
314+
if opts.InitRuntime == nil {
315+
opts.InitRuntime = &config.InitRuntimeOptions{
316+
EnvVars: claim.Spec.EnvVars,
317+
AccessToken: uuid.NewString(),
318+
}
319+
}
320+
}
277321

278322
// Validate and initialize
279323
return sandboxcr.ValidateAndInitClaimOptions(opts)

0 commit comments

Comments
 (0)