/
glue.go
203 lines (187 loc) · 5.75 KB
/
glue.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package glue
import (
"context"
"fmt"
"maps"
"strings"
awsv1alpha1 "github.com/90poe/glue-jobs-operator/api/v1alpha1"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
awsglue "github.com/aws/aws-sdk-go-v2/service/glue"
"github.com/aws/aws-sdk-go-v2/service/glue/types"
"github.com/aws/aws-sdk-go-v2/service/sts"
)
const (
glueETL = "glueetl"
)
var (
// jobOwner is map which we will add to Tags so we know,
// that this GlueJob is owned by this operator
jobOwnedByOperator = map[string]string{
"glue-jobs-operator": "true",
}
)
type Job struct {
ctx context.Context
job awsv1alpha1.GlueJobSpec
exists bool
awsClient *awsglue.Client
accountID string
region string
}
// NewJob will return a new Job struct
func NewJob(ctx context.Context, job awsv1alpha1.GlueJobSpec) (*Job, error) {
gJob := &Job{
ctx: ctx,
job: job,
exists: false,
}
// Load the Shared AWS Configuration from the Shared config file or IAM Roles
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return nil, err
}
// Create an Amazon Glue service client
gJob.awsClient = awsglue.NewFromConfig(cfg)
// Get Account ID and Region
stsClient := sts.NewFromConfig(cfg)
result, err := stsClient.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
if err != nil {
return nil, fmt.Errorf("failed to call GetCallerIdentity: %w", err)
}
gJob.accountID = *result.Account
gJob.region = cfg.Region
// check that GlueJob exists on AWS
gJob.exists, err = gJob.checkJobExistsOnAWS()
if err != nil {
return nil, fmt.Errorf("failed to check if GlueJob %s exists on AWS: %w", job.Name, err)
}
return gJob, nil
}
// JobExists will return true if GlueJob exists on AWS
func (g *Job) JobExists() bool {
return g.exists
}
// CreateJob will create Glue Job
func (g *Job) CreateJob() error {
command := &types.JobCommand{
Name: aws.String(g.job.Command.Name),
PythonVersion: aws.String(fmt.Sprintf("%v", g.job.Command.PythonVersion)),
ScriptLocation: aws.String(g.job.Command.ScriptLocation),
}
if strings.ToLower(g.job.Command.Name) != glueETL {
command.Runtime = aws.String(g.job.Command.Runtime)
}
input := &awsglue.CreateJobInput{
Name: aws.String(g.job.Name),
Command: command,
Role: aws.String(g.job.Role),
Timeout: aws.Int32(g.job.TimeoutInMinutes),
GlueVersion: aws.String(g.job.GlueVersion),
NumberOfWorkers: aws.Int32(g.job.NumberOfWorkers),
WorkerType: types.WorkerType(g.job.WorkerType),
ExecutionClass: types.ExecutionClass(g.job.ExecutionClass),
ExecutionProperty: &types.ExecutionProperty{
MaxConcurrentRuns: g.job.ExecutionProperty.MaxConcurrentRuns,
},
MaxRetries: g.job.MaxRetries,
DefaultArguments: g.job.DefaultArguments,
Tags: g.getTags(),
}
// create job
_, err := g.awsClient.CreateJob(g.ctx, input)
if err != nil {
return fmt.Errorf("failed to create Glue Job %s: %w", g.job.Name, err)
}
return nil
}
// UpdateJob will update Glue Job
func (g *Job) UpateJob() error {
command := &types.JobCommand{
Name: aws.String(g.job.Command.Name),
PythonVersion: aws.String(fmt.Sprintf("%v", g.job.Command.PythonVersion)),
ScriptLocation: aws.String(g.job.Command.ScriptLocation),
}
if strings.ToLower(g.job.Command.Name) != glueETL {
command.Runtime = aws.String(g.job.Command.Runtime)
}
input := &awsglue.UpdateJobInput{
JobName: aws.String(g.job.Name),
JobUpdate: &types.JobUpdate{
Command: command,
Role: aws.String(g.job.Role),
Timeout: aws.Int32(g.job.TimeoutInMinutes),
GlueVersion: aws.String(g.job.GlueVersion),
NumberOfWorkers: aws.Int32(g.job.NumberOfWorkers),
WorkerType: types.WorkerType(g.job.WorkerType),
ExecutionClass: types.ExecutionClass(g.job.ExecutionClass),
ExecutionProperty: &types.ExecutionProperty{
MaxConcurrentRuns: g.job.ExecutionProperty.MaxConcurrentRuns,
},
MaxRetries: g.job.MaxRetries,
DefaultArguments: g.job.DefaultArguments,
},
}
// update job
_, err := g.awsClient.UpdateJob(g.ctx, input)
if err != nil {
return fmt.Errorf("failed to update Glue Job %s: %w", g.job.Name, err)
}
// Update tags
_, err = g.awsClient.TagResource(g.ctx, &awsglue.TagResourceInput{
ResourceArn: aws.String(fmt.Sprintf("arn:aws:glue:%s:%s:job/%s", g.region, g.accountID, g.job.Name)),
TagsToAdd: g.getTags(),
})
if err != nil {
return fmt.Errorf("failed to update Glue Job tags %s: %w", g.job.Name, err)
}
return nil
}
// DeleteJob will delete Glue Job
func (g *Job) DeleteJob() error {
if !g.exists {
return nil
}
// delete job
_, err := g.awsClient.DeleteJob(g.ctx, &awsglue.DeleteJobInput{
JobName: aws.String(g.job.Name),
})
if err != nil {
return fmt.Errorf("failed to delete Glue Job %s: %w", g.job.Name, err)
}
return nil
}
// getTags will return tags for Glue Job with merged required tags for operator
func (g *Job) getTags() map[string]string {
tags := make(map[string]string, len(g.job.Tags)+len(jobOwnedByOperator))
maps.Copy(tags, g.job.Tags)
maps.Copy(tags, jobOwnedByOperator)
return tags
}
func (g *Job) checkJobExistsOnAWS() (bool, error) {
// get all jobs paginator
jobsPaginator := awsglue.NewListJobsPaginator(g.awsClient, &awsglue.ListJobsInput{
MaxResults: aws.Int32(100),
Tags: jobOwnedByOperator,
})
// Iterate through all jobs in paginator
for {
if !jobsPaginator.HasMorePages() {
// no more pages, break
break
}
jobsOut, err := jobsPaginator.NextPage(g.ctx)
if err != nil {
return false, fmt.Errorf("failed to get next page from Jobs paginator: %w", err)
}
// we have 2 criteria to match a job:
// 1. job name
// 2. job tags
for _, jobName := range jobsOut.JobNames {
if jobName == g.job.Name {
return true, nil
}
}
}
return false, nil
}