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

Commit 516dbee

Browse files
committed
Removes unnecessary code
Signed-off-by: Chuck Ha <[email protected]>
1 parent 17dd198 commit 516dbee

11 files changed

+107
-112
lines changed

api/v1alpha2/kubeadmbootstrapconfig_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
type KubeadmConfigSpec struct {
2727
// ClusterConfiguration along with InitConfiguration are the configurations necessary for the init command
2828
// +optional
29-
ClusterConfiguration *kubeadmv1beta1.ClusterConfiguration `json:"clusterConfiguration"`
29+
ClusterConfiguration *kubeadmv1beta1.ClusterConfiguration `json:"clusterConfiguration,omitempty"`
3030
// InitConfiguration along with ClusterConfiguration are the configurations necessary for the init command
3131
// +optional
3232
InitConfiguration *kubeadmv1beta1.InitConfiguration `json:"initConfiguration,omitempty"`

api/v1alpha2/zz_generated.deepcopy.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package v1alpha2
2222

2323
import (
2424
runtime "k8s.io/apimachinery/pkg/runtime"
25+
"sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/kubeadm/v1beta1"
2526
)
2627

2728
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
@@ -86,9 +87,21 @@ func (in *KubeadmConfigList) DeepCopyObject() runtime.Object {
8687
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
8788
func (in *KubeadmConfigSpec) DeepCopyInto(out *KubeadmConfigSpec) {
8889
*out = *in
89-
in.ClusterConfiguration.DeepCopyInto(&out.ClusterConfiguration)
90-
in.InitConfiguration.DeepCopyInto(&out.InitConfiguration)
91-
in.JoinConfiguration.DeepCopyInto(&out.JoinConfiguration)
90+
if in.ClusterConfiguration != nil {
91+
in, out := &in.ClusterConfiguration, &out.ClusterConfiguration
92+
*out = new(v1beta1.ClusterConfiguration)
93+
(*in).DeepCopyInto(*out)
94+
}
95+
if in.InitConfiguration != nil {
96+
in, out := &in.InitConfiguration, &out.InitConfiguration
97+
*out = new(v1beta1.InitConfiguration)
98+
(*in).DeepCopyInto(*out)
99+
}
100+
if in.JoinConfiguration != nil {
101+
in, out := &in.JoinConfiguration, &out.JoinConfiguration
102+
*out = new(v1beta1.JoinConfiguration)
103+
(*in).DeepCopyInto(*out)
104+
}
92105
}
93106

94107
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KubeadmConfigSpec.

cloudinit/cloudinit.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,25 @@ type BaseUserData struct {
3737
WriteFiles []Files
3838
}
3939

40-
func generate(kind string, tpl string, data interface{}) (string, error) {
40+
func generate(kind string, tpl string, data interface{}) ([]byte, error) {
4141
tm := template.New(kind).Funcs(defaultTemplateFuncMap)
4242
if _, err := tm.Parse(filesTemplate); err != nil {
43-
return "", errors.Wrap(err, "failed to parse files template")
43+
return nil, errors.Wrap(err, "failed to parse files template")
4444
}
4545

4646
if _, err := tm.Parse(commandsTemplate); err != nil {
47-
return "", errors.Wrap(err, "failed to parse commands template")
47+
return nil, errors.Wrap(err, "failed to parse commands template")
4848
}
4949

5050
t, err := tm.Parse(tpl)
5151
if err != nil {
52-
return "", errors.Wrapf(err, "failed to parse %s template", kind)
52+
return nil, errors.Wrapf(err, "failed to parse %s template", kind)
5353
}
5454

5555
var out bytes.Buffer
5656
if err := t.Execute(&out, data); err != nil {
57-
return "", errors.Wrapf(err, "failed to generate %s template", kind)
57+
return nil, errors.Wrapf(err, "failed to generate %s template", kind)
5858
}
5959

60-
return out.String(), nil
60+
return out.Bytes(), nil
6161
}

cloudinit/controlplane_init.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,22 @@ type ControlPlaneInput struct {
3838
BaseUserData
3939
Certificates
4040

41-
ClusterConfiguration string
42-
InitConfiguration string
41+
ClusterConfiguration []byte
42+
InitConfiguration []byte
4343
}
4444

4545
// NewInitControlPlane returns the user data string to be used on a controlplane instance.
46-
func NewInitControlPlane(input *ControlPlaneInput) (string, error) {
46+
func NewInitControlPlane(input *ControlPlaneInput) ([]byte, error) {
4747
input.Header = cloudConfigHeader
4848
if err := input.Certificates.validate(); err != nil {
49-
return "", err
49+
return nil, err
5050
}
5151

5252
input.WriteFiles = certificatesToFiles(input.Certificates)
5353
input.WriteFiles = append(input.WriteFiles, input.AdditionalFiles...)
5454
userData, err := generate("InitControlplane", controlPlaneCloudInit, input)
5555
if err != nil {
56-
return "", err
56+
return nil, err
5757
}
5858

5959
return userData, nil

cloudinit/controlplane_join.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,28 @@ runcmd:
3434
`
3535
)
3636

37-
// ControlPlaneJoinInput defines context to generate controlplane instance user data for controlplane node join.
37+
// ControlPlaneJoinInput defines context to generate controlplane instance user data for control plane node join.
3838
type ControlPlaneJoinInput struct {
3939
BaseUserData
4040
Certificates
4141

4242
BootstrapToken string
4343
ControlPlaneAddress string
44-
JoinConfiguration string
44+
JoinConfiguration []byte
4545
}
4646

47-
// NewJoinControlPlane returns the user data string to be used on a new contrplplane instance.
48-
func NewJoinControlPlane(input *ControlPlaneJoinInput) (string, error) {
47+
// NewJoinControlPlane returns the user data string to be used on a new control plane instance.
48+
func NewJoinControlPlane(input *ControlPlaneJoinInput) ([]byte, error) {
4949
input.Header = cloudConfigHeader
5050
if err := input.Certificates.validate(); err != nil {
51-
return "", errors.Wrapf(err, "ControlPlaneInput is invalid")
51+
return nil, errors.Wrapf(err, "ControlPlaneInput is invalid")
5252
}
5353

5454
input.WriteFiles = certificatesToFiles(input.Certificates)
5555
input.WriteFiles = append(input.WriteFiles, input.AdditionalFiles...)
5656
userData, err := generate("JoinControlplane", controlPlaneJoinCloudInit, input)
5757
if err != nil {
58-
return "", errors.Wrapf(err, "failed to generate user data for machine joining control plane")
58+
return nil, errors.Wrapf(err, "failed to generate user data for machine joining control plane")
5959
}
6060

6161
return userData, err

cloudinit/node.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ runcmd:
3535
type NodeInput struct {
3636
BaseUserData
3737

38-
JoinConfiguration string
38+
JoinConfiguration []byte
3939
}
4040

4141
// NewNode returns the user data string to be used on a node instance.
42-
func NewNode(input *NodeInput) (string, error) {
42+
func NewNode(input *NodeInput) ([]byte, error) {
4343
input.Header = cloudConfigHeader
4444
input.WriteFiles = append(input.WriteFiles, input.AdditionalFiles...)
4545
return generate("Node", nodeCloudInit, input)

config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigs.yaml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ spec:
2828
metadata:
2929
type: object
3030
spec:
31-
description: KubeadmConfigSpec defines the desired state of KubeadmConfig
31+
description: KubeadmConfigSpec defines the desired state of KubeadmConfig.
32+
Either ClusterConfiguration and InitConfiguration should be defined or
33+
the JoinConfiguration should be defined.
3234
properties:
3335
clusterConfiguration:
34-
description: ClusterConfiguration contains cluster-wide configuration
35-
for a kubeadm cluster
36+
description: ClusterConfiguration along with InitConfiguration are the
37+
configurations necessary for the init command
3638
properties:
3739
apiServer:
3840
description: APIServer contains extra settings for the API server
@@ -349,8 +351,8 @@ spec:
349351
- networking
350352
type: object
351353
initConfiguration:
352-
description: InitConfiguration contains a list of elements that is specific
353-
"kubeadm init"-only runtime information.
354+
description: InitConfiguration along with ClusterConfiguration are the
355+
configurations necessary for the init command
354356
properties:
355357
apiVersion:
356358
description: 'APIVersion defines the versioned schema of this representation
@@ -499,8 +501,8 @@ spec:
499501
type: object
500502
type: object
501503
joinConfiguration:
502-
description: JoinConfiguration contains elements describing a particular
503-
node.
504+
description: JoinConfiguration is the kubeadm configuration for the
505+
join command
504506
properties:
505507
apiVersion:
506508
description: 'APIVersion defines the versioned schema of this representation
@@ -677,8 +679,6 @@ spec:
677679
- discovery
678680
- nodeRegistration
679681
type: object
680-
required:
681-
- clusterConfiguration
682682
type: object
683683
status:
684684
description: KubeadmConfigStatus defines the observed state of KubeadmConfig

controllers/bootstrapdata.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

controllers/kubeadmconfig_controller.go

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controllers
1919
import (
2020
"context"
2121
"encoding/json"
22+
"fmt"
2223

2324
"github.com/go-logr/logr"
2425
"github.com/pkg/errors"
@@ -36,15 +37,10 @@ var (
3637
machineKind = v1alpha2.SchemeGroupVersion.WithKind("Machine")
3738
)
3839

39-
type bootstrapDataGenerator interface {
40-
GenerateControlPlaneInit(config *kubeadmv1alpha2.KubeadmConfig) error
41-
}
42-
4340
// KubeadmConfigReconciler reconciles a KubeadmConfig object
4441
type KubeadmConfigReconciler struct {
4542
client.Client
4643
Log logr.Logger
47-
BootstrapDataGenerator bootstrapDataGenerator
4844
}
4945

5046
// +kubebuilder:rbac:groups=bootstrap.cluster.x-k8s.io,resources=kubeadmconfigs,verbs=get;list;watch;create;update;patch;delete
@@ -116,13 +112,6 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
116112
return ctrl.Result{}, err
117113
}
118114

119-
// Generate the cloud-init for controlplane init
120-
if r.BootstrapDataGenerator == nil {
121-
r.BootstrapDataGenerator = &BootstrapDataGenerator{
122-
Logger: r.Log.WithName("user-data-generator"),
123-
}
124-
}
125-
126115
// if the machine has cluster and or init defined on it then generate the init regular join
127116
if config.Spec.InitConfiguration != nil && config.Spec.ClusterConfiguration != nil {
128117
// get both of these to strings to pass to the cloud init control plane generator
@@ -137,15 +126,63 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
137126
return ctrl.Result{}, err
138127
}
139128

140-
cloudinit.NewInitControlPlane(&cloudinit.ControlPlaneInput{
141-
InitConfiguration: string(initdata),
142-
ClusterConfiguration: string(clusterdata),
129+
cloudInitData, err :=cloudinit.NewInitControlPlane(&cloudinit.ControlPlaneInput{
130+
InitConfiguration: initdata,
131+
ClusterConfiguration: clusterdata,
143132
})
133+
if err != nil {
134+
log.Error(err, "failed to generate cloud init for bootstrap control plane")
135+
return ctrl.Result{}, err
136+
}
137+
138+
config.Status.BootstrapData = cloudInitData
139+
config.Status.Ready = true
140+
141+
if err := r.Update(ctx, &config); err != nil {
142+
log.Error(err, "failed to update config")
143+
return ctrl.Result{}, err
144+
}
145+
return ctrl.Result{}, nil
144146
}
145147

148+
// Every other case it's a join scenario
149+
joinBytes, err := json.Marshal(config.Spec.JoinConfiguration)
150+
if err != nil {
151+
log.Error(err, "failed to marshal join configuration")
152+
return ctrl.Result{}, err
153+
}
154+
155+
// it's a control plane join
146156
if util.IsControlPlaneMachine(machine) {
157+
// TODO return a sensible error if join config is not specified (implies empty configuration)
158+
joinData, err := cloudinit.NewJoinControlPlane(&cloudinit.ControlPlaneJoinInput{
159+
// TODO do a len check or something here
160+
ControlPlaneAddress: fmt.Sprintf("https://%s:%d", cluster.Status.APIEndpoints[0].Host, cluster.Status.APIEndpoints[0].Port),
161+
JoinConfiguration: joinBytes,
162+
})
163+
if err != nil {
164+
log.Error(err, "failed to create a control plane join configuration")
165+
return ctrl.Result{}, err
166+
}
147167

168+
config.Status.BootstrapData = joinData
169+
config.Status.Ready = true
170+
if err := r.Update(ctx, &config); err != nil {
171+
log.Error(err, "failed to update config")
172+
return ctrl.Result{}, err
173+
}
174+
return ctrl.Result{}, nil
175+
}
176+
177+
joinData, err := cloudinit.NewNode(&cloudinit.NodeInput{
178+
JoinConfiguration: joinBytes,
179+
})
180+
if err != nil {
181+
log.Error(err, "failed to create a worker join configuration")
182+
return ctrl.Result{}, err
148183
}
184+
config.Status.BootstrapData = joinData
185+
config.Status.Ready = true
149186

150187
if err := r.Update(ctx, &config); err != nil {
151188
log.Error(err, "failed to update config")

controllers/kubeadmconfig_controller_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,16 @@ func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
9393
},
9494
},
9595
},
96-
"ns/my-cluster": &v1alpha2.Cluster{},
96+
"ns/my-cluster": &v1alpha2.Cluster{
97+
Status:v1alpha2.ClusterStatus{
98+
APIEndpoints: []v1alpha2.APIEndpoint{
99+
{
100+
Host: "example.com",
101+
Port: 6443,
102+
},
103+
},
104+
},
105+
},
97106
}
98107
myclient := &myClient{
99108
db: objects,

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ k8s.io/utils v0.0.0-20190506122338-8fab8cb257d5 h1:VBM/0P5TWxwk+Nw6Z+lAw3DKgO76g
256256
k8s.io/utils v0.0.0-20190506122338-8fab8cb257d5/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew=
257257
sigs.k8s.io/cluster-api v0.0.0-20190725170330-835ee872f98d h1:DjeaMyqyw5tpps/prbudaojzp8gRMPV6L6MU4C8M8Eg=
258258
sigs.k8s.io/cluster-api v0.0.0-20190725170330-835ee872f98d/go.mod h1:DIiQEP2FjsAiEgBTgT5EEUAVM7XRy7aLgeERFXvZHaQ=
259-
sigs.k8s.io/controller-runtime v0.1.12 h1:ovDq28E64PeY1yR+6H7DthakIC09soiDCrKvfP2tPYo=
260259
sigs.k8s.io/controller-runtime v0.2.0-beta.4 h1:S1XVfRWR1MuIXZdkYx3jN8JDw+bbQxmWZroy0i87z/A=
261260
sigs.k8s.io/controller-runtime v0.2.0-beta.4/go.mod h1:HweyYKQ8fBuzdu2bdaeBJvsFgAi/OqBBnrVGXcqKhME=
262261
sigs.k8s.io/controller-tools v0.2.0-beta.4 h1:W+coTe+nkVNclQrikwlRp6GJKwgcrHzvIQZ9kCaak5A=

0 commit comments

Comments
 (0)