Skip to content

Commit f7d3575

Browse files
committed
feat: allowing nodes creation when cp is externally managed
Signed-off-by: Dario Tranchitella <[email protected]>
1 parent ee2c5f4 commit f7d3575

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

config/rbac/role.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ rules:
119119
- get
120120
- list
121121
- watch
122+
- apiGroups:
123+
- controlplane.cluster.x-k8s.io
124+
resources:
125+
- '*'
126+
verbs:
127+
- get
128+
- list
129+
- watch
122130
- apiGroups:
123131
- controlplane.cluster.x-k8s.io
124132
resources:

controllers/awsmachine_controller.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/pkg/errors"
3333
corev1 "k8s.io/api/core/v1"
3434
apierrors "k8s.io/apimachinery/pkg/api/errors"
35+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3536
kerrors "k8s.io/apimachinery/pkg/util/errors"
3637
"k8s.io/client-go/tools/record"
3738
"k8s.io/klog/v2"
@@ -60,6 +61,7 @@ import (
6061
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services/userdata"
6162
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger"
6263
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
64+
"sigs.k8s.io/cluster-api/controllers/external"
6365
capierrors "sigs.k8s.io/cluster-api/errors"
6466
"sigs.k8s.io/cluster-api/util"
6567
"sigs.k8s.io/cluster-api/util/annotations"
@@ -200,10 +202,16 @@ func (r *AWSMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request)
200202

201203
infrav1.SetDefaults_AWSMachineSpec(&awsMachine.Spec)
202204

205+
cp, err := r.getControlPlane(ctx, log, cluster)
206+
if err != nil {
207+
return ctrl.Result{}, err
208+
}
209+
203210
// Create the machine scope
204211
machineScope, err := scope.NewMachineScope(scope.MachineScopeParams{
205212
Client: r.Client,
206213
Cluster: cluster,
214+
ControlPlane: cp,
207215
Machine: machine,
208216
InfraCluster: infraCluster,
209217
AWSMachine: awsMachine,
@@ -1197,3 +1205,22 @@ func (r *AWSMachineReconciler) ensureInstanceMetadataOptions(ec2svc services.EC2
11971205

11981206
return ec2svc.ModifyInstanceMetadataOptions(instance.ID, machine.Spec.InstanceMetadataOptions)
11991207
}
1208+
1209+
// +kubebuilder:rbac:groups=controlplane.cluster.x-k8s.io,resources=*,verbs=get;list;watch
1210+
1211+
func (r *AWSMachineReconciler) getControlPlane(ctx context.Context, log *logger.Logger, cluster *clusterv1.Cluster) (*unstructured.Unstructured, error) {
1212+
var ns string
1213+
1214+
if ns = cluster.Spec.ControlPlaneRef.Namespace; ns == "" {
1215+
ns = cluster.Namespace
1216+
}
1217+
1218+
controlPlane, err := external.Get(ctx, r.Client, cluster.Spec.ControlPlaneRef, ns)
1219+
if err != nil {
1220+
log.Error(err, "unable to get ControlPlane referenced in the given cluster", "cluster", fmt.Sprintf("%s/%s", cluster.Namespace, cluster.Name))
1221+
1222+
return nil, err
1223+
}
1224+
1225+
return controlPlane, nil
1226+
}

pkg/cloud/scope/machine.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/pkg/errors"
2525
corev1 "k8s.io/api/core/v1"
26+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2627
"k8s.io/apimachinery/pkg/types"
2728
"k8s.io/klog/v2"
2829
"k8s.io/utils/ptr"
@@ -43,6 +44,7 @@ import (
4344
type MachineScopeParams struct {
4445
Client client.Client
4546
Logger *logger.Logger
47+
ControlPlane *unstructured.Unstructured
4648
Cluster *clusterv1.Cluster
4749
Machine *clusterv1.Machine
4850
InfraCluster EC2Scope
@@ -67,6 +69,9 @@ func NewMachineScope(params MachineScopeParams) (*MachineScope, error) {
6769
if params.InfraCluster == nil {
6870
return nil, errors.New("aws cluster is required when creating a MachineScope")
6971
}
72+
if params.ControlPlane == nil {
73+
return nil, errors.New("cluster control plane is required when creating a MachineScope")
74+
}
7075

7176
if params.Logger == nil {
7277
log := klog.Background()
@@ -78,10 +83,10 @@ func NewMachineScope(params MachineScopeParams) (*MachineScope, error) {
7883
return nil, errors.Wrap(err, "failed to init patch helper")
7984
}
8085
return &MachineScope{
81-
Logger: *params.Logger,
82-
client: params.Client,
83-
patchHelper: helper,
84-
86+
Logger: *params.Logger,
87+
client: params.Client,
88+
patchHelper: helper,
89+
ControlPlane: params.ControlPlane,
8590
Cluster: params.Cluster,
8691
Machine: params.Machine,
8792
InfraCluster: params.InfraCluster,
@@ -97,6 +102,7 @@ type MachineScope struct {
97102

98103
Cluster *clusterv1.Cluster
99104
Machine *clusterv1.Machine
105+
ControlPlane *unstructured.Unstructured
100106
InfraCluster EC2Scope
101107
AWSMachine *infrav1.AWSMachine
102108
}
@@ -371,6 +377,10 @@ func (m *MachineScope) IsEKSManaged() bool {
371377
return m.InfraCluster.InfraCluster().GetObjectKind().GroupVersionKind().Kind == ekscontrolplanev1.AWSManagedControlPlaneKind
372378
}
373379

380+
func (m *MachineScope) IsControlPlaneExternallyManaged() bool {
381+
return util.IsExternalManagedControlPlane(m.ControlPlane)
382+
}
383+
374384
// IsExternallyManaged checks if the machine is externally managed.
375385
func (m *MachineScope) IsExternallyManaged() bool {
376386
return annotations.IsExternallyManaged(m.InfraCluster.InfraCluster())

pkg/cloud/services/ec2/instances.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (s *Service) CreateInstance(scope *scope.MachineScope, userData []byte, use
181181
}
182182
input.SubnetID = subnetID
183183

184-
if !scope.IsExternallyManaged() && !scope.IsEKSManaged() && s.scope.Network().APIServerELB.DNSName == "" {
184+
if !scope.IsControlPlaneExternallyManaged() && !scope.IsExternallyManaged() && !scope.IsEKSManaged() && s.scope.Network().APIServerELB.DNSName == "" {
185185
record.Eventf(s.scope.InfraCluster(), "FailedCreateInstance", "Failed to run controlplane, APIServer ELB not available")
186186

187187
return nil, awserrors.NewFailedDependency("failed to run controlplane, APIServer ELB not available")

0 commit comments

Comments
 (0)