-
Notifications
You must be signed in to change notification settings - Fork 40
/
gitsync_env.go
276 lines (254 loc) · 8.97 KB
/
gitsync_env.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"context"
"fmt"
"strconv"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
"kpt.dev/configsync/pkg/api/configsync"
)
const (
// git-sync container specific environment variables.
// gitSyncUsername represents the environment variable key for specifying the username to use for git auth.
gitSyncUsername = "GITSYNC_USERNAME"
// gitSyncPassword represents the environment variable key for specifying the password to use for git auth.
gitSyncPassword = "GITSYNC_PASSWORD"
// gitSyncHTTPSProxy represents the environment variable key for setting `HTTPS_PROXY` in git-sync.
gitSyncHTTPSProxy = "HTTPS_PROXY"
// GitSyncRepo represents the environment variable key for specifying the Git repository to sync.
GitSyncRepo = "GITSYNC_REPO"
// gitSyncRef represents the environment variable key for specifying the Git revision to sync.
gitSyncRef = "GITSYNC_REF"
// GitSyncDepth represents the environment variable key for setting the depth of the Git clone, truncating history to a specific number of commits.
GitSyncDepth = "GITSYNC_DEPTH"
// gitSyncPeriod represents the environment variable key for specifying the sync interval duration.
gitSyncPeriod = "GITSYNC_PERIOD"
// gitSyncSSH represents the environment variable key for specifying the SSH key to use.
gitSyncSSH = "GITSYNC_SSH"
// gitSyncAskpassURL represents the environment variable key for the URL used to query git credentials.
gitSyncAskpassURL = "GITSYNC_ASKPASS_URL"
// gitSyncCookieFile represents the environment variable key for specifying the use of a git cookiefile.
gitSyncCookieFile = "GITSYNC_COOKIE_FILE"
// GitSSLCAInfo represents the environment variable key for SSL certificates.
GitSSLCAInfo = "GIT_SSL_CAINFO"
// GitSyncKnownHosts represents the environment variable key for GIT_KNOWN_HOSTS.
GitSyncKnownHosts = "GITSYNC_SSH_KNOWN_HOSTS"
// GitSSLNoVerify represents the environment variable key for GIT_SSL_NO_VERIFY.
GitSSLNoVerify = "GIT_SSL_NO_VERIFY"
// DefaultSyncRev is the default git revision.
DefaultSyncRev = "HEAD"
// DefaultSyncBranch is the default git branch.
DefaultSyncBranch = "master"
// DefaultSyncDir is the default sync directory.
DefaultSyncDir = "."
// DefaultSyncWaitSecs is the default wait seconds.
DefaultSyncWaitSecs = 15
// SyncDepthNoRev is the default git depth if syncing with default sync revision (`HEAD`).
SyncDepthNoRev = "1"
// SyncDepthRev is the default git depth if syncing with a specific sync revision (tag or hash).
SyncDepthRev = "500"
// KnownHostsKey is the key for known_hosts information
KnownHostsKey = "known_hosts"
)
var gceNodeAskpassURL = fmt.Sprintf("http://localhost:%v/git_askpass", gceNodeAskpassPort)
type options struct {
// ref is the git revision being synced.
ref string
// branch is the git branch being synced.
branch string
// repo is the git repo being synced.
repo string
// secretType used to connect to the repo.
secretType configsync.AuthType
// proxy used to connect to the repo.
proxy string
// period is the time duration between consecutive syncs.
period time.Duration
// depth is the number of git commits to sync.
depth *int64
// noSSLVerify specifies whether to skip the SSL certificate verification in Git.
noSSLVerify bool
// caCertSecretRef specifies the name of a secret containing a CA certificate
caCertSecretRef string
// knownHost specifies whether known_hosts configuration is included
knownHost bool
}
// gitSyncTokenAuthEnv returns environment variables for git-sync container for 'token' Auth.
func gitSyncTokenAuthEnv(secretRef string) []corev1.EnvVar {
username := &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: secretRef,
},
Key: "username",
},
}
passwd := &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: secretRef,
},
Key: "token",
},
}
return []corev1.EnvVar{
{
Name: gitSyncUsername,
ValueFrom: username,
},
{
Name: gitSyncPassword,
ValueFrom: passwd,
},
}
}
// gitSyncHttpsProxyEnv returns environment variables for git-sync container for https_proxy env.
func gitSyncHTTPSProxyEnv(secretRef string, keys map[string]bool) []corev1.EnvVar {
var envVars []corev1.EnvVar
if keys["https_proxy"] {
httpsProxy := &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: secretRef,
},
Key: "https_proxy",
},
}
envVars = append(envVars, corev1.EnvVar{
Name: gitSyncHTTPSProxy,
ValueFrom: httpsProxy,
})
}
return envVars
}
func authTypeToken(secret configsync.AuthType) bool {
return configsync.AuthToken == secret
}
func useCACert(caCertSecretRef string) bool {
return caCertSecretRef != ""
}
func gitSyncEnvs(_ context.Context, opts options) []corev1.EnvVar {
var result []corev1.EnvVar
result = append(result, corev1.EnvVar{
Name: GitSyncRepo,
Value: opts.repo,
})
// GitSyncKnownHosts must default to false for backwards compatibility
if opts.knownHost {
result = append(result, corev1.EnvVar{
Name: GitSyncKnownHosts,
Value: "true",
})
} else {
result = append(result, corev1.EnvVar{
Name: GitSyncKnownHosts,
Value: "false",
})
}
if opts.noSSLVerify {
result = append(result, corev1.EnvVar{
Name: GitSSLNoVerify,
Value: "true",
})
}
if useCACert(opts.caCertSecretRef) {
result = append(result, corev1.EnvVar{
Name: GitSSLCAInfo,
Value: fmt.Sprintf("%s/%s", CACertPath, CACertSecretKey),
})
}
if opts.depth != nil && *opts.depth >= 0 {
// git-sync would do a shallow clone if *opts.depth > 0;
// git-sync would do a full clone if *opts.depth == 0.
result = append(result, corev1.EnvVar{
Name: GitSyncDepth,
Value: strconv.FormatInt(*opts.depth, 10),
})
} else {
// git-sync would do a shallow clone.
//
// If syncRev is set, git-sync checks out the source repo at master and then resets to
// the specified rev. This means that the rev has to be in the pulled history and thus
// will fail if rev is earlier than the configured depth.
// However, if history is too large git-sync will OOM when it tries to pull all of it.
// Try to set a happy medium here -- if syncRev is set, pull 500 commits from master;
// if it isn't, just the latest commit will do and will save memory.
// See b/175088702 and b/158988143
if opts.ref == "" || opts.ref == DefaultSyncRev {
result = append(result, corev1.EnvVar{
Name: GitSyncDepth,
Value: SyncDepthNoRev,
})
} else {
result = append(result, corev1.EnvVar{
Name: GitSyncDepth,
Value: SyncDepthRev,
})
}
}
result = append(result, corev1.EnvVar{
Name: gitSyncPeriod,
Value: opts.period.String(),
})
// We can't use default values in git-sync because of the breaking change: https://github.com/kubernetes/git-sync/issues/841.
// For backward compatibility, we set gitSyncRef to branch when ref is HEAD.
// If ref is HEAD or empty,
// - If branch is not set, set gitSyncRef to master (same behavior in git-sync v3).
// - If branch is set, set gitSyncRef to user-specified branch.
// If ref is neither HEAD nor empty, set gitSyncRef to ref regardless whether branch is set or not.
adjustedRef := opts.ref
if adjustedRef != "" && adjustedRef != "HEAD" {
adjustedRef = opts.ref // sync from a commit or a tag if ref is not HEAD
} else if opts.branch != "" {
adjustedRef = opts.branch // sync from the HEAD of the user-provided branch
} else {
adjustedRef = "master" // sync from the HEAD of master for backward compatibility
}
result = append(result, corev1.EnvVar{
Name: gitSyncRef,
Value: adjustedRef,
})
switch opts.secretType {
case configsync.AuthGCENode, configsync.AuthGCPServiceAccount:
result = append(result, corev1.EnvVar{
Name: gitSyncAskpassURL,
Value: gceNodeAskpassURL,
})
case configsync.AuthSSH:
result = append(result, corev1.EnvVar{
Name: gitSyncSSH,
Value: "true",
})
case configsync.AuthCookieFile:
result = append(result, corev1.EnvVar{
Name: gitSyncCookieFile,
Value: "true",
})
fallthrough
case GitSecretConfigKeyToken, "", configsync.AuthNone:
if opts.proxy != "" {
result = append(result, corev1.EnvVar{
Name: gitSyncHTTPSProxy,
Value: opts.proxy,
})
}
default:
// TODO b/168553377 Return error while setting up gitSyncData.
klog.Errorf("Unrecognized secret type %s", opts.secretType)
}
return result
}