@@ -18,21 +18,20 @@ import (
18
18
"path/filepath"
19
19
"strings"
20
20
21
- dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
22
21
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
23
22
"github.com/devfile/devworkspace-operator/pkg/constants"
24
23
corev1 "k8s.io/api/core/v1"
25
24
apierrors "k8s.io/apimachinery/pkg/api/errors"
26
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
- "k8s.io/apimachinery/pkg/runtime"
28
26
"k8s.io/apimachinery/pkg/types"
29
27
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
30
- "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
31
28
)
32
29
33
30
const gitCredentialsName = "credentials"
34
31
const gitConfigName = "gitconfig"
35
32
const gitConfigLocation = "/etc/" + gitConfigName
33
+ const gitCredentialsSecretName = "devworkspace-merged-git-credentials"
34
+ const gitCredentialsConfigMapName = "devworkspace-gitconfig"
36
35
const credentialTemplate = "[credential]\n \t helper = store --file %s\n "
37
36
38
37
// getDevWorkspaceGitConfig takes care of mounting git credentials and a gitconfig into a devworkspace.
@@ -41,8 +40,7 @@ const credentialTemplate = "[credential]\n\thelper = store --file %s\n"
41
40
// and condensing them into one string
42
41
// 2. Creating and mounting a gitconfig config map to /etc/gitconfig that points to where the credentials are stored
43
42
// 3. Creating and mounting a credentials secret to mountpath/credentials that stores the users git credentials
44
- func getDevWorkspaceGitConfig (devworkspace * dw.DevWorkspace , client k8sclient.Client , scheme * runtime.Scheme ) (* v1alpha1.PodAdditions , error ) {
45
- namespace := devworkspace .GetNamespace ()
43
+ func getDevWorkspaceGitConfig (client k8sclient.Client , namespace string ) (* v1alpha1.PodAdditions , error ) {
46
44
secrets := & corev1.SecretList {}
47
45
err := client .List (context .TODO (), secrets , k8sclient .InNamespace (namespace ), k8sclient.MatchingLabels {
48
46
constants .DevWorkspaceGitCredentialLabel : "true" ,
@@ -64,10 +62,8 @@ func getDevWorkspaceGitConfig(devworkspace *dw.DevWorkspace, client k8sclient.Cl
64
62
65
63
podAdditions := & v1alpha1.PodAdditions {}
66
64
if len (credentials ) > 0 {
67
- gitCredsName := devworkspace .Status .DevWorkspaceId + "-" + gitConfigName
68
-
69
65
// mount the gitconfig
70
- configMapAdditions , err := mountGitConfigMap (gitCredsName , mountpath , devworkspace , client , scheme )
66
+ configMapAdditions , err := mountGitConfigMap (gitCredentialsConfigMapName , mountpath , namespace , client )
71
67
if err != nil {
72
68
return podAdditions , err
73
69
}
@@ -76,7 +72,7 @@ func getDevWorkspaceGitConfig(devworkspace *dw.DevWorkspace, client k8sclient.Cl
76
72
77
73
// mount the users git credentials
78
74
joinedCredentials := strings .Join (credentials , "\n " )
79
- secretAdditions , err := mountGitCredentialsSecret (gitCredsName , mountpath , joinedCredentials , devworkspace , client , scheme )
75
+ secretAdditions , err := mountGitCredentialsSecret (gitCredentialsSecretName , mountpath , joinedCredentials , namespace , client )
80
76
if err != nil {
81
77
return podAdditions , err
82
78
}
@@ -91,14 +87,14 @@ func getDevWorkspaceGitConfig(devworkspace *dw.DevWorkspace, client k8sclient.Cl
91
87
// 1. Creating the configmap that stores the gitconfig if it does not exist
92
88
// 2. Setting the proper owner ref to the devworkspace
93
89
// 3. Adding the new config map volume and volume mount to the pod additions
94
- func mountGitConfigMap (configMapName , mountPath string , devworkspace * dw. DevWorkspace , client k8sclient.Client , scheme * runtime. Scheme ) (* v1alpha1.PodAdditions , error ) {
90
+ func mountGitConfigMap (configMapName , mountPath , namespace string , client k8sclient.Client ) (* v1alpha1.PodAdditions , error ) {
95
91
podAdditions := & v1alpha1.PodAdditions {}
96
92
97
93
// Initialize the gitconfig template
98
94
credentialsGitConfig := fmt .Sprintf (credentialTemplate , filepath .Join (mountPath , gitCredentialsName ))
99
95
100
96
// Create the configmap that stores the gitconfig
101
- err := createOrUpdateGitConfigMap (configMapName , devworkspace . GetNamespace () , credentialsGitConfig , devworkspace , client , scheme )
97
+ err := createOrUpdateGitConfigMap (configMapName , namespace , credentialsGitConfig , client )
102
98
if err != nil {
103
99
return nil , err
104
100
}
@@ -120,11 +116,11 @@ func mountGitConfigMap(configMapName, mountPath string, devworkspace *dw.DevWork
120
116
// 1. Creating the secret that stores the credentials if it does not exist
121
117
// 2. Setting the proper owner ref to the devworkspace
122
118
// 3. Adding the new secret volume and volume mount to the pod additions
123
- func mountGitCredentialsSecret (secretName , mountPath , credentials string , devworkspace * dw. DevWorkspace , client k8sclient.Client , scheme * runtime. Scheme ) (* v1alpha1.PodAdditions , error ) {
119
+ func mountGitCredentialsSecret (secretName , mountPath , credentials , namespace string , client k8sclient.Client ) (* v1alpha1.PodAdditions , error ) {
124
120
podAdditions := & v1alpha1.PodAdditions {}
125
121
126
122
// Create the configmap that stores all the users credentials
127
- err := createOrUpdateGitSecret (secretName , devworkspace . GetNamespace () , credentials , devworkspace , client , scheme )
123
+ err := createOrUpdateGitSecret (secretName , namespace , credentials , client )
128
124
if err != nil {
129
125
return nil , err
130
126
}
@@ -141,12 +137,8 @@ func mountGitCredentialsSecret(secretName, mountPath, credentials string, devwor
141
137
return podAdditions , nil
142
138
}
143
139
144
- func createOrUpdateGitSecret (secretName string , namespace string , config string , devworkspace * dw. DevWorkspace , client k8sclient.Client , scheme * runtime. Scheme ) error {
140
+ func createOrUpdateGitSecret (secretName string , namespace string , config string , client k8sclient.Client ) error {
145
141
secret := getGitSecret (secretName , namespace , config )
146
- err := controllerutil .SetOwnerReference (devworkspace , secret , scheme )
147
- if err != nil {
148
- return err
149
- }
150
142
if err := client .Create (context .TODO (), secret ); err != nil {
151
143
if ! apierrors .IsAlreadyExists (err ) {
152
144
return err
@@ -197,12 +189,8 @@ func getGitSecret(secretName string, namespace string, config string) *corev1.Se
197
189
return gitConfigMap
198
190
}
199
191
200
- func createOrUpdateGitConfigMap (configMapName string , namespace string , config string , devworkspace * dw. DevWorkspace , client k8sclient.Client , scheme * runtime. Scheme ) error {
192
+ func createOrUpdateGitConfigMap (configMapName string , namespace string , config string , client k8sclient.Client ) error {
201
193
configMap := getGitConfigMap (configMapName , namespace , config )
202
- err := controllerutil .SetOwnerReference (devworkspace , configMap , scheme )
203
- if err != nil {
204
- return err
205
- }
206
194
if err := client .Create (context .TODO (), configMap ); err != nil {
207
195
if ! apierrors .IsAlreadyExists (err ) {
208
196
return err
0 commit comments