2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-07-29 09:00:15 -04:00
|
|
|
package deploy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
2023-01-03 06:09:53 -05:00
|
|
|
"time"
|
2022-07-29 09:00:15 -04:00
|
|
|
|
2023-01-04 17:28:24 -05:00
|
|
|
mainconstants "github.com/edgelesssys/constellation/v2/internal/constants"
|
2024-01-08 07:19:38 -05:00
|
|
|
updatev1alpha1 "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/api/v1alpha1"
|
|
|
|
cspapi "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/internal/cloud/api"
|
|
|
|
"github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/internal/constants"
|
2022-07-29 09:00:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-01-03 06:09:53 -05:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2022-07-29 09:00:15 -04:00
|
|
|
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2023-01-03 06:09:53 -05:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2022-07-29 09:00:15 -04:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInitialResources(t *testing.T) {
|
2023-01-03 06:09:53 -05:00
|
|
|
k8sComponentsReference := "k8s-components-sha256-ABC"
|
2022-07-29 09:00:15 -04:00
|
|
|
testCases := map[string]struct {
|
|
|
|
items []scalingGroupStoreItem
|
|
|
|
imageErr error
|
|
|
|
nameErr error
|
|
|
|
listErr error
|
|
|
|
createErr error
|
|
|
|
wantResources int
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"creating initial resources works": {
|
|
|
|
items: []scalingGroupStoreItem{
|
|
|
|
{groupID: "control-plane", image: "image-1", name: "control-plane", isControlPlane: true},
|
|
|
|
{groupID: "worker", image: "image-1", name: "worker"},
|
|
|
|
},
|
2023-07-05 01:27:34 -04:00
|
|
|
wantResources: 2,
|
2022-07-29 09:00:15 -04:00
|
|
|
},
|
2023-07-05 01:27:34 -04:00
|
|
|
"missing groups": {
|
|
|
|
items: []scalingGroupStoreItem{},
|
2022-07-29 09:00:15 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"listing groups fails": {
|
|
|
|
listErr: errors.New("list failed"),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"creating resources fails": {
|
|
|
|
items: []scalingGroupStoreItem{
|
|
|
|
{groupID: "control-plane", image: "image-1", name: "control-plane", isControlPlane: true},
|
|
|
|
{groupID: "worker", image: "image-1", name: "worker"},
|
|
|
|
},
|
|
|
|
createErr: errors.New("create failed"),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"getting image fails": {
|
|
|
|
items: []scalingGroupStoreItem{
|
|
|
|
{groupID: "control-plane", image: "image-1", name: "control-plane", isControlPlane: true},
|
|
|
|
{groupID: "worker", image: "image-1", name: "worker"},
|
|
|
|
},
|
|
|
|
imageErr: errors.New("getting image failed"),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2023-01-03 06:09:53 -05:00
|
|
|
k8sClient := &fakeK8sClient{
|
|
|
|
createErr: tc.createErr,
|
|
|
|
listConfigMaps: []corev1.ConfigMap{
|
|
|
|
{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: k8sComponentsReference,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-07-29 09:00:15 -04:00
|
|
|
scalingGroupGetter := newScalingGroupGetter(tc.items, tc.imageErr, tc.nameErr, tc.listErr)
|
2022-11-25 08:49:26 -05:00
|
|
|
err := InitialResources(context.Background(), k8sClient, &stubImageInfo{}, scalingGroupGetter, "uid")
|
2022-07-29 09:00:15 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Len(k8sClient.createdObjects, tc.wantResources)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateAutoscalingStrategy(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
createErr error
|
|
|
|
wantStrategy *updatev1alpha1.AutoscalingStrategy
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"create works": {
|
|
|
|
wantStrategy: &updatev1alpha1.AutoscalingStrategy{
|
|
|
|
TypeMeta: metav1.TypeMeta{APIVersion: "update.edgeless.systems/v1alpha1", Kind: "AutoscalingStrategy"},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: constants.AutoscalingStrategyResourceName,
|
|
|
|
},
|
|
|
|
Spec: updatev1alpha1.AutoscalingStrategySpec{
|
|
|
|
Enabled: true,
|
|
|
|
DeploymentName: "constellation-cluster-autoscaler",
|
|
|
|
DeploymentNamespace: "kube-system",
|
2022-09-15 10:48:32 -04:00
|
|
|
AutoscalerExtraArgs: map[string]string{
|
|
|
|
"cloud-provider": "stub",
|
|
|
|
"logtostderr": "true",
|
|
|
|
"stderrthreshold": "info",
|
|
|
|
"v": "2",
|
|
|
|
"namespace": "kube-system",
|
|
|
|
},
|
2022-07-29 09:00:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"create fails": {
|
|
|
|
createErr: errors.New("create failed"),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"strategy exists": {
|
|
|
|
createErr: k8sErrors.NewAlreadyExists(schema.GroupResource{}, constants.AutoscalingStrategyResourceName),
|
|
|
|
wantStrategy: &updatev1alpha1.AutoscalingStrategy{
|
|
|
|
TypeMeta: metav1.TypeMeta{APIVersion: "update.edgeless.systems/v1alpha1", Kind: "AutoscalingStrategy"},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: constants.AutoscalingStrategyResourceName,
|
|
|
|
},
|
|
|
|
Spec: updatev1alpha1.AutoscalingStrategySpec{
|
|
|
|
Enabled: true,
|
|
|
|
DeploymentName: "constellation-cluster-autoscaler",
|
|
|
|
DeploymentNamespace: "kube-system",
|
2022-09-15 10:48:32 -04:00
|
|
|
AutoscalerExtraArgs: map[string]string{
|
|
|
|
"cloud-provider": "stub",
|
|
|
|
"logtostderr": "true",
|
|
|
|
"stderrthreshold": "info",
|
|
|
|
"v": "2",
|
|
|
|
"namespace": "kube-system",
|
|
|
|
},
|
2022-07-29 09:00:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2023-01-03 06:09:53 -05:00
|
|
|
k8sClient := &fakeK8sClient{createErr: tc.createErr}
|
2022-09-15 10:48:32 -04:00
|
|
|
err := createAutoscalingStrategy(context.Background(), k8sClient, "stub")
|
2022-07-29 09:00:15 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Len(k8sClient.createdObjects, 1)
|
|
|
|
assert.Equal(tc.wantStrategy, k8sClient.createdObjects[0])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 06:09:53 -05:00
|
|
|
func TestCreateNodeVersion(t *testing.T) {
|
|
|
|
k8sComponentsReference := "k8s-components-sha256-reference"
|
2023-01-04 17:28:24 -05:00
|
|
|
k8sClusterVersion := "1.20.0"
|
2022-07-29 09:00:15 -04:00
|
|
|
testCases := map[string]struct {
|
2023-01-03 06:09:53 -05:00
|
|
|
createErr error
|
|
|
|
existingNodeVersion *updatev1alpha1.NodeVersion
|
|
|
|
wantNodeVersion *updatev1alpha1.NodeVersion
|
|
|
|
wantErr bool
|
2022-07-29 09:00:15 -04:00
|
|
|
}{
|
|
|
|
"create works": {
|
2023-01-03 06:09:53 -05:00
|
|
|
wantNodeVersion: &updatev1alpha1.NodeVersion{
|
|
|
|
TypeMeta: metav1.TypeMeta{APIVersion: "update.edgeless.systems/v1alpha1", Kind: "NodeVersion"},
|
2022-07-29 09:00:15 -04:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2023-02-09 09:54:12 -05:00
|
|
|
Name: mainconstants.NodeVersionResourceName,
|
2022-07-29 09:00:15 -04:00
|
|
|
},
|
2023-01-03 06:09:53 -05:00
|
|
|
Spec: updatev1alpha1.NodeVersionSpec{
|
|
|
|
ImageReference: "image-reference",
|
|
|
|
ImageVersion: "image-version",
|
|
|
|
KubernetesComponentsReference: k8sComponentsReference,
|
2023-01-04 17:28:24 -05:00
|
|
|
KubernetesClusterVersion: k8sClusterVersion,
|
2022-07-29 09:00:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"create fails": {
|
|
|
|
createErr: errors.New("create failed"),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-01-03 06:09:53 -05:00
|
|
|
"version exists": {
|
2023-02-09 09:54:12 -05:00
|
|
|
createErr: k8sErrors.NewAlreadyExists(schema.GroupResource{}, mainconstants.NodeVersionResourceName),
|
2023-01-03 06:09:53 -05:00
|
|
|
existingNodeVersion: &updatev1alpha1.NodeVersion{
|
|
|
|
TypeMeta: metav1.TypeMeta{APIVersion: "update.edgeless.systems/v1alpha1", Kind: "NodeVersion"},
|
2022-07-29 09:00:15 -04:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2023-02-09 09:54:12 -05:00
|
|
|
Name: mainconstants.NodeVersionResourceName,
|
2022-07-29 09:00:15 -04:00
|
|
|
},
|
2023-01-03 06:09:53 -05:00
|
|
|
Spec: updatev1alpha1.NodeVersionSpec{
|
|
|
|
ImageReference: "image-reference2",
|
|
|
|
ImageVersion: "image-version2",
|
|
|
|
KubernetesComponentsReference: "components-reference2",
|
2023-01-04 17:28:24 -05:00
|
|
|
KubernetesClusterVersion: "cluster-version2",
|
2023-01-03 06:09:53 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantNodeVersion: &updatev1alpha1.NodeVersion{
|
|
|
|
TypeMeta: metav1.TypeMeta{APIVersion: "update.edgeless.systems/v1alpha1", Kind: "NodeVersion"},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2023-02-09 09:54:12 -05:00
|
|
|
Name: mainconstants.NodeVersionResourceName,
|
2023-01-03 06:09:53 -05:00
|
|
|
},
|
|
|
|
Spec: updatev1alpha1.NodeVersionSpec{
|
|
|
|
ImageReference: "image-reference2",
|
|
|
|
ImageVersion: "image-version2",
|
|
|
|
KubernetesComponentsReference: "components-reference2",
|
2023-01-04 17:28:24 -05:00
|
|
|
KubernetesClusterVersion: "cluster-version2",
|
2022-07-29 09:00:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2023-01-03 06:09:53 -05:00
|
|
|
k8sClient := &fakeK8sClient{
|
|
|
|
createErr: tc.createErr,
|
|
|
|
listConfigMaps: []corev1.ConfigMap{
|
|
|
|
{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: k8sComponentsReference,
|
|
|
|
CreationTimestamp: metav1.Time{Time: time.Unix(1, 0)},
|
|
|
|
},
|
2023-01-04 17:28:24 -05:00
|
|
|
Data: map[string]string{
|
|
|
|
mainconstants.K8sVersionFieldName: k8sClusterVersion,
|
|
|
|
},
|
2023-01-03 06:09:53 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if tc.existingNodeVersion != nil {
|
|
|
|
k8sClient.createdObjects = append(k8sClient.createdObjects, tc.existingNodeVersion)
|
|
|
|
}
|
|
|
|
err := createNodeVersion(context.Background(), k8sClient, "image-reference", "image-version")
|
2022-07-29 09:00:15 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Len(k8sClient.createdObjects, 1)
|
2023-01-03 06:09:53 -05:00
|
|
|
assert.Equal(tc.wantNodeVersion, k8sClient.createdObjects[0])
|
2022-07-29 09:00:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 06:09:53 -05:00
|
|
|
type fakeK8sClient struct {
|
2022-07-29 09:00:15 -04:00
|
|
|
createdObjects []client.Object
|
|
|
|
createErr error
|
2023-01-03 06:09:53 -05:00
|
|
|
listConfigMaps []corev1.ConfigMap
|
|
|
|
listErr error
|
|
|
|
getErr error
|
|
|
|
updateErr error
|
|
|
|
client.Client
|
2022-07-29 09:00:15 -04:00
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *fakeK8sClient) Create(_ context.Context, obj client.Object, _ ...client.CreateOption) error {
|
2023-01-03 06:09:53 -05:00
|
|
|
for _, o := range s.createdObjects {
|
|
|
|
if obj.GetName() == o.GetName() {
|
|
|
|
return k8sErrors.NewAlreadyExists(schema.GroupResource{}, obj.GetName())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 09:00:15 -04:00
|
|
|
s.createdObjects = append(s.createdObjects, obj)
|
|
|
|
return s.createErr
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *fakeK8sClient) Get(_ context.Context, key types.NamespacedName, obj client.Object, _ ...client.GetOption) error {
|
2023-01-03 06:09:53 -05:00
|
|
|
if ObjNodeVersion, ok := obj.(*updatev1alpha1.NodeVersion); ok {
|
|
|
|
for _, o := range s.createdObjects {
|
|
|
|
if createdNodeVersion, ok := o.(*updatev1alpha1.NodeVersion); ok && createdNodeVersion != nil {
|
|
|
|
if createdNodeVersion.Name == key.Name {
|
|
|
|
ObjNodeVersion.ObjectMeta = createdNodeVersion.ObjectMeta
|
|
|
|
ObjNodeVersion.TypeMeta = createdNodeVersion.TypeMeta
|
|
|
|
ObjNodeVersion.Spec = createdNodeVersion.Spec
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.getErr
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *fakeK8sClient) Update(_ context.Context, obj client.Object, _ ...client.UpdateOption) error {
|
2023-01-03 06:09:53 -05:00
|
|
|
if updatedObjectNodeVersion, ok := obj.(*updatev1alpha1.NodeVersion); ok {
|
|
|
|
for i, o := range s.createdObjects {
|
|
|
|
if createdObjectNodeVersion, ok := o.(*updatev1alpha1.NodeVersion); ok && createdObjectNodeVersion != nil {
|
|
|
|
if createdObjectNodeVersion.Name == updatedObjectNodeVersion.Name {
|
|
|
|
s.createdObjects[i] = obj
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s.updateErr
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *fakeK8sClient) List(_ context.Context, list client.ObjectList, _ ...client.ListOption) error {
|
2023-01-03 06:09:53 -05:00
|
|
|
if configMapList, ok := list.(*corev1.ConfigMapList); ok {
|
|
|
|
configMapList.Items = append(configMapList.Items, s.listConfigMaps...)
|
|
|
|
}
|
|
|
|
return s.listErr
|
|
|
|
}
|
|
|
|
|
2022-11-25 08:49:26 -05:00
|
|
|
type stubImageInfo struct {
|
|
|
|
imageVersion string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2023-01-04 10:44:47 -05:00
|
|
|
func (s stubImageInfo) ImageVersion() (string, error) {
|
2022-11-25 08:49:26 -05:00
|
|
|
return s.imageVersion, s.err
|
|
|
|
}
|
|
|
|
|
2022-07-29 09:00:15 -04:00
|
|
|
type stubScalingGroupGetter struct {
|
|
|
|
store map[string]scalingGroupStoreItem
|
|
|
|
imageErr error
|
|
|
|
nameErr error
|
|
|
|
listErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func newScalingGroupGetter(items []scalingGroupStoreItem, imageErr, nameErr, listErr error) *stubScalingGroupGetter {
|
|
|
|
store := make(map[string]scalingGroupStoreItem)
|
|
|
|
for _, item := range items {
|
|
|
|
store[item.groupID] = item
|
|
|
|
}
|
|
|
|
return &stubScalingGroupGetter{
|
|
|
|
store: store,
|
|
|
|
imageErr: imageErr,
|
|
|
|
nameErr: nameErr,
|
|
|
|
listErr: listErr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (g *stubScalingGroupGetter) GetScalingGroupImage(_ context.Context, scalingGroupID string) (string, error) {
|
2022-07-29 09:00:15 -04:00
|
|
|
return g.store[scalingGroupID].image, g.imageErr
|
|
|
|
}
|
|
|
|
|
2022-09-15 10:48:32 -04:00
|
|
|
func (g *stubScalingGroupGetter) GetScalingGroupName(scalingGroupID string) (string, error) {
|
|
|
|
return g.store[scalingGroupID].name, g.nameErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *stubScalingGroupGetter) GetAutoscalingGroupName(scalingGroupID string) (string, error) {
|
2022-07-29 09:00:15 -04:00
|
|
|
return g.store[scalingGroupID].name, g.nameErr
|
|
|
|
}
|
|
|
|
|
2023-07-05 01:27:34 -04:00
|
|
|
func (g *stubScalingGroupGetter) ListScalingGroups(_ context.Context, _ string) ([]cspapi.ScalingGroup, error) {
|
|
|
|
var scalingGroups []cspapi.ScalingGroup
|
|
|
|
|
2022-07-29 09:00:15 -04:00
|
|
|
for _, item := range g.store {
|
2023-07-05 01:27:34 -04:00
|
|
|
scalingGroups = append(scalingGroups, cspapi.ScalingGroup{
|
|
|
|
Name: item.name,
|
|
|
|
NodeGroupName: item.nodeGroupName,
|
|
|
|
GroupID: item.groupID,
|
|
|
|
AutoscalingGroupName: item.autoscalingGroupName,
|
|
|
|
Role: func() updatev1alpha1.NodeRole {
|
|
|
|
if item.isControlPlane {
|
|
|
|
return updatev1alpha1.ControlPlaneRole
|
|
|
|
}
|
|
|
|
return updatev1alpha1.WorkerRole
|
|
|
|
}(),
|
|
|
|
})
|
2022-07-29 09:00:15 -04:00
|
|
|
}
|
2023-07-05 01:27:34 -04:00
|
|
|
return scalingGroups, g.listErr
|
2022-07-29 09:00:15 -04:00
|
|
|
}
|
|
|
|
|
2022-09-15 10:48:32 -04:00
|
|
|
func (g *stubScalingGroupGetter) AutoscalingCloudProvider() string {
|
|
|
|
return "stub"
|
|
|
|
}
|
|
|
|
|
2022-07-29 09:00:15 -04:00
|
|
|
type scalingGroupStoreItem struct {
|
2023-07-05 01:27:34 -04:00
|
|
|
name string
|
|
|
|
groupID string
|
|
|
|
autoscalingGroupName string
|
|
|
|
nodeGroupName string
|
|
|
|
image string
|
|
|
|
isControlPlane bool
|
2022-07-29 09:00:15 -04:00
|
|
|
}
|