This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
generated from giantswarm/template-app
/
snsClient.go
77 lines (62 loc) · 2.12 KB
/
snsClient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package aws
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/sns"
"github.com/aws/aws-sdk-go-v2/service/sts"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/log"
"github.com/giantswarm/aws-network-topology-operator/pkg/k8sclient"
)
type SNSClient struct {
ctx context.Context
snsClient *sns.Client
snsTopic string
k8sClient *k8sclient.Cluster
managementCluster types.NamespacedName
}
func NewSNSClient(ctx context.Context, snsTopic string, k8sClient *k8sclient.Cluster, managementCluster types.NamespacedName) *SNSClient {
return &SNSClient{
ctx: ctx,
snsClient: nil,
snsTopic: snsTopic,
k8sClient: k8sClient,
managementCluster: managementCluster,
}
}
func (s *SNSClient) client() *sns.Client {
if s.snsClient == nil {
logger := log.FromContext(s.ctx)
logger.Info("assuming ClusterRoleIdentity role of management cluster")
identity, err := s.k8sClient.GetAWSClusterRoleIdentity(s.ctx, s.managementCluster)
if err != nil {
logger.Error(err, "failed to get ClusterRoleIdentity of management cluster")
os.Exit(1)
}
roleARN := identity.Spec.RoleArn
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
logger.Error(err, "unable to load AWS SDK config")
os.Exit(1)
}
creds := stscreds.NewAssumeRoleProvider(sts.NewFromConfig(cfg), roleARN)
cfg, err = config.LoadDefaultConfig(context.TODO(), config.WithCredentialsProvider(aws.NewCredentialsCache(creds)))
if err != nil {
logger.Error(err, "unable to assume IAM role")
os.Exit(1)
}
s.snsClient = sns.NewFromConfig(cfg)
}
return s.snsClient
}
func (s *SNSClient) PublishSNSMessage(ctx context.Context, params *sns.PublishInput, optFns ...func(*sns.Options)) (*sns.PublishOutput, error) {
params.TopicArn = &s.snsTopic
if params.TopicArn == nil || *params.TopicArn == "" {
return nil, fmt.Errorf("no SNS topic provided")
}
return s.client().Publish(ctx, params, optFns...)
}