Skip to content

Commit 6fb5fa1

Browse files
authored
Merge pull request #48 from posit-dev/ppm-storage-region
Set S3 Storage Region in Package Manager and Chronicle
2 parents f154bfd + 25a522d commit 6fb5fa1

File tree

7 files changed

+79
-23
lines changed

7 files changed

+79
-23
lines changed

.claude/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"allow": [
44
"Bash(just test)",
55
"Bash(just build)",
6-
"Bash(make *)"
6+
"Bash(make :*)"
77
]
88
},
99
"hooks": {

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ coverage.*
1717
*.coverprofile
1818
profile.cov
1919

20+
# Build artifacts
21+
bin/
22+
2023
# Dependency directories (remove the comment below to include it)
2124
# vendor/
2225

api/product/secret.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"os"
87

98
"github.com/aws/aws-sdk-go/aws"
10-
"github.com/aws/aws-sdk-go/aws/endpoints"
119
"github.com/aws/aws-sdk-go/aws/session"
1210
"github.com/aws/aws-sdk-go/service/secretsmanager"
1311
"github.com/pkg/errors"
@@ -121,7 +119,7 @@ func GetSecretProviderClassForAllSecrets(p KubernetesOwnerProvider, name, namesp
121119
SecretObjects: generateSecretObjects(p, kubernetesSecrets),
122120
Parameters: map[string]string{
123121
"objects": secretObjectYaml,
124-
"region": getAWSRegion(),
122+
"region": GetAWSRegion(),
125123
},
126124
},
127125
}, nil
@@ -137,26 +135,12 @@ const (
137135
SiteSecretNone SiteSecretType = ""
138136
)
139137

