Skip to content

Commit

Permalink
fixup! refactor: run a script to configure kube-vip
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoshkin committed Jun 7, 2024
1 parent 7aa1443 commit 0d5c982
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
13 changes: 6 additions & 7 deletions pkg/handlers/generic/mutation/controlplanevirtualip/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,11 @@ func (h *ControlPlaneVirtualIP) Mutate(
selectors.ControlPlane(),
log,
func(obj *controlplanev1.KubeadmControlPlaneTemplate) error {
virtualIPProviderFiles, preKubeadmCommands, postKubeadmCommands, generateErr :=
virtualIPProvider.GenerateFilesAndCommands(
ctx,
controlPlaneEndpointVar,
cluster,
)
files, preKubeadmCommands, postKubeadmCommands, generateErr := virtualIPProvider.GenerateFilesAndCommands(
ctx,
controlPlaneEndpointVar,
cluster,
)
if generateErr != nil {
return generateErr
}
Expand All @@ -153,7 +152,7 @@ func (h *ControlPlaneVirtualIP) Mutate(
))
obj.Spec.Template.Spec.KubeadmConfigSpec.Files = append(
obj.Spec.Template.Spec.KubeadmConfigSpec.Files,
virtualIPProviderFiles...,
files...,
)

if len(preKubeadmCommands) > 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ var (
configureForKubeVIPScriptOnRemote = common.ConfigFilePathOnRemote(
"configure-for-kube-vip.sh")

configureForKubeVIPScriptOnRemotePreKubeadmCommand = "/bin/bash " + configureForKubeVIPScriptOnRemote + " use-super-admin.conf"
configureForKubeVIPScriptOnRemotePostKubeadmCommand = "/bin/bash " + configureForKubeVIPScriptOnRemote + " use-admin.conf"
configureForKubeVIPScriptOnRemotePreKubeadmCommand = "/bin/bash " +
configureForKubeVIPScriptOnRemote + " use-super-admin.conf"
configureForKubeVIPScriptOnRemotePostKubeadmCommand = "/bin/bash " +
configureForKubeVIPScriptOnRemote + " use-admin.conf"

setHostAliasesScriptOnRemoteCommand = "/bin/bash " + configureForKubeVIPScriptOnRemote + " set-host-aliases"
)

var (
//go:embed templates/configure-for-kube-vip.sh
configureForKubeVIPScript []byte
)
//go:embed templates/configure-for-kube-vip.sh
var configureForKubeVIPScript []byte

type kubeVIPFromConfigMapProvider struct {
client client.Reader
Expand Down Expand Up @@ -72,7 +72,7 @@ func (p *kubeVIPFromConfigMapProvider) GenerateFilesAndCommands(
ctx context.Context,
spec v1alpha1.ControlPlaneEndpointSpec,
cluster *clusterv1.Cluster,
) ([]bootstrapv1.File, []string, []string, error) {
) (files []bootstrapv1.File, preKubeadmCommands, postKubeadmCommands []string, err error) {
data, err := getTemplateFromConfigMap(ctx, p.client, p.configMapKey)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed getting template data: %w", err)
Expand All @@ -83,7 +83,7 @@ func (p *kubeVIPFromConfigMapProvider) GenerateFilesAndCommands(
return nil, nil, nil, fmt.Errorf("failed templating static Pod: %w", err)
}

files := []bootstrapv1.File{
files = []bootstrapv1.File{
{
Content: kubeVIPStaticPod,
Owner: kubeVIPFileOwner,
Expand Down Expand Up @@ -114,7 +114,10 @@ func (p *kubeVIPFromConfigMapProvider) GenerateFilesAndCommands(
// See https://github.com/kubernetes/kubernetes/issues/122420#issuecomment-1864609518
needCommands, err := needHackCommands(cluster)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to determine if kube-vip commands are needed: %w", err)
return nil, nil, nil, fmt.Errorf(
"failed to determine if kube-vip commands are needed: %w",
err,
)
}
if !needCommands {
return files, nil, nil, nil
Expand All @@ -129,11 +132,11 @@ func (p *kubeVIPFromConfigMapProvider) GenerateFilesAndCommands(
},
)

preKubeadmCommands := []string{
preKubeadmCommands = []string{
setHostAliasesScriptOnRemoteCommand,
configureForKubeVIPScriptOnRemotePreKubeadmCommand,
}
postKubeadmCommands := []string{configureForKubeVIPScriptOnRemotePostKubeadmCommand}
postKubeadmCommands = []string{configureForKubeVIPScriptOnRemotePostKubeadmCommand}

return files, preKubeadmCommands, postKubeadmCommands, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,11 @@ func Test_GenerateFilesAndCommands(t *testing.T) {
configMapKey: client.ObjectKeyFromObject(tt.configMap),
}

files, preKubeadmCommands, postKubeadmCommands, err :=
provider.GenerateFilesAndCommands(
context.TODO(),
tt.controlPlaneEndpointSpec,
tt.cluster,
)
files, preKubeadmCommands, postKubeadmCommands, err := provider.GenerateFilesAndCommands(
context.TODO(),
tt.controlPlaneEndpointSpec,
tt.cluster,
)
require.Equal(t, tt.expectedErr, err)
assert.Equal(t, tt.expectedFiles, files)
assert.Equal(t, tt.expectedPreKubeadmCommands, preKubeadmCommands)
Expand Down

0 comments on commit 0d5c982

Please sign in to comment.