Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 24e888f

Browse files
committed
Some general thoughts
Signed-off-by: Chuck Ha <[email protected]>
1 parent 1442654 commit 24e888f

File tree

4 files changed

+85
-12
lines changed

4 files changed

+85
-12
lines changed

cloudinit/controlplane_certs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
package cloudinit
1818

19-
import "errors"
19+
import "github.com/pkg/errors"
2020

2121
// Certificates is a template struct to hold certificate data
2222
type Certificates struct {

cloudinit/controlplane_init.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ limitations under the License.
1616

1717
package cloudinit
1818

19-
import (
20-
"github.com/pkg/errors"
21-
)
22-
2319
const (
2420
controlPlaneCloudInit = `{{.Header}}
2521
{{template "files" .WriteFiles}}
@@ -50,15 +46,15 @@ type ControlPlaneInput struct {
5046
func NewInitControlPlane(input *ControlPlaneInput) (string, error) {
5147
input.Header = cloudConfigHeader
5248
if err := input.Certificates.validate(); err != nil {
53-
return "", errors.Wrapf(err, "ControlPlaneInput is invalid")
49+
return "", err
5450
}
5551

5652
input.WriteFiles = certificatesToFiles(input.Certificates)
5753
input.WriteFiles = append(input.WriteFiles, input.AdditionalFiles...)
5854
userData, err := generate("InitControlplane", controlPlaneCloudInit, input)
5955
if err != nil {
60-
return "", errors.Wrapf(err, "failed to generate user data for new control plane machine")
56+
return "", err
6157
}
6258

63-
return userData, err
59+
return userData, nil
6460
}

controllers/control_plane_init.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package controllers
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/go-logr/logr"
7+
"github.com/pkg/errors"
8+
"sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/api/v1alpha1"
9+
"sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/cloudinit"
10+
)
11+
12+
type UserDataGenerator struct {
13+
logr.Logger
14+
}
15+
16+
func (u *UserDataGenerator) GenerateControlPlaneInit(config *v1alpha1.KubeadmBootstrapConfig) error {
17+
u.Info("Generating user data for first control plane")
18+
19+
// TODO: sub in things in the cluster configuration like the cluster name and other data we can extract from the cluster object
20+
config.Spec.ClusterConfiguration.APIVersion = "v1beta1"
21+
config.Spec.ClusterConfiguration.Kind = "ClusterConfiguration"
22+
clusterdata, err := json.Marshal(config.Spec.ClusterConfiguration)
23+
if err != nil {
24+
u.Error(err, "failed to marshal cluster configuration")
25+
return errors.WithStack(err)
26+
}
27+
28+
config.Spec.ClusterConfiguration.Kind = "v1beta1"
29+
config.Spec.ClusterConfiguration.APIVersion = "InitConfiguration"
30+
initdata, err := json.Marshal(config.Spec.InitConfiguration)
31+
if err != nil {
32+
u.Error(err, "failed to marshal init configuration")
33+
return errors.WithStack(err)
34+
}
35+
36+
cicfg, err := cloudinit.NewInitControlPlane(&cloudinit.ControlPlaneInput{
37+
ClusterConfiguration: string(clusterdata),
38+
InitConfiguration: string(initdata),
39+
})
40+
if err != nil {
41+
u.Error(err, "could not create new init control plane")
42+
return err
43+
}
44+
45+
config.Status.BootstrapData = []byte(cicfg)
46+
config.Status.Ready = true
47+
return nil
48+
}

controllers/kubeadmbootstrapconfig_controller.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package controllers
1818

1919
import (
2020
"context"
21-
"fmt"
21+
"time"
2222

2323
"github.com/go-logr/logr"
2424
"github.com/pkg/errors"
@@ -33,10 +33,15 @@ var (
3333
machineKind = v1alpha2.SchemeGroupVersion.WithKind("Machine").String()
3434
)
3535

36+
type userDataGenerator interface {
37+
GenerateControlPlaneInit(config *kubeadmv1alpha1.KubeadmBootstrapConfig) error
38+
}
39+
3640
// KubeadmBootstrapConfigReconciler reconciles a KubeadmBootstrapConfig object
3741
type KubeadmBootstrapConfigReconciler struct {
3842
client.Client
39-
Log logr.Logger
43+
Log logr.Logger
44+
UserDataGenerator userDataGenerator
4045
}
4146

4247
// +kubebuilder:rbac:groups=kubeadm.bootstrap.cluster.sigs.k8s.io,resources=kubeadmbootstrapconfigs,verbs=get;list;watch;create;update;patch;delete
@@ -50,8 +55,8 @@ func (r *KubeadmBootstrapConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Res
5055

5156
config := kubeadmv1alpha1.KubeadmBootstrapConfig{}
5257
if err := r.Get(ctx, req.NamespacedName, &config); err != nil {
53-
log.Error(err, "failed to get config", "stacktrace", fmt.Sprintf("%+v", err))
54-
return ctrl.Result{}, err
58+
log.Error(err, "failed to get config")
59+
return ctrl.Result{}, errors.WithStack(err)
5560
}
5661

5762
// Find the owner reference
@@ -93,6 +98,30 @@ func (r *KubeadmBootstrapConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Res
9398
log.Error(err, "failed to get cluster")
9499
return ctrl.Result{}, errors.WithStack(err)
95100
}
101+
102+
// If the machine is not Ready, requeue until it's ready
103+
if machine.Status.BootstrapReady == nil || *machine.Status.BootstrapReady == false {
104+
log.Info("requeuing as machine is not yet ready", "machine-name", machine.Name)
105+
return ctrl.Result{RequeueAfter: 30 * time.Second}, nil
106+
}
107+
108+
// if the bootstrap data is not nil then it's already been generated and we don't have anything else to do
109+
if machine.Spec.Bootstrap.Data != nil {
110+
log.Info("Nothing to be done for this machine", "machine-name", machine.Name)
111+
return ctrl.Result{}, nil
112+
}
113+
114+
// Generate the cloud-init for controlplane init
115+
if r.UserDataGenerator == nil {
116+
r.UserDataGenerator = &UserDataGenerator{
117+
Logger: r.Log.WithName("user-data-generator"),
118+
}
119+
}
120+
if err := r.UserDataGenerator.GenerateControlPlaneInit(&config); err != nil {
121+
log.Error(err, "error generating control plane init")
122+
return ctrl.Result{}, err
123+
}
124+
// TODO save config object
96125
return ctrl.Result{}, nil
97126
}
98127

0 commit comments

Comments
 (0)