force update if the selfhosted process ends midway

This commit is contained in:
kkb0318
2024-04-17 20:58:48 +09:00
parent f7208963ae
commit 78e97f2858
7 changed files with 83 additions and 44 deletions
+30 -7
View File
@@ -85,17 +85,31 @@ func (a *AwsS3Client) CheckObjectExists(ctx context.Context, key string) (bool,
return true, nil
}
type ObjectInput struct {
Key string
Body []byte
}
func (a *AwsS3Client) CreateObjectsPublic(ctx context.Context, inputs []ObjectInput) error {
for _, input := range inputs {
if err := a.CreateObjectPublic(ctx, input); err != nil {
return err
}
}
return nil
}
// CreateObjectPublic creates a file to an S3 bucket and sets its access level to public read.
// This means the file can be read by anyone on the internet.
func (a *AwsS3Client) CreateObjectPublic(ctx context.Context, key string, body []byte) error {
exists, err := a.CheckObjectExists(ctx, key)
func (a *AwsS3Client) CreateObjectPublic(ctx context.Context, input ObjectInput) error {
exists, err := a.CheckObjectExists(ctx, input.Key)
if err != nil {
return err
}
if exists {
log.Printf("skipped to create bucket object %s \n", key)
log.Printf("skipped to create bucket object %s \n", input.Key)
} else {
err := a.PutObjectPublic(ctx, key, body)
err := a.PutObjectPublic(ctx, input)
if err != nil {
return err
}
@@ -103,14 +117,23 @@ func (a *AwsS3Client) CreateObjectPublic(ctx context.Context, key string, body [
return nil
}
func (a *AwsS3Client) PutObjectsPublic(ctx context.Context, inputs []ObjectInput) error {
for _, input := range inputs {
if err := a.PutObjectPublic(ctx, input); err != nil {
return err
}
}
return nil
}
// PutObjectPublic uploads a file to an S3 bucket and sets its access level to public read.
// This means the file can be read by anyone on the internet.
func (a *AwsS3Client) PutObjectPublic(ctx context.Context, key string, body []byte) error {
func (a *AwsS3Client) PutObjectPublic(ctx context.Context, input ObjectInput) error {
_, err := a.Client.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(a.bucketName),
Key: aws.String(key),
Key: aws.String(input.Key),
ACL: types.ObjectCannedACLPublicRead,
Body: bytes.NewReader(body),
Body: bytes.NewReader(input.Body),
ContentType: aws.String("application/json"),
})
return err
+13 -14
View File
@@ -18,7 +18,6 @@ package controller
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
@@ -154,32 +153,32 @@ func reconcileSelfhosted(ctx context.Context, obj *irsav1alpha1.IRSASetup, awsCl
var reason irsav1alpha1.SelfHostedReason
defer func() {
if e != nil {
*obj = irsav1alpha1.IRSASetupSelfHostedNotReady(*obj, string(reason), e.Error())
*obj = irsav1alpha1.SelfHostedStatusNotReady(*obj, string(reason), e.Error())
}
}()
var forceUpdate bool
condition := irsav1alpha1.IRSASetupSelfHostedReadyStatus(*obj)
switch irsav1alpha1.SelfHostedReason(condition.Reason) {
case irsav1alpha1.SelfHostedReasonFailedKeys, irsav1alpha1.SelfHostedReasonFailedOidc:
forceUpdate = true
default:
forceUpdate = false
}
fmt.Println(forceUpdate) // TODO: force Update logic
err = selfhosted.Execute(ctx, factory)
forceUpdate := irsav1alpha1.HasConditionReason(
irsav1alpha1.SelfHostedReadyStatus(*obj),
string(irsav1alpha1.SelfHostedReasonFailedKeys),
string(irsav1alpha1.SelfHostedReasonFailedOidc),
)
err = selfhosted.Execute(ctx, factory, forceUpdate)
if err != nil {
e = err
reason = irsav1alpha1.SelfHostedReasonFailedOidc
return err
}
err = kubeHandler.CreateAll(ctx)
if forceUpdate {
err = kubeHandler.ApplyAll(ctx)
} else {
err = kubeHandler.CreateAll(ctx)
}
if err != nil {
e = err
reason = irsav1alpha1.SelfHostedReasonFailedKeys
return err
}
*obj = irsav1alpha1.IRSASetupSelfHostedReady(*obj, "SelfHostedSetupReady", e.Error())
*obj = irsav1alpha1.SetupSelfHostedStatusReady(*obj, string(irsav1alpha1.SelfHostedReasonReady), "successfully setup resources for self-hosted")
return nil
}
+1 -1
View File
@@ -22,7 +22,7 @@ type OIDCIdPDiscoveryContents interface {
type OIDCIdPDiscovery interface {
CreateStorage(ctx context.Context) error
Upload(ctx context.Context, o OIDCIdPDiscoveryContents) error
Upload(ctx context.Context, o OIDCIdPDiscoveryContents, forceUpdate bool) error
Delete(ctx context.Context, o OIDCIdPDiscoveryContents) error
}
@@ -32,30 +32,32 @@ func (s *S3IdPDiscovery) CreateStorage(ctx context.Context) error {
// Upload uploads the OIDC provider's discovery configuration and JSON Web Key Set (JWKS) to the specified AWS S3 bucket.
// This method is responsible for uploading the necessary OIDC configuration files to S3, making them accessible for OIDC clients.
func (s *S3IdPDiscovery) Upload(ctx context.Context, o selfhosted.OIDCIdPDiscoveryContents) error {
func (s *S3IdPDiscovery) Upload(ctx context.Context, o selfhosted.OIDCIdPDiscoveryContents, forceUpdate bool) error {
discovery, err := o.Discovery()
if err != nil {
return nil
}
err = s.s3Client.CreateObjectPublic(ctx,
CONFIGURATION_PATH,
discovery,
)
if err != nil {
return fmt.Errorf("unable to upload discovery document, %w", err)
}
// Uplaod JWK
jwk, err := o.JWK()
if err != nil {
return nil
}
err = s.s3Client.CreateObjectPublic(ctx,
o.JWKsFileName(),
jwk,
)
inputs := []client.ObjectInput{
{
Key: CONFIGURATION_PATH,
Body: discovery,
},
{
Key: o.JWKsFileName(),
Body: jwk,
},
}
if forceUpdate {
err = s.s3Client.PutObjectsPublic(ctx, inputs)
} else {
err = s.s3Client.CreateObjectsPublic(ctx, inputs)
}
if err != nil {
return fmt.Errorf("unable to upload JWK, %w", err)
return fmt.Errorf("unable to upload object, %w", err)
}
return nil
}
+2 -2
View File
@@ -2,7 +2,7 @@ package selfhosted
import "context"
func Execute(ctx context.Context, factory OIDCIdPFactory) error {
func Execute(ctx context.Context, factory OIDCIdPFactory, forceUpdate bool) error {
issuerMeta := factory.IssuerMeta()
discovery := factory.IdPDiscovery()
discoveryContents := factory.IdPDiscoveryContents(issuerMeta)
@@ -14,7 +14,7 @@ func Execute(ctx context.Context, factory OIDCIdPFactory) error {
if err != nil {
return err
}
err = discovery.Upload(ctx, discoveryContents)
err = discovery.Upload(ctx, discoveryContents, forceUpdate)
if err != nil {
return err
}