mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-12-15 16:09:39 -05:00
AB#2525 clean up unused code (#504)
* Rename Metadata->Cloud * Remove unused methods, functions, and variables * More privacy for testing stubs Signed-off-by: Daniel Weiße <dw@edgeless.systems>
This commit is contained in:
parent
7d16c02e0d
commit
5efe05d933
22 changed files with 115 additions and 484 deletions
|
|
@ -1,58 +0,0 @@
|
|||
/*
|
||||
Copyright (c) Edgeless Systems GmbH
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package k8sapi
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/shlex"
|
||||
kubeadm "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
||||
)
|
||||
|
||||
// ParseJoinCommand parses API server endpoint, token and CA cert hash from
|
||||
// provided kubeadm join shell command, and returns it as a BootstrapTokenDiscovery.
|
||||
//
|
||||
// Expected format:
|
||||
// kubeadm join [API_SERVER_ENDPOINT] --token [TOKEN] --discovery-token-ca-cert-hash [DISCOVERY_TOKEN_CA_CERT_HASH] --control-plane
|
||||
// .
|
||||
func ParseJoinCommand(joinCommand string) (*kubeadm.BootstrapTokenDiscovery, error) {
|
||||
// split and verify that this is a kubeadm join command
|
||||
argv, err := shlex.Split(joinCommand)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kubadm join command could not be tokenized: %v", joinCommand)
|
||||
}
|
||||
if len(argv) < 3 {
|
||||
return nil, fmt.Errorf("kubadm join command is too short: %v", argv)
|
||||
}
|
||||
if argv[0] != "kubeadm" || argv[1] != "join" {
|
||||
return nil, fmt.Errorf("not a kubeadm join command: %v", argv)
|
||||
}
|
||||
|
||||
result := kubeadm.BootstrapTokenDiscovery{APIServerEndpoint: argv[2]}
|
||||
|
||||
var caCertHash string
|
||||
// parse flags
|
||||
flags := flag.NewFlagSet("", flag.ContinueOnError)
|
||||
flags.StringVar(&result.Token, "token", "", "")
|
||||
flags.StringVar(&caCertHash, "discovery-token-ca-cert-hash", "", "")
|
||||
flags.Bool("control-plane", false, "")
|
||||
flags.String("certificate-key", "", "")
|
||||
if err := flags.Parse(argv[3:]); err != nil {
|
||||
return nil, fmt.Errorf("parsing flag arguments: %v %w", argv, err)
|
||||
}
|
||||
|
||||
if result.Token == "" {
|
||||
return nil, fmt.Errorf("missing flag argument token: %v", argv)
|
||||
}
|
||||
if caCertHash == "" {
|
||||
return nil, fmt.Errorf("missing flag argument discovery-token-ca-cert-hash: %v", argv)
|
||||
}
|
||||
result.CACertHashes = []string{caCertHash}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
|
@ -1,75 +0,0 @@
|
|||
/*
|
||||
Copyright (c) Edgeless Systems GmbH
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package k8sapi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
kubeadm "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
||||
)
|
||||
|
||||
func TestParseJoinCommand(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
joinCommand string
|
||||
wantJoinArgs kubeadm.BootstrapTokenDiscovery
|
||||
wantErr bool
|
||||
}{
|
||||
"join command can be parsed": {
|
||||
joinCommand: "kubeadm join 192.0.2.0:8443 --token dummy-token --discovery-token-ca-cert-hash sha512:dummy-hash --control-plane",
|
||||
wantJoinArgs: kubeadm.BootstrapTokenDiscovery{
|
||||
APIServerEndpoint: "192.0.2.0:8443",
|
||||
Token: "dummy-token",
|
||||
CACertHashes: []string{"sha512:dummy-hash"},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
"incorrect join command returns error": {
|
||||
joinCommand: "some string",
|
||||
wantErr: true,
|
||||
},
|
||||
"missing api server endpoint is checked": {
|
||||
joinCommand: "kubeadm join --token dummy-token --discovery-token-ca-cert-hash sha512:dummy-hash --control-plane",
|
||||
wantErr: true,
|
||||
},
|
||||
"missing token is checked": {
|
||||
joinCommand: "kubeadm join 192.0.2.0:8443 --discovery-token-ca-cert-hash sha512:dummy-hash --control-plane",
|
||||
wantErr: true,
|
||||
},
|
||||
"missing discovery-token-ca-cert-hash is checked": {
|
||||
joinCommand: "kubeadm join 192.0.2.0:8443 --token dummy-token --control-plane",
|
||||
wantErr: true,
|
||||
},
|
||||
"missing control-plane": {
|
||||
joinCommand: "kubeadm join 192.0.2.0:8443 --token dummy-token --discovery-token-ca-cert-hash sha512:dummy-hash",
|
||||
wantJoinArgs: kubeadm.BootstrapTokenDiscovery{
|
||||
APIServerEndpoint: "192.0.2.0:8443",
|
||||
Token: "dummy-token",
|
||||
CACertHashes: []string{"sha512:dummy-hash"},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
|
||||
joinArgs, err := ParseJoinCommand(tc.joinCommand)
|
||||
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
|
||||
assert.Equal(&tc.wantJoinArgs, joinArgs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -252,7 +252,6 @@ type SetupPodNetworkInput struct {
|
|||
NodeName string
|
||||
FirstNodePodCIDR string
|
||||
SubnetworkPodCIDR string
|
||||
ProviderID string
|
||||
LoadBalancerEndpoint string
|
||||
}
|
||||
|
||||
|
|
@ -317,14 +316,6 @@ func (k *KubernetesUtil) FixCilium(log *logger.Logger) {
|
|||
}
|
||||
}
|
||||
|
||||
// SetupAutoscaling deploys the k8s cluster autoscaler.
|
||||
func (k *KubernetesUtil) SetupAutoscaling(kubectl Client, clusterAutoscalerConfiguration kubernetes.Marshaler, secrets kubernetes.Marshaler) error {
|
||||
if err := kubectl.Apply(secrets, true); err != nil {
|
||||
return fmt.Errorf("applying cluster-autoscaler Secrets: %w", err)
|
||||
}
|
||||
return kubectl.Apply(clusterAutoscalerConfiguration, true)
|
||||
}
|
||||
|
||||
// SetupGCPGuestAgent deploys the GCP guest agent daemon set.
|
||||
func (k *KubernetesUtil) SetupGCPGuestAgent(kubectl Client, guestAgentDaemonset kubernetes.Marshaler) error {
|
||||
return kubectl.Apply(guestAgentDaemonset, true)
|
||||
|
|
|
|||
|
|
@ -260,12 +260,6 @@ func (k *KubeadmJoinYAML) Marshal() ([]byte, error) {
|
|||
return kubernetes.MarshalK8SResources(k)
|
||||
}
|
||||
|
||||
// Unmarshal from a k8s resource YAML.
|
||||
func (k *KubeadmJoinYAML) Unmarshal(yamlData []byte) (KubeadmJoinYAML, error) {
|
||||
var tmp KubeadmJoinYAML
|
||||
return tmp, kubernetes.UnmarshalK8SResources(yamlData, &tmp)
|
||||
}
|
||||
|
||||
// KubeadmInitYAML holds configuration for kubeadm init workflow.
|
||||
type KubeadmInitYAML struct {
|
||||
InitConfiguration kubeadm.InitConfiguration
|
||||
|
|
@ -288,11 +282,6 @@ func (k *KubeadmInitYAML) SetCertSANs(certSANs []string) {
|
|||
}
|
||||
}
|
||||
|
||||
// SetAPIServerAdvertiseAddress sets the advertised API server address.
|
||||
func (k *KubeadmInitYAML) SetAPIServerAdvertiseAddress(apiServerAdvertiseAddress string) {
|
||||
k.InitConfiguration.LocalAPIEndpoint.AdvertiseAddress = apiServerAdvertiseAddress
|
||||
}
|
||||
|
||||
// SetControlPlaneEndpoint sets the control plane endpoint if controlPlaneEndpoint is not empty.
|
||||
func (k *KubeadmInitYAML) SetControlPlaneEndpoint(controlPlaneEndpoint string) {
|
||||
if controlPlaneEndpoint != "" {
|
||||
|
|
@ -300,21 +289,6 @@ func (k *KubeadmInitYAML) SetControlPlaneEndpoint(controlPlaneEndpoint string) {
|
|||
}
|
||||
}
|
||||
|
||||
// SetServiceCIDR sets the CIDR of service subnet.
|
||||
func (k *KubeadmInitYAML) SetServiceCIDR(serviceCIDR string) {
|
||||
k.ClusterConfiguration.Networking.ServiceSubnet = serviceCIDR
|
||||
}
|
||||
|
||||
// SetPodNetworkCIDR sets the CIDR of pod subnet.
|
||||
func (k *KubeadmInitYAML) SetPodNetworkCIDR(podNetworkCIDR string) {
|
||||
k.ClusterConfiguration.Networking.PodSubnet = podNetworkCIDR
|
||||
}
|
||||
|
||||
// SetServiceDNSDomain sets the dns domain.
|
||||
func (k *KubeadmInitYAML) SetServiceDNSDomain(serviceDNSDomain string) {
|
||||
k.ClusterConfiguration.Networking.DNSDomain = serviceDNSDomain
|
||||
}
|
||||
|
||||
// SetNodeIP sets the node IP.
|
||||
func (k *KubeadmInitYAML) SetNodeIP(nodeIP string) {
|
||||
if k.InitConfiguration.NodeRegistration.KubeletExtraArgs == nil {
|
||||
|
|
@ -337,9 +311,3 @@ func (k *KubeadmInitYAML) SetProviderID(providerID string) {
|
|||
func (k *KubeadmInitYAML) Marshal() ([]byte, error) {
|
||||
return kubernetes.MarshalK8SResources(k)
|
||||
}
|
||||
|
||||
// Unmarshal from a k8s resource YAML.
|
||||
func (k *KubeadmInitYAML) Unmarshal(yamlData []byte) (KubeadmInitYAML, error) {
|
||||
var tmp KubeadmInitYAML
|
||||
return tmp, kubernetes.UnmarshalK8SResources(yamlData, &tmp)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ package k8sapi
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/edgelesssys/constellation/v2/internal/kubernetes"
|
||||
"github.com/edgelesssys/constellation/v2/internal/versions"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
|
@ -32,11 +33,8 @@ func TestInitConfiguration(t *testing.T) {
|
|||
"kubeadm init config with all fields can be created": {
|
||||
config: func() KubeadmInitYAML {
|
||||
c := kubeadmConfig.InitConfiguration(true, versions.Default)
|
||||
c.SetAPIServerAdvertiseAddress("192.0.2.0")
|
||||
c.SetNodeIP("192.0.2.0")
|
||||
c.SetNodeName("node")
|
||||
c.SetPodNetworkCIDR("10.244.0.0/16")
|
||||
c.SetServiceCIDR("10.245.0.0/24")
|
||||
c.SetProviderID("somecloudprovider://instance-id")
|
||||
return c
|
||||
}(),
|
||||
|
|
@ -49,8 +47,8 @@ func TestInitConfiguration(t *testing.T) {
|
|||
|
||||
config, err := tc.config.Marshal()
|
||||
require.NoError(err)
|
||||
tmp, err := tc.config.Unmarshal(config)
|
||||
require.NoError(err)
|
||||
var tmp KubeadmInitYAML
|
||||
require.NoError(kubernetes.UnmarshalK8SResources(config, &tmp))
|
||||
// test on correct mashalling and unmarshalling
|
||||
assert.Equal(tc.config.ClusterConfiguration, tmp.ClusterConfiguration)
|
||||
assert.Equal(tc.config.InitConfiguration, tmp.InitConfiguration)
|
||||
|
|
@ -120,8 +118,8 @@ func TestJoinConfiguration(t *testing.T) {
|
|||
|
||||
config, err := tc.config.Marshal()
|
||||
require.NoError(err)
|
||||
tmp, err := tc.config.Unmarshal(config)
|
||||
require.NoError(err)
|
||||
var tmp KubeadmJoinYAML
|
||||
require.NoError(kubernetes.UnmarshalK8SResources(config, &tmp))
|
||||
// test on correct mashalling and unmarshalling
|
||||
assert.Equal(tc.config.JoinConfiguration, tmp.JoinConfiguration)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -39,30 +39,30 @@ users:
|
|||
`
|
||||
|
||||
type stubClientConfig struct {
|
||||
RawConfigConfig clientcmdapi.Config
|
||||
RawConfigErr error
|
||||
ClientConfigConfig *restclient.Config
|
||||
ClientConfigErr error
|
||||
NamespaceString string
|
||||
NamespaceOverridden bool
|
||||
NamespaceErr error
|
||||
ConfigAccessResult clientcmd.ConfigAccess
|
||||
rawConfigConfig clientcmdapi.Config
|
||||
rawConfigErr error
|
||||
clientConfigConfig *restclient.Config
|
||||
clientConfigErr error
|
||||
namespaceString string
|
||||
namespaceOverridden bool
|
||||
namespaceErr error
|
||||
configAccessResult clientcmd.ConfigAccess
|
||||
}
|
||||
|
||||
func (s *stubClientConfig) RawConfig() (clientcmdapi.Config, error) {
|
||||
return s.RawConfigConfig, s.RawConfigErr
|
||||
return s.rawConfigConfig, s.rawConfigErr
|
||||
}
|
||||
|
||||
func (s *stubClientConfig) ClientConfig() (*restclient.Config, error) {
|
||||
return s.ClientConfigConfig, s.ClientConfigErr
|
||||
return s.clientConfigConfig, s.clientConfigErr
|
||||
}
|
||||
|
||||
func (s *stubClientConfig) Namespace() (string, bool, error) {
|
||||
return s.NamespaceString, s.NamespaceOverridden, s.NamespaceErr
|
||||
return s.namespaceString, s.namespaceOverridden, s.namespaceErr
|
||||
}
|
||||
|
||||
func (s *stubClientConfig) ConfigAccess() clientcmd.ConfigAccess {
|
||||
return s.ConfigAccessResult
|
||||
return s.configAccessResult
|
||||
}
|
||||
|
||||
func TestNewRESTClientGetter(t *testing.T) {
|
||||
|
|
@ -76,7 +76,7 @@ func TestToRESTConfig(t *testing.T) {
|
|||
require := require.New(t)
|
||||
getter := restClientGetter{
|
||||
clientconfig: &stubClientConfig{
|
||||
ClientConfigConfig: &restclient.Config{},
|
||||
clientConfigConfig: &restclient.Config{},
|
||||
},
|
||||
}
|
||||
result, err := getter.ToRESTConfig()
|
||||
|
|
@ -88,7 +88,7 @@ func TestToDiscoveryClient(t *testing.T) {
|
|||
require := require.New(t)
|
||||
getter := restClientGetter{
|
||||
clientconfig: &stubClientConfig{
|
||||
ClientConfigConfig: &restclient.Config{},
|
||||
clientConfigConfig: &restclient.Config{},
|
||||
},
|
||||
}
|
||||
result, err := getter.ToDiscoveryClient()
|
||||
|
|
@ -100,7 +100,7 @@ func TestToDiscoveryClientFail(t *testing.T) {
|
|||
require := require.New(t)
|
||||
getter := restClientGetter{
|
||||
clientconfig: &stubClientConfig{
|
||||
ClientConfigErr: errors.New("someErr"),
|
||||
clientConfigErr: errors.New("someErr"),
|
||||
},
|
||||
}
|
||||
_, err := getter.ToDiscoveryClient()
|
||||
|
|
@ -111,7 +111,7 @@ func TestToRESTMapper(t *testing.T) {
|
|||
require := require.New(t)
|
||||
getter := restClientGetter{
|
||||
clientconfig: &stubClientConfig{
|
||||
ClientConfigConfig: &restclient.Config{},
|
||||
clientConfigConfig: &restclient.Config{},
|
||||
},
|
||||
}
|
||||
result, err := getter.ToRESTMapper()
|
||||
|
|
@ -123,7 +123,7 @@ func TestToRESTMapperFail(t *testing.T) {
|
|||
require := require.New(t)
|
||||
getter := restClientGetter{
|
||||
clientconfig: &stubClientConfig{
|
||||
ClientConfigErr: errors.New("someErr"),
|
||||
clientConfigErr: errors.New("someErr"),
|
||||
},
|
||||
}
|
||||
_, err := getter.ToRESTMapper()
|
||||
|
|
@ -132,7 +132,7 @@ func TestToRESTMapperFail(t *testing.T) {
|
|||
|
||||
func TestToRawKubeConfigLoader(t *testing.T) {
|
||||
clientConfig := stubClientConfig{
|
||||
ClientConfigConfig: &restclient.Config{},
|
||||
clientConfigConfig: &restclient.Config{},
|
||||
}
|
||||
require := require.New(t)
|
||||
getter := restClientGetter{
|
||||
|
|
|
|||
|
|
@ -22,14 +22,6 @@ const (
|
|||
nodeOperatorCatalogNamespace = "olm"
|
||||
)
|
||||
|
||||
// NodeOperatorCRDNames are the names of the custom resource definitions that are used by the node operator.
|
||||
var NodeOperatorCRDNames = []string{
|
||||
"autoscalingstrategies.update.edgeless.systems",
|
||||
"nodeimages.update.edgeless.systems",
|
||||
"pendingnodes.update.edgeless.systems",
|
||||
"scalinggroups.update.edgeless.systems",
|
||||
}
|
||||
|
||||
// NodeOperatorDeployment groups all deployments for node operator.
|
||||
type NodeOperatorDeployment struct {
|
||||
CatalogSource operatorsv1alpha1.CatalogSource
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue