Skip to content

Commit

Permalink
bugfix s3 bucket name
Browse files Browse the repository at this point in the history
  • Loading branch information
kkb0318 committed Apr 24, 2024
1 parent 3473c3d commit a314698
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
12 changes: 2 additions & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package main

import (
"context"
"crypto/tls"
"flag"
"os"
Expand All @@ -26,7 +25,6 @@ import (
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"

awsclient "github.com/kkb0318/irsa-manager/internal/client"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -124,15 +122,9 @@ func main() {
os.Exit(1)
}

awsClient, err := awsclient.NewAwsClientFactory(context.TODO())
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
if err = (&controller.IRSASetupReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
AwsClient: awsClient,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "IRSASetup")
os.Exit(1)
Expand Down
1 change: 1 addition & 0 deletions internal/client/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (a *AwsS3Client) PutObjectPublic(ctx context.Context, input ObjectInput) er
// CreateBucketPublic creates a new S3 bucket with public access settings in the specified region.
// The function configures the bucket to have its ownership controlled by the bucket creator.
func (a *AwsS3Client) CreateBucketPublic(ctx context.Context) error {
log.Printf("creating S3 bucket... Name: %s, Region: %s \n", a.bucketName, a.Region())
bucket := aws.String(a.bucketName)
_, err := a.Client.CreateBucket(ctx, &s3.CreateBucketInput{
Bucket: bucket,
Expand Down
17 changes: 13 additions & 4 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package client
import (
"context"
"fmt"
"os"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/sts"
Expand Down Expand Up @@ -35,6 +38,12 @@ func NewAwsClientFactory(ctx context.Context) (*AwsClientFactory, error) {
if err != nil {
return nil, fmt.Errorf("unable to load SDK config, %w", err)
}
roleArn := os.Getenv("AWS_ROLE_ARN")
if roleArn != "" {
stsSvc := sts.NewFromConfig(cfg)
creds := stscreds.NewAssumeRoleProvider(stsSvc, roleArn)
cfg.Credentials = aws.NewCredentialsCache(creds)
}
return &AwsClientFactory{config: cfg}, nil
}

Expand All @@ -50,10 +59,10 @@ func (a *AwsClientFactory) StsClient() *AwsStsClient {
}
}

func (a *AwsClientFactory) S3Client(bucketName, region string) *AwsS3Client {
func (a *AwsClientFactory) S3Client(region, bucketName string) *AwsS3Client {
return &AwsS3Client{
s3.NewFromConfig(a.config),
region,
bucketName,
Client: s3.NewFromConfig(a.config),
region: region,
bucketName: bucketName,
}
}
9 changes: 9 additions & 0 deletions internal/controller/irsasetup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func (r *IRSASetupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
if err := r.Get(ctx, req.NamespacedName, obj); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
awsClient, err := awsclient.NewAwsClientFactory(ctx)
if err != nil {
return ctrl.Result{}, err
}
r.AwsClient = awsClient
kubeClient, err := kubernetes.NewKubernetesClient(r.Client, kubernetes.Owner{Field: "irsa-manager"})
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -128,10 +133,13 @@ func (r *IRSASetupReconciler) reconcileDelete(ctx context.Context, obj *irsav1al
// - If the self-hosted setup was previously attempted but failed, or if it's being run for the first time, it will attempt to create all necessary resources. This includes the creation of key pairs, JWKs, OIDC IDP configurations, and Kubernetes secrets.
// - The function enforces a 'force update' strategy in case of failures related to kubernetes Secrets creation or OIDC setup. This means it starts from scratch to ensure all components are correctly configured.
func reconcileSelfhosted(ctx context.Context, obj *irsav1alpha1.IRSASetup, awsClient awsclient.AwsClient, kubeClient *kubernetes.KubernetesClient) error {
log := ctrllog.FromContext(ctx)
if irsav1alpha1.IsSelfHostedReadyConditionTrue(*obj) {
// Selfhosted Setup have already succeeded
log.Info("the self-hosted resources have already set up")
return nil
}
log.Info("the self-hosted resources are setting up")
keyPair, err := selfhosted.CreateKeyPair()
if err != nil {
return err
Expand Down Expand Up @@ -181,6 +189,7 @@ func reconcileSelfhosted(ctx context.Context, obj *irsav1alpha1.IRSASetup, awsCl
return err
}
*obj = irsav1alpha1.SetupSelfHostedStatusReady(*obj, string(irsav1alpha1.SelfHostedReasonReady), "successfully setup resources for self-hosted")
log.Info("the self-hosted resources have successfully set up")
return nil
}

Expand Down
16 changes: 9 additions & 7 deletions internal/selfhosted/oidc/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ type AwsS3IdPFactory struct {
jwksFileName string
}

func NewAwsS3IdpFactory(ctx context.Context,
region, bucketName string,
func NewAwsS3IdpFactory(
ctx context.Context,
region,
bucketName string,
jwk *selfhosted.JWK,
jwksFileName string,
awsClient awsclient.AwsClient,
) (*AwsS3IdPFactory, error) {
return &AwsS3IdPFactory{
region,
bucketName,
awsClient,
jwk,
jwksFileName,
region: region,
bucketName: bucketName,
awsClient: awsClient,
jwk: jwk,
jwksFileName: jwksFileName,
}, nil
}

Expand Down

0 comments on commit a314698

Please sign in to comment.