140-
// getAWSRegion returns the AWS region to use for secret operations.
141-
// It checks the AWS_REGION environment variable first, then falls back to AWS_DEFAULT_REGION,
142-
// and finally defaults to us-east-2 for backwards compatibility.
143-
func getAWSRegion() string {
144-
if region := os.Getenv("AWS_REGION"); region != "" {
145-
return region
146-
}
147-
if region := os.Getenv("AWS_DEFAULT_REGION"); region != "" {
148-
return region
149-
}
150-
// Fallback to the original hardcoded region for backwards compatibility
151-
return endpoints.UsEast2RegionID
152-
}
153-
154138
func FetchSecret(ctx context.Context, r SomeReconciler, req ctrl.Request, secretType SiteSecretType, vaultName, key string) (string, error) {
155139
l := r.GetLogger(ctx)
156140
switch secretType {
157141
case SiteSecretAws:
158142
if sess, err := session.NewSession(&aws.Config{
159-
Region: aws.String(getAWSRegion()),
143+
Region: aws.String(GetAWSRegion()),
160144
}); err != nil {
161145
return "", err
162146
} else {

api/product/util.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package product
33
import (
44
"crypto/sha256"
55
"fmt"
6+
"os"
67
"sort"
78

9+
"github.com/aws/aws-sdk-go/aws/endpoints"
810
"golang.org/x/exp/maps"
911
corev1 "k8s.io/api/core/v1"
1012
)
@@ -89,3 +91,17 @@ func ComputeSha256(in map[string]string) (string, error) {
8991
return fmt.Sprintf("%x", h.Sum(nil)), nil
9092
}
9193
}
94+
95+
// GetAWSRegion returns the AWS region.
96+
// It checks the AWS_REGION environment variable first, then falls back to AWS_DEFAULT_REGION,
97+
// and finally defaults to us-east-2 for backwards compatibility.
98+
func GetAWSRegion() string {
99+
if region := os.Getenv("AWS_REGION"); region != "" {
100+
return region
101+
}
102+
if region := os.Getenv("AWS_DEFAULT_REGION"); region != "" {
103+
return region
104+
}
105+
// Fallback to the original hardcoded region for backwards compatibility
106+
return endpoints.UsEast2RegionID
107+
}

api/product/util_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package product_test
22

33
import (
4+
"os"
45
"testing"
56

7+
"github.com/aws/aws-sdk-go/aws/endpoints"
68
"github.com/posit-dev/team-operator/api/product"
79
"github.com/stretchr/testify/assert"
810
"github.com/stretchr/testify/require"
@@ -80,3 +82,54 @@ func TestLabelMerge(t *testing.T) {
8082
expected = map[string]string{"vorpal": "sword"}
8183
assert.Equal(t, expected, result)
8284
}
85+
86+
func TestGetAWSRegion(t *testing.T) {
87+
tests := []struct {
88+
name string
89+
awsRegion string
90+
awsDefault string
91+
want string
92+
}{
93+
{"AWS_REGION set", "us-west-2", "", "us-west-2"},
94+
{"AWS_DEFAULT_REGION set", "", "eu-west-1", "eu-west-1"},
95+
{"Both set, AWS_REGION wins", "us-west-2", "eu-west-1", "us-west-2"},
96+
{"Neither set, defaults", "", "", endpoints.UsEast2RegionID},
97+
}
98+
99+
for _, tt := range tests {
100+
t.Run(tt.name, func(t *testing.T) {
101+
// Save original values
102+
origRegion := os.Getenv("AWS_REGION")
103+
origDefault := os.Getenv("AWS_DEFAULT_REGION")
104+
defer func() {
105+
// Restore original values
106+
if origRegion != "" {
107+
os.Setenv("AWS_REGION", origRegion)
108+
} else {
109+
os.Unsetenv("AWS_REGION")
110+
}
111+
if origDefault != "" {
112+
os.Setenv("AWS_DEFAULT_REGION", origDefault)
113+
} else {
114+
os.Unsetenv("AWS_DEFAULT_REGION")
115+
}
116+
}()
117+
118+
// Set test values
119+
if tt.awsRegion != "" {
120+
os.Setenv("AWS_REGION", tt.awsRegion)
121+
} else {
122+
os.Unsetenv("AWS_REGION")
123+
}
124+
if tt.awsDefault != "" {
125+
os.Setenv("AWS_DEFAULT_REGION", tt.awsDefault)
126+
} else {
127+
os.Unsetenv("AWS_DEFAULT_REGION")
128+
}
129+
130+
if got := product.GetAWSRegion(); got != tt.want {
131+
t.Errorf("GetAWSRegion() = %v, want %v", got, tt.want)
132+
}
133+
})
134+
}
135+
}

internal/controller/core/site_controller_chronicle.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"github.com/posit-dev/team-operator/api/core/v1beta1"
77
"github.com/posit-dev/team-operator/api/product"
88
"github.com/posit-dev/team-operator/internal"
9-
"k8s.io/apimachinery/pkg/apis/meta/v1"
10-
"sigs.k8s.io/controller-runtime"
9+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
controllerruntime "sigs.k8s.io/controller-runtime"
1111
)
1212

1313
func (r *SiteReconciler) reconcileChronicle(ctx context.Context, req controllerruntime.Request, site *v1beta1.Site) error {
@@ -83,8 +83,7 @@ func (r *SiteReconciler) reconcileChronicle(ctx context.Context, req controllerr
8383
Enabled: true,
8484
Bucket: site.Spec.Chronicle.S3Bucket,
8585
Prefix: site.Name + "/chr-v0",
86-
// TODO: should not be hard-coded
87-
Region: "us-east-2",
86+
Region: product.GetAWSRegion(),
8887
}
8988
}
9089
return nil

internal/controller/core/site_controller_package_manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func (r *SiteReconciler) reconcilePackageManager(
111111
}
112112
pm.Spec.Config.S3Storage.Bucket = site.Spec.PackageManager.S3Bucket
113113
pm.Spec.Config.S3Storage.Prefix = site.Name + "/ppm-v0"
114+
pm.Spec.Config.S3Storage.Region = product.GetAWSRegion()
114115
}
115116
return nil
116117
}); err != nil {

0 commit comments

Comments
 (0)