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-03-22 11:03:15 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-06-29 09:26:29 -04:00
|
|
|
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi"
|
|
|
|
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi/resources"
|
|
|
|
"github.com/edgelesssys/constellation/bootstrapper/role"
|
2022-06-28 12:23:24 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/metadata"
|
2022-07-14 07:30:44 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/logger"
|
2022-07-22 09:05:04 -04:00
|
|
|
"github.com/edgelesssys/constellation/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
|
|
|
masterSecret := []byte("some-master-secret")
|
|
|
|
autoscalingNodeGroups := []string{"0,10,autoscaling_group_0"}
|
|
|
|
|
|
|
|
nodeName := "node-name"
|
|
|
|
providerID := "provider-id"
|
|
|
|
privateIP := "192.0.2.1"
|
|
|
|
publicIP := "192.0.2.2"
|
|
|
|
loadbalancerIP := "192.0.2.3"
|
|
|
|
aliasIPRange := "192.0.2.0/24"
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
clusterUtil stubClusterUtil
|
2022-07-18 06:28:02 -04:00
|
|
|
kubectl stubKubectl
|
2022-05-24 04:04:42 -04:00
|
|
|
providerMetadata ProviderMetadata
|
|
|
|
CloudControllerManager CloudControllerManager
|
|
|
|
CloudNodeManager CloudNodeManager
|
|
|
|
ClusterAutoscaler ClusterAutoscaler
|
|
|
|
kubeconfigReader configReader
|
|
|
|
wantConfig k8sapi.KubeadmInitYAML
|
|
|
|
wantErr bool
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion versions.ValidK8sVersion
|
2022-05-24 04:04:42 -04:00
|
|
|
}{
|
|
|
|
"kubeadm init works without metadata": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
|
|
|
Kubeconfig: []byte("someKubeconfig"),
|
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{SupportedResp: false},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
|
|
|
CloudNodeManager: &stubCloudNodeManager{SupportedResp: false},
|
|
|
|
ClusterAutoscaler: &stubClusterAutoscaler{},
|
|
|
|
wantConfig: k8sapi.KubeadmInitYAML{
|
|
|
|
InitConfiguration: kubeadm.InitConfiguration{
|
|
|
|
NodeRegistration: kubeadm.NodeRegistrationOptions{
|
|
|
|
KubeletExtraArgs: map[string]string{
|
|
|
|
"node-ip": "",
|
|
|
|
"provider-id": "",
|
|
|
|
},
|
2022-06-28 12:23:24 -04:00
|
|
|
Name: privateIP,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
ClusterConfiguration: kubeadm.ClusterConfiguration{},
|
|
|
|
},
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion: versions.Latest,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"kubeadm init works with metadata and loadbalancer": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
|
|
|
Kubeconfig: []byte("someKubeconfig"),
|
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{
|
|
|
|
SupportedResp: true,
|
2022-06-28 12:23:24 -04:00
|
|
|
SelfResp: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
Name: nodeName,
|
|
|
|
ProviderID: providerID,
|
|
|
|
PrivateIPs: []string{privateIP},
|
|
|
|
PublicIPs: []string{publicIP},
|
|
|
|
AliasIPRanges: []string{aliasIPRange},
|
|
|
|
},
|
|
|
|
GetLoadBalancerIPResp: loadbalancerIP,
|
|
|
|
SupportsLoadBalancerResp: true,
|
|
|
|
},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
|
|
|
CloudNodeManager: &stubCloudNodeManager{SupportedResp: false},
|
|
|
|
ClusterAutoscaler: &stubClusterAutoscaler{},
|
|
|
|
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{
|
|
|
|
CertSANs: []string{publicIP, privateIP},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-18 06:28:02 -04:00
|
|
|
wantErr: false,
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion: versions.Latest,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"kubeadm init fails when retrieving metadata self": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
|
|
|
Kubeconfig: []byte("someKubeconfig"),
|
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{
|
|
|
|
SelfErr: someErr,
|
|
|
|
SupportedResp: true,
|
|
|
|
},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
|
|
|
CloudNodeManager: &stubCloudNodeManager{},
|
|
|
|
ClusterAutoscaler: &stubClusterAutoscaler{},
|
|
|
|
wantErr: true,
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion: versions.Latest,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"kubeadm init fails when retrieving metadata subnetwork cidr": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
|
|
|
Kubeconfig: []byte("someKubeconfig"),
|
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{
|
|
|
|
GetSubnetworkCIDRErr: someErr,
|
|
|
|
SupportedResp: true,
|
|
|
|
},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
|
|
|
CloudNodeManager: &stubCloudNodeManager{},
|
|
|
|
ClusterAutoscaler: &stubClusterAutoscaler{},
|
|
|
|
wantErr: true,
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion: versions.Latest,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"kubeadm init fails when retrieving metadata loadbalancer ip": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
|
|
|
Kubeconfig: []byte("someKubeconfig"),
|
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{
|
|
|
|
GetLoadBalancerIPErr: someErr,
|
|
|
|
SupportsLoadBalancerResp: true,
|
|
|
|
SupportedResp: true,
|
|
|
|
},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
|
|
|
CloudNodeManager: &stubCloudNodeManager{},
|
|
|
|
ClusterAutoscaler: &stubClusterAutoscaler{},
|
|
|
|
wantErr: true,
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion: versions.Latest,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"kubeadm init fails when applying the init config": {
|
|
|
|
clusterUtil: stubClusterUtil{initClusterErr: someErr},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
|
|
|
Kubeconfig: []byte("someKubeconfig"),
|
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
|
|
|
CloudNodeManager: &stubCloudNodeManager{},
|
|
|
|
ClusterAutoscaler: &stubClusterAutoscaler{},
|
|
|
|
wantErr: true,
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion: versions.Latest,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"kubeadm init fails when setting up the pod network": {
|
|
|
|
clusterUtil: stubClusterUtil{setupPodNetworkErr: someErr},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
|
|
|
Kubeconfig: []byte("someKubeconfig"),
|
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
|
|
|
CloudNodeManager: &stubCloudNodeManager{},
|
|
|
|
ClusterAutoscaler: &stubClusterAutoscaler{},
|
|
|
|
wantErr: true,
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion: versions.Latest,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
2022-07-05 08:13:19 -04:00
|
|
|
"kubeadm init fails when setting up the join service": {
|
|
|
|
clusterUtil: stubClusterUtil{setupJoinServiceError: someErr},
|
2022-06-15 10:00:48 -04:00
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
|
|
|
Kubeconfig: []byte("someKubeconfig"),
|
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{SupportedResp: true},
|
|
|
|
CloudNodeManager: &stubCloudNodeManager{},
|
|
|
|
ClusterAutoscaler: &stubClusterAutoscaler{},
|
|
|
|
wantErr: true,
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion: versions.Latest,
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
2022-05-24 04:04:42 -04:00
|
|
|
"kubeadm init fails when setting the cloud contoller manager": {
|
|
|
|
clusterUtil: stubClusterUtil{setupCloudControllerManagerError: someErr},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
|
|
|
Kubeconfig: []byte("someKubeconfig"),
|
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{SupportedResp: true},
|
|
|
|
CloudNodeManager: &stubCloudNodeManager{},
|
|
|
|
ClusterAutoscaler: &stubClusterAutoscaler{},
|
|
|
|
wantErr: true,
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion: versions.Latest,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"kubeadm init fails when setting the cloud node manager": {
|
|
|
|
clusterUtil: stubClusterUtil{setupCloudNodeManagerError: someErr},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
|
|
|
Kubeconfig: []byte("someKubeconfig"),
|
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
|
|
|
CloudNodeManager: &stubCloudNodeManager{SupportedResp: true},
|
|
|
|
ClusterAutoscaler: &stubClusterAutoscaler{},
|
|
|
|
wantErr: true,
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion: versions.Latest,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"kubeadm init fails when setting the cluster autoscaler": {
|
|
|
|
clusterUtil: stubClusterUtil{setupAutoscalingError: someErr},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
|
|
|
Kubeconfig: []byte("someKubeconfig"),
|
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
|
|
|
CloudNodeManager: &stubCloudNodeManager{},
|
|
|
|
ClusterAutoscaler: &stubClusterAutoscaler{SupportedResp: true},
|
|
|
|
wantErr: true,
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion: versions.Latest,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"kubeadm init fails when reading kubeconfig": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
|
|
|
ReadErr: someErr,
|
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
|
|
|
CloudNodeManager: &stubCloudNodeManager{},
|
|
|
|
ClusterAutoscaler: &stubClusterAutoscaler{},
|
|
|
|
wantErr: true,
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion: versions.Latest,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
"kubeadm init fails when setting up the kms": {
|
|
|
|
clusterUtil: stubClusterUtil{setupKMSError: someErr},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
|
|
|
Kubeconfig: []byte("someKubeconfig"),
|
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{SupportedResp: false},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
|
|
|
CloudNodeManager: &stubCloudNodeManager{SupportedResp: false},
|
|
|
|
ClusterAutoscaler: &stubClusterAutoscaler{},
|
|
|
|
wantErr: true,
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion: versions.Latest,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
2022-06-28 11:03:28 -04:00
|
|
|
"kubeadm init fails when setting up verification service": {
|
|
|
|
clusterUtil: stubClusterUtil{setupVerificationServiceErr: someErr},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
|
|
|
Kubeconfig: []byte("someKubeconfig"),
|
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{SupportedResp: false},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
|
|
|
CloudNodeManager: &stubCloudNodeManager{SupportedResp: false},
|
|
|
|
ClusterAutoscaler: &stubClusterAutoscaler{},
|
|
|
|
wantErr: true,
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion: versions.Latest,
|
2022-07-18 06:28:02 -04:00
|
|
|
},
|
|
|
|
"unsupported k8sVersion fails cluster creation": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
kubeconfigReader: &stubKubeconfigReader{
|
|
|
|
Kubeconfig: []byte("someKubeconfig"),
|
|
|
|
},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
|
|
|
CloudNodeManager: &stubCloudNodeManager{},
|
|
|
|
ClusterAutoscaler: &stubClusterAutoscaler{},
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion: "1.19",
|
2022-07-18 06:28:02 -04:00
|
|
|
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{
|
|
|
|
clusterUtil: &tc.clusterUtil,
|
|
|
|
providerMetadata: tc.providerMetadata,
|
|
|
|
cloudControllerManager: tc.CloudControllerManager,
|
|
|
|
cloudNodeManager: tc.CloudNodeManager,
|
|
|
|
clusterAutoscaler: tc.ClusterAutoscaler,
|
|
|
|
configProvider: &stubConfigProvider{InitConfig: k8sapi.KubeadmInitYAML{}},
|
2022-07-18 06:28:02 -04:00
|
|
|
client: &tc.kubectl,
|
2022-05-24 04:04:42 -04:00
|
|
|
kubeconfigReader: tc.kubeconfigReader,
|
2022-06-28 12:23:24 -04:00
|
|
|
getIPAddr: func() (string, error) { return privateIP, nil },
|
2022-05-24 04:04:42 -04:00
|
|
|
}
|
2022-07-26 04:58:39 -04:00
|
|
|
_, err := kube.InitCluster(context.Background(), autoscalingNodeGroups, serviceAccountURI, string(tc.k8sVersion), nil, KMSConfig{MasterSecret: masterSecret}, nil, logger.NewTest(t))
|
2022-05-24 04:04:42 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
var kubeadmConfig k8sapi.KubeadmInitYAML
|
|
|
|
require.NoError(resources.UnmarshalK8SResources(tc.clusterUtil.initConfigs[0], &kubeadmConfig))
|
|
|
|
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{
|
|
|
|
APIServerEndpoint: "192.0.2.0:6443",
|
|
|
|
Token: "kube-fake-token",
|
|
|
|
CACertHashes: []string{"sha256:a60ebe9b0879090edd83b40a4df4bebb20506bac1e51d518ff8f4505a721930f"},
|
|
|
|
}
|
|
|
|
|
2022-06-28 12:23:24 -04:00
|
|
|
privateIP := "192.0.2.1"
|
2022-07-22 09:05:04 -04:00
|
|
|
k8sVersion := versions.Latest
|
2022-05-24 04:04:42 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
clusterUtil stubClusterUtil
|
|
|
|
providerMetadata ProviderMetadata
|
|
|
|
CloudControllerManager CloudControllerManager
|
|
|
|
wantConfig kubeadm.JoinConfiguration
|
|
|
|
role role.Role
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"kubeadm join worker works without metadata": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
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{
|
2022-06-28 12:23:24 -04:00
|
|
|
Name: privateIP,
|
|
|
|
KubeletExtraArgs: map[string]string{"node-ip": privateIP},
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"kubeadm join worker works with metadata": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
providerMetadata: &stubProviderMetadata{
|
|
|
|
SupportedResp: true,
|
2022-06-28 12:23:24 -04:00
|
|
|
SelfResp: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
ProviderID: "provider-id",
|
|
|
|
Name: "metadata-name",
|
|
|
|
PrivateIPs: []string{"192.0.2.1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
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 worker works with metadata and cloud controller manager": {
|
|
|
|
clusterUtil: stubClusterUtil{},
|
|
|
|
providerMetadata: &stubProviderMetadata{
|
|
|
|
SupportedResp: true,
|
2022-06-28 12:23:24 -04:00
|
|
|
SelfResp: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
ProviderID: "provider-id",
|
|
|
|
Name: "metadata-name",
|
|
|
|
PrivateIPs: []string{"192.0.2.1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{
|
|
|
|
SupportedResp: true,
|
|
|
|
},
|
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{
|
|
|
|
SupportedResp: true,
|
2022-06-28 12:23:24 -04:00
|
|
|
SelfResp: metadata.InstanceMetadata{
|
2022-05-24 04:04:42 -04:00
|
|
|
ProviderID: "provider-id",
|
|
|
|
Name: "metadata-name",
|
|
|
|
PrivateIPs: []string{"192.0.2.1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
2022-06-29 09:26:29 -04: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",
|
|
|
|
BindPort: 6443,
|
|
|
|
},
|
|
|
|
},
|
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{
|
|
|
|
SupportedResp: true,
|
|
|
|
SelfErr: someErr,
|
|
|
|
},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
2022-06-29 09:26:29 -04:00
|
|
|
role: role.Worker,
|
2022-05-24 04:04:42 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"kubeadm join worker fails when applying the join config": {
|
|
|
|
clusterUtil: stubClusterUtil{joinClusterErr: someErr},
|
|
|
|
providerMetadata: &stubProviderMetadata{},
|
|
|
|
CloudControllerManager: &stubCloudControllerManager{},
|
2022-06-29 09:26:29 -04:00
|
|
|
role: role.Worker,
|
2022-05-24 04:04:42 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
kube := KubeWrapper{
|
|
|
|
clusterUtil: &tc.clusterUtil,
|
|
|
|
providerMetadata: tc.providerMetadata,
|
|
|
|
cloudControllerManager: tc.CloudControllerManager,
|
|
|
|
configProvider: &stubConfigProvider{},
|
2022-06-28 12:23:24 -04:00
|
|
|
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
|
|
|
|
joinYaml, err = joinYaml.Unmarshal(tc.clusterUtil.joinConfigs[0])
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
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-05-19 11:18:22 -04:00
|
|
|
installComponentsErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
initClusterErr error
|
|
|
|
setupPodNetworkErr error
|
|
|
|
setupAutoscalingError error
|
2022-07-05 08:13:19 -04:00
|
|
|
setupJoinServiceError error
|
2022-03-25 05:42:27 -04:00
|
|
|
setupCloudControllerManagerError error
|
2022-03-25 05:49:18 -04:00
|
|
|
setupCloudNodeManagerError error
|
2022-04-12 10:07:17 -04:00
|
|
|
setupKMSError error
|
2022-06-13 10:23:19 -04:00
|
|
|
setupAccessManagerError error
|
2022-06-28 11:03:28 -04:00
|
|
|
setupVerificationServiceErr error
|
2022-07-05 08:14:11 -04:00
|
|
|
setupGCPGuestAgentErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
joinClusterErr error
|
2022-05-19 11:18:22 -04:00
|
|
|
startKubeletErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
restartKubeletErr error
|
|
|
|
|
|
|
|
initConfigs [][]byte
|
|
|
|
joinConfigs [][]byte
|
|
|
|
}
|
|
|
|
|
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-07-15 03:33:11 -04:00
|
|
|
func (s *stubClusterUtil) InitCluster(ctx context.Context, initConfig []byte, nodeName string, ips []net.IP, 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-07-14 15:15:31 -04:00
|
|
|
func (s *stubClusterUtil) SetupPodNetwork(context.Context, k8sapi.SetupPodNetworkInput, k8sapi.Client) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
return s.setupPodNetworkErr
|
|
|
|
}
|
|
|
|
|
2022-03-29 07:25:04 -04:00
|
|
|
func (s *stubClusterUtil) SetupAutoscaling(kubectl k8sapi.Client, clusterAutoscalerConfiguration resources.Marshaler, secrets resources.Marshaler) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
return s.setupAutoscalingError
|
|
|
|
}
|
|
|
|
|
2022-07-05 08:13:19 -04:00
|
|
|
func (s *stubClusterUtil) SetupJoinService(kubectl k8sapi.Client, joinServiceConfiguration resources.Marshaler) error {
|
|
|
|
return s.setupJoinServiceError
|
2022-06-15 10:00:48 -04:00
|
|
|
}
|
|
|
|
|
2022-07-05 08:14:11 -04:00
|
|
|
func (s *stubClusterUtil) SetupGCPGuestAgent(kubectl k8sapi.Client, gcpGuestAgentConfiguration resources.Marshaler) error {
|
|
|
|
return s.setupGCPGuestAgentErr
|
|
|
|
}
|
|
|
|
|
2022-03-25 05:42:27 -04:00
|
|
|
func (s *stubClusterUtil) SetupCloudControllerManager(kubectl k8sapi.Client, cloudControllerManagerConfiguration resources.Marshaler, configMaps resources.Marshaler, secrets resources.Marshaler) error {
|
|
|
|
return s.setupCloudControllerManagerError
|
|
|
|
}
|
2022-03-25 05:49:18 -04:00
|
|
|
|
2022-04-12 10:07:17 -04:00
|
|
|
func (s *stubClusterUtil) SetupKMS(kubectl k8sapi.Client, kmsDeployment resources.Marshaler) error {
|
|
|
|
return s.setupKMSError
|
|
|
|
}
|
|
|
|
|
2022-06-13 10:23:19 -04:00
|
|
|
func (s *stubClusterUtil) SetupAccessManager(kubectl k8sapi.Client, accessManagerConfiguration resources.Marshaler) error {
|
|
|
|
return s.setupAccessManagerError
|
|
|
|
}
|
|
|
|
|
2022-03-25 05:49:18 -04:00
|
|
|
func (s *stubClusterUtil) SetupCloudNodeManager(kubectl k8sapi.Client, cloudNodeManagerConfiguration resources.Marshaler) error {
|
|
|
|
return s.setupCloudNodeManagerError
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-06-28 11:03:28 -04:00
|
|
|
func (s *stubClusterUtil) SetupVerificationService(kubectl k8sapi.Client, verificationServiceConfiguration resources.Marshaler) error {
|
|
|
|
return s.setupVerificationServiceErr
|
|
|
|
}
|
|
|
|
|
2022-07-14 07:30:44 -04:00
|
|
|
func (s *stubClusterUtil) JoinCluster(ctx context.Context, joinConfig []byte, 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-03-22 11:03:15 -04:00
|
|
|
func (s *stubClusterUtil) RestartKubelet() error {
|
|
|
|
return s.restartKubeletErr
|
|
|
|
}
|
|
|
|
|
2022-06-13 10:01:21 -04:00
|
|
|
func (s *stubClusterUtil) FixCilium(nodeName string) {
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
type stubConfigProvider struct {
|
|
|
|
InitConfig k8sapi.KubeadmInitYAML
|
|
|
|
JoinConfig k8sapi.KubeadmJoinYAML
|
|
|
|
}
|
|
|
|
|
2022-07-22 09:05:04 -04:00
|
|
|
func (s *stubConfigProvider) InitConfiguration(_ bool, _ versions.ValidK8sVersion) k8sapi.KubeadmInitYAML {
|
2022-03-22 11:03:15 -04:00
|
|
|
return s.InitConfig
|
|
|
|
}
|
|
|
|
|
2022-04-27 10:37:05 -04:00
|
|
|
func (s *stubConfigProvider) JoinConfiguration(_ bool) k8sapi.KubeadmJoinYAML {
|
2022-03-22 11:03:15 -04:00
|
|
|
s.JoinConfig = k8sapi.KubeadmJoinYAML{
|
|
|
|
JoinConfiguration: kubeadm.JoinConfiguration{
|
|
|
|
Discovery: kubeadm.Discovery{
|
|
|
|
BootstrapToken: &kubeadm.BootstrapTokenDiscovery{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return s.JoinConfig
|
|
|
|
}
|
|
|
|
|
2022-07-18 06:28:02 -04:00
|
|
|
type stubKubectl struct {
|
2022-07-14 15:15:31 -04:00
|
|
|
ApplyErr error
|
|
|
|
createConfigMapErr error
|
|
|
|
AddTolerationsToDeploymentErr error
|
2022-07-26 04:10:34 -04:00
|
|
|
AddTNodeSelectorsToDeploymentErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
resources []resources.Marshaler
|
|
|
|
kubeconfigs [][]byte
|
|
|
|
}
|
|
|
|
|
2022-07-18 06:28:02 -04:00
|
|
|
func (s *stubKubectl) Apply(resources resources.Marshaler, forceConflicts bool) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
s.resources = append(s.resources, resources)
|
|
|
|
return s.ApplyErr
|
|
|
|
}
|
|
|
|
|
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-07-14 15:15:31 -04:00
|
|
|
return s.AddTolerationsToDeploymentErr
|
|
|
|
}
|
|
|
|
|
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-07-26 04:10:34 -04:00
|
|
|
return s.AddTNodeSelectorsToDeploymentErr
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
type stubKubeconfigReader struct {
|
|
|
|
Kubeconfig []byte
|
|
|
|
ReadErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubKubeconfigReader) ReadKubeconfig() ([]byte, error) {
|
|
|
|
return s.Kubeconfig, s.ReadErr
|
|
|
|
}
|