Skip to content

Commit

Permalink
Add a log message when creating secret
Browse files Browse the repository at this point in the history
Fixes #222
  • Loading branch information
chmouel committed Sep 16, 2021
1 parent 4031e66 commit 62935b5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ type KubeInteractionIntf interface {
// TODO: we don't need tektonv1beta1client stuff here
WaitForPipelineRunSucceed(context.Context, tektonv1beta1client.TektonV1beta1Interface, *v1beta1.PipelineRun, time.Duration) error
CleanupPipelines(context.Context, string, string, int) error
CreateBasicAuthSecret(context.Context, webvcs.RunInfo, string, string) error
CreateBasicAuthSecret(context.Context, webvcs.RunInfo, string) error
}
7 changes: 3 additions & 4 deletions pkg/kubeinteraction/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ func (k Interaction) createSecret(ctx context.Context, secretData map[string]str
}

// CreateBasicAuthSecret Create a secret for git-clone basic-auth workspace
func (k Interaction) CreateBasicAuthSecret(ctx context.Context, runinfo webvcs.RunInfo, targetNamespace, token string) error {
func (k Interaction) CreateBasicAuthSecret(ctx context.Context, runinfo webvcs.RunInfo, targetNamespace string) error {
repoURL, err := url.Parse(runinfo.URL)
if err != nil {
return err
}
urlWithToken := fmt.Sprintf("%s://git:%s@%s%s", repoURL.Scheme, token, repoURL.Host, repoURL.Path)

urlWithToken := fmt.Sprintf("%s://git:%s@%s%s", repoURL.Scheme, k.Clients.GithubClient.Token, repoURL.Host, repoURL.Path)
secretData := map[string]string{
".gitconfig": fmt.Sprintf(basicAuthGitConfigData, runinfo.URL),
".git-credentials": urlWithToken,
Expand All @@ -52,6 +51,6 @@ func (k Interaction) CreateBasicAuthSecret(ctx context.Context, runinfo webvcs.R
err = k.createSecret(ctx, secretData, targetNamespace, secretName)
}
}

k.Clients.Log.Infof("Secret %s has been generated in namespace %s", secretName, targetNamespace)
return err
}
14 changes: 11 additions & 3 deletions pkg/kubeinteraction/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
testclient "github.com/openshift-pipelines/pipelines-as-code/pkg/test/clients"
"github.com/openshift-pipelines/pipelines-as-code/pkg/webvcs"
"go.uber.org/zap"
zapobserver "go.uber.org/zap/zaptest/observer"
"gotest.tools/v3/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -50,9 +52,14 @@ func TestCreateBasicAuthSecret(t *testing.T) {
},
}
stdata, _ := testclient.SeedTestData(t, ctx, tdata)

observer, _ := zapobserver.New(zap.InfoLevel)
fakelogger := zap.New(observer).Sugar()
kint := Interaction{
Clients: &cli.Clients{Kube: stdata.Kube},
Clients: &cli.Clients{
Kube: stdata.Kube,
GithubClient: webvcs.GithubVCS{Token: token},
Log: fakelogger,
},
}
runinfo := webvcs.RunInfo{
Owner: "owner",
Expand All @@ -75,10 +82,11 @@ func TestCreateBasicAuthSecret(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := kint.CreateBasicAuthSecret(ctx, runinfo, tt.targetNS, token)
err := kint.CreateBasicAuthSecret(ctx, runinfo, tt.targetNS)
assert.NilError(t, err)
slist, err := kint.Clients.Kube.CoreV1().Secrets(tt.targetNS).List(ctx, metav1.ListOptions{})
assert.NilError(t, err)
assert.Assert(t, len(slist.Items) > 0, "Secret has not been created")
assert.Equal(t, slist.Items[0].Name, "pac-git-basic-auth-owner-repo")
assert.Equal(t, slist.Items[0].StringData[".git-credentials"], "https://git:verysecrete@forge/owner/repo")
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/pipelineascode/pipelineascode.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func Run(ctx context.Context, cs *cli.Clients, k8int cli.KubeInteractionIntf, ru

// Automatically create a secret with the token to be reused by git-clone task
if runinfo.SecretAutoCreation {
err = k8int.CreateBasicAuthSecret(ctx, *runinfo, repo.Spec.Namespace, cs.GithubClient.Token)
err = k8int.CreateBasicAuthSecret(ctx, *runinfo, repo.Spec.Namespace)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/test/kubernetestint/kubernetesint.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (k *KinterfaceTest) GetConsoleUI(ctx context.Context, ns string, pr string)
return k.ConsoleURL, nil
}

func (k *KinterfaceTest) CreateBasicAuthSecret(ctx context.Context, runinfo webvcs.RunInfo, namespace, token string) error {
func (k *KinterfaceTest) CreateBasicAuthSecret(ctx context.Context, runinfo webvcs.RunInfo, ns string) error {
return nil
}

Expand Down

0 comments on commit 62935b5

Please sign in to comment.