2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2022-05-19 11:18:22 -04:00
|
|
|
"context"
|
2022-03-22 11:03:15 -04:00
|
|
|
"errors"
|
2022-07-15 03:33:11 -04:00
|
|
|
"net"
|
2022-08-01 10:51:34 -04:00
|
|
|
"strconv"
|
2022-03-22 11:03:15 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/bootstrapper/internal/kubernetes/k8sapi"
|
2023-01-19 09:57:50 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/bootstrapper/internal/kubernetes/kubewaiter"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/metadata"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/kubernetes"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/role"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions"
|
2023-01-06 06:04:36 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions/components"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/goleak"
|
2022-07-18 06:28:02 -04:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2022-03-22 11:03:15 -04:00
|
|
|
kubeadm "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2022-05-04 06:10:23 -04:00
|
|
|
goleak.VerifyTestMain(m)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-05-24 04:04:42 -04:00
|
|
|
func TestInitCluster(t *testing.T) {
|
|
|
|
nodeName := "node-name"
|
|
|
|
providerID := "provider-id"
|
|
|
|
privateIP := "192.0.2.1"
|
|
|
|
loadbalancerIP := "192.0.2.3"
|
|
|
|
aliasIPRange := "192.0.2.0/24"
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-11-09 08:43:48 -05:00
|
|
|
clusterUtil stubClusterUtil
|
|
|
|
kubectl stubKubectl
|
|
|
|
kubeAPIWaiter stubKubeAPIWaiter
|
|
|
|
providerMetadata ProviderMetadata
|
|
|
|
wantConfig k8sapi.KubeadmInitYAML
|
|
|
|
wantErr bool
|
|
|
|
k8sVersion versions.ValidK8sVersion
|
2022-05-24 04:04:42 -04:00
|
|
|
}{
|
|
|
|
"kubeadm init works with metadata and loadbalancer": {
|
2023-02-10 07:27:22 -05:00
|
|
|
clusterUtil: stubClusterUtil{kubeconfig: []byte("someKubeconfig")},
|
2022-11-04 07:36:26 -04:00
|
|
|
kubeAPIWaiter: stubKubeAPIWaiter{},
|
2022-05-24 04:04:42 -04:00
|
|
|
providerMetadata: &stubProviderMetadata{
|
2022-11-15 04:31:55 -05:00
|
|
|
selfResp: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: nodeName,
|
|
|
|
ProviderID: providerID,
|
2022-08-04 05:08:20 -04:00
|
|
|
VPCIP: privateIP,
|
2022-05-24 04:04:42 -04:00
|
|
|
AliasIPRanges: []string{aliasIPRange},
|
|
|
|
},
|
2023-07-21 10:43:51 -04:00
|
|
|
getLoadBalancerHostResp: loadbalancerIP,
|
|
|
|
getLoadBalancerPortResp: strconv.Itoa(constants.KubernetesPort),
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
wantConfig: k8sapi.KubeadmInitYAML{
|
|
|
|
InitConfiguration: kubeadm.InitConfiguration{
|
|
|
|
NodeRegistration: kubeadm.NodeRegistrationOptions{
|
|
|
|
KubeletExtraArgs: map[string]string{
|
|
|
|
"node-ip": privateIP,
|
|
|
|
"provider-id": providerID,
|
|
|
|
},
|
|
|
|
Name: nodeName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ClusterConfiguration: kubeadm.ClusterConfiguration{
|
2023-02-10 07:27:22 -05:00
|
|
|
ClusterName: "kubernetes",
|
2022-05-24 04:04:42 -04:00
|
|
|
ControlPlaneEndpoint: loadbalancerIP,
|
|
|
|
APIServer: kubeadm.APIServer{
|
2022-11-02 07:56:16 -04:00
|
|
|
CertSANs: []string{privateIP},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-18 06:28:02 -04:00
|
|
|
wantErr: false,
|
2022-09-02 07:57:57 -04:00
|
|
|
k8sVersion: versions.Default,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
2022-12-06 12:48:01 -05:00
|
|
|
"kubeadm init fails when annotating itself": {
|
2023-02-10 07:27:22 -05:00
|
|
|
clusterUtil: stubClusterUtil{kubeconfig: []byte("someKubeconfig")},
|
2022-12-06 12:48:01 -05:00
|
|
|
kubeAPIWaiter: stubKubeAPIWaiter{},
|
|
|
|
providerMetadata: &stubProviderMetadata{
|
|
|
|
selfResp: metadata.InstanceMetadata{
|
|
|
|
Name: nodeName,
|
|
|
|
ProviderID: providerID,
|
|
|
|
VPCIP: privateIP,
|
|
|
|
AliasIPRanges: []string{aliasIPRange},
|
|
|
|
},
|
2023-07-21 10:43:51 -04:00
|
|
|
getLoadBalancerHostResp: loadbalancerIP,
|
|
|
|
getLoadBalancerPortResp: strconv.Itoa(constants.KubernetesPort),
|
2022-12-06 12:48:01 -05:00
|
|
|
},
|
2023-06-22 10:31:35 -04:00
|
|
|
kubectl: stubKubectl{annotateNodeErr: assert.AnError},
|
2022-12-06 12:48:01 -05:00
|
|
|
wantErr: true,
|
|
|
|
k8sVersion: versions.Default,
|
|
|
|
},
|
2022-05-24 04:04:42 -04:00
|
|
|
"kubeadm init fails when retrieving metadata self": {
|
2023-02-10 07:27:22 -05:00
|
|
|
clusterUtil: stubClusterUtil{kubeconfig: []byte("someKubeconfig")},
|
2022-11-04 07:36:26 -04:00
|
|
|
kubeAPIWaiter: stubKubeAPIWaiter{},
|
2022-05-24 04:04:42 -04:00
|
|
|
providerMetadata: &stubProviderMetadata{
|
2023-06-22 10:31:35 -04:00
|
|
|
selfErr: assert.AnError,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
2022-11-09 08:43:48 -05:00
|
|
|
wantErr: true,
|
|
|
|
k8sVersion: versions.Default,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"kubeadm init fails when retrieving metadata loadbalancer ip": {
|
2023-02-10 07:27:22 -05:00
|
|
|
clusterUtil: stubClusterUtil{kubeconfig: []byte("someKubeconfig")},
|
2022-05-24 04:04:42 -04:00
|
|
|
providerMetadata: &stubProviderMetadata{
|
2023-06-22 10:31:35 -04:00
|
|
|
getLoadBalancerEndpointErr: assert.AnError,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
2022-11-09 08:43:48 -05:00
|
|
|
wantErr: true,
|
|
|
|
k8sVersion: versions.Default,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"kubeadm init fails when applying the init config": {
|
2023-02-10 07:27:22 -05:00
|
|
|
clusterUtil: stubClusterUtil{
|
2023-06-22 10:31:35 -04:00
|
|
|
initClusterErr: assert.AnError,
|
2023-02-10 07:27:22 -05:00
|
|
|
kubeconfig: []byte("someKubeconfig"),
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
2022-11-09 08:43:48 -05:00
|
|
|
kubeAPIWaiter: stubKubeAPIWaiter{},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
wantErr: true,
|
|
|
|
k8sVersion: versions.Default,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
2022-10-18 07:15:54 -04:00
|
|
|
"kubeadm init fails when deploying cilium": {
|
2023-02-10 07:27:22 -05:00
|
|
|
clusterUtil: stubClusterUtil{kubeconfig: []byte("someKubeconfig")},
|
2022-11-09 08:43:48 -05:00
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
wantErr: true,
|
|
|
|
k8sVersion: versions.Default,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
2022-10-26 04:37:10 -04:00
|
|
|
"kubeadm init fails when setting up constellation-services chart": {
|
2023-02-10 07:27:22 -05:00
|
|
|
clusterUtil: stubClusterUtil{kubeconfig: []byte("someKubeconfig")},
|
2022-11-09 08:43:48 -05:00
|
|
|
kubeAPIWaiter: stubKubeAPIWaiter{},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
wantErr: true,
|
|
|
|
k8sVersion: versions.Default,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"kubeadm init fails when reading kubeconfig": {
|
2023-02-10 07:27:22 -05:00
|
|
|
clusterUtil: stubClusterUtil{kubeconfig: []byte("someKubeconfig")},
|
2022-11-09 08:43:48 -05:00
|
|
|
kubeAPIWaiter: stubKubeAPIWaiter{},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
wantErr: true,
|
|
|
|
k8sVersion: versions.Default,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
2022-06-28 11:03:28 -04:00
|
|
|
"kubeadm init fails when setting up verification service": {
|
2023-02-10 07:27:22 -05:00
|
|
|
clusterUtil: stubClusterUtil{kubeconfig: []byte("someKubeconfig")},
|
2022-11-09 08:43:48 -05:00
|
|
|
kubeAPIWaiter: stubKubeAPIWaiter{},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
wantErr: true,
|
|
|
|
k8sVersion: versions.Default,
|
2022-07-18 06:28:02 -04:00
|
|
|
},
|
2022-11-04 07:36:26 -04:00
|
|
|
"kubeadm init fails when waiting for kubeAPI server": {
|
2023-02-10 07:27:22 -05:00
|
|
|
clusterUtil: stubClusterUtil{kubeconfig: []byte("someKubeconfig")},
|
2023-06-22 10:31:35 -04:00
|
|
|
kubeAPIWaiter: stubKubeAPIWaiter{waitErr: assert.AnError},
|
2022-11-09 08:43:48 -05:00
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
k8sVersion: versions.Default,
|
|
|
|
wantErr: true,
|
2022-11-04 07:36:26 -04:00
|
|
|
},
|
2022-07-18 06:28:02 -04:00
|
|
|
"unsupported k8sVersion fails cluster creation": {
|
2023-02-10 07:27:22 -05:00
|
|
|
clusterUtil: stubClusterUtil{kubeconfig: []byte("someKubeconfig")},
|
2022-11-09 08:43:48 -05:00
|
|
|
kubeAPIWaiter: stubKubeAPIWaiter{},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
k8sVersion: "1.19",
|
|
|
|
wantErr: true,
|
2022-06-28 11:03:28 -04:00
|
|
|
},
|
2022-05-24 04:04:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
kube := KubeWrapper{
|
2023-07-24 04:30:53 -04:00
|
|
|
cloudProvider: "aws", // provide a valid cloud provider for cilium installation
|
2022-11-09 08:43:48 -05:00
|
|
|
clusterUtil: &tc.clusterUtil,
|
|
|
|
providerMetadata: tc.providerMetadata,
|
|
|
|
kubeAPIWaiter: &tc.kubeAPIWaiter,
|
2022-11-15 04:31:55 -05:00
|
|
|
configProvider: &stubConfigProvider{initConfig: k8sapi.KubeadmInitYAML{}},
|
2022-11-09 08:43:48 -05:00
|
|
|
client: &tc.kubectl,
|
|
|
|
getIPAddr: func() (string, error) { return privateIP, nil },
|
2022-05-24 04:04:42 -04:00
|
|
|
}
|
2022-08-12 09:59:45 -04:00
|
|
|
|
|
|
|
_, err := kube.InitCluster(
|
2023-07-31 04:53:05 -04:00
|
|
|
context.Background(), string(tc.k8sVersion), "kubernetes",
|
2023-08-02 09:49:40 -04:00
|
|
|
false, nil, nil, logger.NewTest(t),
|
2022-08-12 09:59:45 -04:00
|
|
|
)
|
2022-05-24 04:04:42 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
var kubeadmConfig k8sapi.KubeadmInitYAML
|
2022-08-29 08:30:20 -04:00
|
|
|
require.NoError(kubernetes.UnmarshalK8SResources(tc.clusterUtil.initConfigs[0], &kubeadmConfig))
|
2022-05-24 04:04:42 -04:00
|
|
|
require.Equal(tc.wantConfig.ClusterConfiguration, kubeadmConfig.ClusterConfiguration)
|
|
|
|
require.Equal(tc.wantConfig.InitConfiguration, kubeadmConfig.InitConfiguration)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJoinCluster(t *testing.T) {
|
|
|
|
joinCommand := &kubeadm.BootstrapTokenDiscovery{
|
2022-08-01 10:51:34 -04:00
|
|
|
APIServerEndpoint: "192.0.2.0:" + strconv.Itoa(constants.KubernetesPort),
|
2022-05-24 04:04:42 -04:00
|
|
|
Token: "kube-fake-token",
|
|
|
|
CACertHashes: []string{"sha256:a60ebe9b0879090edd83b40a4df4bebb20506bac1e51d518ff8f4505a721930f"},
|
|
|
|
}
|
|
|
|
|
2022-06-28 12:23:24 -04:00
|
|
|
privateIP := "192.0.2.1"
|
2022-05-24 04:04:42 -04:00
|
|
|
|
2023-01-06 06:04:36 -05:00
|
|
|
k8sComponents := components.Components{
|
2022-11-23 04:29:36 -05:00
|
|
|
{
|
|
|
|
URL: "URL",
|
|
|
|
Hash: "Hash",
|
|
|
|
InstallPath: "InstallPath",
|
|
|
|
Extract: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-05-24 04:04:42 -04:00
|
|
|
testCases := map[string]struct {
|
2022-12-29 08:51:26 -05:00
|
|
|
clusterUtil stubClusterUtil
|
|
|
|
providerMetadata ProviderMetadata
|
|
|
|
wantConfig kubeadm.JoinConfiguration
|
|
|
|
role role.Role
|
2023-01-06 06:04:36 -05:00
|
|
|
k8sComponents components.Components
|
2022-12-29 08:51:26 -05:00
|
|
|
wantErr bool
|
2022-05-24 04:04:42 -04:00
|
|
|
}{
|
2022-11-23 04:29:36 -05:00
|
|
|
"kubeadm join worker works with metadata and remote Kubernetes Components": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
providerMetadata: &stubProviderMetadata{
|
|
|
|
selfResp: metadata.InstanceMetadata{
|
|
|
|
ProviderID: "provider-id",
|
|
|
|
Name: "metadata-name",
|
|
|
|
VPCIP: "192.0.2.1",
|
|
|
|
},
|
|
|
|
},
|
2022-12-29 08:51:26 -05:00
|
|
|
k8sComponents: k8sComponents,
|
|
|
|
role: role.Worker,
|
2022-11-23 04:29:36 -05:00
|
|
|
wantConfig: kubeadm.JoinConfiguration{
|
|
|
|
Discovery: kubeadm.Discovery{
|
|
|
|
BootstrapToken: joinCommand,
|
|
|
|
},
|
|
|
|
NodeRegistration: kubeadm.NodeRegistrationOptions{
|
|
|
|
Name: "metadata-name",
|
|
|
|
KubeletExtraArgs: map[string]string{"node-ip": "192.0.2.1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"kubeadm join worker works with metadata and local Kubernetes components": {
|
2022-05-24 04:04:42 -04:00
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
providerMetadata: &stubProviderMetadata{
|
2022-11-15 04:31:55 -05:00
|
|
|
selfResp: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
ProviderID: "provider-id",
|
|
|
|
Name: "metadata-name",
|
2022-08-04 05:08:20 -04:00
|
|
|
VPCIP: "192.0.2.1",
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
2022-11-09 08:43:48 -05:00
|
|
|
role: role.Worker,
|
2022-05-24 04:04:42 -04:00
|
|
|
wantConfig: kubeadm.JoinConfiguration{
|
|
|
|
Discovery: kubeadm.Discovery{
|
|
|
|
BootstrapToken: joinCommand,
|
|
|
|
},
|
|
|
|
NodeRegistration: kubeadm.NodeRegistrationOptions{
|
|
|
|
Name: "metadata-name",
|
|
|
|
KubeletExtraArgs: map[string]string{"node-ip": "192.0.2.1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"kubeadm join worker works with metadata and cloud controller manager": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
providerMetadata: &stubProviderMetadata{
|
2022-11-15 04:31:55 -05:00
|
|
|
selfResp: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
ProviderID: "provider-id",
|
|
|
|
Name: "metadata-name",
|
2022-08-04 05:08:20 -04:00
|
|
|
VPCIP: "192.0.2.1",
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
2022-06-29 09:26:29 -04:00
|
|
|
role: role.Worker,
|
2022-05-24 04:04:42 -04:00
|
|
|
wantConfig: kubeadm.JoinConfiguration{
|
|
|
|
Discovery: kubeadm.Discovery{
|
|
|
|
BootstrapToken: joinCommand,
|
|
|
|
},
|
|
|
|
NodeRegistration: kubeadm.NodeRegistrationOptions{
|
|
|
|
Name: "metadata-name",
|
|
|
|
KubeletExtraArgs: map[string]string{"node-ip": "192.0.2.1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"kubeadm join control-plane node works with metadata": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
providerMetadata: &stubProviderMetadata{
|
2022-11-15 04:31:55 -05:00
|
|
|
selfResp: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
ProviderID: "provider-id",
|
|
|
|
Name: "metadata-name",
|
2022-08-04 05:08:20 -04:00
|
|
|
VPCIP: "192.0.2.1",
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
2022-11-09 08:43:48 -05:00
|
|
|
role: role.ControlPlane,
|
2022-05-24 04:04:42 -04:00
|
|
|
wantConfig: kubeadm.JoinConfiguration{
|
|
|
|
Discovery: kubeadm.Discovery{
|
|
|
|
BootstrapToken: joinCommand,
|
|
|
|
},
|
|
|
|
NodeRegistration: kubeadm.NodeRegistrationOptions{
|
|
|
|
Name: "metadata-name",
|
|
|
|
KubeletExtraArgs: map[string]string{"node-ip": "192.0.2.1"},
|
|
|
|
},
|
|
|
|
ControlPlane: &kubeadm.JoinControlPlane{
|
|
|
|
LocalAPIEndpoint: kubeadm.APIEndpoint{
|
|
|
|
AdvertiseAddress: "192.0.2.1",
|
2022-08-01 10:51:34 -04:00
|
|
|
BindPort: constants.KubernetesPort,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
2022-07-11 07:29:22 -04:00
|
|
|
SkipPhases: []string{"control-plane-prepare/download-certs"},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
2022-11-23 04:29:36 -05:00
|
|
|
"kubeadm join worker fails when installing remote Kubernetes components": {
|
|
|
|
clusterUtil: stubClusterUtil{installComponentsErr: errors.New("error")},
|
|
|
|
providerMetadata: &stubProviderMetadata{
|
|
|
|
selfResp: metadata.InstanceMetadata{
|
|
|
|
ProviderID: "provider-id",
|
|
|
|
Name: "metadata-name",
|
|
|
|
VPCIP: "192.0.2.1",
|
|
|
|
},
|
|
|
|
},
|
2022-12-29 08:51:26 -05:00
|
|
|
k8sComponents: k8sComponents,
|
|
|
|
role: role.Worker,
|
|
|
|
wantErr: true,
|
2022-11-23 04:29:36 -05:00
|
|
|
},
|
2022-05-24 04:04:42 -04:00
|
|
|
"kubeadm join worker fails when retrieving self metadata": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
providerMetadata: &stubProviderMetadata{
|
2023-06-22 10:31:35 -04:00
|
|
|
selfErr: assert.AnError,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
2022-11-09 08:43:48 -05:00
|
|
|
role: role.Worker,
|
|
|
|
wantErr: true,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"kubeadm join worker fails when applying the join config": {
|
2023-06-22 10:31:35 -04:00
|
|
|
clusterUtil: stubClusterUtil{joinClusterErr: assert.AnError},
|
2022-11-09 08:43:48 -05:00
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
role: role.Worker,
|
|
|
|
wantErr: true,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
kube := KubeWrapper{
|
2022-11-09 08:43:48 -05:00
|
|
|
clusterUtil: &tc.clusterUtil,
|
|
|
|
providerMetadata: tc.providerMetadata,
|
|
|
|
configProvider: &stubConfigProvider{},
|
|
|
|
getIPAddr: func() (string, error) { return privateIP, nil },
|
2022-05-24 04:04:42 -04:00
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
err := kube.JoinCluster(context.Background(), joinCommand, tc.role, tc.k8sComponents, logger.NewTest(t))
|
2022-05-24 04:04:42 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
var joinYaml k8sapi.KubeadmJoinYAML
|
2022-11-15 04:31:55 -05:00
|
|
|
require.NoError(kubernetes.UnmarshalK8SResources(tc.clusterUtil.joinConfigs[0], &joinYaml))
|
2022-05-24 04:04:42 -04:00
|
|
|
|
|
|
|
assert.Equal(tc.wantConfig, joinYaml.JoinConfiguration)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestK8sCompliantHostname(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-12-23 12:59:15 -05:00
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
wantErr bool
|
2022-05-24 04:04:42 -04:00
|
|
|
}{
|
2022-12-23 12:59:15 -05:00
|
|
|
"no change": {
|
|
|
|
input: "test",
|
|
|
|
expected: "test",
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
2022-12-23 12:59:15 -05:00
|
|
|
"uppercase": {
|
|
|
|
input: "TEST",
|
|
|
|
expected: "test",
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
2022-12-23 12:59:15 -05:00
|
|
|
"underscore": {
|
|
|
|
input: "test_node",
|
|
|
|
expected: "test-node",
|
|
|
|
},
|
|
|
|
"empty": {
|
|
|
|
input: "",
|
|
|
|
expected: "",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"error": {
|
|
|
|
input: "test_node_",
|
|
|
|
expected: "",
|
|
|
|
wantErr: true,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-12-23 12:59:15 -05:00
|
|
|
actual, err := k8sCompliantHostname(tc.input)
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.expected, actual)
|
2022-05-24 04:04:42 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
type stubClusterUtil struct {
|
2022-12-29 08:51:26 -05:00
|
|
|
installComponentsErr error
|
|
|
|
initClusterErr error
|
|
|
|
setupAutoscalingError error
|
|
|
|
setupKonnectivityError error
|
|
|
|
setupGCPGuestAgentErr error
|
|
|
|
setupOLMErr error
|
|
|
|
setupNMOErr error
|
|
|
|
setupNodeOperatorErr error
|
|
|
|
joinClusterErr error
|
|
|
|
startKubeletErr error
|
2022-11-23 04:29:36 -05:00
|
|
|
|
2023-02-10 07:27:22 -05:00
|
|
|
kubeconfig []byte
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
initConfigs [][]byte
|
|
|
|
joinConfigs [][]byte
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubClusterUtil) SetupKonnectivity(_ k8sapi.Client, _ kubernetes.Marshaler) error {
|
2022-08-31 21:40:29 -04:00
|
|
|
return s.setupKonnectivityError
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubClusterUtil) InstallComponents(_ context.Context, _ components.Components) error {
|
2022-05-19 11:18:22 -04:00
|
|
|
return s.installComponentsErr
|
|
|
|
}
|
|
|
|
|
2023-07-21 10:43:51 -04:00
|
|
|
func (s *stubClusterUtil) InitCluster(_ context.Context, initConfig []byte, _, _ string, _ []net.IP, _, _ string, _ bool, _ *logger.Logger) ([]byte, error) {
|
2022-03-22 11:03:15 -04:00
|
|
|
s.initConfigs = append(s.initConfigs, initConfig)
|
2023-02-10 07:27:22 -05:00
|
|
|
return s.kubeconfig, s.initClusterErr
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubClusterUtil) SetupAutoscaling(_ k8sapi.Client, _ kubernetes.Marshaler, _ kubernetes.Marshaler) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
return s.setupAutoscalingError
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubClusterUtil) SetupGCPGuestAgent(_ k8sapi.Client, _ kubernetes.Marshaler) error {
|
2022-07-05 08:14:11 -04:00
|
|
|
return s.setupGCPGuestAgentErr
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubClusterUtil) SetupOperatorLifecycleManager(_ context.Context, _ k8sapi.Client, _, _ kubernetes.Marshaler, _ []string) error {
|
2022-08-04 10:15:52 -04:00
|
|
|
return s.setupOLMErr
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubClusterUtil) SetupNodeMaintenanceOperator(_ k8sapi.Client, _ kubernetes.Marshaler) error {
|
2022-08-04 10:15:52 -04:00
|
|
|
return s.setupNMOErr
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubClusterUtil) SetupNodeOperator(_ context.Context, _ k8sapi.Client, _ kubernetes.Marshaler) error {
|
2022-08-04 10:15:52 -04:00
|
|
|
return s.setupNodeOperatorErr
|
|
|
|
}
|
|
|
|
|
2023-07-21 10:43:51 -04:00
|
|
|
func (s *stubClusterUtil) JoinCluster(_ context.Context, joinConfig []byte, _ role.Role, _, _ string, _ *logger.Logger) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
s.joinConfigs = append(s.joinConfigs, joinConfig)
|
|
|
|
return s.joinClusterErr
|
|
|
|
}
|
|
|
|
|
2022-05-19 11:18:22 -04:00
|
|
|
func (s *stubClusterUtil) StartKubelet() error {
|
|
|
|
return s.startKubeletErr
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubClusterUtil) WaitForCilium(_ context.Context, _ *logger.Logger) error {
|
2023-03-10 12:27:01 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubClusterUtil) FixCilium(_ context.Context) error {
|
2023-03-10 12:27:01 -05:00
|
|
|
return nil
|
2022-06-13 10:01:21 -04:00
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
type stubConfigProvider struct {
|
2022-11-15 04:31:55 -05:00
|
|
|
initConfig k8sapi.KubeadmInitYAML
|
|
|
|
joinConfig k8sapi.KubeadmJoinYAML
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 11:03:40 -05:00
|
|
|
func (s *stubConfigProvider) InitConfiguration(_ bool, _ string) k8sapi.KubeadmInitYAML {
|
2022-11-15 04:31:55 -05:00
|
|
|
return s.initConfig
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-27 10:37:05 -04:00
|
|
|
func (s *stubConfigProvider) JoinConfiguration(_ bool) k8sapi.KubeadmJoinYAML {
|
2022-11-15 04:31:55 -05:00
|
|
|
s.joinConfig = k8sapi.KubeadmJoinYAML{
|
2022-03-22 11:03:15 -04:00
|
|
|
JoinConfiguration: kubeadm.JoinConfiguration{
|
|
|
|
Discovery: kubeadm.Discovery{
|
|
|
|
BootstrapToken: &kubeadm.BootstrapTokenDiscovery{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-11-15 04:31:55 -05:00
|
|
|
return s.joinConfig
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 06:28:02 -04:00
|
|
|
type stubKubectl struct {
|
2022-07-14 15:15:31 -04:00
|
|
|
createConfigMapErr error
|
2022-11-15 04:31:55 -05:00
|
|
|
addTNodeSelectorsToDeploymentErr error
|
2022-08-04 10:15:52 -04:00
|
|
|
waitForCRDsErr error
|
2022-11-04 07:36:26 -04:00
|
|
|
listAllNamespacesErr error
|
2022-12-06 12:48:01 -05:00
|
|
|
annotateNodeErr error
|
2023-06-13 03:58:39 -04:00
|
|
|
enforceCoreDNSSpreadErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-11-04 07:36:26 -04:00
|
|
|
listAllNamespacesResp *corev1.NamespaceList
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubKubectl) Initialize(_ []byte) error {
|
2022-11-25 05:19:22 -05:00
|
|
|
return nil
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
func (s *stubKubectl) CreateConfigMap(_ context.Context, _ *corev1.ConfigMap) error {
|
2022-07-18 06:28:02 -04:00
|
|
|
return s.createConfigMapErr
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubKubectl) AddNodeSelectorsToDeployment(_ context.Context, _ map[string]string, _, _ string) error {
|
2022-11-15 04:31:55 -05:00
|
|
|
return s.addTNodeSelectorsToDeploymentErr
|
2022-07-26 04:10:34 -04:00
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubKubectl) AnnotateNode(_ context.Context, _, _, _ string) error {
|
2022-12-06 12:48:01 -05:00
|
|
|
return s.annotateNodeErr
|
|
|
|
}
|
|
|
|
|
2023-08-02 09:49:40 -04:00
|
|
|
func (s *stubKubectl) PatchFirstNodePodCIDR(_ context.Context, _ string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubKubectl) WaitForCRDs(_ context.Context, _ []string) error {
|
2022-08-04 10:15:52 -04:00
|
|
|
return s.waitForCRDsErr
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubKubectl) ListAllNamespaces(_ context.Context) (*corev1.NamespaceList, error) {
|
2022-11-04 07:36:26 -04:00
|
|
|
return s.listAllNamespacesResp, s.listAllNamespacesErr
|
|
|
|
}
|
|
|
|
|
2023-06-13 03:58:39 -04:00
|
|
|
func (s *stubKubectl) EnforceCoreDNSSpread(_ context.Context) error {
|
|
|
|
return s.enforceCoreDNSSpreadErr
|
|
|
|
}
|
|
|
|
|
2022-11-04 07:36:26 -04:00
|
|
|
type stubKubeAPIWaiter struct {
|
|
|
|
waitErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubKubeAPIWaiter) Wait(_ context.Context, _ kubewaiter.KubernetesClient) error {
|
|
|
|
return s.waitErr
|
|
|
|
}
|