Skip to content

Commit

Permalink
rosa: load identity dynamically
Browse files Browse the repository at this point in the history
Instead of requiring users to provide their account IDs and ARNs in the
spec of their hosted control planes, we can refer to AWS credentials
like the other managed control plane controllers do in this provider.

Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
  • Loading branch information
stevekuznetsov committed Feb 9, 2024
1 parent 1fd6d67 commit 71d5769
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 38 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ DOCKER_BUILDKIT=1
export ACK_GINKGO_DEPRECATIONS := 1.16.4

# Set --output-base for conversion-gen if we are not within GOPATH
ifneq ($(abspath $(REPO_ROOT)),$(shell go env GOPATH)/src/sigs.k8s.io/cluster-api-provider-aws)
ifneq ($(abspath $(REPO_ROOT)),$(abspath $(shell go env GOPATH)/src/sigs.k8s.io/cluster-api-provider-aws))
GEN_OUTPUT_BASE := --output-base=$(REPO_ROOT)
else
export GOPATH := $(shell go env GOPATH)
Expand Down Expand Up @@ -190,6 +190,7 @@ defaulters: $(DEFAULTER_GEN) ## Generate all Go types
$(DEFAULTER_GEN) \
--input-dirs=./api/v1beta2 \
--input-dirs=./$(EXP_DIR)/api/v1beta2 \
--input-dirs=./controlplane/rosa/api/v1beta2 \
--input-dirs=./cmd/clusterawsadm/api/bootstrap/v1beta1 \
--input-dirs=./cmd/clusterawsadm/api/bootstrap/v1alpha1 \
--extra-peer-dirs=sigs.k8s.io/cluster-api/api/v1beta1 \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ spec:
type: object
spec:
properties:
accountID:
description: 'TODO: these are to satisfy ocm sdk. Explore how to drop
them.'
type: string
availabilityZones:
description: AWS AvailabilityZones of the worker nodes should match
the AvailabilityZones of the Subnets.
Expand All @@ -70,8 +66,6 @@ spec:
- host
- port
type: object
creatorARN:
type: string
credentialsSecretRef:
description: 'CredentialsSecretRef references a secret with necessary
credentials to connect to the OCM API. The secret should contain
Expand All @@ -84,7 +78,28 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
identityRef:
description: IdentityRef is a reference to an identity to be used
when reconciling the managed control plane.
properties:
kind:
description: Kind of the identity.
enum:
- AWSClusterControllerIdentity
- AWSClusterRoleIdentity
- AWSClusterStaticIdentity
type: string
name:
description: Name of the identity.
minLength: 1
type: string
required:
- kind
- name
type: object
installerRoleARN:
description: 'TODO: these are to satisfy ocm sdk. Explore how to drop
them.'
type: string
machineCIDR:
description: Block of IP addresses used by OpenShift while installing
Expand Down Expand Up @@ -276,9 +291,7 @@ spec:
workerRoleARN:
type: string
required:
- accountID
- availabilityZones
- creatorARN
- installerRoleARN
- machineCIDR
- oidcID
Expand Down
2 changes: 2 additions & 0 deletions controllers/rosacluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
rosacontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/rosa/api/v1beta2"
expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
Expand All @@ -48,6 +49,7 @@ type ROSAClusterReconciler struct {
client.Client
Recorder record.EventRecorder
WatchFilterValue string
Endpoints []scope.ServiceEndpoint
}

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=rosaclusters,verbs=get;list;watch;update;patch;delete
Expand Down
13 changes: 13 additions & 0 deletions controlplane/rosa/api/v1beta2/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v1beta2

import "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"

// SetDefaults_RosaControlPlaneSpec is used by defaulter-gen.
func SetDefaults_RosaControlPlaneSpec(s *RosaControlPlaneSpec) { //nolint:golint,stylecheck
if s.IdentityRef == nil {
s.IdentityRef = &v1beta2.AWSIdentityReference{
Kind: v1beta2.ControllerIdentityKind,
Name: v1beta2.AWSClusterControllerIdentityName,
}
}
}
8 changes: 6 additions & 2 deletions controlplane/rosa/api/v1beta2/rosacontrolplane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

