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-05-24 04:04:42 -04:00
|
|
|
"regexp"
|
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"
|
2022-11-04 07:36:26 -04:00
|
|
|
kubewaiter "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"
|
2022-10-18 07:15:54 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/deploy/helm"
|
2022-09-21 07:47:57 -04:00
|
|
|
"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"
|
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) {
|
|
|
|
someErr := errors.New("failed")
|
2022-07-08 04:59:59 -04:00
|
|
|
serviceAccountURI := "some-service-account-uri"
|
2022-05-24 04:04:42 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
helmClient stubHelmClient
|
|
|
|
kubectl stubKubectl
|
|
|
|
kubeAPIWaiter stubKubeAPIWaiter
|
|
|
|
providerMetadata ProviderMetadata
|
|
|
|
kubeconfigReader configReader
|
|
|
|
wantConfig k8sapi.KubeadmInitYAML
|
|
|
|
wantErr bool
|
|
|
|
k8sVersion versions.ValidK8sVersion
|
2022-05-24 04:04:42 -04:00
|
|
|
}{
|
|
|
|
"kubeadm init works with metadata and loadbalancer": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
2022-11-15 04:31:55 -05:00
|
|
|
kubeconfig: []byte("someKubeconfig"),
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
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},
|
|
|
|
},
|
2022-11-15 04:31:55 -05:00
|
|
|
getLoadBalancerEndpointResp: loadbalancerIP,
|
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{
|
|
|
|
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
|
|
|
},
|
|
|
|
"kubeadm init fails when retrieving metadata self": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
2022-11-15 04:31:55 -05:00
|
|
|
kubeconfig: []byte("someKubeconfig"),
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
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
|
|
|
selfErr: someErr,
|
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": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
2022-11-15 04:31:55 -05:00
|
|
|
kubeconfig: []byte("someKubeconfig"),
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{
|
2022-11-15 04:31:55 -05:00
|
|
|
getLoadBalancerEndpointErr: someErr,
|
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": {
|
|
|
|
clusterUtil: stubClusterUtil{initClusterErr: someErr},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
2022-11-15 04:31:55 -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": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
2022-10-21 06:01:28 -04:00
|
|
|
helmClient: stubHelmClient{ciliumError: someErr},
|
2022-05-24 04:04:42 -04:00
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
2022-11-15 04:31:55 -05:00
|
|
|
kubeconfig: []byte("someKubeconfig"),
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
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": {
|
2022-10-24 06:23:18 -04:00
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
helmClient: stubHelmClient{servicesError: someErr},
|
2022-06-15 10:00:48 -04:00
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
2022-11-15 04:31:55 -05:00
|
|
|
kubeconfig: []byte("someKubeconfig"),
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
2022-11-09 08:43:48 -05:00
|
|
|
kubeAPIWaiter: stubKubeAPIWaiter{},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
wantErr: true,
|
|
|
|
k8sVersion: versions.Default,
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
2022-05-24 04:04:42 -04:00
|
|
|
"kubeadm init fails when setting the cloud node manager": {
|
2022-11-02 12:47:10 -04:00
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
helmClient: stubHelmClient{servicesError: someErr},
|
2022-05-24 04:04:42 -04:00
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
2022-11-15 04:31:55 -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
|
|
|
},
|
|
|
|
"kubeadm init fails when setting the cluster autoscaler": {
|
2022-11-03 11:42:19 -04:00
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
helmClient: stubHelmClient{servicesError: someErr},
|
2022-05-24 04:04:42 -04:00
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
2022-11-15 04:31:55 -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
|
|
|
},
|
|
|
|
"kubeadm init fails when reading kubeconfig": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
2022-11-15 04:31:55 -05:00
|
|
|
readErr: someErr,
|
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-08-31 21:40:29 -04:00
|
|
|
"kubeadm init fails when setting up konnectivity": {
|
2022-11-23 02:26:09 -05:00
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
helmClient: stubHelmClient{servicesError: someErr},
|
2022-08-31 21:40:29 -04:00
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
2022-11-15 04:31:55 -05:00
|
|
|
kubeconfig: []byte("someKubeconfig"),
|
2022-08-31 21:40:29 -04:00
|
|
|
},
|
2022-11-09 08:43:48 -05:00
|
|
|
kubeAPIWaiter: stubKubeAPIWaiter{},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
wantErr: true,
|
|
|
|
k8sVersion: versions.Default,
|
2022-08-31 21:40:29 -04:00
|
|
|
},
|
2022-06-28 11:03:28 -04:00
|
|
|
"kubeadm init fails when setting up verification service": {
|
2022-11-21 11:06:41 -05:00
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
helmClient: stubHelmClient{servicesError: someErr},
|
2022-06-28 11:03:28 -04:00
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
2022-11-15 04:31:55 -05:00
|
|
|
kubeconfig: []byte("someKubeconfig"),
|
2022-06-28 11:03:28 -04:00
|
|
|
},
|
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": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
2022-11-15 04:31:55 -05:00
|
|
|
kubeconfig: []byte("someKubeconfig"),
|
2022-11-04 07:36:26 -04:00
|
|
|
},
|
2022-11-09 08:43:48 -05:00
|
|
|
kubeAPIWaiter: stubKubeAPIWaiter{waitErr: someErr},
|
|
|
|
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": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
2022-11-15 04:31:55 -05:00
|
|
|
kubeconfig: []byte("someKubeconfig"),
|
2022-07-18 06:28:02 -04:00
|
|
|
},
|
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{
|
2022-11-09 08:43:48 -05:00
|
|
|
clusterUtil: &tc.clusterUtil,
|
|
|
|
helmClient: &tc.helmClient,
|
|
|
|
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,
|
|
|
|
kubeconfigReader: tc.kubeconfigReader,
|
|
|
|
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(
|
2022-09-15 10:51:07 -04:00
|
|
|
context.Background(), serviceAccountURI, string(tc.k8sVersion),
|
2022-11-14 13:09:49 -05:00
|
|
|
nil, nil, false, nil, true, []byte("{}"), false, 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) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
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-09-02 07:57:57 -04:00
|
|
|
k8sVersion := versions.Default
|
2022-05-24 04:04:42 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-11-09 08:43:48 -05:00
|
|
|
clusterUtil stubClusterUtil
|
|
|
|
providerMetadata ProviderMetadata
|
|
|
|
wantConfig kubeadm.JoinConfiguration
|
|
|
|
role role.Role
|
|
|
|
wantErr bool
|
2022-05-24 04:04:42 -04:00
|
|
|
}{
|
|
|
|
"kubeadm join worker 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.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
|
|
|
},
|
|
|
|
},
|
|
|
|
"kubeadm join worker fails when retrieving self metadata": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
providerMetadata: &stubProviderMetadata{
|
2022-11-15 04:31:55 -05:00
|
|
|
selfErr: someErr,
|
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": {
|
2022-11-09 08:43:48 -05:00
|
|
|
clusterUtil: stubClusterUtil{joinClusterErr: someErr},
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-22 09:05:04 -04:00
|
|
|
err := kube.JoinCluster(context.Background(), joinCommand, tc.role, string(k8sVersion), 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) {
|
|
|
|
compliantHostname := regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`)
|
|
|
|
testCases := map[string]struct {
|
|
|
|
hostname string
|
|
|
|
wantHostname string
|
|
|
|
}{
|
|
|
|
"azure scale set names work": {
|
2022-06-29 09:26:29 -04:00
|
|
|
hostname: "constellation-scale-set-bootstrappers-name_0",
|
|
|
|
wantHostname: "constellation-scale-set-bootstrappers-name-0",
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"compliant hostname is not modified": {
|
|
|
|
hostname: "abcd-123",
|
|
|
|
wantHostname: "abcd-123",
|
|
|
|
},
|
|
|
|
"uppercase hostnames are lowercased": {
|
|
|
|
hostname: "ABCD",
|
|
|
|
wantHostname: "abcd",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
hostname := k8sCompliantHostname(tc.hostname)
|
|
|
|
|
|
|
|
assert.Equal(tc.wantHostname, hostname)
|
|
|
|
assert.Regexp(compliantHostname, hostname)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
type stubClusterUtil struct {
|
2022-11-14 13:09:49 -05:00
|
|
|
installComponentsErr error
|
|
|
|
installComponentsFromCLIErr error
|
|
|
|
initClusterErr error
|
|
|
|
setupAutoscalingError error
|
|
|
|
setupKonnectivityError error
|
|
|
|
setupGCPGuestAgentErr error
|
|
|
|
setupOLMErr error
|
|
|
|
setupNMOErr error
|
|
|
|
setupNodeOperatorErr error
|
|
|
|
joinClusterErr error
|
|
|
|
startKubeletErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
initConfigs [][]byte
|
|
|
|
joinConfigs [][]byte
|
|
|
|
}
|
|
|
|
|
2022-08-31 21:40:29 -04:00
|
|
|
func (s *stubClusterUtil) SetupKonnectivity(kubectl k8sapi.Client, konnectivityAgentsDaemonSet kubernetes.Marshaler) error {
|
|
|
|
return s.setupKonnectivityError
|
|
|
|
}
|
|
|
|
|
2022-07-22 09:05:04 -04:00
|
|
|
func (s *stubClusterUtil) InstallComponents(ctx context.Context, version versions.ValidK8sVersion) error {
|
2022-05-19 11:18:22 -04:00
|
|
|
return s.installComponentsErr
|
|
|
|
}
|
|
|
|
|
2022-11-14 13:09:49 -05:00
|
|
|
func (s *stubClusterUtil) InstallComponentsFromCLI(ctx context.Context, kubernetesComponents versions.ComponentVersions) error {
|
|
|
|
return s.installComponentsFromCLIErr
|
|
|
|
}
|
|
|
|
|
2022-09-20 04:07:55 -04:00
|
|
|
func (s *stubClusterUtil) InitCluster(ctx context.Context, initConfig []byte, nodeName string, ips []net.IP, controlPlaneEndpoint string, conformanceMode bool, log *logger.Logger) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
s.initConfigs = append(s.initConfigs, initConfig)
|
2022-05-04 08:32:34 -04:00
|
|
|
return s.initClusterErr
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-08-29 08:30:20 -04:00
|
|
|
func (s *stubClusterUtil) SetupAutoscaling(kubectl k8sapi.Client, clusterAutoscalerConfiguration kubernetes.Marshaler, secrets kubernetes.Marshaler) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
return s.setupAutoscalingError
|
|
|
|
}
|
|
|
|
|
2022-08-29 08:30:20 -04:00
|
|
|
func (s *stubClusterUtil) SetupGCPGuestAgent(kubectl k8sapi.Client, gcpGuestAgentConfiguration kubernetes.Marshaler) error {
|
2022-07-05 08:14:11 -04:00
|
|
|
return s.setupGCPGuestAgentErr
|
|
|
|
}
|
|
|
|
|
2022-08-29 08:30:20 -04:00
|
|
|
func (s *stubClusterUtil) SetupOperatorLifecycleManager(ctx context.Context, kubectl k8sapi.Client, olmCRDs, olmConfiguration kubernetes.Marshaler, crdNames []string) error {
|
2022-08-04 10:15:52 -04:00
|
|
|
return s.setupOLMErr
|
|
|
|
}
|
|
|
|
|
2022-08-29 08:30:20 -04:00
|
|
|
func (s *stubClusterUtil) SetupNodeMaintenanceOperator(kubectl k8sapi.Client, nodeMaintenanceOperatorConfiguration kubernetes.Marshaler) error {
|
2022-08-04 10:15:52 -04:00
|
|
|
return s.setupNMOErr
|
|
|
|
}
|
|
|
|
|
2022-08-29 08:30:20 -04:00
|
|
|
func (s *stubClusterUtil) SetupNodeOperator(ctx context.Context, kubectl k8sapi.Client, nodeOperatorConfiguration kubernetes.Marshaler) error {
|
2022-08-04 10:15:52 -04:00
|
|
|
return s.setupNodeOperatorErr
|
|
|
|
}
|
|
|
|
|
2022-08-31 21:40:29 -04:00
|
|
|
func (s *stubClusterUtil) JoinCluster(ctx context.Context, joinConfig []byte, peerRole role.Role, controlPlaneEndpoint string, log *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
|
|
|
|
}
|
|
|
|
|
2022-09-08 08:45:27 -04:00
|
|
|
func (s *stubClusterUtil) FixCilium(log *logger.Logger) {
|
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
|
|
|
}
|
|
|
|
|
2022-07-22 09:05:04 -04:00
|
|
|
func (s *stubConfigProvider) InitConfiguration(_ bool, _ versions.ValidK8sVersion) 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-11-15 04:31:55 -05:00
|
|
|
applyErr error
|
2022-07-14 15:15:31 -04:00
|
|
|
createConfigMapErr error
|
2022-11-15 04:31:55 -05:00
|
|
|
addTolerationsToDeploymentErr error
|
|
|
|
addTNodeSelectorsToDeploymentErr error
|
2022-08-04 10:15:52 -04:00
|
|
|
waitForCRDsErr error
|
2022-11-04 07:36:26 -04:00
|
|
|
listAllNamespacesErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-11-04 07:36:26 -04:00
|
|
|
listAllNamespacesResp *corev1.NamespaceList
|
|
|
|
resources []kubernetes.Marshaler
|
|
|
|
kubeconfigs [][]byte
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-08-29 08:30:20 -04:00
|
|
|
func (s *stubKubectl) Apply(resources kubernetes.Marshaler, forceConflicts bool) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
s.resources = append(s.resources, resources)
|
2022-11-15 04:31:55 -05:00
|
|
|
return s.applyErr
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 06:28:02 -04:00
|
|
|
func (s *stubKubectl) SetKubeconfig(kubeconfig []byte) {
|
2022-03-22 11:03:15 -04:00
|
|
|
s.kubeconfigs = append(s.kubeconfigs, kubeconfig)
|
|
|
|
}
|
|
|
|
|
2022-07-18 06:28:02 -04:00
|
|
|
func (s *stubKubectl) CreateConfigMap(ctx context.Context, configMap corev1.ConfigMap) error {
|
|
|
|
return s.createConfigMapErr
|
|
|
|
}
|
|
|
|
|
2022-07-28 10:07:29 -04:00
|
|
|
func (s *stubKubectl) AddTolerationsToDeployment(ctx context.Context, tolerations []corev1.Toleration, name string, namespace string) error {
|
2022-11-15 04:31:55 -05:00
|
|
|
return s.addTolerationsToDeploymentErr
|
2022-07-14 15:15:31 -04:00
|
|
|
}
|
|
|
|
|
2022-07-28 10:07:29 -04:00
|
|
|
func (s *stubKubectl) AddNodeSelectorsToDeployment(ctx context.Context, selectors map[string]string, name string, namespace string) error {
|
2022-11-15 04:31:55 -05:00
|
|
|
return s.addTNodeSelectorsToDeploymentErr
|
2022-07-26 04:10:34 -04:00
|
|
|
}
|
|
|
|
|
2022-08-04 10:15:52 -04:00
|
|
|
func (s *stubKubectl) WaitForCRDs(ctx context.Context, crds []string) error {
|
|
|
|
return s.waitForCRDsErr
|
|
|
|
}
|
|
|
|
|
2022-11-04 07:36:26 -04:00
|
|
|
func (s *stubKubectl) ListAllNamespaces(ctx context.Context) (*corev1.NamespaceList, error) {
|
|
|
|
return s.listAllNamespacesResp, s.listAllNamespacesErr
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
type stubKubeconfigReader struct {
|
2022-11-15 04:31:55 -05:00
|
|
|
kubeconfig []byte
|
|
|
|
readErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubKubeconfigReader) ReadKubeconfig() ([]byte, error) {
|
2022-11-15 04:31:55 -05:00
|
|
|
return s.kubeconfig, s.readErr
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-10-18 07:15:54 -04:00
|
|
|
|
|
|
|
type stubHelmClient struct {
|
2022-11-21 04:35:40 -05:00
|
|
|
ciliumError error
|
|
|
|
certManagerError error
|
|
|
|
operatorsError error
|
|
|
|
servicesError error
|
2022-10-18 07:15:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubHelmClient) InstallCilium(ctx context.Context, kubectl k8sapi.Client, release helm.Release, in k8sapi.SetupPodNetworkInput) error {
|
|
|
|
return s.ciliumError
|
|
|
|
}
|
|
|
|
|
2022-11-21 04:35:40 -05:00
|
|
|
func (s *stubHelmClient) InstallCertManager(ctx context.Context, release helm.Release) error {
|
|
|
|
return s.certManagerError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubHelmClient) InstallOperators(ctx context.Context, release helm.Release, extraVals map[string]any) error {
|
|
|
|
return s.operatorsError
|
|
|
|
}
|
|
|
|
|
2022-10-25 09:51:23 -04:00
|
|
|
func (s *stubHelmClient) InstallConstellationServices(ctx context.Context, release helm.Release, extraVals map[string]any) error {
|
2022-10-21 06:01:28 -04:00
|
|
|
return s.servicesError
|
2022-10-18 07:15:54 -04:00
|
|
|
}
|
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
|
|
|
|
}
|