Skip to content

Commit d86c005

Browse files
authored
Merge pull request #594 from devfile/single-git-credential
fix: Update devworkspace git credentials automounting to only use one conf…
2 parents ddb78ec + f84ffd7 commit d86c005

File tree

3 files changed

+14
-29
lines changed

3 files changed

+14
-29
lines changed

pkg/provision/workspace/automount/common.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ package automount
1515
import (
1616
"fmt"
1717

18-
"k8s.io/apimachinery/pkg/runtime"
1918
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
2019

21-
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
2220
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
2321
corev1 "k8s.io/api/core/v1"
2422
)
@@ -38,9 +36,8 @@ func (e *FatalError) Unwrap() error {
3836
return e.Err
3937
}
4038

41-
func GetAutoMountResources(devworkspace *dw.DevWorkspace, client k8sclient.Client, scheme *runtime.Scheme) ([]v1alpha1.PodAdditions, []corev1.EnvFromSource, error) {
42-
namespace := devworkspace.GetNamespace()
43-
gitCMPodAdditions, err := getDevWorkspaceGitConfig(devworkspace, client, scheme)
39+
func GetAutoMountResources(client k8sclient.Client, namespace string) ([]v1alpha1.PodAdditions, []corev1.EnvFromSource, error) {
40+
gitCMPodAdditions, err := getDevWorkspaceGitConfig(client, namespace)
4441
if err != nil {
4542
return nil, nil, err
4643
}

pkg/provision/workspace/automount/git-credentials.go

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,20 @@ import (
1818
"path/filepath"
1919
"strings"
2020

21-
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
2221
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
2322
"github.com/devfile/devworkspace-operator/pkg/constants"
2423
corev1 "k8s.io/api/core/v1"
2524
apierrors "k8s.io/apimachinery/pkg/api/errors"
2625
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27-
"k8s.io/apimachinery/pkg/runtime"
2826
"k8s.io/apimachinery/pkg/types"
2927
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
30-
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3128
)
3229

3330
const gitCredentialsName = "credentials"
3431
const gitConfigName = "gitconfig"
3532
const gitConfigLocation = "/etc/" + gitConfigName
33+
const gitCredentialsSecretName = "devworkspace-merged-git-credentials"
34+
const gitCredentialsConfigMapName = "devworkspace-gitconfig"
3635
const credentialTemplate = "[credential]\n\thelper = store --file %s\n"
3736

3837
// 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"
4140
// and condensing them into one string
4241
// 2. Creating and mounting a gitconfig config map to /etc/gitconfig that points to where the credentials are stored
4342
// 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) {
4644
secrets := &corev1.SecretList{}
4745
err := client.List(context.TODO(), secrets, k8sclient.InNamespace(namespace), k8sclient.MatchingLabels{
4846
constants.DevWorkspaceGitCredentialLabel: "true",
@@ -64,10 +62,8 @@ func getDevWorkspaceGitConfig(devworkspace *dw.DevWorkspace, client k8sclient.Cl
6462

6563
podAdditions := &v1alpha1.PodAdditions{}
6664
if len(credentials) > 0 {
67-
gitCredsName := devworkspace.Status.DevWorkspaceId + "-" + gitConfigName
68-
6965
// mount the gitconfig
70-
configMapAdditions, err := mountGitConfigMap(gitCredsName, mountpath, devworkspace, client, scheme)
66+
configMapAdditions, err := mountGitConfigMap(gitCredentialsConfigMapName, mountpath, namespace, client)
7167
if err != nil {
7268
return podAdditions, err
7369
}
@@ -76,7 +72,7 @@ func getDevWorkspaceGitConfig(devworkspace *dw.DevWorkspace, client k8sclient.Cl
7672

7773
// mount the users git credentials
7874
joinedCredentials := strings.Join(credentials, "\n")
79-
secretAdditions, err := mountGitCredentialsSecret(gitCredsName, mountpath, joinedCredentials, devworkspace, client, scheme)
75+
secretAdditions, err := mountGitCredentialsSecret(gitCredentialsSecretName, mountpath, joinedCredentials, namespace, client)
8076
if err != nil {
8177
return podAdditions, err
8278
}
@@ -91,14 +87,14 @@ func getDevWorkspaceGitConfig(devworkspace *dw.DevWorkspace, client k8sclient.Cl
9187
// 1. Creating the configmap that stores the gitconfig if it does not exist
9288
// 2. Setting the proper owner ref to the devworkspace
9389
// 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) {
9591
podAdditions := &v1alpha1.PodAdditions{}
9692

9793
// Initialize the gitconfig template
9894
credentialsGitConfig := fmt.Sprintf(credentialTemplate, filepath.Join(mountPath, gitCredentialsName))
9995

10096
// Create the configmap that stores the gitconfig
101-
err := createOrUpdateGitConfigMap(configMapName, devworkspace.GetNamespace(), credentialsGitConfig, devworkspace, client, scheme)
97+
err := createOrUpdateGitConfigMap(configMapName, namespace, credentialsGitConfig, client)
10298
if err != nil {
10399
return nil, err
104100
}
@@ -120,11 +116,11 @@ func mountGitConfigMap(configMapName, mountPath string, devworkspace *dw.DevWork
120116
// 1. Creating the secret that stores the credentials if it does not exist
121117
// 2. Setting the proper owner ref to the devworkspace
122118
// 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) {
124120
podAdditions := &v1alpha1.PodAdditions{}
125121

126122
// 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)
128124
if err != nil {
129125
return nil, err
130126
}
@@ -141,12 +137,8 @@ func mountGitCredentialsSecret(secretName, mountPath, credentials string, devwor
141137
return podAdditions, nil
142138
}
143139

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 {
145141
secret := getGitSecret(secretName, namespace, config)
146-
err := controllerutil.SetOwnerReference(devworkspace, secret, scheme)
147-
if err != nil {
148-
return err
149-
}
150142
if err := client.Create(context.TODO(), secret); err != nil {
151143
if !apierrors.IsAlreadyExists(err) {
152144
return err
@@ -197,12 +189,8 @@ func getGitSecret(secretName string, namespace string, config string) *corev1.Se
197189
return gitConfigMap
198190
}
199191

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 {
201193
configMap := getGitConfigMap(configMapName, namespace, config)
202-
err := controllerutil.SetOwnerReference(devworkspace, configMap, scheme)
203-
if err != nil {
204-
return err
205-
}
206194
if err := client.Create(context.TODO(), configMap); err != nil {
207195
if !apierrors.IsAlreadyExists(err) {
208196
return err

pkg/provision/workspace/deployment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func SyncDeploymentToCluster(
8484
saName string,
8585
clusterAPI ClusterAPI) DeploymentProvisioningStatus {
8686

87-
automountPodAdditions, automountEnv, err := automount.GetAutoMountResources(workspace, clusterAPI.Client, clusterAPI.Scheme)
87+
automountPodAdditions, automountEnv, err := automount.GetAutoMountResources(clusterAPI.Client, workspace.GetNamespace())
8888
if err != nil {
8989
var fatalErr *automount.FatalError
9090
if errors.As(err, &fatalErr) {

0 commit comments

Comments
 (0)