Expand Down Expand Up @@ -64,8 +65,6 @@ type RosaControlPlaneSpec struct { //nolint: maligned
OIDCID *string `json:"oidcID"`

// TODO: these are to satisfy ocm sdk. Explore how to drop them.
AccountID *string `json:"accountID"`
CreatorARN *string `json:"creatorARN"`
InstallerRoleARN *string `json:"installerRoleARN"`
SupportRoleARN *string `json:"supportRoleARN"`
WorkerRoleARN *string `json:"workerRoleARN"`
Expand All @@ -76,6 +75,10 @@ type RosaControlPlaneSpec struct { //nolint: maligned
// - ocmApiUrl: Optional, defaults to 'https://api.openshift.com'
// +optional
CredentialsSecretRef *corev1.LocalObjectReference `json:"credentialsSecretRef,omitempty"`

// IdentityRef is a reference to an identity to be used when reconciling the managed control plane.
// +optional
IdentityRef *infrav1.AWSIdentityReference `json:"identityRef,omitempty"`
}

// AWSRolesRef contains references to various AWS IAM roles required for operators to make calls against the AWS API.
Expand Down Expand Up @@ -489,6 +492,7 @@ type RosaControlPlaneStatus struct {
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Cluster",type="string",JSONPath=".metadata.labels.cluster\\.x-k8s\\.io/cluster-name",description="Cluster to which this RosaControl belongs"
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.ready",description="Control plane infrastructure is ready for worker nodes"
// +k8s:defaulter-gen=true

type ROSAControlPlane struct {
metav1.TypeMeta `json:",inline"`
Expand Down
16 changes: 6 additions & 10 deletions controlplane/rosa/api/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions controlplane/rosa/api/v1beta2/zz_generated.defaults.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions controlplane/rosa/controllers/rosacontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type ROSAControlPlaneReconciler struct {
client.Client
WatchFilterValue string
WaitInfraPeriod time.Duration
Endpoints []scope.ServiceEndpoint
}

// SetupWithManager is used to setup the controller.
Expand Down Expand Up @@ -148,6 +149,7 @@ func (r *ROSAControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.Req
Cluster: cluster,
ControlPlane: rosaControlPlane,
ControllerName: strings.ToLower(rosaControlPlaneKind),
Endpoints: r.Endpoints,
Logger: log,
})
if err != nil {
Expand Down Expand Up @@ -344,8 +346,8 @@ func (r *ROSAControlPlaneReconciler) reconcileNormal(ctx context.Context, rosaSc
stsBuilder.AutoMode(true)

awsBuilder := cmv1.NewAWS().
AccountID(*rosaScope.ControlPlane.Spec.AccountID).
BillingAccountID(*rosaScope.ControlPlane.Spec.AccountID).
AccountID(*rosaScope.Identity.Account).
BillingAccountID(*rosaScope.Identity.Account).
SubnetIDs(rosaScope.ControlPlane.Spec.Subnets...).
STS(stsBuilder)
clusterBuilder = clusterBuilder.AWS(awsBuilder)
Expand All @@ -355,7 +357,7 @@ func (r *ROSAControlPlaneReconciler) reconcileNormal(ctx context.Context, rosaSc
clusterBuilder = clusterBuilder.Nodes(clusterNodesBuilder)

clusterProperties := map[string]string{}
clusterProperties[rosaCreatorArnProperty] = *rosaScope.ControlPlane.Spec.CreatorARN
clusterProperties[rosaCreatorArnProperty] = *rosaScope.Identity.Arn

clusterBuilder = clusterBuilder.Properties(clusterProperties)
clusterSpec, err := clusterBuilder.Build()
Expand Down
1 change: 1 addition & 0 deletions exp/controllers/rosamachinepool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ROSAMachinePoolReconciler struct {
client.Client
Recorder record.EventRecorder
WatchFilterValue string
Endpoints []scope.ServiceEndpoint
}

// SetupWithManager is used to setup the controller.
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ func main() {
Client: mgr.GetClient(),
WatchFilterValue: watchFilterValue,
WaitInfraPeriod: waitInfraPeriod,
Endpoints: awsServiceEndpoints,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: awsClusterConcurrency, RecoverPanic: ptr.To[bool](true)}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ROSAControlPlane")
os.Exit(1)
Expand All @@ -237,6 +238,7 @@ func main() {
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor("rosacluster-controller"),
WatchFilterValue: watchFilterValue,
Endpoints: awsServiceEndpoints,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: awsClusterConcurrency, RecoverPanic: ptr.To[bool](true)}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ROSACluster")
os.Exit(1)
Expand All @@ -247,6 +249,7 @@ func main() {
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor("rosamachinepool-controller"),
WatchFilterValue: watchFilterValue,
Endpoints: awsServiceEndpoints,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: awsClusterConcurrency, RecoverPanic: ptr.To[bool](true)}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ROSAMachinePool")
os.Exit(1)
Expand Down
12 changes: 12 additions & 0 deletions pkg/cloud/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,15 @@ type ClusterScoper interface {
// Close closes the current scope persisting the cluster configuration and status.
Close() error
}

// SessionMetadata knows how to extract the information for managing AWS sessions for a resource.
type SessionMetadata interface {
// Namespace returns the cluster namespace.
Namespace() string
// InfraClusterName returns the AWS infrastructure cluster name.
InfraClusterName() string
// InfraCluster returns the AWS infrastructure cluster object.
InfraCluster() ClusterObject
// IdentityRef returns the AWS infrastructure cluster identityRef.
IdentityRef() *infrav1.AWSIdentityReference
}
Loading

0 comments on commit 71d5769

Please sign in to comment.