Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
feat(resource-service): Move history of previous upstream to new upst…
Browse files Browse the repository at this point in the history
…ream (#8947)

* feat(resource-service): Move history of previous upstream to new upstream (#8906)

* feat(resource-service): Move history of previous upstream to new upstream

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>

(cherry picked from commit d24ace1)
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>

* added missing function

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
  • Loading branch information
bacherfl committed Oct 5, 2022
1 parent 9efdf4a commit a7507ed
Show file tree
Hide file tree
Showing 12 changed files with 1,069 additions and 170 deletions.
21 changes: 18 additions & 3 deletions resource-service/common/credentials.go
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/keptn/keptn/resource-service/common_models"
errors2 "github.com/keptn/keptn/resource-service/errors"
Expand All @@ -14,6 +15,9 @@ import (
"k8s.io/client-go/kubernetes"
)

const upstreamCredentialsPrefix = "git-credentials-"
const tmpUpstreamCredentialsPrefix = "tmp-git-credentials-"

//go:generate moq -pkg common_mock -skip-ensure -out ./fake/credential_reader_mock.go . CredentialReader
type CredentialReader interface {
GetCredentials(project string) (*common_models.GitCredentials, error)
Expand All @@ -27,9 +31,12 @@ func NewK8sCredentialReader(k8sClient kubernetes.Interface) *K8sCredentialReader
return &K8sCredentialReader{k8sClient: k8sClient}
}

func (kr K8sCredentialReader) GetCredentials(project string) (*common_models.GitCredentials, error) {
secretName := fmt.Sprintf("git-credentials-%s", project)

func (kr K8sCredentialReader) GetCredentials(secretName string) (*common_models.GitCredentials, error) {
// check if the secretName already contains the required prefix
if !strings.HasPrefix(secretName, upstreamCredentialsPrefix) && !strings.HasPrefix(secretName, tmpUpstreamCredentialsPrefix) {
// if the prefix is not there, prepend the 'git-credentials-' prefix
secretName = GetUpstreamCredentialsSecretName(secretName)
}
secret, err := kr.k8sClient.CoreV1().Secrets(GetKeptnNamespace()).Get(context.TODO(), secretName, metav1.GetOptions{})
if err != nil && k8serrors.IsNotFound(err) {
logger.Debug("Could not retrieve credentials named: ", secretName)
Expand All @@ -52,6 +59,14 @@ func (kr K8sCredentialReader) GetCredentials(project string) (*common_models.Git
return credentials, nil
}

func GetUpstreamCredentialsSecretName(projectName string) string {
return fmt.Sprintf("%s%s", upstreamCredentialsPrefix, projectName)
}

func GetTemporaryUpstreamCredentialsSecretName(projectName string) string {
return fmt.Sprintf("%s%s", tmpUpstreamCredentialsPrefix, projectName)
}

func GetKeptnNamespace() string {
return os.Getenv("POD_NAMESPACE")
}
26 changes: 26 additions & 0 deletions resource-service/common/credentials_test.go
Expand Up @@ -129,3 +129,29 @@ func getK8sSecret() *corev1.Secret {
Type: corev1.SecretTypeOpaque,
}
}

func TestGetTemporaryUpstreamCredentialsSecretName(t *testing.T) {
type args struct {
projectName string
}
tests := []struct {
name string
args args
want string
}{
{
name: "get secret name",
args: args{
"my-project",
},
want: "tmp-git-credentials-my-project",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetTemporaryUpstreamCredentialsSecretName(tt.args.projectName); got != tt.want {
t.Errorf("GetTemporaryUpstreamCredentialsSecretName() = %v, want %v", got, tt.want)
}
})
}
}
130 changes: 114 additions & 16 deletions resource-service/common/fake/git_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a7507ed

Please sign in to comment.