forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdfs.go
215 lines (192 loc) · 5.74 KB
/
hdfs.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
204
205
206
207
208
209
210
211
212
213
214
215
package hdfs
import (
"fmt"
"os"
"path/filepath"
"github.com/argoproj/pkg/file"
"gopkg.in/jcmturner/gokrb5.v5/credentials"
"gopkg.in/jcmturner/gokrb5.v5/keytab"
"github.com/argoproj/argo/errors"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo/util"
"github.com/argoproj/argo/workflow/artifacts/resource"
)
// ArtifactDriver is a driver for HDFS
type ArtifactDriver struct {
Addresses []string // comma-separated name nodes
Path string
Force bool
HDFSUser string
KrbOptions *KrbOptions
}
// KrbOptions is options for Kerberos
type KrbOptions struct {
CCacheOptions *CCacheOptions
KeytabOptions *KeytabOptions
Config string
ServicePrincipalName string
}
// CCacheOptions is options for ccache
type CCacheOptions struct {
CCache credentials.CCache
}
// KeytabOptions is options for keytab
type KeytabOptions struct {
Keytab keytab.Keytab
Username string
Realm string
}
// ValidateArtifact validates HDFS artifact
func ValidateArtifact(errPrefix string, art *wfv1.HDFSArtifact) error {
if len(art.Addresses) == 0 {
return errors.Errorf(errors.CodeBadRequest, "%s.addresses is required", errPrefix)
}
if art.Path == "" {
return errors.Errorf(errors.CodeBadRequest, "%s.path is required", errPrefix)
}
if !filepath.IsAbs(art.Path) {
return errors.Errorf(errors.CodeBadRequest, "%s.path must be a absolute file path", errPrefix)
}
hasKrbCCache := art.KrbCCacheSecret != nil
hasKrbKeytab := art.KrbKeytabSecret != nil
if art.HDFSUser == "" && !hasKrbCCache && !hasKrbKeytab {
return errors.Errorf(errors.CodeBadRequest, "either %s.hdfsUser, %s.krbCCacheSecret or %s.krbKeytabSecret is required", errPrefix, errPrefix, errPrefix)
}
if hasKrbKeytab && (art.KrbServicePrincipalName == "" || art.KrbConfigConfigMap == nil || art.KrbUsername == "" || art.KrbRealm == "") {
return errors.Errorf(errors.CodeBadRequest, "%s.krbServicePrincipalName, %s.krbConfigConfigMap, %s.krbUsername and %s.krbRealm are required with %s.krbKeytabSecret", errPrefix, errPrefix, errPrefix, errPrefix, errPrefix)
}
if hasKrbCCache && (art.KrbServicePrincipalName == "" || art.KrbConfigConfigMap == nil) {
return errors.Errorf(errors.CodeBadRequest, "%s.krbServicePrincipalName and %s.krbConfigConfigMap are required with %s.krbCCacheSecret", errPrefix, errPrefix, errPrefix)
}
return nil
}
// CreateDriver constructs ArtifactDriver
func CreateDriver(ci resource.Interface, art *wfv1.HDFSArtifact) (*ArtifactDriver, error) {
var krbConfig string
var krbOptions *KrbOptions
var err error
if art.KrbConfigConfigMap != nil && art.KrbConfigConfigMap.Name != "" {
krbConfig, err = ci.GetConfigMapKey(art.KrbConfigConfigMap.Name, art.KrbConfigConfigMap.Key)
if err != nil {
return nil, err
}
}
if art.KrbCCacheSecret != nil && art.KrbCCacheSecret.Name != "" {
bytes, err := ci.GetSecret(art.KrbCCacheSecret.Name, art.KrbCCacheSecret.Key)
if err != nil {
return nil, err
}
ccache, err := credentials.ParseCCache([]byte(bytes))
if err != nil {
return nil, err
}
krbOptions = &KrbOptions{
CCacheOptions: &CCacheOptions{
CCache: ccache,
},
Config: krbConfig,
ServicePrincipalName: art.KrbServicePrincipalName,
}
}
if art.KrbKeytabSecret != nil && art.KrbKeytabSecret.Name != "" {
bytes, err := ci.GetSecret(art.KrbKeytabSecret.Name, art.KrbKeytabSecret.Key)
if err != nil {
return nil, err
}
ktb, err := keytab.Parse([]byte(bytes))
if err != nil {
return nil, err
}
krbOptions = &KrbOptions{
KeytabOptions: &KeytabOptions{
Keytab: ktb,
Username: art.KrbUsername,
Realm: art.KrbRealm,
},
Config: krbConfig,
ServicePrincipalName: art.KrbServicePrincipalName,
}
}
driver := ArtifactDriver{
Addresses: art.Addresses,
Path: art.Path,
Force: art.Force,
HDFSUser: art.HDFSUser,
KrbOptions: krbOptions,
}
return &driver, nil
}
// Load downloads artifacts from HDFS compliant storage
func (driver *ArtifactDriver) Load(inputArtifact *wfv1.Artifact, path string) error {
hdfscli, err := createHDFSClient(driver.Addresses, driver.HDFSUser, driver.KrbOptions)
if err != nil {
return err
}
defer util.Close(hdfscli)
srcStat, err := hdfscli.Stat(driver.Path)
if err != nil {
return err
}
if srcStat.IsDir() {
return fmt.Errorf("HDFS artifact does not suppot directory copy")
}
_, err = os.Stat(path)
if err != nil && !os.IsNotExist(err) {
return err
}
if os.IsNotExist(err) {
dirPath := filepath.Dir(driver.Path)
if dirPath != "." && dirPath != "/" {
// Follow umask for the permission
err = os.MkdirAll(dirPath, 0777)
if err != nil {
return err
}
}
} else {
if driver.Force {
err = os.Remove(path)
if err != nil && !os.IsNotExist(err) {
return err
}
}
}
return hdfscli.CopyToLocal(driver.Path, path)
}
// Save saves an artifact to HDFS compliant storage
func (driver *ArtifactDriver) Save(path string, outputArtifact *wfv1.Artifact) error {
hdfscli, err := createHDFSClient(driver.Addresses, driver.HDFSUser, driver.KrbOptions)
if err != nil {
return err
}
defer util.Close(hdfscli)
isDir, err := file.IsDirectory(path)
if err != nil {
return err
}
if isDir {
return fmt.Errorf("HDFS artifact does not suppot directory copy")
}
_, err = hdfscli.Stat(driver.Path)
if err != nil && !os.IsNotExist(err) {
return err
}
if os.IsNotExist(err) {
dirPath := filepath.Dir(driver.Path)
if dirPath != "." && dirPath != "/" {
// Follow umask for the permission
err = hdfscli.MkdirAll(dirPath, 0777)
if err != nil {
return err
}
}
} else {
if driver.Force {
err = hdfscli.Remove(driver.Path)
if err != nil && !os.IsNotExist(err) {
return err
}
}
}
return hdfscli.CopyToRemote(path, driver.Path)
}