Use single structure and function for CP ignition generation

Signed-off-by: Alexandr Demicev <alexandr.demicev@suse.com>
This commit is contained in:
Alexandr Demicev 2023-05-04 11:34:16 +02:00
parent 00c1b759d2
commit 244768ff83
No known key found for this signature in database
GPG Key ID: D179B8D78CB2D9B7
3 changed files with 49 additions and 73 deletions

View File

@ -414,23 +414,23 @@ func (r *RKE2ConfigReconciler) handleClusterNotInitialized(ctx context.Context,
Certificates: certificates,
}
var cloudInitData []byte
var userData []byte
switch scope.Config.Spec.AgentConfig.Format {
case bootstrapv1.Ignition:
cloudInitData, err = ignition.NewInitControlPlane(&ignition.ControlPlaneInitInput{
userData, err = ignition.NewInitControlPlane(&ignition.ControlPlaneInput{
ControlPlaneInput: cpinput,
AdditionalIgnition: &scope.Config.Spec.AgentConfig.AdditionalUserData,
})
default:
cloudInitData, err = cloudinit.NewInitControlPlane(cpinput)
userData, err = cloudinit.NewInitControlPlane(cpinput)
}
if err != nil {
return ctrl.Result{}, err
}
if err := r.storeBootstrapData(ctx, scope, cloudInitData); err != nil {
if err := r.storeBootstrapData(ctx, scope, userData); err != nil {
return ctrl.Result{}, err
}
@ -593,23 +593,23 @@ func (r *RKE2ConfigReconciler) joinControlplane(ctx context.Context, scope *Scop
return ctrl.Result{}, err
}
var cloudInitData []byte
var userData []byte
switch scope.Config.Spec.AgentConfig.Format {
case bootstrapv1.Ignition:
cloudInitData, err = ignition.NewJoinControlPlane(&ignition.ControlPlaneJoinInput{
userData, err = ignition.NewJoinControlPlane(&ignition.ControlPlaneInput{
ControlPlaneInput: cpinput,
AdditionalIgnition: &scope.Config.Spec.AgentConfig.AdditionalUserData,
})
default:
cloudInitData, err = cloudinit.NewJoinControlPlane(cpinput)
userData, err = cloudinit.NewJoinControlPlane(cpinput)
}
if err != nil {
return ctrl.Result{}, err
}
if err := r.storeBootstrapData(ctx, scope, cloudInitData); err != nil {
if err := r.storeBootstrapData(ctx, scope, userData); err != nil {
return ctrl.Result{}, err
}
@ -695,23 +695,23 @@ func (r *RKE2ConfigReconciler) joinWorker(ctx context.Context, scope *Scope) (re
AdditionalCloudInit: scope.Config.Spec.AgentConfig.AdditionalUserData.Config,
}
var cloudInitData []byte
var userData []byte
switch scope.Config.Spec.AgentConfig.Format {
case bootstrapv1.Ignition:
cloudInitData, err = ignition.NewJoinWorker(&ignition.JoinWorkerInput{
userData, err = ignition.NewJoinWorker(&ignition.JoinWorkerInput{
BaseUserData: wkInput,
AdditionalIgnition: &scope.Config.Spec.AgentConfig.AdditionalUserData,
})
default:
cloudInitData, err = cloudinit.NewJoinWorker(wkInput)
userData, err = cloudinit.NewJoinWorker(wkInput)
}
if err != nil {
return ctrl.Result{}, err
}
if err := r.storeBootstrapData(ctx, scope, cloudInitData); err != nil {
if err := r.storeBootstrapData(ctx, scope, userData); err != nil {
return ctrl.Result{}, err
}

View File

@ -12,7 +12,7 @@ limitations under the License.
*/
// Package ignition aggregates all Ignition flavors into a single package to be consumed
// by the bootstrap provider by exposing an API similar to 'internal/cloudinit' package.
// by the bootstrap provider by exposing an API similar to 'bootstrap/internal/ignition' package.
package ignition
import (
@ -49,15 +49,8 @@ type JoinWorkerInput struct {
AdditionalIgnition *bootstrapv1.AdditionalUserData
}
// ControlPlaneJoinInput defines context to generate controlplane instance user data for control plane node join.
type ControlPlaneJoinInput struct {
*cloudinit.ControlPlaneInput
AdditionalIgnition *bootstrapv1.AdditionalUserData
}
// ControlPlaneInitInput defines the context to generate a controlplane instance user data.
type ControlPlaneInitInput struct {
// ControlPlaneInput defines the context to generate a controlplane instance user data.
type ControlPlaneInput struct {
*cloudinit.ControlPlaneInput
AdditionalIgnition *bootstrapv1.AdditionalUserData
@ -70,7 +63,7 @@ func NewJoinWorker(input *JoinWorkerInput) ([]byte, error) {
}
if input.BaseUserData == nil {
return nil, fmt.Errorf("node input can't be nil")
return nil, fmt.Errorf("base userdata can't be nil")
}
deployRKE2Command, err := getWorkerRKE2Commands(input.BaseUserData)
@ -85,29 +78,26 @@ func NewJoinWorker(input *JoinWorkerInput) ([]byte, error) {
}
// NewJoinControlPlane returns Ignition configuration for new controlplane node joining the cluster.
func NewJoinControlPlane(input *ControlPlaneJoinInput) ([]byte, error) {
if input == nil {
return nil, fmt.Errorf("input can't be nil")
}
if input.ControlPlaneInput == nil {
return nil, fmt.Errorf("controlplane join input can't be nil")
}
deployRKE2Command, err := getControlPlaneRKE2Commands(&input.BaseUserData)
func NewJoinControlPlane(input *ControlPlaneInput) ([]byte, error) {
processedInput, err := controlPlaneConfigInput(input)
if err != nil {
return nil, fmt.Errorf("failed to get rke2 command: %w", err)
return nil, fmt.Errorf("failed to process controlplane input: %w", err)
}
input.DeployRKE2Commands = deployRKE2Command
input.WriteFiles = append(input.WriteFiles, input.Certificates.AsFiles()...)
input.WriteFiles = append(input.WriteFiles, input.ConfigFile)
return render(&input.BaseUserData, input.AdditionalIgnition)
return render(&processedInput.BaseUserData, processedInput.AdditionalIgnition)
}
// NewInitControlPlane returns Ignition configuration for bootstrapping new cluster.
func NewInitControlPlane(input *ControlPlaneInitInput) ([]byte, error) {
func NewInitControlPlane(input *ControlPlaneInput) ([]byte, error) {
processedInput, err := controlPlaneConfigInput(input)
if err != nil {
return nil, fmt.Errorf("failed to process controlplane input: %w", err)
}
return render(&processedInput.BaseUserData, processedInput.AdditionalIgnition)
}
func controlPlaneConfigInput(input *ControlPlaneInput) (*ControlPlaneInput, error) {
if input == nil {
return nil, fmt.Errorf("input can't be nil")
}
@ -125,7 +115,7 @@ func NewInitControlPlane(input *ControlPlaneInitInput) ([]byte, error) {
input.WriteFiles = append(input.WriteFiles, input.Certificates.AsFiles()...)
input.WriteFiles = append(input.WriteFiles, input.ConfigFile)
return render(&input.BaseUserData, input.AdditionalIgnition)
return input, nil
}
func render(input *cloudinit.BaseUserData, ignitionConfig *bootstrapv1.AdditionalUserData) ([]byte, error) {
@ -138,28 +128,14 @@ func render(input *cloudinit.BaseUserData, ignitionConfig *bootstrapv1.Additiona
}
func getControlPlaneRKE2Commands(baseUserData *cloudinit.BaseUserData) ([]string, error) {
if baseUserData == nil {
return nil, fmt.Errorf("base user data can't be nil")
}
if baseUserData.RKE2Version == "" {
return nil, fmt.Errorf("rke2 version can't be empty")
}
rke2Commands := []string{}
if baseUserData.AirGapped {
rke2Commands = append(rke2Commands, airGappedControlPlaneCommand)
} else {
rke2Commands = append(rke2Commands, fmt.Sprintf(controlPlaneCommand, baseUserData.RKE2Version))
}
rke2Commands = append(rke2Commands, serverSystemdServices...)
return rke2Commands, nil
return getRKE2Commands(baseUserData, controlPlaneCommand, airGappedControlPlaneCommand, serverSystemdServices)
}
func getWorkerRKE2Commands(baseUserData *cloudinit.BaseUserData) ([]string, error) {
return getRKE2Commands(baseUserData, workerCommand, airGappedWorkerCommand, workerSystemdServices)
}
func getRKE2Commands(baseUserData *cloudinit.BaseUserData, command, airgappedCommand string, systemdServices []string) ([]string, error) {
if baseUserData == nil {
return nil, fmt.Errorf("base user data can't be nil")
}
@ -171,12 +147,12 @@ func getWorkerRKE2Commands(baseUserData *cloudinit.BaseUserData) ([]string, erro
rke2Commands := []string{}
if baseUserData.AirGapped {
rke2Commands = append(rke2Commands, airGappedWorkerCommand)
rke2Commands = append(rke2Commands, airgappedCommand)
} else {
rke2Commands = append(rke2Commands, fmt.Sprintf(workerCommand, baseUserData.RKE2Version))
rke2Commands = append(rke2Commands, fmt.Sprintf(command, baseUserData.RKE2Version))
}
rke2Commands = append(rke2Commands, workerSystemdServices...)
rke2Commands = append(rke2Commands, systemdServices...)
return rke2Commands, nil
}

View File

@ -95,10 +95,10 @@ var _ = Describe("NewJoinWorker", func() {
})
var _ = Describe("NewJoinControlPlane", func() {
var input *ControlPlaneJoinInput
var input *ControlPlaneInput
BeforeEach(func() {
input = &ControlPlaneJoinInput{
input = &ControlPlaneInput{
ControlPlaneInput: &cloudinit.ControlPlaneInput{
BaseUserData: cloudinit.BaseUserData{
RKE2Version: "v1.21.3+rke2r1",
@ -123,7 +123,7 @@ var _ = Describe("NewJoinControlPlane", func() {
}
})
It("should return ignition data for worker", func() {
It("should return ignition data for control plane", func() {
ignition, err := NewJoinControlPlane(input)
Expect(err).ToNot(HaveOccurred())
Expect(ignition).ToNot(BeNil())
@ -145,10 +145,10 @@ var _ = Describe("NewJoinControlPlane", func() {
})
var _ = Describe("NewInitControlPlane", func() {
var input *ControlPlaneInitInput
var input *ControlPlaneInput
BeforeEach(func() {
input = &ControlPlaneInitInput{
input = &ControlPlaneInput{
ControlPlaneInput: &cloudinit.ControlPlaneInput{
BaseUserData: cloudinit.BaseUserData{
RKE2Version: "v1.21.3+rke2r1",
@ -173,7 +173,7 @@ var _ = Describe("NewInitControlPlane", func() {
}
})
It("should return ignition data for worker", func() {
It("should return ignition data for control plane", func() {
ignition, err := NewInitControlPlane(input)
Expect(err).ToNot(HaveOccurred())
Expect(ignition).ToNot(BeNil())
@ -203,14 +203,14 @@ var _ = Describe("getControlPlaneRKE2Commands", func() {
}
})
It("should return slice of commands", func() {
It("should return slice of control plane commands", func() {
commands, err := getControlPlaneRKE2Commands(baseUserData)
Expect(err).ToNot(HaveOccurred())
Expect(commands).To(HaveLen(3))
Expect(commands).To(ContainElements(fmt.Sprintf(controlPlaneCommand, baseUserData.RKE2Version), serverSystemdServices[0], serverSystemdServices[1]))
})
It("should return slice of commands with air gapped", func() {
It("should return slice of control plane commands with air gapped", func() {
baseUserData.AirGapped = true
commands, err := getControlPlaneRKE2Commands(baseUserData)
Expect(err).ToNot(HaveOccurred())
@ -243,14 +243,14 @@ var _ = Describe("getWorkerRKE2Commands", func() {
}
})
It("should return slice of commands", func() {
It("should return slice of worker commands", func() {
commands, err := getWorkerRKE2Commands(baseUserData)
Expect(err).ToNot(HaveOccurred())
Expect(commands).To(HaveLen(3))
Expect(commands).To(ContainElements(fmt.Sprintf(workerCommand, baseUserData.RKE2Version), workerSystemdServices[0], workerSystemdServices[1]))
})
It("should return slice of commands with air gapped", func() {
It("should return slice of worker commands with air gapped", func() {
baseUserData.AirGapped = true
commands, err := getWorkerRKE2Commands(baseUserData)
Expect(err).ToNot(HaveOccurred())