Skip to content

Commit

Permalink
simplify clusterparams
Browse files Browse the repository at this point in the history
  • Loading branch information
r2k1 committed Oct 25, 2024
1 parent 1268fc7 commit 508b925
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 58 deletions.
5 changes: 1 addition & 4 deletions e2e/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ func prepareCluster(ctx context.Context, t *testing.T, cluster *armcontainerserv
}

t.Log("getting the node bootstrapping configuration for cluster")
clusterParams, err := extractClusterParameters(ctx, t, kube)
if err != nil {
return nil, fmt.Errorf("extract cluster parameters: %w", err)
}
clusterParams := extractClusterParameters(ctx, t, kube)

nbc, err := getBaseNodeBootstrappingConfiguration(clusterParams)
if err != nil {
Expand Down
52 changes: 30 additions & 22 deletions e2e/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"bytes"
"context"
"fmt"
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"
Expand Down Expand Up @@ -94,35 +96,41 @@ func extractLogsFromVM(ctx context.Context, t *testing.T, vmssName, privateIP, s
}

type ClusterParams struct {
AzureJSON []byte
CACert []byte
BootstrapKubeconfig []byte
CACert []byte
BootstrapToken string
FQDN string
}

func extractClusterParameters(ctx context.Context, t *testing.T, kube *Kubeclient) (ClusterParams, error) {
func extractClusterParameters(ctx context.Context, t *testing.T, kube *Kubeclient) ClusterParams {
podName, err := getHostNetworkDebugPodName(ctx, kube, t)
if err != nil {
return ClusterParams{}, err
}
require.NoError(t, err)

var resultErr error
exec := func(command string) *podExecResult {
t.Logf("executing privileged command on pod %s/%s: %q", defaultNamespace, podName, command)
execResult, err := execOnPrivilegedPod(ctx, kube, defaultNamespace, podName, command)
if execResult != nil {
execResult.dumpStderr(t)
}
if err != nil {
resultErr = err
}
return execResult
execResult, err := execOnPrivilegedPod(ctx, kube, defaultNamespace, podName, "cat /var/lib/kubelet/bootstrap-kubeconfig")
require.NoError(t, err)

bootstrapConfig := execResult.stdout.Bytes()
bootstrapToken, err := extractKeyValuePair("token", string(bootstrapConfig))
require.NoError(t, err)

bootstrapToken, err = strconv.Unquote(bootstrapToken)
require.NoError(t, err)

server, err := extractKeyValuePair("server", string(bootstrapConfig))
require.NoError(t, err)
tokens := strings.Split(server, ":")
if len(tokens) != 3 {
t.Fatalf("expected 3 tokens from fqdn %q, got %d", server, len(tokens))
}
fqdn := tokens[1][2:]

caCert, err := execOnPrivilegedPod(ctx, kube, defaultNamespace, podName, "cat /etc/kubernetes/certs/ca.crt")
require.NoError(t, err)

return ClusterParams{
AzureJSON: exec("cat /etc/kubernetes/azure.json").stdout.Bytes(),
CACert: exec("cat etc/kubernetes/certs/ca.crt").stdout.Bytes(),
BootstrapKubeconfig: exec("cat /var/lib/kubelet/bootstrap-kubeconfig").stdout.Bytes(),
}, resultErr
CACert: caCert.stdout.Bytes(),
BootstrapToken: bootstrapToken,
FQDN: fqdn,
}
}

func execOnVM(ctx context.Context, kube *Kubeclient, vmPrivateIP, jumpboxPodName, sshPrivateKey, command string, isShellBuiltIn bool) (*podExecResult, error) {
Expand Down
33 changes: 2 additions & 31 deletions e2e/nodebootstrapping.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,14 @@
package e2e

import (
"fmt"
"strconv"
"strings"

"github.com/Azure/agentbaker/pkg/agent/datamodel"
"github.com/Azure/agentbakere2e/config"
)

func getBaseNodeBootstrappingConfiguration(clusterParams ClusterParams) (*datamodel.NodeBootstrappingConfiguration, error) {
nbc := baseTemplate(config.Config.Location)
nbc.ContainerService.Properties.CertificateProfile.CaCertificate = string(clusterParams.CACert)

bootstrapKubeconfig := string(clusterParams.BootstrapKubeconfig)

bootstrapToken, err := extractKeyValuePair("token", bootstrapKubeconfig)
if err != nil {
return nil, fmt.Errorf("failed to extract bootstrap token via regex: %w", err)
}

bootstrapToken, err = strconv.Unquote(bootstrapToken)
if err != nil {
return nil, fmt.Errorf("failed to unquote bootstrap token: %w", err)
}

server, err := extractKeyValuePair("server", bootstrapKubeconfig)
if err != nil {
return nil, fmt.Errorf("failed to extract fqdn via regex: %w", err)
}
tokens := strings.Split(server, ":")
if len(tokens) != 3 {
return nil, fmt.Errorf("expected 3 tokens from fqdn %q, got %d", server, len(tokens))
}
// strip off the // prefix from https://
fqdn := tokens[1][2:]

nbc.KubeletClientTLSBootstrapToken = &bootstrapToken
nbc.ContainerService.Properties.HostedMasterProfile.FQDN = fqdn

nbc.KubeletClientTLSBootstrapToken = &clusterParams.BootstrapToken
nbc.ContainerService.Properties.HostedMasterProfile.FQDN = clusterParams.FQDN
return nbc, nil
}
3 changes: 2 additions & 1 deletion e2e/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
"github.com/Azure/go-autorest/autorest/to"
)

// is a temporary workaround
// eventually we want to phase out usage of nbc
func nbcToNbcContractV1(nbc *datamodel.NodeBootstrappingConfiguration) *nbcontractv1.Configuration {
cs := nbc.ContainerService
agentPool := nbc.AgentPoolProfile
// TODO: delete me
agent.ValidateAndSetLinuxNodeBootstrappingConfiguration(nbc)

config := &nbcontractv1.Configuration{
Expand Down

0 comments on commit 508b925

Please sign in to comment.