2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
package kubecmd
|
2022-08-29 10:49:44 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-21 10:15:32 -04:00
|
|
|
"encoding/json"
|
2022-08-29 10:49:44 -04:00
|
|
|
"errors"
|
2023-02-09 09:54:12 -05:00
|
|
|
"io"
|
2022-08-29 10:49:44 -04:00
|
|
|
"testing"
|
2023-08-29 05:40:44 -04:00
|
|
|
"time"
|
2022-08-29 10:49:44 -04:00
|
|
|
|
2023-08-23 02:14:39 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2023-06-09 09:41:02 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/variant"
|
2023-05-23 03:17:27 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
2023-02-28 04:23:09 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/compatibility"
|
2023-03-03 03:38:23 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2023-02-09 09:54:12 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-03-03 03:38:23 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions"
|
2023-02-09 09:54:12 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions/components"
|
|
|
|
updatev1alpha1 "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/api/v1alpha1"
|
2022-08-29 10:49:44 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-07-24 06:15:11 -04:00
|
|
|
"github.com/stretchr/testify/mock"
|
2022-08-29 10:49:44 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
2023-08-24 10:40:47 -04:00
|
|
|
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
2023-08-23 02:14:39 -04:00
|
|
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
2023-02-09 09:54:12 -05:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2022-08-29 10:49:44 -04:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2023-02-09 09:54:12 -05:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2023-07-24 06:15:11 -04:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2022-08-29 10:49:44 -04:00
|
|
|
)
|
|
|
|
|
2023-03-03 03:38:23 -05:00
|
|
|
func TestUpgradeNodeVersion(t *testing.T) {
|
2023-02-09 09:54:12 -05:00
|
|
|
testCases := map[string]struct {
|
2023-08-21 10:15:32 -04:00
|
|
|
kubectl *stubKubectl
|
2023-03-03 03:38:23 -05:00
|
|
|
conditions []metav1.Condition
|
|
|
|
currentImageVersion string
|
2023-04-13 09:58:37 -04:00
|
|
|
newImageReference string
|
|
|
|
badImageVersion string
|
2023-03-03 03:38:23 -05:00
|
|
|
currentClusterVersion string
|
|
|
|
conf *config.Config
|
2023-06-21 09:49:42 -04:00
|
|
|
force bool
|
2023-08-21 10:15:32 -04:00
|
|
|
getCRErr error
|
2023-03-03 03:38:23 -05:00
|
|
|
wantErr bool
|
|
|
|
wantUpdate bool
|
|
|
|
assertCorrectError func(t *testing.T, err error) bool
|
2023-08-21 10:15:32 -04:00
|
|
|
customClientFn func(nodeVersion updatev1alpha1.NodeVersion) unstructuredInterface
|
2023-02-09 09:54:12 -05:00
|
|
|
}{
|
|
|
|
"success": {
|
2023-03-03 03:38:23 -05:00
|
|
|
conf: func() *config.Config {
|
|
|
|
conf := config.Default()
|
|
|
|
conf.Image = "v1.2.3"
|
|
|
|
conf.KubernetesVersion = versions.SupportedK8sVersions()[1]
|
|
|
|
return conf
|
|
|
|
}(),
|
|
|
|
currentImageVersion: "v1.2.2",
|
|
|
|
currentClusterVersion: versions.SupportedK8sVersions()[0],
|
2023-08-21 10:15:32 -04:00
|
|
|
kubectl: &stubKubectl{
|
2023-03-22 06:53:47 -04:00
|
|
|
configMaps: map[string]*corev1.ConfigMap{
|
|
|
|
constants.JoinConfigMap: newJoinConfigMap(`{"0":{"expected":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","warnOnly":false}}`),
|
2023-03-03 03:38:23 -05:00
|
|
|
},
|
2023-02-09 09:54:12 -05:00
|
|
|
},
|
2023-03-03 03:38:23 -05:00
|
|
|
wantUpdate: true,
|
2023-02-09 09:54:12 -05:00
|
|
|
},
|
2023-03-03 03:38:23 -05:00
|
|
|
"only k8s upgrade": {
|
|
|
|
conf: func() *config.Config {
|
|
|
|
conf := config.Default()
|
|
|
|
conf.Image = "v1.2.2"
|
|
|
|
conf.KubernetesVersion = versions.SupportedK8sVersions()[1]
|
|
|
|
return conf
|
|
|
|
}(),
|
|
|
|
currentImageVersion: "v1.2.2",
|
|
|
|
currentClusterVersion: versions.SupportedK8sVersions()[0],
|
2023-08-21 10:15:32 -04:00
|
|
|
kubectl: &stubKubectl{
|
2023-03-22 06:53:47 -04:00
|
|
|
configMaps: map[string]*corev1.ConfigMap{
|
|
|
|
constants.JoinConfigMap: newJoinConfigMap(`{"0":{"expected":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","warnOnly":false}}`),
|
2023-03-03 03:38:23 -05:00
|
|
|
},
|
2023-02-09 09:54:12 -05:00
|
|
|
},
|
2023-03-03 03:38:23 -05:00
|
|
|
wantUpdate: true,
|
|
|
|
wantErr: true,
|
2023-02-09 09:54:12 -05:00
|
|
|
assertCorrectError: func(t *testing.T, err error) bool {
|
2023-04-13 09:58:37 -04:00
|
|
|
var upgradeErr *compatibility.InvalidUpgradeError
|
2023-03-03 03:38:23 -05:00
|
|
|
return assert.ErrorAs(t, err, &upgradeErr)
|
2023-02-09 09:54:12 -05:00
|
|
|
},
|
|
|
|
},
|
2023-03-03 03:38:23 -05:00
|
|
|
"only image upgrade": {
|
|
|
|
conf: func() *config.Config {
|
|
|
|
conf := config.Default()
|
|
|
|
conf.Image = "v1.2.3"
|
|
|
|
conf.KubernetesVersion = versions.SupportedK8sVersions()[0]
|
|
|
|
return conf
|
|
|
|
}(),
|
|
|
|
currentImageVersion: "v1.2.2",
|
|
|
|
currentClusterVersion: versions.SupportedK8sVersions()[0],
|
2023-08-21 10:15:32 -04:00
|
|
|
kubectl: &stubKubectl{
|
2023-03-22 06:53:47 -04:00
|
|
|
configMaps: map[string]*corev1.ConfigMap{
|
|
|
|
constants.JoinConfigMap: newJoinConfigMap(`{"0":{"expected":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","warnOnly":false}}`),
|
2023-02-09 09:54:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantUpdate: true,
|
2023-03-03 03:38:23 -05:00
|
|
|
wantErr: true,
|
2023-02-09 09:54:12 -05:00
|
|
|
assertCorrectError: func(t *testing.T, err error) bool {
|
2023-04-13 09:58:37 -04:00
|
|
|
var upgradeErr *compatibility.InvalidUpgradeError
|
2023-03-03 03:38:23 -05:00
|
|
|
return assert.ErrorAs(t, err, &upgradeErr)
|
2023-02-09 09:54:12 -05:00
|
|
|
},
|
|
|
|
},
|
2023-03-03 03:38:23 -05:00
|
|
|
"not an upgrade": {
|
|
|
|
conf: func() *config.Config {
|
|
|
|
conf := config.Default()
|
|
|
|
conf.Image = "v1.2.2"
|
|
|
|
conf.KubernetesVersion = versions.SupportedK8sVersions()[0]
|
|
|
|
return conf
|
|
|
|
}(),
|
|
|
|
currentImageVersion: "v1.2.2",
|
|
|
|
currentClusterVersion: versions.SupportedK8sVersions()[0],
|
2023-08-21 10:15:32 -04:00
|
|
|
kubectl: &stubKubectl{},
|
2023-03-03 03:38:23 -05:00
|
|
|
wantErr: true,
|
2023-02-09 09:54:12 -05:00
|
|
|
assertCorrectError: func(t *testing.T, err error) bool {
|
2023-04-13 09:58:37 -04:00
|
|
|
var upgradeErr *compatibility.InvalidUpgradeError
|
2023-03-03 03:38:23 -05:00
|
|
|
return assert.ErrorAs(t, err, &upgradeErr)
|
2023-02-09 09:54:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"upgrade in progress": {
|
2023-03-03 03:38:23 -05:00
|
|
|
conf: func() *config.Config {
|
|
|
|
conf := config.Default()
|
|
|
|
conf.Image = "v1.2.3"
|
|
|
|
conf.KubernetesVersion = versions.SupportedK8sVersions()[1]
|
|
|
|
return conf
|
|
|
|
}(),
|
2023-02-09 09:54:12 -05:00
|
|
|
conditions: []metav1.Condition{{
|
|
|
|
Type: updatev1alpha1.ConditionOutdated,
|
|
|
|
Status: metav1.ConditionTrue,
|
|
|
|
}},
|
2023-03-03 03:38:23 -05:00
|
|
|
currentImageVersion: "v1.2.2",
|
|
|
|
currentClusterVersion: versions.SupportedK8sVersions()[0],
|
2023-08-21 10:15:32 -04:00
|
|
|
kubectl: &stubKubectl{},
|
2023-03-03 03:38:23 -05:00
|
|
|
wantErr: true,
|
2023-02-09 09:54:12 -05:00
|
|
|
assertCorrectError: func(t *testing.T, err error) bool {
|
|
|
|
return assert.ErrorIs(t, err, ErrInProgress)
|
|
|
|
},
|
|
|
|
},
|
2023-06-21 09:49:42 -04:00
|
|
|
"success with force and upgrade in progress": {
|
|
|
|
conf: func() *config.Config {
|
|
|
|
conf := config.Default()
|
|
|
|
conf.Image = "v1.2.3"
|
|
|
|
conf.KubernetesVersion = versions.SupportedK8sVersions()[1]
|
|
|
|
return conf
|
|
|
|
}(),
|
|
|
|
conditions: []metav1.Condition{{
|
|
|
|
Type: updatev1alpha1.ConditionOutdated,
|
|
|
|
Status: metav1.ConditionTrue,
|
|
|
|
}},
|
|
|
|
currentImageVersion: "v1.2.2",
|
|
|
|
currentClusterVersion: versions.SupportedK8sVersions()[0],
|
2023-08-21 10:15:32 -04:00
|
|
|
kubectl: &stubKubectl{},
|
2023-06-21 09:49:42 -04:00
|
|
|
force: true,
|
|
|
|
wantUpdate: true,
|
|
|
|
},
|
2023-02-09 09:54:12 -05:00
|
|
|
"get error": {
|
2023-03-03 03:38:23 -05:00
|
|
|
conf: func() *config.Config {
|
|
|
|
conf := config.Default()
|
|
|
|
conf.Image = "v1.2.3"
|
2023-08-21 10:15:32 -04:00
|
|
|
conf.KubernetesVersion = versions.SupportedK8sVersions()[1]
|
2023-03-03 03:38:23 -05:00
|
|
|
return conf
|
|
|
|
}(),
|
2023-08-21 10:15:32 -04:00
|
|
|
currentImageVersion: "v1.2.2",
|
|
|
|
currentClusterVersion: versions.SupportedK8sVersions()[0],
|
|
|
|
kubectl: &stubKubectl{
|
|
|
|
configMaps: map[string]*corev1.ConfigMap{
|
|
|
|
constants.JoinConfigMap: newJoinConfigMap(`{"0":{"expected":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","warnOnly":false}}`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
getCRErr: assert.AnError,
|
|
|
|
wantErr: true,
|
2023-02-09 09:54:12 -05:00
|
|
|
assertCorrectError: func(t *testing.T, err error) bool {
|
2023-08-21 10:15:32 -04:00
|
|
|
return assert.ErrorIs(t, err, assert.AnError)
|
2023-02-09 09:54:12 -05:00
|
|
|
},
|
|
|
|
},
|
2023-04-13 09:58:37 -04:00
|
|
|
"image too new valid k8s": {
|
|
|
|
conf: func() *config.Config {
|
|
|
|
conf := config.Default()
|
|
|
|
conf.Image = "v1.4.2"
|
|
|
|
conf.KubernetesVersion = versions.SupportedK8sVersions()[1]
|
|
|
|
return conf
|
|
|
|
}(),
|
|
|
|
newImageReference: "path/to/image:v1.4.2",
|
|
|
|
currentImageVersion: "v1.2.2",
|
|
|
|
currentClusterVersion: versions.SupportedK8sVersions()[0],
|
2023-08-21 10:15:32 -04:00
|
|
|
kubectl: &stubKubectl{
|
2023-04-13 09:58:37 -04:00
|
|
|
configMaps: map[string]*corev1.ConfigMap{
|
2023-08-11 09:18:59 -04:00
|
|
|
constants.JoinConfigMap: newJoinConfigMap(`{"0":{"expected":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","warnOnly":true}}`),
|
2023-04-13 09:58:37 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantUpdate: true,
|
|
|
|
wantErr: true,
|
|
|
|
assertCorrectError: func(t *testing.T, err error) bool {
|
2023-06-30 10:46:05 -04:00
|
|
|
var upgradeErr *compatibility.InvalidUpgradeError
|
2023-04-13 09:58:37 -04:00
|
|
|
return assert.ErrorAs(t, err, &upgradeErr)
|
|
|
|
},
|
|
|
|
},
|
2023-06-21 09:49:42 -04:00
|
|
|
"success with force and image too new": {
|
|
|
|
conf: func() *config.Config {
|
|
|
|
conf := config.Default()
|
|
|
|
conf.Image = "v1.4.2"
|
|
|
|
conf.KubernetesVersion = versions.SupportedK8sVersions()[1]
|
|
|
|
return conf
|
|
|
|
}(),
|
|
|
|
newImageReference: "path/to/image:v1.4.2",
|
|
|
|
currentImageVersion: "v1.2.2",
|
|
|
|
currentClusterVersion: versions.SupportedK8sVersions()[0],
|
2023-08-21 10:15:32 -04:00
|
|
|
kubectl: &stubKubectl{
|
2023-06-21 09:49:42 -04:00
|
|
|
configMaps: map[string]*corev1.ConfigMap{
|
|
|
|
constants.JoinConfigMap: newJoinConfigMap(`{"0":{"expected":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","warnOnly":false}}`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantUpdate: true,
|
|
|
|
force: true,
|
|
|
|
},
|
2023-04-13 09:58:37 -04:00
|
|
|
"apply returns bad object": {
|
|
|
|
conf: func() *config.Config {
|
|
|
|
conf := config.Default()
|
|
|
|
conf.Image = "v1.2.3"
|
|
|
|
conf.KubernetesVersion = versions.SupportedK8sVersions()[1]
|
|
|
|
return conf
|
|
|
|
}(),
|
|
|
|
currentImageVersion: "v1.2.2",
|
|
|
|
currentClusterVersion: versions.SupportedK8sVersions()[0],
|
|
|
|
badImageVersion: "v3.2.1",
|
2023-08-21 10:15:32 -04:00
|
|
|
kubectl: &stubKubectl{
|
2023-04-13 09:58:37 -04:00
|
|
|
configMaps: map[string]*corev1.ConfigMap{
|
|
|
|
constants.JoinConfigMap: newJoinConfigMap(`{"0":{"expected":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","warnOnly":false}}`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantUpdate: true,
|
|
|
|
wantErr: true,
|
|
|
|
assertCorrectError: func(t *testing.T, err error) bool {
|
|
|
|
var target *applyError
|
|
|
|
return assert.ErrorAs(t, err, &target)
|
|
|
|
},
|
|
|
|
},
|
2023-06-05 03:13:02 -04:00
|
|
|
"outdated k8s version skips k8s upgrade": {
|
|
|
|
conf: func() *config.Config {
|
|
|
|
conf := config.Default()
|
|
|
|
conf.Image = "v1.2.2"
|
|
|
|
conf.KubernetesVersion = "v1.25.8"
|
|
|
|
return conf
|
|
|
|
}(),
|
|
|
|
currentImageVersion: "v1.2.2",
|
|
|
|
currentClusterVersion: versions.SupportedK8sVersions()[0],
|
2023-08-21 10:15:32 -04:00
|
|
|
kubectl: &stubKubectl{
|
2023-06-05 03:13:02 -04:00
|
|
|
configMaps: map[string]*corev1.ConfigMap{
|
|
|
|
constants.JoinConfigMap: newJoinConfigMap(`{"0":{"expected":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","warnOnly":false}}`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantUpdate: false,
|
|
|
|
wantErr: true,
|
|
|
|
assertCorrectError: func(t *testing.T, err error) bool {
|
|
|
|
var upgradeErr *compatibility.InvalidUpgradeError
|
|
|
|
return assert.ErrorAs(t, err, &upgradeErr)
|
|
|
|
},
|
|
|
|
},
|
2023-07-24 06:15:11 -04:00
|
|
|
"succeed after update retry when the updated node object is outdated": {
|
|
|
|
conf: func() *config.Config {
|
|
|
|
conf := config.Default()
|
|
|
|
conf.Image = "v1.2.3"
|
|
|
|
conf.KubernetesVersion = versions.SupportedK8sVersions()[1]
|
|
|
|
return conf
|
|
|
|
}(),
|
|
|
|
currentImageVersion: "v1.2.2",
|
|
|
|
currentClusterVersion: versions.SupportedK8sVersions()[0],
|
2023-08-21 10:15:32 -04:00
|
|
|
kubectl: &stubKubectl{
|
2023-07-24 06:15:11 -04:00
|
|
|
configMaps: map[string]*corev1.ConfigMap{
|
|
|
|
constants.JoinConfigMap: newJoinConfigMap(`{"0":{"expected":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","warnOnly":false}}`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantUpdate: false, // because customClient is used
|
2023-08-21 10:15:32 -04:00
|
|
|
customClientFn: func(nodeVersion updatev1alpha1.NodeVersion) unstructuredInterface {
|
|
|
|
fakeClient := &fakeUnstructuredClient{}
|
|
|
|
fakeClient.On("GetCR", mock.Anything, mock.Anything).Return(unstructedObjectWithGeneration(nodeVersion, 1), nil)
|
2023-08-23 02:14:39 -04:00
|
|
|
fakeClient.On("UpdateCR", mock.Anything, mock.Anything).Return(nil, k8serrors.NewConflict(schema.GroupResource{Resource: nodeVersion.Name}, nodeVersion.Name, nil)).Once()
|
2023-08-21 10:15:32 -04:00
|
|
|
fakeClient.On("UpdateCR", mock.Anything, mock.Anything).Return(unstructedObjectWithGeneration(nodeVersion, 2), nil).Once()
|
2023-07-24 06:15:11 -04:00
|
|
|
return fakeClient
|
|
|
|
},
|
|
|
|
},
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
nodeVersion := updatev1alpha1.NodeVersion{
|
|
|
|
Spec: updatev1alpha1.NodeVersionSpec{
|
2023-03-03 03:38:23 -05:00
|
|
|
ImageVersion: tc.currentImageVersion,
|
|
|
|
KubernetesClusterVersion: tc.currentClusterVersion,
|
2023-02-09 09:54:12 -05:00
|
|
|
},
|
|
|
|
Status: updatev1alpha1.NodeVersionStatus{
|
|
|
|
Conditions: tc.conditions,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-04-13 09:58:37 -04:00
|
|
|
var badUpdatedObject *unstructured.Unstructured
|
|
|
|
if tc.badImageVersion != "" {
|
|
|
|
nodeVersion.Spec.ImageVersion = tc.badImageVersion
|
|
|
|
unstrBadNodeVersion, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&nodeVersion)
|
|
|
|
require.NoError(err)
|
|
|
|
badUpdatedObject = &unstructured.Unstructured{Object: unstrBadNodeVersion}
|
|
|
|
}
|
|
|
|
|
2023-07-24 06:15:11 -04:00
|
|
|
unstrNodeVersion, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&nodeVersion)
|
|
|
|
require.NoError(err)
|
2023-08-21 10:15:32 -04:00
|
|
|
unstructuredClient := &stubUnstructuredClient{
|
|
|
|
object: &unstructured.Unstructured{Object: unstrNodeVersion},
|
|
|
|
badUpdatedObject: badUpdatedObject,
|
|
|
|
getCRErr: tc.getCRErr,
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
2023-08-21 10:15:32 -04:00
|
|
|
tc.kubectl.unstructuredInterface = unstructuredClient
|
2023-07-24 06:15:11 -04:00
|
|
|
if tc.customClientFn != nil {
|
2023-08-21 10:15:32 -04:00
|
|
|
tc.kubectl.unstructuredInterface = tc.customClientFn(nodeVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
upgrader := KubeCmd{
|
|
|
|
kubectl: tc.kubectl,
|
|
|
|
imageFetcher: &stubImageFetcher{reference: tc.newImageReference},
|
|
|
|
log: logger.NewTest(t),
|
|
|
|
outWriter: io.Discard,
|
2023-07-24 06:15:11 -04:00
|
|
|
}
|
2023-02-09 09:54:12 -05:00
|
|
|
|
2023-09-08 08:55:07 -04:00
|
|
|
err = upgrader.UpgradeNodeVersion(context.Background(), tc.conf, tc.force, false, false)
|
2023-02-09 09:54:12 -05:00
|
|
|
// Check upgrades first because if we checked err first, UpgradeImage may error due to other reasons and still trigger an upgrade.
|
|
|
|
if tc.wantUpdate {
|
2023-08-21 10:15:32 -04:00
|
|
|
assert.NotNil(unstructuredClient.updatedObject)
|
2023-02-09 09:54:12 -05:00
|
|
|
} else {
|
2023-08-21 10:15:32 -04:00
|
|
|
assert.Nil(unstructuredClient.updatedObject)
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
tc.assertCorrectError(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 10:49:44 -04:00
|
|
|
func TestUpdateImage(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
testCases := map[string]struct {
|
2022-11-29 05:39:07 -05:00
|
|
|
newImageReference string
|
|
|
|
newImageVersion string
|
2023-03-03 03:38:23 -05:00
|
|
|
oldImageReference string
|
2023-02-09 09:54:12 -05:00
|
|
|
oldImageVersion string
|
|
|
|
updateErr error
|
2022-11-29 05:39:07 -05:00
|
|
|
wantUpdate bool
|
|
|
|
wantErr bool
|
2022-08-29 10:49:44 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
2023-03-03 03:38:23 -05:00
|
|
|
oldImageReference: "old-image-ref",
|
|
|
|
oldImageVersion: "v0.0.0",
|
|
|
|
newImageReference: "new-image-ref",
|
|
|
|
newImageVersion: "v0.1.0",
|
|
|
|
wantUpdate: true,
|
|
|
|
},
|
|
|
|
"same version fails": {
|
|
|
|
oldImageVersion: "v0.0.0",
|
|
|
|
newImageVersion: "v0.0.0",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"update error": {
|
|
|
|
updateErr: someErr,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
upgrader := &KubeCmd{
|
2023-03-03 03:38:23 -05:00
|
|
|
log: logger.NewTest(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeVersion := updatev1alpha1.NodeVersion{
|
2023-02-09 09:54:12 -05:00
|
|
|
Spec: updatev1alpha1.NodeVersionSpec{
|
2023-03-03 03:38:23 -05:00
|
|
|
ImageReference: tc.oldImageReference,
|
|
|
|
ImageVersion: tc.oldImageVersion,
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
2023-03-03 03:38:23 -05:00
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
err := upgrader.isValidImageUpgrade(nodeVersion, tc.newImageVersion, false)
|
2023-03-03 03:38:23 -05:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateK8s(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
testCases := map[string]struct {
|
|
|
|
newClusterVersion string
|
|
|
|
oldClusterVersion string
|
|
|
|
updateErr error
|
|
|
|
wantUpdate bool
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
oldClusterVersion: "v0.0.0",
|
|
|
|
newClusterVersion: "v0.1.0",
|
2022-11-29 05:39:07 -05:00
|
|
|
wantUpdate: true,
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
2023-03-03 03:38:23 -05:00
|
|
|
"same version fails": {
|
|
|
|
oldClusterVersion: "v0.0.0",
|
|
|
|
newClusterVersion: "v0.0.0",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-08-29 10:49:44 -04:00
|
|
|
"update error": {
|
2023-02-09 09:54:12 -05:00
|
|
|
updateErr: someErr,
|
|
|
|
wantErr: true,
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
upgrader := &KubeCmd{
|
2023-03-03 03:38:23 -05:00
|
|
|
log: logger.NewTest(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeVersion := updatev1alpha1.NodeVersion{
|
|
|
|
Spec: updatev1alpha1.NodeVersionSpec{
|
|
|
|
KubernetesClusterVersion: tc.oldClusterVersion,
|
|
|
|
},
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-06-21 09:49:42 -04:00
|
|
|
_, err := upgrader.updateK8s(&nodeVersion, tc.newClusterVersion, components.Components{}, false)
|
2022-08-29 10:49:44 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
if tc.wantUpdate {
|
2023-03-03 03:38:23 -05:00
|
|
|
assert.Equal(tc.newClusterVersion, nodeVersion.Spec.KubernetesClusterVersion)
|
2022-08-29 10:49:44 -04:00
|
|
|
} else {
|
2023-03-03 03:38:23 -05:00
|
|
|
assert.Equal(tc.oldClusterVersion, nodeVersion.Spec.KubernetesClusterVersion)
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 06:53:47 -04:00
|
|
|
func newJoinConfigMap(data string) *corev1.ConfigMap {
|
|
|
|
return &corev1.ConfigMap{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: constants.JoinConfigMap,
|
|
|
|
},
|
|
|
|
Data: map[string]string{
|
2023-05-03 05:11:53 -04:00
|
|
|
constants.AttestationConfigFilename: data,
|
2023-03-22 06:53:47 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 05:40:44 -04:00
|
|
|
func TestApplyJoinConfig(t *testing.T) {
|
2023-08-21 10:15:32 -04:00
|
|
|
mustMarshal := func(cfg config.AttestationCfg) string {
|
|
|
|
data, err := json.Marshal(cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return string(data)
|
|
|
|
}
|
2023-08-29 05:40:44 -04:00
|
|
|
// repeatedErrors returns the given error multiple times
|
|
|
|
// This is needed in tests, since the retry logic will retry multiple times
|
|
|
|
// If the retry limit is raised in [KubeCmd.ApplyJoinConfig], it should also
|
|
|
|
// be updated here
|
|
|
|
repeatedErrors := func(err error) []error {
|
|
|
|
var errs []error
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|
2023-08-21 10:15:32 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
newAttestationCfg config.AttestationCfg
|
2023-08-29 05:40:44 -04:00
|
|
|
kubectl *fakeConfigMapClient
|
|
|
|
wantUpdate bool
|
2023-08-21 10:15:32 -04:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
2023-08-23 02:14:39 -04:00
|
|
|
newAttestationCfg: &config.QEMUVTPM{
|
|
|
|
Measurements: measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0x00, measurements.WarnOnly, measurements.PCRMeasurementLength),
|
|
|
|
},
|
|
|
|
},
|
2023-08-29 05:40:44 -04:00
|
|
|
kubectl: &fakeConfigMapClient{
|
2023-08-21 10:15:32 -04:00
|
|
|
configMaps: map[string]*corev1.ConfigMap{
|
2023-08-23 02:14:39 -04:00
|
|
|
constants.JoinConfigMap: newJoinConfigMap(mustMarshal(&config.QEMUVTPM{
|
|
|
|
Measurements: measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0xFF, measurements.WarnOnly, measurements.PCRMeasurementLength),
|
|
|
|
},
|
|
|
|
})),
|
2023-08-21 10:15:32 -04:00
|
|
|
},
|
|
|
|
},
|
2023-08-29 05:40:44 -04:00
|
|
|
wantUpdate: true,
|
2023-08-21 10:15:32 -04:00
|
|
|
},
|
2023-08-23 02:14:39 -04:00
|
|
|
"Get ConfigMap error": {
|
|
|
|
newAttestationCfg: &config.QEMUVTPM{
|
|
|
|
Measurements: measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0x00, measurements.WarnOnly, measurements.PCRMeasurementLength),
|
|
|
|
},
|
|
|
|
},
|
2023-08-29 05:40:44 -04:00
|
|
|
kubectl: &fakeConfigMapClient{
|
|
|
|
getErrs: repeatedErrors(assert.AnError),
|
2023-08-21 10:15:32 -04:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-08-29 05:40:44 -04:00
|
|
|
"Get ConfigMap fails then returns ConfigMap": {
|
|
|
|
newAttestationCfg: &config.QEMUVTPM{
|
|
|
|
Measurements: measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0x00, measurements.WarnOnly, measurements.PCRMeasurementLength),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
kubectl: &fakeConfigMapClient{
|
|
|
|
configMaps: map[string]*corev1.ConfigMap{
|
|
|
|
constants.JoinConfigMap: newJoinConfigMap(mustMarshal(&config.QEMUVTPM{
|
|
|
|
Measurements: measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0xFF, measurements.WarnOnly, measurements.PCRMeasurementLength),
|
|
|
|
},
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
getErrs: []error{assert.AnError, assert.AnError},
|
|
|
|
},
|
|
|
|
wantUpdate: true,
|
|
|
|
},
|
|
|
|
"Get ConfigMap fails then fails with NotFound": {
|
|
|
|
newAttestationCfg: &config.QEMUVTPM{
|
|
|
|
Measurements: measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0x00, measurements.WarnOnly, measurements.PCRMeasurementLength),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
kubectl: &fakeConfigMapClient{
|
|
|
|
getErrs: []error{assert.AnError, assert.AnError, k8serrors.NewNotFound(schema.GroupResource{}, "")},
|
|
|
|
},
|
|
|
|
},
|
2023-08-23 02:14:39 -04:00
|
|
|
"ConfigMap does not exist yet": {
|
|
|
|
newAttestationCfg: &config.QEMUVTPM{
|
|
|
|
Measurements: measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0x00, measurements.WarnOnly, measurements.PCRMeasurementLength),
|
|
|
|
},
|
|
|
|
},
|
2023-08-29 05:40:44 -04:00
|
|
|
kubectl: &fakeConfigMapClient{
|
|
|
|
getErrs: repeatedErrors(k8serrors.NewNotFound(schema.GroupResource{}, "")),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"Create ConfigMap fails": {
|
|
|
|
newAttestationCfg: &config.QEMUVTPM{
|
|
|
|
Measurements: measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0x00, measurements.WarnOnly, measurements.PCRMeasurementLength),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
kubectl: &fakeConfigMapClient{
|
|
|
|
getErrs: repeatedErrors(k8serrors.NewNotFound(schema.GroupResource{}, "")),
|
|
|
|
createErrs: repeatedErrors(assert.AnError),
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"Create ConfigMap fails then succeeds": {
|
|
|
|
newAttestationCfg: &config.QEMUVTPM{
|
|
|
|
Measurements: measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0x00, measurements.WarnOnly, measurements.PCRMeasurementLength),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
kubectl: &fakeConfigMapClient{
|
|
|
|
getErrs: repeatedErrors(k8serrors.NewNotFound(schema.GroupResource{}, "")),
|
|
|
|
createErrs: []error{assert.AnError, assert.AnError},
|
2023-08-23 02:14:39 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"Update ConfigMap error": {
|
|
|
|
newAttestationCfg: &config.QEMUVTPM{
|
|
|
|
Measurements: measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0x00, measurements.WarnOnly, measurements.PCRMeasurementLength),
|
|
|
|
},
|
|
|
|
},
|
2023-08-29 05:40:44 -04:00
|
|
|
kubectl: &fakeConfigMapClient{
|
2023-08-23 02:14:39 -04:00
|
|
|
configMaps: map[string]*corev1.ConfigMap{
|
|
|
|
constants.JoinConfigMap: newJoinConfigMap(mustMarshal(&config.QEMUVTPM{
|
|
|
|
Measurements: measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0xFF, measurements.WarnOnly, measurements.PCRMeasurementLength),
|
|
|
|
},
|
|
|
|
})),
|
|
|
|
},
|
2023-08-29 05:40:44 -04:00
|
|
|
updateErrs: repeatedErrors(assert.AnError),
|
2023-08-23 02:14:39 -04:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-08-29 05:40:44 -04:00
|
|
|
"Update ConfigMap fails then succeeds": {
|
|
|
|
newAttestationCfg: &config.QEMUVTPM{
|
|
|
|
Measurements: measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0x00, measurements.WarnOnly, measurements.PCRMeasurementLength),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
kubectl: &fakeConfigMapClient{
|
|
|
|
configMaps: map[string]*corev1.ConfigMap{
|
|
|
|
constants.JoinConfigMap: newJoinConfigMap(mustMarshal(&config.QEMUVTPM{
|
|
|
|
Measurements: measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0xFF, measurements.WarnOnly, measurements.PCRMeasurementLength),
|
|
|
|
},
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
updateErrs: []error{assert.AnError, assert.AnError},
|
|
|
|
},
|
|
|
|
wantUpdate: true,
|
|
|
|
},
|
2023-08-21 10:15:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2023-08-23 02:14:39 -04:00
|
|
|
require := require.New(t)
|
2023-08-21 10:15:32 -04:00
|
|
|
|
|
|
|
cmd := &KubeCmd{
|
2023-08-29 05:40:44 -04:00
|
|
|
kubectl: tc.kubectl,
|
|
|
|
log: logger.NewTest(t),
|
|
|
|
retryInterval: time.Millisecond,
|
|
|
|
outWriter: io.Discard,
|
2023-08-21 10:15:32 -04:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:14:39 -04:00
|
|
|
err := cmd.ApplyJoinConfig(context.Background(), tc.newAttestationCfg, []byte{0x11})
|
2023-08-21 10:15:32 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
2023-08-29 05:40:44 -04:00
|
|
|
|
|
|
|
var cfg *corev1.ConfigMap
|
|
|
|
var ok bool
|
|
|
|
if tc.wantUpdate {
|
|
|
|
cfg, ok = tc.kubectl.updatedConfigMaps[constants.JoinConfigMap]
|
|
|
|
} else {
|
|
|
|
cfg, ok = tc.kubectl.configMaps[constants.JoinConfigMap]
|
|
|
|
}
|
2023-08-23 02:14:39 -04:00
|
|
|
require.True(ok)
|
|
|
|
assert.Equal(mustMarshal(tc.newAttestationCfg), cfg.Data[constants.AttestationConfigFilename])
|
2023-08-21 10:15:32 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeUnstructuredClient struct {
|
2023-07-24 06:15:11 -04:00
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
func (u *fakeUnstructuredClient) GetCR(ctx context.Context, _ schema.GroupVersionResource, str string) (*unstructured.Unstructured, error) {
|
2023-07-24 06:15:11 -04:00
|
|
|
args := u.Called(ctx, str)
|
|
|
|
return args.Get(0).(*unstructured.Unstructured), args.Error(1)
|
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
func (u *fakeUnstructuredClient) UpdateCR(ctx context.Context, _ schema.GroupVersionResource, updatedObject *unstructured.Unstructured) (*unstructured.Unstructured, error) {
|
2023-07-24 06:15:11 -04:00
|
|
|
args := u.Called(ctx, updatedObject)
|
|
|
|
if args.Get(0) == nil {
|
|
|
|
return nil, args.Error(1)
|
|
|
|
}
|
|
|
|
return updatedObject, args.Error(1)
|
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
type stubUnstructuredClient struct {
|
2023-04-13 09:58:37 -04:00
|
|
|
object *unstructured.Unstructured
|
|
|
|
updatedObject *unstructured.Unstructured
|
|
|
|
badUpdatedObject *unstructured.Unstructured
|
2023-08-21 10:15:32 -04:00
|
|
|
getCRErr error
|
|
|
|
updateCRErr error
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
func (u *stubUnstructuredClient) GetCR(_ context.Context, _ schema.GroupVersionResource, _ string) (*unstructured.Unstructured, error) {
|
|
|
|
return u.object, u.getCRErr
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
func (u *stubUnstructuredClient) UpdateCR(_ context.Context, _ schema.GroupVersionResource, updatedObject *unstructured.Unstructured) (*unstructured.Unstructured, error) {
|
2023-02-09 09:54:12 -05:00
|
|
|
u.updatedObject = updatedObject
|
2023-04-13 09:58:37 -04:00
|
|
|
if u.badUpdatedObject != nil {
|
2023-08-21 10:15:32 -04:00
|
|
|
return u.badUpdatedObject, u.updateCRErr
|
2023-04-13 09:58:37 -04:00
|
|
|
}
|
2023-08-21 10:15:32 -04:00
|
|
|
return u.updatedObject, u.updateCRErr
|
|
|
|
}
|
|
|
|
|
|
|
|
type unstructuredInterface interface {
|
|
|
|
GetCR(ctx context.Context, gvr schema.GroupVersionResource, name string) (*unstructured.Unstructured, error)
|
|
|
|
UpdateCR(ctx context.Context, gvr schema.GroupVersionResource, obj *unstructured.Unstructured) (*unstructured.Unstructured, error)
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
type stubKubectl struct {
|
|
|
|
unstructuredInterface
|
2023-03-22 06:53:47 -04:00
|
|
|
configMaps map[string]*corev1.ConfigMap
|
|
|
|
updatedConfigMaps map[string]*corev1.ConfigMap
|
|
|
|
k8sVersion string
|
2023-08-21 10:15:32 -04:00
|
|
|
getCMErr error
|
|
|
|
updateCMErr error
|
|
|
|
createCMErr error
|
2023-03-22 06:53:47 -04:00
|
|
|
k8sErr error
|
2023-08-21 10:15:32 -04:00
|
|
|
nodes []corev1.Node
|
|
|
|
nodesErr error
|
2023-08-24 10:40:47 -04:00
|
|
|
crds []apiextensionsv1.CustomResourceDefinition
|
|
|
|
getCRDsError error
|
|
|
|
crs []unstructured.Unstructured
|
|
|
|
getCRsError error
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
func (s *stubKubectl) GetConfigMap(_ context.Context, _, name string) (*corev1.ConfigMap, error) {
|
|
|
|
return s.configMaps[name], s.getCMErr
|
2023-02-09 09:54:12 -05:00
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
func (s *stubKubectl) UpdateConfigMap(_ context.Context, configMap *corev1.ConfigMap) (*corev1.ConfigMap, error) {
|
2023-03-22 06:53:47 -04:00
|
|
|
if s.updatedConfigMaps == nil {
|
|
|
|
s.updatedConfigMaps = map[string]*corev1.ConfigMap{}
|
|
|
|
}
|
|
|
|
s.updatedConfigMaps[configMap.ObjectMeta.Name] = configMap
|
2023-08-21 10:15:32 -04:00
|
|
|
return s.updatedConfigMaps[configMap.ObjectMeta.Name], s.updateCMErr
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
func (s *stubKubectl) CreateConfigMap(_ context.Context, configMap *corev1.ConfigMap) error {
|
2023-03-22 06:53:47 -04:00
|
|
|
if s.configMaps == nil {
|
|
|
|
s.configMaps = map[string]*corev1.ConfigMap{}
|
|
|
|
}
|
|
|
|
s.configMaps[configMap.ObjectMeta.Name] = configMap
|
2023-08-21 10:15:32 -04:00
|
|
|
return s.createCMErr
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
func (s *stubKubectl) KubernetesVersion() (string, error) {
|
2023-02-09 09:54:12 -05:00
|
|
|
return s.k8sVersion, s.k8sErr
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
2023-03-30 10:13:14 -04:00
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
func (s *stubKubectl) GetNodes(_ context.Context) ([]corev1.Node, error) {
|
|
|
|
return s.nodes, s.nodesErr
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:13:14 -04:00
|
|
|
type stubImageFetcher struct {
|
|
|
|
reference string
|
|
|
|
fetchReferenceErr error
|
|
|
|
}
|
|
|
|
|
2023-05-23 03:17:27 -04:00
|
|
|
func (f *stubImageFetcher) FetchReference(_ context.Context,
|
|
|
|
_ cloudprovider.Provider, _ variant.Variant,
|
|
|
|
_, _ string,
|
|
|
|
) (string, error) {
|
2023-03-30 10:13:14 -04:00
|
|
|
return f.reference, f.fetchReferenceErr
|
|
|
|
}
|
2023-07-24 06:15:11 -04:00
|
|
|
|
|
|
|
func unstructedObjectWithGeneration(nodeVersion updatev1alpha1.NodeVersion, generation int64) *unstructured.Unstructured {
|
|
|
|
unstrNodeVersion, _ := runtime.DefaultUnstructuredConverter.ToUnstructured(&nodeVersion)
|
|
|
|
object := &unstructured.Unstructured{Object: unstrNodeVersion}
|
|
|
|
object.SetGeneration(generation)
|
|
|
|
return object
|
|
|
|
}
|
2023-08-29 05:40:44 -04:00
|
|
|
|
|
|
|
type fakeConfigMapClient struct {
|
|
|
|
getErrs []error
|
|
|
|
updatedConfigMaps map[string]*corev1.ConfigMap
|
|
|
|
updateErrs []error
|
|
|
|
configMaps map[string]*corev1.ConfigMap
|
|
|
|
createErrs []error
|
|
|
|
kubectlInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeConfigMapClient) GetConfigMap(_ context.Context, _, name string) (*corev1.ConfigMap, error) {
|
|
|
|
if len(f.getErrs) > 0 {
|
|
|
|
err := f.getErrs[0]
|
|
|
|
if len(f.getErrs) > 1 {
|
|
|
|
f.getErrs = f.getErrs[1:]
|
|
|
|
} else {
|
|
|
|
f.getErrs = nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f.configMaps[name], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeConfigMapClient) UpdateConfigMap(_ context.Context, configMap *corev1.ConfigMap) (*corev1.ConfigMap, error) {
|
|
|
|
if len(f.updateErrs) > 0 {
|
|
|
|
err := f.updateErrs[0]
|
|
|
|
if len(f.updateErrs) > 1 {
|
|
|
|
f.updateErrs = f.updateErrs[1:]
|
|
|
|
} else {
|
|
|
|
f.updateErrs = nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.updatedConfigMaps == nil {
|
|
|
|
f.updatedConfigMaps = map[string]*corev1.ConfigMap{}
|
|
|
|
}
|
|
|
|
f.updatedConfigMaps[configMap.ObjectMeta.Name] = configMap
|
|
|
|
return f.updatedConfigMaps[configMap.ObjectMeta.Name], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeConfigMapClient) CreateConfigMap(_ context.Context, configMap *corev1.ConfigMap) error {
|
|
|
|
if len(f.createErrs) > 0 {
|
|
|
|
err := f.createErrs[0]
|
|
|
|
if len(f.createErrs) > 1 {
|
|
|
|
f.createErrs = f.createErrs[1:]
|
|
|
|
} else {
|
|
|
|
f.createErrs = nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.configMaps == nil {
|
|
|
|
f.configMaps = map[string]*corev1.ConfigMap{}
|
|
|
|
}
|
|
|
|
f.configMaps[configMap.ObjectMeta.Name] = configMap
|
|
|
|
return nil
|
|
|
|
}
|