Skip to content
Open
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
11 changes: 6 additions & 5 deletions pkg/sandbox-manager/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
"testing"
"time"

"github.com/openkruise/agents/pkg/proxy"
"github.com/openkruise/agents/pkg/sandbox-manager/config"
"github.com/openkruise/agents/pkg/sandbox-manager/infra/sandboxcr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
Expand All @@ -18,6 +15,10 @@ import (
"k8s.io/client-go/util/retry"
"k8s.io/utils/ptr"

"github.com/openkruise/agents/pkg/proxy"
"github.com/openkruise/agents/pkg/sandbox-manager/config"
"github.com/openkruise/agents/pkg/sandbox-manager/infra/sandboxcr"

agentsv1alpha1 "github.com/openkruise/agents/api/v1alpha1"
"github.com/openkruise/agents/client/clientset/versioned"
"github.com/openkruise/agents/pkg/sandbox-manager/clients"
Expand Down Expand Up @@ -778,13 +779,13 @@ func TestSandboxManager_CloneSandbox(t *testing.T) {

// Decorator: DefaultCreateSandbox - set sandbox ready after creation
origCreateSandbox := sandboxcr.DefaultCreateSandbox
sandboxcr.DefaultCreateSandbox = func(ctx context.Context, sbx *agentsv1alpha1.Sandbox, c *clients.ClientSet) (*agentsv1alpha1.Sandbox, error) {
sandboxcr.DefaultCreateSandbox = func(ctx context.Context, sbx *agentsv1alpha1.Sandbox, c *clients.ClientSet, cache infra.CacheProvider) (*agentsv1alpha1.Sandbox, error) {
if override, ok := ctx.Value(sbxOverrideKey{}).(sbxOverride); ok {
if override.Name != "" {
sbx.Name = override.Name
}
}
created, err := origCreateSandbox(ctx, sbx, c)
created, err := origCreateSandbox(ctx, sbx, c, cache)
if err != nil {
return nil, err
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/sandbox-manager/infra/sandboxcr/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Cache struct {
sandboxTemplateInformer cache.SharedIndexInformer
persistentVolumeInformer cache.SharedIndexInformer
secretInformer cache.SharedIndexInformer
configmapInformer cache.SharedIndexInformer
stopCh chan struct{}
waitHooks *sync.Map // Key: client.ObjectKey; Value: *waitEntry
listSandboxesGroup singleflight.Group
Expand Down Expand Up @@ -69,6 +70,7 @@ func NewCache(client *clients.ClientSet, opts config.SandboxManagerOptions) (*Ca
k8sInformerFactoryWithSystemNs := k8sinformers.NewSharedInformerFactoryWithOptions(client.K8sClient, time.Minute*10, k8sinformers.WithNamespace(opts.SystemNamespace))
// to generate informers only for the specified namespace to avoid potential security privilege escalation risks.
secretInformer := k8sInformerFactoryWithSystemNs.Core().V1().Secrets().Informer()
configmapInformer := k8sInformerFactoryWithSystemNs.Core().V1().ConfigMaps().Informer()

if err := AddIndexersToSandboxInformer(sandboxInformer); err != nil {
return nil, err
Expand All @@ -85,6 +87,7 @@ func NewCache(client *clients.ClientSet, opts config.SandboxManagerOptions) (*Ca
k8sInformerFactory: k8sInformerFactory,
k8sInformerFactoryWithSystemNs: k8sInformerFactoryWithSystemNs,
secretInformer: secretInformer,
configmapInformer: configmapInformer,
sandboxInformer: sandboxInformer,
sandboxSetInformer: sandboxSetInformer,
checkpointInformer: checkpointInformer,
Expand Down Expand Up @@ -134,6 +137,7 @@ func (c *Cache) Run(ctx context.Context) error {
c.sandboxTemplateInformer.HasSynced,
c.persistentVolumeInformer.HasSynced,
c.secretInformer.HasSynced,
c.configmapInformer.HasSynced,
c.checkpointInformer.HasSynced) {
return fmt.Errorf("timed out waiting for caches to sync")
}
Expand Down Expand Up @@ -391,6 +395,22 @@ func (c *Cache) GetSecret(namespace, name string) (*corev1.Secret, error) {
return nil, fmt.Errorf("object with key %s is not a Secret", key)
}

// GetConfigmap retrieves a Configmap from the cache by namespace and name
func (c *Cache) GetConfigmap(namespace, name string) (*corev1.ConfigMap, error) {
key := fmt.Sprintf("%s/%s", namespace, name)
obj, exists, err := c.configmapInformer.GetStore().GetByKey(key)
if err != nil {
return nil, fmt.Errorf("failed to get configmap %s/%s from cache: %v", namespace, name, err)
}
if !exists {
return nil, fmt.Errorf("configmap %s/%s not found in cache", namespace, name)
}
if configmap, ok := obj.(*corev1.ConfigMap); ok {
return configmap, nil
}
return nil, fmt.Errorf("object with key %s is not a Configmap object", key)
}

func (c *Cache) GetCheckpoint(checkpointID string) (*agentsv1alpha1.Checkpoint, error) {
list, err := managerutils.SelectObjectWithIndex[*agentsv1alpha1.Checkpoint](c.checkpointInformer, IndexCheckpointID, checkpointID)
if err != nil {
Expand Down
47 changes: 40 additions & 7 deletions pkg/sandbox-manager/infra/sandboxcr/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ import (
"testing"
"time"

agentsv1alpha1 "github.com/openkruise/agents/api/v1alpha1"
sandboxfake "github.com/openkruise/agents/client/clientset/versioned/fake"
"github.com/openkruise/agents/pkg/sandbox-manager/clients"
"github.com/openkruise/agents/pkg/sandbox-manager/config"
constantUtils "github.com/openkruise/agents/pkg/utils"
sandboxManagerUtils "github.com/openkruise/agents/pkg/utils/sandbox-manager"
utils "github.com/openkruise/agents/pkg/utils/sandbox-manager"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
Expand All @@ -20,6 +13,14 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
k8stesting "k8s.io/client-go/testing"

agentsv1alpha1 "github.com/openkruise/agents/api/v1alpha1"
sandboxfake "github.com/openkruise/agents/client/clientset/versioned/fake"
"github.com/openkruise/agents/pkg/sandbox-manager/clients"
"github.com/openkruise/agents/pkg/sandbox-manager/config"
constantUtils "github.com/openkruise/agents/pkg/utils"
sandboxManagerUtils "github.com/openkruise/agents/pkg/utils/sandbox-manager"
utils "github.com/openkruise/agents/pkg/utils/sandbox-manager"
)

func TestCache_WaitForSandboxSatisfied(t *testing.T) {
Expand Down Expand Up @@ -445,6 +446,38 @@ func TestCache_GetSecret_FromSync(t *testing.T) {
assert.Equal(t, testSecret.Type, result.Type)
}

func TestCache_GetConfigmap_FromSync(t *testing.T) {
sandboxManagerUtils.InitLogOutput()

cache, clientSet, err := NewTestCache(t)
require.NoError(t, err)
defer cache.Stop()
k8sClient := clientSet.K8sClient

testConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-configmap-sync",
Namespace: constantUtils.DefaultSandboxDeployNamespace,
},
Data: map[string]string{
"key1": "value1",
"key2": "value2",
},
}
// Create the configmap in the cluster using the client
_, err = k8sClient.CoreV1().ConfigMaps(constantUtils.DefaultSandboxDeployNamespace).Create(context.TODO(), testConfigMap, metav1.CreateOptions{})
assert.NoError(t, err)
// Wait for cache to be ready
time.Sleep(300 * time.Millisecond)
// Verify that the configmap is found in cache
result, err := cache.GetConfigmap(constantUtils.DefaultSandboxDeployNamespace, "test-configmap-sync")
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, "test-configmap-sync", result.Name)
assert.Equal(t, constantUtils.DefaultSandboxDeployNamespace, result.Namespace)
assert.Equal(t, testConfigMap.Data, result.Data)
}

func TestCache_InformerWithFilter_GetSandboxSet(t *testing.T) {
sandboxManagerUtils.InitLogOutput()

Expand Down
11 changes: 6 additions & 5 deletions pkg/sandbox-manager/infra/sandboxcr/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"time"

"github.com/google/uuid"
commonutils "github.com/openkruise/agents/pkg/utils"
"golang.org/x/time/rate"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -22,6 +21,8 @@ import (
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"

commonutils "github.com/openkruise/agents/pkg/utils"

"github.com/openkruise/agents/api/v1alpha1"
"github.com/openkruise/agents/pkg/controller/sandboxset"
"github.com/openkruise/agents/pkg/sandbox-manager/clients"
Expand Down Expand Up @@ -131,7 +132,7 @@ func TryClaimSandbox(ctx context.Context, opts infra.ClaimSandboxOptions, pickCa
return
}

err = performLockSandbox(ctx, sbx, lockType, opts, client)
err = performLockSandbox(ctx, sbx, lockType, opts, client, cache)
if err != nil {
// TODO: these lines cannot be covered by tests currently, which will be fixed when the cache is converted to controller-runtime
log.Error(err, "failed to lock sandbox")
Expand Down Expand Up @@ -444,7 +445,7 @@ func modifyPickedSandbox(sbx *Sandbox, lockType infra.LockType, opts infra.Claim

var DefaultCreateSandbox = createSandbox

func createSandbox(ctx context.Context, sbx *v1alpha1.Sandbox, client *clients.ClientSet) (*v1alpha1.Sandbox, error) {
func createSandbox(ctx context.Context, sbx *v1alpha1.Sandbox, client *clients.ClientSet, cache infra.CacheProvider) (*v1alpha1.Sandbox, error) {
select {
case <-ctx.Done():
return nil, fmt.Errorf("context canceled before creating sandbox: %w", ctx.Err())
Expand All @@ -453,15 +454,15 @@ func createSandbox(ctx context.Context, sbx *v1alpha1.Sandbox, client *clients.C
return client.ApiV1alpha1().Sandboxes(sbx.Namespace).Create(ctx, sbx, metav1.CreateOptions{})
}

func performLockSandbox(ctx context.Context, sbx *Sandbox, lockType infra.LockType, opts infra.ClaimSandboxOptions, client *clients.ClientSet) error {
func performLockSandbox(ctx context.Context, sbx *Sandbox, lockType infra.LockType, opts infra.ClaimSandboxOptions, client *clients.ClientSet, cache infra.CacheProvider) error {
ctx = logs.Extend(ctx, "action", "performLockSandbox")
log := klog.FromContext(ctx)
utils.LockSandbox(sbx.Sandbox, opts.LockString, opts.User)
var updated *v1alpha1.Sandbox
var err error
if lockType == infra.LockTypeCreate {
log.Info("locking new sandbox via create", "sandbox", klog.KObj(sbx.Sandbox))
updated, err = DefaultCreateSandbox(ctx, sbx.Sandbox, client)
updated, err = DefaultCreateSandbox(ctx, sbx.Sandbox, client, cache)
} else {
log.Info("locking existing sandbox via update", "sandbox", klog.KObj(sbx.Sandbox))
updated, err = client.ApiV1alpha1().Sandboxes(sbx.Namespace).Update(ctx, sbx.Sandbox, metav1.UpdateOptions{})
Expand Down
11 changes: 6 additions & 5 deletions pkg/sandbox-manager/infra/sandboxcr/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
"time"

"github.com/google/uuid"
"github.com/openkruise/agents/pkg/sandbox-manager/clients"
"github.com/openkruise/agents/pkg/sandbox-manager/config"
testutils "github.com/openkruise/agents/test/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/time/rate"
Expand All @@ -21,6 +18,10 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"

"github.com/openkruise/agents/pkg/sandbox-manager/clients"
"github.com/openkruise/agents/pkg/sandbox-manager/config"
testutils "github.com/openkruise/agents/test/utils"

"github.com/openkruise/agents/api/v1alpha1"
agentsv1alpha1 "github.com/openkruise/agents/api/v1alpha1"
"github.com/openkruise/agents/client/clientset/versioned"
Expand Down Expand Up @@ -67,11 +68,11 @@ func TestInfra_ClaimSandbox(t *testing.T) {
utils.InitLogOutput()

origCreateSandbox := DefaultCreateSandbox
DefaultCreateSandbox = func(ctx context.Context, sbx *v1alpha1.Sandbox, client *clients.ClientSet) (*v1alpha1.Sandbox, error) {
DefaultCreateSandbox = func(ctx context.Context, sbx *v1alpha1.Sandbox, client *clients.ClientSet, cache infra.CacheProvider) (*v1alpha1.Sandbox, error) {
if sbx.Name == "" && sbx.GenerateName != "" {
sbx.Name = sbx.GenerateName + rand.String(5)
}
created, err := origCreateSandbox(ctx, sbx, client)
created, err := origCreateSandbox(ctx, sbx, client, cache)
if err != nil {
return nil, err
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/sandbox-manager/infra/sandboxcr/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import (
"fmt"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"

"github.com/openkruise/agents/api/v1alpha1"
"github.com/openkruise/agents/pkg/sandbox-manager/clients"
"github.com/openkruise/agents/pkg/sandbox-manager/config"
"github.com/openkruise/agents/pkg/sandbox-manager/consts"
"github.com/openkruise/agents/pkg/sandbox-manager/infra"
"github.com/openkruise/agents/pkg/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
)

var (
Expand Down Expand Up @@ -168,7 +169,7 @@ func stepCreateSandboxFromCheckpoint(ctx context.Context, opts infra.CloneSandbo
}
DefaultPostProcessClonedSandbox(sbx.Sandbox)
log.Info("creating new sandbox from checkpoint")
sbx.Sandbox, err = DefaultCreateSandbox(ctx, sbx.Sandbox, client)
sbx.Sandbox, err = DefaultCreateSandbox(ctx, sbx.Sandbox, client, cache)
if err != nil {
log.Error(err, "failed to create sandbox")
return nil, nil, metrics, err
Expand Down
19 changes: 10 additions & 9 deletions pkg/sandbox-manager/infra/sandboxcr/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import (
"testing"
"time"

"github.com/openkruise/agents/api/v1alpha1"
"github.com/openkruise/agents/pkg/sandbox-manager/clients"
"github.com/openkruise/agents/pkg/sandbox-manager/config"
"github.com/openkruise/agents/pkg/sandbox-manager/consts"
"github.com/openkruise/agents/pkg/sandbox-manager/infra"
utils "github.com/openkruise/agents/pkg/utils/sandbox-manager"
testutils "github.com/openkruise/agents/test/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/time/rate"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"

"github.com/openkruise/agents/api/v1alpha1"
"github.com/openkruise/agents/pkg/sandbox-manager/clients"
"github.com/openkruise/agents/pkg/sandbox-manager/config"
"github.com/openkruise/agents/pkg/sandbox-manager/consts"
"github.com/openkruise/agents/pkg/sandbox-manager/infra"
utils "github.com/openkruise/agents/pkg/utils/sandbox-manager"
testutils "github.com/openkruise/agents/test/utils"
)

func TestValidateAndInitCloneOptions(t *testing.T) {
Expand Down Expand Up @@ -135,7 +136,7 @@ func TestCloneSandbox(t *testing.T) {

// Decorator: DefaultCreateSandbox - set sandbox ready after creation
origCreateSandbox := DefaultCreateSandbox
DefaultCreateSandbox = func(ctx context.Context, sbx *v1alpha1.Sandbox, client *clients.ClientSet) (*v1alpha1.Sandbox, error) {
DefaultCreateSandbox = func(ctx context.Context, sbx *v1alpha1.Sandbox, client *clients.ClientSet, cache infra.CacheProvider) (*v1alpha1.Sandbox, error) {
if override, ok := ctx.Value(sbxOverrideKey{}).(sbxOverride); ok {
if override.Name != "" {
sbx.Name = override.Name
Expand All @@ -153,7 +154,7 @@ func TestCloneSandbox(t *testing.T) {
sbx.Annotations[v1alpha1.AnnotationRuntimeAccessToken] = override.AccessToken
}
}
created, err := origCreateSandbox(ctx, sbx, client)
created, err := origCreateSandbox(ctx, sbx, client, cache)
if err != nil {
return nil, err
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/sandbox-manager/infra/sandboxcr/infra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"testing"
"time"

"github.com/openkruise/agents/pkg/sandbox-manager/clients"
"github.com/openkruise/agents/pkg/sandbox-manager/config"
testutils "github.com/openkruise/agents/test/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/openkruise/agents/pkg/sandbox-manager/clients"
"github.com/openkruise/agents/pkg/sandbox-manager/config"
testutils "github.com/openkruise/agents/test/utils"

"github.com/openkruise/agents/api/v1alpha1"
"github.com/openkruise/agents/pkg/proxy"
"github.com/openkruise/agents/pkg/sandbox-manager/infra"
Expand Down Expand Up @@ -623,7 +624,7 @@ func TestInfra_CloneSandbox(t *testing.T) {

// Decorator: DefaultCreateSandbox - set sandbox ready after creation
origCreateSandbox := DefaultCreateSandbox
DefaultCreateSandbox = func(ctx context.Context, sbx *v1alpha1.Sandbox, cli *clients.ClientSet) (*v1alpha1.Sandbox, error) {
DefaultCreateSandbox = func(ctx context.Context, sbx *v1alpha1.Sandbox, cli *clients.ClientSet, cache infra.CacheProvider) (*v1alpha1.Sandbox, error) {
if override, ok := ctx.Value(infraSbxOverrideKey{}).(infraSbxOverride); ok {
if override.Name != "" {
sbx.Name = override.Name
Expand All @@ -635,7 +636,7 @@ func TestInfra_CloneSandbox(t *testing.T) {
sbx.Annotations[v1alpha1.AnnotationRuntimeURL] = override.RuntimeURL
}
}
created, err := origCreateSandbox(ctx, sbx, cli)
created, err := origCreateSandbox(ctx, sbx, cli, cache)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading