Skip to content

Commit

Permalink
Merge pull request #427 from qlik-oss/remove-fetchsource
Browse files Browse the repository at this point in the history
remove fetch source
  • Loading branch information
Foysal Iqbal authored Jun 18, 2020
2 parents 627a4a1 + 1f8bcf3 commit 7091da0
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 78 deletions.
30 changes: 15 additions & 15 deletions pkg/api/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,17 @@ func (cr *QliksenseCR) IsRepoExist() bool {
}

func (cr *QliksenseCR) GetFetchUrl() string {
if cr.Spec.FetchSource == nil || cr.Spec.FetchSource.Repository == "" {
if cr.Spec.Git == nil || cr.Spec.Git.Repository == "" {
return QLIK_GIT_REPO
}
return cr.Spec.FetchSource.Repository
return cr.Spec.Git.Repository
}

func (cr *QliksenseCR) GetFetchAccessToken(encryptionKey string) string {
if cr.Spec.FetchSource == nil {
if cr.Spec.Git == nil {
return ""
}
if tok, err := cr.Spec.FetchSource.GetAccessToken(); err != nil {
if tok, err := cr.Spec.Git.GetAccessToken(); err != nil {
fmt.Println(err)
return ""
} else if tok == "" {
Expand All @@ -171,29 +171,29 @@ func (cr *QliksenseCR) GetFetchAccessToken(encryptionKey string) string {
}

func (cr *QliksenseCR) SetFetchUrl(url string) {
if cr.Spec.FetchSource == nil {
cr.Spec.FetchSource = &config.Repo{}
if cr.Spec.Git == nil {
cr.Spec.Git = &config.Repo{}
}
cr.Spec.FetchSource.Repository = url
cr.Spec.Git.Repository = url
}

func (cr *QliksenseCR) SetFetchAccessToken(token, encryptionKey string) error {
if cr.Spec.FetchSource == nil {
cr.Spec.FetchSource = &config.Repo{}
if cr.Spec.Git == nil {
cr.Spec.Git = &config.Repo{}
}
res, err := EncryptData([]byte(token), encryptionKey)
if err != nil {
return err
}
cr.Spec.FetchSource.AccessToken = b64.StdEncoding.EncodeToString(res)
cr.Spec.Git.AccessToken = b64.StdEncoding.EncodeToString(res)
return nil
}

func (cr *QliksenseCR) SetFetchAccessSecretName(sec string) {
if cr.Spec.FetchSource == nil {
cr.Spec.FetchSource = &config.Repo{}
if cr.Spec.Git == nil {
cr.Spec.Git = &config.Repo{}
}
cr.Spec.FetchSource.SecretName = sec
cr.Spec.Git.SecretName = sec
}

//DeleteRepo delete the manifest repo and unset manifestsRoot
Expand Down Expand Up @@ -524,9 +524,9 @@ func (qc *QliksenseConfig) GetDecryptedCr(cr *QliksenseCR) (*QliksenseCR, error)
}
newCr.Spec.Secrets = finalSecrets

if newCr.Spec.FetchSource != nil && newCr.Spec.FetchSource.AccessToken != "" {
if newCr.Spec.Git != nil && newCr.Spec.Git.AccessToken != "" {
decData := cr.GetFetchAccessToken(encryptionKey)
newCr.Spec.FetchSource.AccessToken = decData
newCr.Spec.Git.AccessToken = decData
}
return newCr, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/apis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestGetDecryptedCr(t *testing.T) {
if decryptedValue == orignalValue {
t.Fail()
}
if newCr.Spec.FetchSource.AccessToken != "mytoken" {
if newCr.Spec.Git.AccessToken != "mytoken" {
t.Fail()
}
td()
Expand Down
38 changes: 19 additions & 19 deletions pkg/api/clientgo_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ func TestClientGoUtils_CreatePreflightTestPod(t *testing.T) {
ImagePullPolicy: apiv1.PullIfNotPresent,
Command: []string{"echo"},
VolumeMounts: []apiv1.VolumeMount{
apiv1.VolumeMount{
{
Name: "secret1",
MountPath: filepath.Dir("/etc/secret1"),
ReadOnly: true,
Expand All @@ -517,7 +517,7 @@ func TestClientGoUtils_CreatePreflightTestPod(t *testing.T) {
},
},
Volumes: []apiv1.Volume{
apiv1.Volume{
{
Name: "secret1",
VolumeSource: apiv1.VolumeSource{
Secret: &apiv1.SecretVolumeSource{
Expand Down Expand Up @@ -844,7 +844,7 @@ func TestClientGoUtils_WaitForPod(t *testing.T) {
},
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
apiv1.Container{},
{},
},
},
Status: apiv1.PodStatus{
Expand Down Expand Up @@ -895,7 +895,7 @@ func TestClientGoUtils_WaitForPod(t *testing.T) {
},
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
apiv1.Container{},
{},
},
},
Status: apiv1.PodStatus{
Expand Down Expand Up @@ -942,7 +942,7 @@ func TestClientGoUtils_WaitForPod(t *testing.T) {
},
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
apiv1.Container{},
{},
},
},
Status: apiv1.PodStatus{
Expand Down Expand Up @@ -1059,7 +1059,7 @@ func TestClientGoUtils_WaitForPodToDie(t *testing.T) {
},
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
apiv1.Container{},
{},
},
},
Status: apiv1.PodStatus{
Expand Down Expand Up @@ -1131,7 +1131,7 @@ func TestClientGoUtils_WaitForPodToDie(t *testing.T) {
},
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
apiv1.Container{},
{},
},
},
Status: apiv1.PodStatus{
Expand Down Expand Up @@ -1885,10 +1885,10 @@ func TestClientGoUtils_waitForStatefulsetToDelete(t *testing.T) {
statefulsetName string
}
tests := []struct {
name string
fields fields
args args
wantErr bool
name string
fields fields
args args
wantErr bool
timeoutForChangingReplicaCount time.Duration
}{
{
Expand All @@ -1897,8 +1897,8 @@ func TestClientGoUtils_waitForStatefulsetToDelete(t *testing.T) {
Verbose: true,
},
args: args{
clientset: fake.NewSimpleClientset(ss),
namespace: "test-ns",
clientset: fake.NewSimpleClientset(ss),
namespace: "test-ns",
statefulsetName: ss.Name,
},
wantErr: false,
Expand All @@ -1910,23 +1910,23 @@ func TestClientGoUtils_waitForStatefulsetToDelete(t *testing.T) {
Verbose: true,
},
args: args{
clientset: fake.NewSimpleClientset(),
namespace: "test-ns",
clientset: fake.NewSimpleClientset(),
namespace: "test-ns",
statefulsetName: ss.Name,
},
wantErr: false,
wantErr: false,
},
{
name: "timeout",
fields: fields{
Verbose: true,
},
args: args{
clientset: fake.NewSimpleClientset(ss),
namespace: "test-ns",
clientset: fake.NewSimpleClientset(ss),
namespace: "test-ns",
statefulsetName: ss.Name,
},
wantErr: true,
wantErr: true,
},
}
for _, tt := range tests {
Expand Down
4 changes: 4 additions & 0 deletions pkg/qliksense/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v2"

"github.com/qlik-oss/k-apis/pkg/cr"
"github.com/qlik-oss/sense-installer/pkg/api"
Expand Down Expand Up @@ -85,6 +86,9 @@ func (q *Qliksense) applyConfigToK8s(qcr *qapi.QliksenseCR) error {
}
fmt.Println("Manifests root: " + qcr.Spec.GetManifestsRoot())
qcr.SetNamespace(qapi.GetKubectlNamespace())
b, _ := yaml.Marshal(qcr.KApiCr)
fmt.Printf("%v", string(b))
// os.Exit(0)
// generate patches
cr.GeneratePatches(&qcr.KApiCr, path.Join(userHomeDir, ".kube", "config"))
// apply generated manifests
Expand Down
49 changes: 13 additions & 36 deletions pkg/qliksense/context_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,7 @@ func (q *Qliksense) SetOtherConfigs(args []string) error {
}

for _, arg := range args {
if strings.HasPrefix(arg, "fetchSource.") {
if err := q.processSetFetchSource(arg, qliksenseCR); err != nil {
return err
}
} else if strings.HasPrefix(arg, "git.") {
if strings.HasPrefix(arg, "git.") {
if err := q.processSetGit(arg, qliksenseCR); err != nil {
return err
}
Expand Down Expand Up @@ -285,52 +281,33 @@ func processSetSingleArg(arg string, cr *api.QliksenseCR) error {
}
cr.Spec.RotateKeys = nv[1]
default:
return errors.New("Please enter one of: profile, storageClassName,rotateKeys, manifestRoot to configure the current context")
return errors.New("Please enter one of: profile, storageClassName,rotateKeys, manifestRoot, git to configure the current context")
}
return nil
}

func (q *Qliksense) processSetFetchSource(arg string, cr *api.QliksenseCR) error {
args := strings.Split(arg, "=")
subs := strings.Split(args[0], ".")
if cr.Spec.FetchSource == nil {
cr.Spec.FetchSource = &config.Repo{}
func (q *Qliksense) processSetGit(arg string, cr *api.QliksenseCR) error {
s := strings.Split(arg, "=")
tArg0 := strings.TrimSpace(s[0])
tArg1 := strings.TrimSpace(s[1])
subs := strings.Split(tArg0, ".")
if cr.Spec.Git == nil {
cr.Spec.Git = &config.Repo{}
}
switch subs[1] {
case "repository":
cr.Spec.FetchSource.Repository = args[1]
cr.Spec.Git.Repository = tArg1
case "accessToken":
qConfig := api.NewQConfig(q.QliksenseHome)
key, err := qConfig.GetEncryptionKeyFor(cr.GetName())
if err != nil {
return err
}
return cr.SetFetchAccessToken(args[1], key)
case "secretName":
cr.Spec.FetchSource.SecretName = args[1]
case "userName":
cr.Spec.FetchSource.UserName = args[1]
default:
return errors.New(arg + " does not match any cr spec")
}
return nil
}

func (q *Qliksense) processSetGit(arg string, cr *api.QliksenseCR) error {
args := strings.Split(arg, "=")
subs := strings.Split(args[0], ".")
if cr.Spec.Git == nil {
cr.Spec.Git = &config.Repo{}
}
switch subs[1] {
case "repository":
cr.Spec.Git.Repository = args[1]
case "accessToken":
cr.Spec.Git.AccessToken = args[1]
return cr.SetFetchAccessToken(tArg1, key)
case "secretName":
cr.Spec.Git.SecretName = args[1]
cr.Spec.Git.SecretName = tArg1
case "userName":
cr.Spec.Git.UserName = args[1]
cr.Spec.Git.UserName = tArg1
default:
return errors.New(arg + " does not match any cr spec")
}
Expand Down
1 change: 0 additions & 1 deletion pkg/qliksense/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ func fetchAndUpdateCR(qConfig *qapi.QliksenseConfig, version string) error {
if err != nil {
return err
}

destDir := qConfig.BuildRepoPath(version)
fmt.Printf("fetching version [%s] from %s\n", version, qcr.GetFetchUrl())
if err := qapi.CopyDirectory(tempDest, destDir); err != nil {
Expand Down
27 changes: 23 additions & 4 deletions pkg/qliksense/get_installable_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"

"github.com/Masterminds/semver/v3"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/qlik-oss/k-apis/pkg/git"
qapi "github.com/qlik-oss/sense-installer/pkg/api"
)
Expand All @@ -22,8 +24,20 @@ func (q *Qliksense) GetInstallableVersions(opts *LsRemoteCmdOptions) error {
}

var repoPath string
var auth transport.AuthMethod
if qcr.Spec.GetManifestsRoot() != "" {
repoPath = qcr.Spec.GetManifestsRoot()
encKey, err := qConfig.GetEncryptionKeyFor(qcr.GetName())
if err != nil {
return err
}
accessToken := qcr.GetFetchAccessToken(encKey)
if accessToken != "" {
auth = &http.BasicAuth{
Username: "something",
Password: accessToken,
}
}
} else {
repoPath, err = DownloadFromGitRepoToTmpDir(defaultConfigRepoGitUrl, "master")
if err != nil {
Expand All @@ -36,7 +50,7 @@ func (q *Qliksense) GetInstallableVersions(opts *LsRemoteCmdOptions) error {
return err
}

remoteRefsList, err := git.GetRemoteRefs(r, nil,
remoteRefsList, err := git.GetRemoteRefs(r, auth,
&git.RemoteRefConstraints{
Include: true,
Sort: true,
Expand Down Expand Up @@ -96,13 +110,18 @@ func getLatestTag(repoUrl, accessToken string) (string, error) {
if err != nil {
return "", err
}

r, err := git.OpenRepository(repoPath)
if err != nil {
return "", err
}

remoteRefsList, err := git.GetRemoteRefs(r, nil,
var auth transport.AuthMethod
if accessToken != "" {
auth = &http.BasicAuth{
Username: "something",
Password: accessToken,
}
}
remoteRefsList, err := git.GetRemoteRefs(r, auth,
&git.RemoteRefConstraints{
Include: true,
Sort: true,
Expand Down
4 changes: 2 additions & 2 deletions pkg/qliksense/load_cr.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func (q *Qliksense) loadCrStringIntoFileSystem(crstr string, overwriteExistingCo
}
}
}
if cr.Spec.FetchSource != nil && cr.Spec.FetchSource.AccessToken != "" {
if err := cr.SetFetchAccessToken(cr.Spec.FetchSource.AccessToken, encryptionKey); err != nil {
if cr.Spec.Git != nil && cr.Spec.Git.AccessToken != "" {
if err := cr.SetFetchAccessToken(cr.Spec.Git.AccessToken, encryptionKey); err != nil {
return "", err
}
}
Expand Down

0 comments on commit 7091da0

Please sign in to comment.