Compare commits
1 Commits
random-fix
...
testing-ou
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c3a913f242 |
55
.circleci/config.yml
Normal file
55
.circleci/config.yml
Normal file
@@ -0,0 +1,55 @@
|
||||
# Golang CircleCI 2.0 configuration file
|
||||
#
|
||||
# Check https://circleci.com/docs/2.0/language-go/ for more details
|
||||
version: 2
|
||||
jobs:
|
||||
build:
|
||||
docker:
|
||||
- image: circleci/golang:stretch
|
||||
working_directory: /go/src/github.com/qlik-oss/sense-installer
|
||||
steps:
|
||||
- checkout
|
||||
- run: make build
|
||||
build_release:
|
||||
docker:
|
||||
- image: circleci/golang:stretch
|
||||
working_directory: /go/src/github.com/qlik-oss/sense-installer
|
||||
steps:
|
||||
- checkout
|
||||
- run: make xbuild-all
|
||||
- run:
|
||||
name: "Publish Release on GitHub"
|
||||
command: |
|
||||
go get github.com/tcnksm/ghr
|
||||
# ghr -t ${GITHUB_TOKEN} -u ${CIRCLE_PROJECT_USERNAME} -r ${CIRCLE_PROJECT_REPONAME} -c ${CIRCLE_SHA1} -delete ${CIRCLE_TAG} /go/src/github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/bin/${CIRCLE_TAG}/
|
||||
workflows:
|
||||
version: 2
|
||||
commit:
|
||||
jobs:
|
||||
- build:
|
||||
filters:
|
||||
branches:
|
||||
ignore: master
|
||||
|
||||
- build_release:
|
||||
requires:
|
||||
- build
|
||||
filters:
|
||||
branches:
|
||||
ignore: master
|
||||
build_release:
|
||||
jobs:
|
||||
- build:
|
||||
filters:
|
||||
branches:
|
||||
ignore: /.*/
|
||||
tags:
|
||||
only: /v.*/
|
||||
- build_release:
|
||||
requires:
|
||||
- build
|
||||
filters:
|
||||
branches:
|
||||
ignore: /.*/
|
||||
tags:
|
||||
only: /v.*/
|
||||
5
.gitattributes
vendored
5
.gitattributes
vendored
@@ -1,5 +0,0 @@
|
||||
# Ignore all files and folders that start with .; .circleci, .github, .git, etc.
|
||||
# Warning! This will ignore files in subfolders as well.
|
||||
# If you needs files starting with . then change condition below to be specific
|
||||
# for each file and folder that needs to be ignored
|
||||
.* export-ignore
|
||||
27
.github/workflows/build.yml
vendored
27
.github/workflows/build.yml
vendored
@@ -1,27 +0,0 @@
|
||||
name: Build Sense installer
|
||||
|
||||
on: [pull_request]
|
||||
|
||||
jobs:
|
||||
|
||||
build:
|
||||
name: Build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Set up Go 1.13
|
||||
uses: actions/setup-go@v1
|
||||
with:
|
||||
go-version: 1.13
|
||||
|
||||
- uses: actions/checkout@v2
|
||||
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
|
||||
- name: Set GOPATH
|
||||
# temporary fix
|
||||
# see https://github.com/actions/setup-go/issues/14
|
||||
run: |
|
||||
echo "##[set-env name=GOPATH;]$(dirname $GITHUB_WORKSPACE)"
|
||||
echo "##[add-path]$(dirname $GITHUB_WORKSPACE)/bin"
|
||||
shell: bash
|
||||
|
||||
- run: make test
|
||||
- run: make build
|
||||
21
.github/workflows/mkdocs.yml
vendored
21
.github/workflows/mkdocs.yml
vendored
@@ -1,21 +0,0 @@
|
||||
name: Publish docs via GitHub Pages
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
paths:
|
||||
- 'docs/**'
|
||||
- 'mkdocs.yml'
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Deploy docs
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout master
|
||||
uses: actions/checkout@v1
|
||||
|
||||
- name: Deploy docs
|
||||
uses: mhausenblas/mkdocs-deploy-gh-pages@1.11
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
37
.github/workflows/release.yml
vendored
37
.github/workflows/release.yml
vendored
@@ -1,37 +0,0 @@
|
||||
name: Release Sense installer binaries
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*.*.*'
|
||||
|
||||
jobs:
|
||||
|
||||
release:
|
||||
name: Build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Set up Go 1.13
|
||||
uses: actions/setup-go@v1
|
||||
with:
|
||||
go-version: 1.13
|
||||
|
||||
- uses: actions/checkout@v2
|
||||
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* # Needed in makefile for versioning
|
||||
- name: Set GOPATH
|
||||
# temporary fix
|
||||
# see https://github.com/actions/setup-go/issues/14
|
||||
run: |
|
||||
echo "##[set-env name=GOPATH;]$(dirname $GITHUB_WORKSPACE)"
|
||||
echo "##[add-path]$(dirname $GITHUB_WORKSPACE)/bin"
|
||||
shell: bash
|
||||
|
||||
- run: make test
|
||||
- run: make xbuild-all
|
||||
|
||||
- name: Release
|
||||
uses: softprops/action-gh-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
files: bin/**/*
|
||||
9
.gitignore
vendored
9
.gitignore
vendored
@@ -1,11 +1,2 @@
|
||||
bin
|
||||
.vscode
|
||||
cmd/qliksense/__debug_bin
|
||||
pkg/qliksense/crds
|
||||
pkg/qliksense/packrd
|
||||
pkg/qliksense/qliksense-packr.go
|
||||
pkg/qliksense/docker-registry
|
||||
/pkg/qliksense/tests
|
||||
.DS_Store
|
||||
|
||||
.idea/
|
||||
628
.qliksense/porter-runtime
Normal file
628
.qliksense/porter-runtime
Normal file
File diff suppressed because one or more lines are too long
628
.qliksense/porter.exe
Normal file
628
.qliksense/porter.exe
Normal file
File diff suppressed because one or more lines are too long
191
LICENSE
191
LICENSE
@@ -1,191 +0,0 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
https://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
Copyright 2019 QlikTech International AB
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
23
MKDOCS.md
23
MKDOCS.md
@@ -1,23 +0,0 @@
|
||||
# Qlik Sense installer documentation
|
||||
|
||||
## Local development of documentation
|
||||
|
||||
Documentation is built using [mkdocs](https://www.mkdocs.org/) and uses [Material for MKDocs theme](https://squidfunk.github.io/mkdocs-material/)
|
||||
|
||||
Requirements: Python and PIP or Docker
|
||||
|
||||
```console
|
||||
pip install mkdocs
|
||||
pip install mkdocs-material
|
||||
```
|
||||
|
||||
View live changes locally at http://localhost:8000
|
||||
```console
|
||||
mkdocs serve
|
||||
```
|
||||
|
||||
### Docker
|
||||
|
||||
```console
|
||||
docker run --rm -it -p 8000:8000 -v ${PWD}:/docs squidfunk/mkdocs-material
|
||||
```
|
||||
72
Makefile
72
Makefile
@@ -4,15 +4,13 @@ PKG = github.com/qlik-oss/sense-installer
|
||||
MAKE_OPTS ?= --no-print-directory
|
||||
|
||||
LDFLAGS = -w -X $(PKG)/pkg.Version=$(VERSION) -X $(PKG)/pkg.Commit=$(COMMIT) -X "$(PKG)/pkg.CommitDate=$(COMMIT_DATE)"
|
||||
XBUILD = CGO_ENABLED=0 go build -a -tags "$(BUILDTAGS)" -ldflags '$(LDFLAGS)'
|
||||
XBUILD = CGO_ENABLED=0 go build -a -tags netgo -ldflags '$(LDFLAGS)'
|
||||
BINDIR = bin
|
||||
|
||||
COMMIT ?= $(shell git rev-parse --short HEAD)
|
||||
COMMIT_DATE ?= $(shell git show --no-patch --no-notes --pretty='%cd' $(COMMIT) --date=iso)
|
||||
VERSION ?= $(shell git describe --tags 2> /dev/null || echo v0)
|
||||
PERMALINK ?= $(shell git describe --tags --exact-match &> /dev/null && echo latest || echo canary)
|
||||
BUILDTAGS = netgo containers_image_ostree_stub exclude_graphdriver_devicemapper exclude_graphdriver_btrfs containers_image_openpgp
|
||||
|
||||
|
||||
CLIENT_PLATFORM ?= $(shell go env GOOS)
|
||||
CLIENT_ARCH ?= $(shell go env GOARCH)
|
||||
@@ -24,13 +22,8 @@ SUPPORTED_ARCHES = amd64
|
||||
|
||||
MIXIN = qliksense
|
||||
|
||||
DEVNUL := /dev/null
|
||||
WHICH := which
|
||||
|
||||
ifeq ($(CLIENT_PLATFORM),windows)
|
||||
FILE_EXT=.exe
|
||||
DEVNUL := NUL
|
||||
WHICH := where
|
||||
else ifeq ($(RUNTIME_PLATFORM),windows)
|
||||
FILE_EXT=.exe
|
||||
else
|
||||
@@ -38,69 +31,18 @@ FILE_EXT=
|
||||
endif
|
||||
|
||||
.PHONY: build
|
||||
build: clean generate
|
||||
build:
|
||||
mkdir -p $(BINDIR)
|
||||
go build -ldflags '$(LDFLAGS)' -tags "$(BUILDTAGS)" -o $(BINDIR)/$(MIXIN)$(FILE_EXT) ./cmd/$(MIXIN)
|
||||
$(MAKE) clean
|
||||
go build -ldflags '$(LDFLAGS)' -o $(BINDIR)/$(MIXIN)$(FILE_EXT) ./cmd/$(MIXIN)
|
||||
|
||||
.PHONY: test
|
||||
test: clean generate
|
||||
ifeq ($(shell ${WHICH} docker-registry 2>${DEVNUL}),)
|
||||
$(eval TMP-docker-distribution := $(shell mktemp -d))
|
||||
git clone https://github.com/docker/distribution.git $(TMP-docker-distribution)/docker-distribution
|
||||
cd $(TMP-docker-distribution)/docker-distribution; git checkout -b v2.7.1; make
|
||||
cp $(TMP-docker-distribution)/docker-distribution/bin/registry pkg/qliksense/docker-registry
|
||||
-rm -rf $(TMP-docker-distribution)
|
||||
endif
|
||||
go test -short -count=1 -tags "$(BUILDTAGS)" -v ./...
|
||||
$(MAKE) clean
|
||||
|
||||
xbuild-all: clean generate
|
||||
xbuild-all:
|
||||
$(foreach OS, $(SUPPORTED_PLATFORMS), \
|
||||
$(foreach ARCH, $(SUPPORTED_ARCHES), \
|
||||
$(MAKE) $(MAKE_OPTS) CLIENT_PLATFORM=$(OS) CLIENT_ARCH=$(ARCH) MIXIN=$(MIXIN) xbuild; \
|
||||
))
|
||||
|
||||
$(MAKE) clean
|
||||
$(foreach ARCH, $(SUPPORTED_ARCHES), \
|
||||
$(MAKE) $(MAKE_OPTS) CLIENT_PLATFORM=$(OS) CLIENT_ARCH=$(ARCH) MIXIN=$(MIXIN) xbuild; \
|
||||
))
|
||||
|
||||
xbuild: $(BINDIR)/$(VERSION)/$(MIXIN)-$(CLIENT_PLATFORM)-$(CLIENT_ARCH)$(FILE_EXT)
|
||||
|
||||
$(BINDIR)/$(VERSION)/$(MIXIN)-$(CLIENT_PLATFORM)-$(CLIENT_ARCH)$(FILE_EXT):
|
||||
mkdir -p $(dir $@)
|
||||
GOOS=$(CLIENT_PLATFORM) GOARCH=$(CLIENT_ARCH) $(XBUILD) -o $@ ./cmd/$(MIXIN)
|
||||
|
||||
ifeq ($(CLIENT_PLATFORM),windows)
|
||||
zip $(BINDIR)/$(VERSION)/$(MIXIN)-$(CLIENT_PLATFORM)-$(CLIENT_ARCH).zip $(BINDIR)/$(VERSION)/$(MIXIN)-$(CLIENT_PLATFORM)-$(CLIENT_ARCH)$(FILE_EXT)
|
||||
else
|
||||
tar -czvf $(BINDIR)/$(VERSION)/$(MIXIN)-$(CLIENT_PLATFORM)-$(CLIENT_ARCH).tar.gz -C $(BINDIR)/$(VERSION)/ $(MIXIN)-$(CLIENT_PLATFORM)-$(CLIENT_ARCH)$(FILE_EXT)
|
||||
endif
|
||||
upx $(BINDIR)/$(VERSION)/$(MIXIN)-$(CLIENT_PLATFORM)-$(CLIENT_ARCH)$(FILE_EXT)
|
||||
|
||||
generate: get-crds packr2
|
||||
go generate ./...
|
||||
|
||||
packr2:
|
||||
ifeq ($(shell ${WHICH} packr2 2>${DEVNUL}),)
|
||||
go get -u github.com/gobuffalo/packr/v2/packr2@v2.7.1
|
||||
endif
|
||||
|
||||
clean: clean-packr
|
||||
-rm -fr pkg/qliksense/crds
|
||||
|
||||
clean-packr: packr2
|
||||
cd pkg/qliksense && packr2 clean
|
||||
|
||||
get-crds:
|
||||
ifeq ($(QLIKSENSE_OPERATOR_DIR),)
|
||||
$(eval TMP-operator := $(shell mktemp -d))
|
||||
git clone https://github.com/qlik-oss/qliksense-operator.git -b master $(TMP-operator)/operator
|
||||
$(MAKE) QLIKSENSE_OPERATOR_DIR=$(TMP-operator)/operator get-crds
|
||||
-rm -rf $(TMP-operator)
|
||||
else
|
||||
mkdir -p pkg/qliksense/crds/cr
|
||||
mkdir -p pkg/qliksense/crds/crd
|
||||
mkdir -p pkg/qliksense/crds/crd-deploy
|
||||
cp $(QLIKSENSE_OPERATOR_DIR)/deploy/*.yaml pkg/qliksense/crds/crd-deploy
|
||||
cp $(QLIKSENSE_OPERATOR_DIR)/deploy/crds/*_crd.yaml pkg/qliksense/crds/crd
|
||||
cp $(QLIKSENSE_OPERATOR_DIR)/deploy/crds/*_cr.yaml pkg/qliksense/crds/cr
|
||||
endif
|
||||
|
||||
165
README.md
165
README.md
@@ -1,25 +1,162 @@
|
||||
# (WIP) Qlik Sense installation and operations CLI
|
||||
|
||||
## Documentation
|
||||
|
||||
To learn more about Qlik Sense installer go to https://qlik-oss.github.io/sense-installer/
|
||||
# Qlik Sense installation and operations CLI
|
||||
|
||||
- [Qlik Sense installation and operations CLI](#qlik-sense-installation-and-operations-cli)
|
||||
- [About](#about)
|
||||
- [Future Direction](#future-direction)
|
||||
- [Getting Started](#getting-started)
|
||||
- [Requirements](#requirements)
|
||||
- [Download](#download)
|
||||
- [Porter CLI](#porter-cli)
|
||||
- [Generate Credentials from published bundle](#generate-credentials-from-published-bundle)
|
||||
- [Qlik Sense version and image list](#qliksense-version-and-image-list)
|
||||
- [Optional: Pulling images in manifest locally, "air gap"](#optional-pulling-images-in-manifest-locally-%22air-gap%22)
|
||||
- [Running Preflight checks](#running-preflight-checks)
|
||||
- [Installation](#installation)
|
||||
- [Supported Parameters during install](#supported-parameters-during-install)
|
||||
- [How To Add Identity Provider Config](#how-to-add-identity-provider-config)
|
||||
- [Packaging a Custom bundle](#packaging-a-custom-bundle)
|
||||
|
||||
## About
|
||||
|
||||
The Qlik Sense installer CLI (qliksense) provides an imperative interface to many of the configurations that need to be applied against the declarative structure described in [qliksense-k8s](https://github.com/qlik-oss/qliksense-k8s). This cli facilitates:
|
||||
The Qlik Sense installer CLI (sense-installer) provides an imperitive interface to many of the configurations that need to be applied against the declaritive structure described in [qliksense-k8s](https://github.com/qlik-oss/qliksense-k8s).
|
||||
|
||||
- installation of QSEoK
|
||||
- installation of qliksense operator to manage QSEoK
|
||||
- air gapped installation of QSEoK
|
||||
This is a technology preview that uses [porter](https://porter.sh) to execute "actions" (operations) and bundle versions of the [qliksense-k8s](https://github.com/qlik-oss/qliksense-k8s) repository.
|
||||
|
||||
This is a technology preview that uses Qlik modified [kustomize](https://github.com/qlik-oss/kustomize) to kubernetes manifests of the versions of the [qliksense-k8s](https://github.com/qlik-oss/qliksense-k8s) repository.
|
||||
These bundles are posted to [docker hub](https://hub.docker.com/) at the following location: [qliksense-cnab-bundle](https://hub.docker.com/r/qlik/qliksense-cnab-bundle/tags).
|
||||
|
||||
For each version of a qliksense edge build there should be a corresponding release in [qliksense-k8s] repository under [releases](https://github.com/qlik-oss/qliksense-k8s/releases)
|
||||
For each version of a qliksense sense edge build there should be a corresponding release current posted on docker hub. ex. `qlik/qliksense-cnab-bundle:v1.21.23-edge` for `v1.21.23` edge release of qliksense. The latest version posted will also be labelled as `latest`
|
||||
|
||||
### Future Direction
|
||||
|
||||
- Porter is currently used as a core technology for the CLI. In the future Porter will be moved "up the stack" to allow the CLI to perform the current and expanded operations independently and encapsulate core functionality currently provided by Porter and other dependent tooling.
|
||||
- More operations:
|
||||
- Expand preflight checks
|
||||
- Expanded preflight checks
|
||||
- backup/restore operations
|
||||
- fully support airgap installation of QSEoK
|
||||
- restore unwanted deletion of kubernetes resources
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Requirements
|
||||
|
||||
- Docker Client connected to a docker engine into which images can built, pulled and pushed.
|
||||
- (Docker Desktop setup tested for these instructions)
|
||||
|
||||
### Download
|
||||
|
||||
- Download the appropriate executable for your platform from the [releases page](https://github.com/qlik-oss/sense-installer/releases).
|
||||
- To allow the CLI to download and initialize dependencies (including porter and it's associated mixins), simply execute `qliksense` with no arguments
|
||||
- `qliksense`
|
||||
|
||||
#### Porter CLI
|
||||
- *Optional*: If wanting to use porter CLI directly, two environment variables will need to be set so as not to conflict with an existing porter installation:
|
||||
- _Bash_
|
||||
```shell
|
||||
bash# export PORTER_HOME="$HOME\.qliksense"
|
||||
bash# export PATH="$HOME\.qliksense;$PATH"
|
||||
```
|
||||
|
||||
- _PowerShell_
|
||||
```shell
|
||||
PS> $Env:PORTER_HOME="$Env:USERPROFILE\.qliksense"
|
||||
PS> $Env:PATH="$Env:USERPROFILE\.qliksense;$Env:PATH"
|
||||
```
|
||||
|
||||
### Generate Credentials from published bundle
|
||||
|
||||
- Ensure connectivity to the target cluster create a kubeconfig credential for a target bundle.
|
||||
- generating a file as follows, replace `<credential_name>` with a name of your choosing.
|
||||
- _Bash_
|
||||
```shell
|
||||
bash# CREDENTIAL_NAME=<credential_name>
|
||||
bash# cat <<EOF > $HOME/.qliksense/credentials/$CREDENTIAL_NAME.yaml
|
||||
name: $CREDENTIAL_NAME
|
||||
credentials:
|
||||
- name: kubeconfig
|
||||
source:
|
||||
path: $HOME/.kube/config
|
||||
EOF
|
||||
```
|
||||
- _PowerShell_
|
||||
```shell
|
||||
PS> $CREDENTIAL_NAME="<credential_name>"
|
||||
PS> Add-Content -Value @"
|
||||
name: $CREDENTIAL_NAME
|
||||
credentials:
|
||||
- name: kubeconfig
|
||||
source:
|
||||
path: $Env:USERPROFILE\.kube\config
|
||||
"@ -Path $Env:USERPROFILE\.qliksense\credentials\$CREDENTIAL_NAME".yaml"
|
||||
```
|
||||
- credentials can also be created using the [porter](https://porter.sh) CLI *(the correct environmental variable need to have been set up as shown in [Porter CLI](#porter-cli) above)*
|
||||
- `porter cred generate <credential_name> --tag qlik/qliksense-cnab-bundle:v1.21.23-edge`, replace `<credential_name>` with a name of your choosing.
|
||||
- Select `file path` and specify full path to a kube config file ex. _Bash_: `/home/user/.kube/config` or _PowerShell_ `C:\Users\user\.kube\config`
|
||||
|
||||
### Qlik Sense version and image list
|
||||
|
||||
It is possible verify the version of the [qliksense-k8s](https://github.com/qlik-oss/qliksense-k8s) repository bundled into the `qlik/qliksense-cnab-bundle` image and retreive the list of images included in that release. (This operation can take a minute or so)<https://github.com/qlik-oss/kustomize/issues/13> as the entire manifests needs to be rendered:
|
||||
|
||||
- `qliksense about --tag qlik/qliksense-cnab-bundle:<qliksense_version>`
|
||||
|
||||
### Optional: Pulling images in manifest locally, "air gap"
|
||||
|
||||
If the `dockerRegistry` parameter is specified as the private docker registry to be used by the kubernetes cluster hosting qliksense, it is possible to pull images to the local docker engine for an eventual push during a `qliksense install` or `qliksense upgrade`
|
||||
|
||||
- `qliksense pull --tag qlik/qliksense-cnab-bundle:<qliksense_version>`
|
||||
|
||||
### Running Preflight checks
|
||||
|
||||
You can run preflight checks to ensure that the cluster is in a healthy state before installing Qliksense.
|
||||
|
||||
- `qliksense preflight -c <credential_name> --tag qlik/qliksense-cnab-bundle:<qliksense_version>`
|
||||
|
||||
The above command runs the checks in the default namespace. If you want to specify the namespace to run preflight checks on:
|
||||
|
||||
- `qliksense preflight --param namespace=<value> -c <credential_name> --tag qlik/qliksense-cnab-bundle:<qliksense_version>`
|
||||
|
||||
### Installation
|
||||
|
||||
- Install the bundle : `qliksense install --param acceptEULA=yes -c <credential_name> --tag qlik/qliksense-cnab-bundle:<qliksense_version>`
|
||||
|
||||
#### Supported Parameters during install
|
||||
|
||||
| Name | Descriptions | Default |
|
||||
| ------------- |:-------------:| -----:|
|
||||
| profile | select a profile i.e docker-desktop, aws-eks, gke | docker-desktop |
|
||||
| acceptEULA | yes | has to be yes |
|
||||
| namespace | any kubernetes namespace | default |
|
||||
| dockerRegistry | A private docker image regitry for pods | not specified (public) |
|
||||
| rotateKeys | regenerate application PKI keys on upgrade (yes/no) | no |
|
||||
| mongoDbUri | the mongodb URI to use | URI of development mongodb |
|
||||
| scName | storage class name | none |
|
||||
|
||||
#### How To Add Identity Provider Config
|
||||
|
||||
Since idp configs are usually multiline configs it is not conventional to pass to porter during install as a `param`. Rather put the configs in a file and refer to that file during `porter install` command. For example to add `keycloak` IDP create file named `idpconfigs.txt` and put
|
||||
|
||||
```shell
|
||||
idpConfigs=[{"discoveryUrl":"http://keycloak-insecure:8089/keycloak/realms/master22/.well-known/openid-configuration","clientId":"edge-auth","clientSecret":"e15b5075-9399-4b20-a95e-023022aa4aed","realm":"master","hostname":"elastic.example","claimsMapping":{"sub":["sub","client_id"],"name":["name","given_name","family_name","preferred_username"]}}]
|
||||
|
||||
```
|
||||
|
||||
Then pass that file during install command like this
|
||||
|
||||
```shell
|
||||
qliksense install --param acceptEULA=yes -c <credential_name> --param-file idpconfigs.txt --tag qlik/qliksense-cnab-bundle:<qliksense_version>`
|
||||
```
|
||||
|
||||
## Packaging a Custom bundle
|
||||
|
||||
If files need to be added to the [qliksense-k8s repository](https://github.com/qlik-oss/qliksense-k8s) in order to perform advanced configuration outside the scope of the what the operator provides, a custom bundle needs to be built.
|
||||
|
||||
Packaging of Qlik Sense on Kubernetes is done through a [Porter](https://porter.sh/) definition in the [Qlik Sense on Kubernetes configuration repository](https://github.com/qlik-oss/qliksense-k8s/blob/master/porter.yaml), the resulting bundle published on DockerHub as a [Cloud Natvie Application Bundle](https://cnab.io/) called [qliksense-cnab-bundle](https://hub.docker.com/r/qlik/qliksense-cnab-bundle)
|
||||
|
||||
To start, clone [qliksense-k8s](https://github.com/qlik-oss/qliksense-k8s) and modify the repo as desired, once finished make sure to be in the `qliksense-k8s` directory from which the porter bundle can be built:
|
||||
|
||||
```shell
|
||||
git clone git@github.com:qlik-oss/qliksense-k8s.git
|
||||
|
||||
cd qliksense-k8s
|
||||
|
||||
qliksense build
|
||||
```
|
||||
|
||||
Once built, all of the `porter` command that were used with `--tag` can be now be used without this flag provided that porter is executed with the `qliksense-k8s` directory. `porter` will automatically use the qliksense-k8s (and the porter.yaml) in the current directory.
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type aboutCommandOptions struct {
|
||||
Profile string
|
||||
}
|
||||
|
||||
func about(q *qliksense.Qliksense) *cobra.Command {
|
||||
opts := &aboutCommandOptions{}
|
||||
|
||||
c := &cobra.Command{
|
||||
Use: "about ref",
|
||||
Short: "Displays information pertaining to Qliksense on Kubernetes",
|
||||
Long: "Gives the version of QLik Sense on Kubernetes and versions of images.",
|
||||
Example: `
|
||||
qliksense about 1.0.0
|
||||
- display default profile (docker-desktop) for Git ref 1.0.0 in the qliksense-k8s repo
|
||||
qliksense about 1.0.0 --profile=docker-desktop
|
||||
- specifying profile
|
||||
qliksense about
|
||||
qliksense about --profile=test
|
||||
- if no Git ref is provided, then get version information from the configuration on disk:
|
||||
- if user's current directory has a subdirectory "manifests/${profile}",
|
||||
then get version information from that
|
||||
- if using other supported commands the user has built a CR in ~/.qliksense,
|
||||
then get version information based on the path derived like so:
|
||||
- ${spec.manifestsRoot}/${spec.profile} # if no profile flag provided
|
||||
- ${spec.manifestsRoot}/${profile} # if profile is provided using the --profile command flag
|
||||
- if no config found on disk in locations described above,
|
||||
then get version information based on the default profile in the qliksense-k8s repo master
|
||||
`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if gitRef, err := getAboutCommandGitRef(args); err != nil {
|
||||
return err
|
||||
} else if vout, err := q.About(gitRef, opts.Profile); err != nil {
|
||||
return err
|
||||
} else if out, err := yaml.Marshal(vout); err != nil {
|
||||
return err
|
||||
} else if _, err := fmt.Println(string(out)); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
f := c.Flags()
|
||||
f.StringVar(&opts.Profile, "profile", "", "Configuration profile")
|
||||
return c
|
||||
}
|
||||
|
||||
func getAboutCommandGitRef(args []string) (string, error) {
|
||||
if len(args) > 1 {
|
||||
return "", errors.New("too many arguments, only 1 expected")
|
||||
} else if len(args) == 1 {
|
||||
return strings.TrimSpace(args[0]), nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
423
cmd/qliksense/aliases.go
Normal file
423
cmd/qliksense/aliases.go
Normal file
@@ -0,0 +1,423 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func buildAliasCommands(porterCmd *cobra.Command, q *qliksense.Qliksense) []*cobra.Command {
|
||||
|
||||
return []*cobra.Command{
|
||||
buildBuildAlias(porterCmd, q),
|
||||
buildInstallAlias(porterCmd, q),
|
||||
buildUpgradeAlias(porterCmd, q),
|
||||
buildAboutAlias(porterCmd, q),
|
||||
buildPreflightAlias(porterCmd, q),
|
||||
buildUninstallAlias(porterCmd, q),
|
||||
}
|
||||
}
|
||||
|
||||
func buildBuildAlias(porterCmd *cobra.Command, q *qliksense.Qliksense) *cobra.Command {
|
||||
var (
|
||||
c *cobra.Command
|
||||
)
|
||||
c = &cobra.Command{
|
||||
Use: "build",
|
||||
Short: "Build a bundle",
|
||||
Long: "Builds the bundle in the current directory by generating a Dockerfile and a CNAB bundle.json, and then building the invocation image.",
|
||||
DisableFlagParsing: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return porterCmd.RunE(porterCmd, append([]string{"build"}, args...))
|
||||
},
|
||||
Annotations: map[string]string{
|
||||
"group": "alias",
|
||||
},
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
type paramOptions struct {
|
||||
aboutOptions
|
||||
Params []string
|
||||
ParamFiles []string
|
||||
Name string
|
||||
InsecureRegistry bool
|
||||
|
||||
// CredentialIdentifiers is a list of credential names or paths to make available to the bundle.
|
||||
CredentialIdentifiers []string
|
||||
Driver string
|
||||
Force bool
|
||||
Insecure bool
|
||||
}
|
||||
|
||||
func buildInstallAlias(porterCmd *cobra.Command, q *qliksense.Qliksense) *cobra.Command {
|
||||
var (
|
||||
c *cobra.Command
|
||||
opts *paramOptions
|
||||
registry *string
|
||||
)
|
||||
|
||||
opts = ¶mOptions{}
|
||||
|
||||
c = &cobra.Command{
|
||||
Use: "install [INSTANCE]",
|
||||
Short: "Install qliksense",
|
||||
Long: `Install a new instance of a bundle.
|
||||
|
||||
The first argument is the bundle instance name to create for the installation. This defaults to the name of the bundle.
|
||||
|
||||
Porter uses the Docker driver as the default runtime for executing a bundle's invocation image, but an alternate driver may be supplied via '--driver/-d'.
|
||||
For example, the 'debug' driver may be specified, which simply logs the info given to it and then exits.`,
|
||||
Example: ` qliksense install
|
||||
qliksense install --version v1.0.0
|
||||
qliksense install --insecure
|
||||
qliksense install qliksense --file qliksense/bundle.json
|
||||
qliksense install --param-file base-values.txt --param-file dev-values.txt --param test-mode=true --param header-color=blue
|
||||
qliksense install --cred kubernetes
|
||||
qliksense install --driver debug
|
||||
qliksense install MyAppFromTag --tag qlik/qliksense-cnab-bundle:v1.0.0
|
||||
`,
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
checkMinVersion(opts.Tag, q)
|
||||
},
|
||||
//DisableFlagParsing: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
// Push images here.
|
||||
// TODO: Need to get the private reg from params
|
||||
args = append(os.Args[2:], opts.getTagValue(args)...)
|
||||
if registry = opts.findKey("dockerRegistry"); registry != nil {
|
||||
if len(*registry) > 0 {
|
||||
q.TagAndPushImages(*registry)
|
||||
}
|
||||
}
|
||||
return porterCmd.RunE(porterCmd, append([]string{"install"}, args...))
|
||||
},
|
||||
Annotations: map[string]string{
|
||||
"group": "alias",
|
||||
},
|
||||
}
|
||||
f := c.Flags()
|
||||
f.StringVarP(&opts.Version, "version", "v", "latest",
|
||||
"Version of Qlik Sense to install")
|
||||
f.BoolVar(&opts.Insecure, "insecure", true,
|
||||
"Allow working with untrusted bundles")
|
||||
f.StringVarP(&opts.File, "file", "f", "",
|
||||
"Path to the porter manifest file. Defaults to the bundle in the current directory.")
|
||||
f.StringVar(&opts.CNABFile, "cnab-file", "",
|
||||
"Path to the CNAB bundle.json file.")
|
||||
f.StringSliceVar(&opts.ParamFiles, "param-file", nil,
|
||||
"Path to a parameters definition file for the bundle, each line in the form of NAME=VALUE. May be specified multiple times.")
|
||||
f.StringSliceVar(&opts.Params, "param", nil,
|
||||
"Define an individual parameter in the form NAME=VALUE. Overrides parameters set with the same name using --param-file. May be specified multiple times.")
|
||||
f.StringSliceVarP(&opts.CredentialIdentifiers, "cred", "c", nil,
|
||||
"Credential to use when installing the bundle. May be either a named set of credentials or a filepath, and specified multiple times.")
|
||||
f.StringVarP(&opts.Driver, "driver", "d", "docker",
|
||||
"Specify a driver to use. Allowed values: docker, debug")
|
||||
f.StringVarP(&opts.Tag, "tag", "t", "",
|
||||
"Use a bundle in an OCI registry specified by the given tag")
|
||||
f.BoolVar(&opts.InsecureRegistry, "insecure-registry", false,
|
||||
"Don't require TLS for the registry")
|
||||
f.BoolVar(&opts.Force, "force", false,
|
||||
"Force a fresh pull of the bundle and all dependencies")
|
||||
return c
|
||||
}
|
||||
|
||||
func buildUpgradeAlias(porterCmd *cobra.Command, q *qliksense.Qliksense) *cobra.Command {
|
||||
var (
|
||||
c *cobra.Command
|
||||
opts *paramOptions
|
||||
registry *string
|
||||
)
|
||||
|
||||
opts = ¶mOptions{}
|
||||
|
||||
c = &cobra.Command{
|
||||
Use: "upgrade [INSTANCE]",
|
||||
Short: "Upgrade qliksense",
|
||||
Long: `Upgrade to a new instance of a bundle.
|
||||
|
||||
The first argument is the bundle instance name to upgrade for the installation. This defaults to the name of the bundle.
|
||||
|
||||
Porter uses the Docker driver as the default runtime for executing a bundle's invocation image, but an alternate driver may be supplied via '--driver/-d'.
|
||||
For example, the 'debug' driver may be specified, which simply logs the info given to it and then exits.`,
|
||||
Example: ` qliksense upgrade
|
||||
qliksense upgrade --version v1.0.0
|
||||
qliksense upgrade --insecure
|
||||
qliksense upgrade qliksense --file qliksense/bundle.json
|
||||
qliksense upgrade --param-file base-values.txt --param-file dev-values.txt --param test-mode=true --param header-color=blue
|
||||
qliksense upgrade --cred kubernetes
|
||||
qliksense upgrade --driver debug
|
||||
qliksense upgrade MyAppFromTag --tag qlik/qliksense-cnab-bundle:v1.0.0
|
||||
`,
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
checkMinVersion(opts.Tag, q)
|
||||
},
|
||||
//DisableFlagParsing: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
// Push images here.
|
||||
// TODO: Need to get the private reg from params
|
||||
args = append(os.Args[2:], opts.getTagValue(args)...)
|
||||
if registry = opts.findKey("dockerRegistry"); registry != nil {
|
||||
if len(*registry) > 0 {
|
||||
q.TagAndPushImages(*registry)
|
||||
}
|
||||
}
|
||||
return porterCmd.RunE(porterCmd, append([]string{"upgrade"}, args...))
|
||||
},
|
||||
Annotations: map[string]string{
|
||||
"group": "alias",
|
||||
},
|
||||
}
|
||||
f := c.Flags()
|
||||
f.StringVarP(&opts.Version, "version", "v", "latest",
|
||||
"Version of Qlik Sense to upgrade to")
|
||||
f.BoolVar(&opts.Insecure, "insecure", true,
|
||||
"Allow working with untrusted bundles")
|
||||
f.StringVarP(&opts.File, "file", "f", "",
|
||||
"Path to the porter manifest file. Defaults to the bundle in the current directory.")
|
||||
f.StringVar(&opts.CNABFile, "cnab-file", "",
|
||||
"Path to the CNAB bundle.json file.")
|
||||
f.StringSliceVar(&opts.ParamFiles, "param-file", nil,
|
||||
"Path to a parameters definition file for the bundle, each line in the form of NAME=VALUE. May be specified multiple times.")
|
||||
f.StringSliceVar(&opts.Params, "param", nil,
|
||||
"Define an individual parameter in the form NAME=VALUE. Overrides parameters set with the same name using --param-file. May be specified multiple times.")
|
||||
f.StringSliceVarP(&opts.CredentialIdentifiers, "cred", "c", nil,
|
||||
"Credential to use when installing the bundle. May be either a named set of credentials or a filepath, and specified multiple times.")
|
||||
f.StringVarP(&opts.Driver, "driver", "d", "docker",
|
||||
"Specify a driver to use. Allowed values: docker, debug")
|
||||
f.StringVarP(&opts.Tag, "tag", "t", "",
|
||||
"Use a bundle in an OCI registry specified by the given tag")
|
||||
f.BoolVar(&opts.InsecureRegistry, "insecure-registry", false,
|
||||
"Don't require TLS for the registry")
|
||||
f.BoolVar(&opts.Force, "force", false,
|
||||
"Force a fresh pull of the bundle and all dependencies")
|
||||
return c
|
||||
}
|
||||
|
||||
func buildUninstallAlias(porterCmd *cobra.Command, q *qliksense.Qliksense) *cobra.Command {
|
||||
var (
|
||||
c *cobra.Command
|
||||
opts *paramOptions
|
||||
)
|
||||
|
||||
opts = ¶mOptions{}
|
||||
|
||||
c = &cobra.Command{
|
||||
Use: "uninstall [INSTANCE]",
|
||||
Short: "Uninstall a bundle instance",
|
||||
Long: `Uninstall a bundle instance
|
||||
The first argument is the bundle instance name to uninstall. This defaults to the name of the bundle.
|
||||
Porter uses the Docker driver as the default runtime for executing a bundle's invocation image, but an alternate driver may be supplied via '--driver/-d'.
|
||||
For example, the 'debug' driver may be specified, which simply logs the info given to it and then exits.`,
|
||||
Example: ` qliksense uninstall
|
||||
qliksense uninstall --insecure
|
||||
qliksense uninstall MyAppInDev --file myapp/bundle.json
|
||||
qliksense uninstall --param-file base-values.txt --param-file dev-values.txt --param test-mode=true --param header-color=blue
|
||||
qliksense uninstall --cred azure --cred kubernetes
|
||||
qliksense uninstall --driver debug
|
||||
qliksense uninstall MyAppFromTag --tag deislabs/porter-kube-bundle:v1.0
|
||||
`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return porterCmd.RunE(porterCmd, append([]string{"uninstall"}, os.Args[2:]...))
|
||||
},
|
||||
Annotations: map[string]string{
|
||||
"group": "alias",
|
||||
},
|
||||
}
|
||||
|
||||
f := c.Flags()
|
||||
f.BoolVar(&opts.Insecure, "insecure", true,
|
||||
"Allow working with untrusted bundles")
|
||||
f.StringVarP(&opts.File, "file", "f", "",
|
||||
"Path to the porter manifest file. Defaults to the bundle in the current directory. Optional unless a newer version of the bundle should be used to uninstall the bundle.")
|
||||
f.StringVar(&opts.CNABFile, "cnab-file", "",
|
||||
"Path to the CNAB bundle.json file.")
|
||||
f.StringSliceVar(&opts.ParamFiles, "param-file", nil,
|
||||
"Path to a parameters definition file for the bundle, each line in the form of NAME=VALUE. May be specified multiple times.")
|
||||
f.StringSliceVar(&opts.Params, "param", nil,
|
||||
"Define an individual parameter in the form NAME=VALUE. Overrides parameters set with the same name using --param-file. May be specified multiple times.")
|
||||
f.StringSliceVarP(&opts.CredentialIdentifiers, "cred", "c", nil,
|
||||
"Credential to use when uninstalling the bundle. May be either a named set of credentials or a filepath, and specified multiple times.")
|
||||
f.StringVarP(&opts.Driver, "driver", "d", "docker",
|
||||
"Specify a driver to use. Allowed values: docker, debug")
|
||||
f.StringVarP(&opts.Tag, "tag", "t", "",
|
||||
"Use a bundle in an OCI registry specified by the given tag")
|
||||
f.BoolVar(&opts.InsecureRegistry, "insecure-registry", false,
|
||||
"Don't require TLS for the registry")
|
||||
f.BoolVar(&opts.Force, "force", false,
|
||||
"Force a fresh pull of the bundle and all dependencies")
|
||||
|
||||
return c
|
||||
}
|
||||
func (o *aboutOptions) getTagDefaults(args []string) []string {
|
||||
args = append(args, o.getTagValue(args)...)
|
||||
return args
|
||||
}
|
||||
|
||||
func (o *aboutOptions) getTagValue(args []string) []string {
|
||||
tagArr := []string{}
|
||||
if len(o.Tag) > 1 {
|
||||
tagArr = []string{"--tag", o.Tag}
|
||||
}
|
||||
if len(o.Tag) <= 0 && len(o.File) <= 0 && len(o.CNABFile) <= 0 {
|
||||
if _, err := os.Stat("porter.yaml"); err != nil {
|
||||
tagArr = []string{"--tag", "qlik/qliksense-cnab-bundle:" + o.Version}
|
||||
}
|
||||
}
|
||||
return tagArr
|
||||
}
|
||||
|
||||
type aboutOptions struct {
|
||||
Version string
|
||||
Tag string
|
||||
File string
|
||||
CNABFile string
|
||||
}
|
||||
|
||||
func buildAboutAlias(porterCmd *cobra.Command, q *qliksense.Qliksense) *cobra.Command {
|
||||
var (
|
||||
c *cobra.Command
|
||||
opts *aboutOptions
|
||||
)
|
||||
|
||||
opts = &aboutOptions{}
|
||||
|
||||
c = &cobra.Command{
|
||||
Use: "about",
|
||||
Short: "About Qlik Sense",
|
||||
Long: "Gives the verion of QLik Sense on Kuberntetes and versions of images.",
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
checkMinVersion(opts.Tag, q)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
args = opts.getTagDefaults(args)
|
||||
return porterCmd.RunE(porterCmd, append([]string{"invoke", "--action", "about"}, args...))
|
||||
},
|
||||
Annotations: map[string]string{
|
||||
"group": "alias",
|
||||
},
|
||||
}
|
||||
f := c.Flags()
|
||||
f.StringVarP(&opts.Version, "version", "v", "latest",
|
||||
"Version of Qlik Sense to install")
|
||||
f.StringVarP(&opts.Tag, "tag", "t", "",
|
||||
"Use a bundle in an OCI registry specified by the given tag")
|
||||
f.StringVarP(&opts.File, "file", "f", "",
|
||||
"Path to the porter manifest file. Defaults to the bundle in the current directory.")
|
||||
f.StringVar(&opts.CNABFile, "cnab-file", "",
|
||||
"Path to the CNAB bundle.json file.")
|
||||
return c
|
||||
}
|
||||
|
||||
func buildPreflightAlias(porterCmd *cobra.Command, q *qliksense.Qliksense) *cobra.Command {
|
||||
var (
|
||||
c *cobra.Command
|
||||
opts *paramOptions
|
||||
)
|
||||
|
||||
opts = ¶mOptions{}
|
||||
|
||||
c = &cobra.Command{
|
||||
Use: "preflight",
|
||||
Short: "Preflight Checks",
|
||||
Long: "Perform Preflight Checks",
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
checkMinVersion(opts.Tag, q)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
args = append(os.Args[1:], opts.getTagDefaults(args)...)
|
||||
return porterCmd.RunE(porterCmd, append([]string{"invoke", "--action", "preflight"}, args...))
|
||||
},
|
||||
Annotations: map[string]string{
|
||||
"group": "alias",
|
||||
},
|
||||
}
|
||||
f := c.Flags()
|
||||
f.StringSliceVar(&opts.Params, "param", nil,
|
||||
"Define an individual parameter in the form NAME=VALUE. Overrides parameters set with the same name using --param-file. May be specified multiple times.")
|
||||
f.StringSliceVar(&opts.ParamFiles, "param-file", nil,
|
||||
"Path to a parameters definition file for the bundle, each line in the form of NAME=VALUE. May be specified multiple times.")
|
||||
f.StringVarP(&opts.Tag, "tag", "t", "",
|
||||
"Use a bundle in an OCI registry specified by the given tag")
|
||||
f.StringVarP(&opts.Version, "version", "v", "latest",
|
||||
"Version of Qlik Sense to install")
|
||||
f.StringSliceVarP(&opts.CredentialIdentifiers, "cred", "c", nil,
|
||||
"Credential to use when installing the bundle. May be either a named set of credentials or a filepath, and specified multiple times.")
|
||||
return c
|
||||
}
|
||||
|
||||
func (o *paramOptions) findKey(param string) *string {
|
||||
var (
|
||||
value *string
|
||||
)
|
||||
if value = o.findParams(param); value != nil {
|
||||
return value
|
||||
}
|
||||
|
||||
if value = o.findParamFiles(param); value != nil {
|
||||
return value
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// parsedParams parses the variable assignments in Params.
|
||||
func (o *paramOptions) findParams(param string) *string {
|
||||
return o.findVariableKey(param, o.Params)
|
||||
}
|
||||
|
||||
// parseParamFiles parses the variable assignments in ParamFiles.
|
||||
func (o *paramOptions) findParamFiles(param string) *string {
|
||||
var (
|
||||
path string
|
||||
retStr *string
|
||||
)
|
||||
|
||||
for _, path = range o.ParamFiles {
|
||||
retStr = o.findParamFile(param, path)
|
||||
}
|
||||
|
||||
return retStr
|
||||
}
|
||||
|
||||
func (o *paramOptions) findParamFile(param string, path string) *string {
|
||||
var (
|
||||
f *os.File
|
||||
err error
|
||||
scanner *bufio.Scanner
|
||||
lines []string
|
||||
retStr *string
|
||||
)
|
||||
if f, err = os.Open(path); err == nil {
|
||||
defer f.Close()
|
||||
|
||||
scanner = bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
|
||||
retStr = o.findVariableKey(param, lines)
|
||||
}
|
||||
return retStr
|
||||
}
|
||||
|
||||
func (o *paramOptions) findVariableKey(param string, params []string) *string {
|
||||
var (
|
||||
variable, value string
|
||||
)
|
||||
for _, p := range params {
|
||||
parts := strings.SplitN(p, "=", 2)
|
||||
if len(parts) >= 2 {
|
||||
variable = strings.TrimSpace(parts[0])
|
||||
if variable == param {
|
||||
value = strings.TrimSpace(parts[1])
|
||||
return &value
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func applyCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
opts := &qliksense.InstallCommandOptions{}
|
||||
filePath := ""
|
||||
keepPatchFiles := false
|
||||
c := &cobra.Command{
|
||||
Use: "apply",
|
||||
Short: "install qliksense based on provided cr file",
|
||||
Long: `install qliksense based on provided cr file`,
|
||||
Example: `qliksense apply -f file_name or cat cr_file | qliksense apply -f -`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runLoadOrApplyCommandE(cmd, func(reader io.Reader) error {
|
||||
return q.ApplyCRFromReader(reader, opts, keepPatchFiles, true)
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
f := c.Flags()
|
||||
f.StringVarP(&filePath, "file", "f", "", "Install from a CR file")
|
||||
c.MarkFlagRequired("file")
|
||||
f.StringVarP(&opts.StorageClass, "storageClass", "s", "", "Storage class for qliksense")
|
||||
f.StringVarP(&opts.MongoDbUri, "mongoDbUri", "m", "", "mongoDbUri for qliksense (i.e. mongodb://qlik-default-mongodb:27017/qliksense?ssl=false)")
|
||||
f.StringVarP(&opts.RotateKeys, "rotateKeys", "r", "", "Rotate JWT keys for qliksense (yes:rotate keys/ no:use exising keys from cluster/ None: use default EJSON_KEY from env")
|
||||
f.BoolVar(&keepPatchFiles, keepPatchFilesFlagName, keepPatchFiles, keepPatchFilesFlagUsage)
|
||||
|
||||
eulaPreRunHooks.addValidator(c.Name(), loadOrApplyCommandEulaPreRunHook)
|
||||
|
||||
return c
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func configCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var configCmd = &cobra.Command{
|
||||
Use: "config",
|
||||
Short: "do operations on/around CR",
|
||||
Long: `do operations on/around CR`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.ConfigViewCR()
|
||||
},
|
||||
}
|
||||
return configCmd
|
||||
}
|
||||
|
||||
func configApplyCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
c := &cobra.Command{
|
||||
Use: "apply",
|
||||
Short: "generate the patches and apply manifests to k8s",
|
||||
Long: `generate patches based on CR and apply manifests to k8s`,
|
||||
Example: `qliksense config apply`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.ConfigApplyQK8s()
|
||||
},
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func configViewCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
c := &cobra.Command{
|
||||
Use: "view",
|
||||
Short: "view the qliksense operator CR",
|
||||
Long: `display the operator CR, that has been created for the current context`,
|
||||
Example: `qliksense config view`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.ConfigViewCR()
|
||||
},
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func configEditCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
c := &cobra.Command{
|
||||
Use: "edit [context-name]",
|
||||
Short: "Edit the context cr",
|
||||
Long: `edit the context cr. if no context name provided default context will be edited
|
||||
It will open the vim editor unless KUBE_EDITOR is defined`,
|
||||
Example: `qliksense config edit [context-name]`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 1 {
|
||||
return q.EditCR(args[0])
|
||||
}
|
||||
return q.EditCR("")
|
||||
},
|
||||
}
|
||||
return c
|
||||
}
|
||||
@@ -1,195 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func setContextConfigCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var (
|
||||
cmd *cobra.Command
|
||||
)
|
||||
|
||||
cmd = &cobra.Command{
|
||||
Use: "set-context",
|
||||
Short: "Sets the context in which the Kubernetes cluster and resources live in",
|
||||
Example: `
|
||||
qliksense config set-context <context_name>
|
||||
- The above configuration will be displayed in the CR
|
||||
`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.SetContextConfig(args)
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
func listContextConfigCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var (
|
||||
cmd *cobra.Command
|
||||
)
|
||||
|
||||
cmd = &cobra.Command{
|
||||
Use: "list-contexts",
|
||||
Short: "retrieves the contexts and lists them",
|
||||
Example: `qliksense config list-contexts`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.ListContextConfigs()
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
func setOtherConfigsCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var (
|
||||
cmd *cobra.Command
|
||||
)
|
||||
|
||||
cmd = &cobra.Command{
|
||||
Use: "set",
|
||||
Short: "configure a key value pair into the current context",
|
||||
Example: `
|
||||
qliksense config set <key>=<value>
|
||||
- The above configuration will be displayed in the CR
|
||||
`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.SetOtherConfigs(args)
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
func setConfigsCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var (
|
||||
cmd *cobra.Command
|
||||
)
|
||||
|
||||
cmd = &cobra.Command{
|
||||
Use: "set-configs",
|
||||
Short: "set configurations into the qliksense context as key-value pairs",
|
||||
Example: `
|
||||
qliksense config set-configs <service_name>.<attribute>="<value>"
|
||||
- The above configuration will be displayed in the CR
|
||||
`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.SetConfigs(args)
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
func setSecretsCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var (
|
||||
cmd *cobra.Command
|
||||
secret bool
|
||||
)
|
||||
|
||||
cmd = &cobra.Command{
|
||||
Use: "set-secrets",
|
||||
Short: "set secrets configurations into the qliksense context as key-value pairs",
|
||||
Example: `
|
||||
qliksense config set-secrets <service_name>.<attribute>="<value>" --secret=true
|
||||
- Encrypt the secret value into a new Kubernetes secret resource
|
||||
- The secret resource is placed in the location: <qliksense_home>/<contexts>/<context_name>/secrets/<service_name>.yaml
|
||||
- Include it's key reference in the current context
|
||||
|
||||
qliksense config set-secrets <service_name>.<attribute>="<value>" --secret=false
|
||||
- Encrypt the secret value and display it in the current context
|
||||
- No secret resource is created
|
||||
- The above configuration will be displayed in the CR `,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.SetSecrets(args, secret)
|
||||
},
|
||||
}
|
||||
f := cmd.Flags()
|
||||
f.BoolVar(&secret, "secret", false, "Whether secrets should be encrypted as a Kubernetes Secret resource")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func deleteContextConfigCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var (
|
||||
cmd *cobra.Command
|
||||
)
|
||||
|
||||
cmd = &cobra.Command{
|
||||
Use: "delete-context",
|
||||
Short: "deletes a specific context locally (not in-cluster)",
|
||||
Example: `qliksense config delete-contexts <context_name>`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.DeleteContextConfig(args)
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
func setImageRegistryCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var (
|
||||
cmd *cobra.Command
|
||||
pushUsername string
|
||||
pushPassword string
|
||||
pullUsername string
|
||||
pullPassword string
|
||||
username string
|
||||
password string
|
||||
)
|
||||
|
||||
cmd = &cobra.Command{
|
||||
Use: "set-image-registry",
|
||||
Short: "set private image registry",
|
||||
Example: `
|
||||
qliksense config set-image-registry https://your.private.registry.example.com:5000 --push-username foo1 --push-password bar1 --pull-username foo2 --pull-password bar2
|
||||
qliksense config set-image-registry https://your.private.registry.example.com:5000 --username foo --password bar
|
||||
`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 1 {
|
||||
return errors.New("private docker image registry FQDN is required")
|
||||
}
|
||||
registry := args[0]
|
||||
|
||||
if username != "" {
|
||||
pullUsername = username
|
||||
pushUsername = username
|
||||
}
|
||||
if password != "" {
|
||||
pullPassword = password
|
||||
pushPassword = password
|
||||
}
|
||||
if (pullUsername != "" && pushUsername == "") || (pullUsername == "" && pushUsername != "") {
|
||||
return errors.New("if you specify pull credentials, you must specify push credentials as well and vise versa")
|
||||
}
|
||||
if (pullUsername == "" && pullPassword != "") || (pushUsername == "" && pushPassword != "") {
|
||||
return errors.New("if you specify passwords, you must specify usernames as well")
|
||||
}
|
||||
return q.SetImageRegistry(registry, pushUsername, pushPassword, pullUsername, pullPassword)
|
||||
},
|
||||
}
|
||||
f := cmd.Flags()
|
||||
f.StringVar(&pushUsername, "push-username", "", "Username used for pushing images")
|
||||
f.StringVar(&pushPassword, "push-password", "", "Password used for pushing images")
|
||||
f.StringVar(&pullUsername, "pull-username", "", "Username used for pulling images")
|
||||
f.StringVar(&pullPassword, "pull-password", "", "Password used for pulling images")
|
||||
f.StringVar(&username, "username", "", "Username used for both pushing and pulling images")
|
||||
f.StringVar(&password, "password", "", "Password used for both pushing and pulling images")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func cleanConfigRepoPatchesCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "clean-config-repo-patches",
|
||||
Short: "Clean config repo patch files",
|
||||
Example: "qliksense config clean-config-repo-patches",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
if err := q.DiscardAllUnstagedChangesFromGitRepo(qConfig); err != nil {
|
||||
return fmt.Errorf("error removing temporary changes to the config: %v\n", err)
|
||||
}
|
||||
fmt.Println("done")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var crdsCmd = &cobra.Command{
|
||||
Use: "crds",
|
||||
Short: "crds for qliksense and operators",
|
||||
Long: `crds for qliksense and operators`,
|
||||
}
|
||||
|
||||
func crdsViewCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
opts := &qliksense.CrdCommandOptions{}
|
||||
c := &cobra.Command{
|
||||
Use: "view",
|
||||
Short: "View CRDs for qliksense application. use view --all to see opearator crd as well ",
|
||||
Long: `View CRDs for qliksense application. use view --all to see opearator crd as well`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.ViewCrds(opts)
|
||||
},
|
||||
}
|
||||
f := c.Flags()
|
||||
f.BoolVarP(&opts.All, "all", "", false, "Include All CRDs")
|
||||
return c
|
||||
}
|
||||
|
||||
func crdsInstallCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
opts := &qliksense.CrdCommandOptions{}
|
||||
c := &cobra.Command{
|
||||
Use: "install",
|
||||
Short: "Install CRDs fro Qliksense application. Use install --all to include operator crd",
|
||||
Long: `Install CRDs fro Qliksense application. Use install --all to include operator crd`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.InstallCrds(opts)
|
||||
},
|
||||
}
|
||||
f := c.Flags()
|
||||
f.BoolVarP(&opts.All, "all", "", false, "Include All CRDs")
|
||||
return c
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/mattn/go-tty"
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type eulaPreRunHooksT struct {
|
||||
validators map[string]func(cmd *cobra.Command, q *qliksense.Qliksense) (bool, error)
|
||||
postValidationArtifacts map[string]interface{}
|
||||
}
|
||||
|
||||
func (e *eulaPreRunHooksT) addValidator(command string, validator func(cmd *cobra.Command, q *qliksense.Qliksense) (bool, error)) {
|
||||
e.validators[command] = validator
|
||||
}
|
||||
|
||||
func (e *eulaPreRunHooksT) getValidator(command string) func(cmd *cobra.Command, q *qliksense.Qliksense) (bool, error) {
|
||||
if validator, ok := e.validators[command]; ok {
|
||||
return validator
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *eulaPreRunHooksT) addPostValidationArtifact(artifactName string, artifact interface{}) {
|
||||
e.postValidationArtifacts[artifactName] = artifact
|
||||
}
|
||||
|
||||
func (e *eulaPreRunHooksT) getPostValidationArtifact(artifactName string) interface{} {
|
||||
if artifact, ok := e.postValidationArtifacts[artifactName]; ok {
|
||||
return artifact
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var eulaEnforced = os.Getenv("QLIKSENSE_EULA_ENFORCE") == "true"
|
||||
var eulaText = "Please read the end user license agreement at: https://www.qlik.com/us/legal/license-terms"
|
||||
var eulaPrompt = "Do you accept our EULA? (y/n): "
|
||||
var eulaErrorInstruction = `You must enter "y" to continue`
|
||||
var eulaPreRunHooks = eulaPreRunHooksT{
|
||||
validators: make(map[string]func(cmd *cobra.Command, q *qliksense.Qliksense) (bool, error)),
|
||||
postValidationArtifacts: make(map[string]interface{}),
|
||||
}
|
||||
|
||||
func commandAlwaysRequiresEulaAcceptance(commandName string) bool {
|
||||
return commandName == "install" || commandName == "upgrade" || commandName == "apply"
|
||||
}
|
||||
|
||||
func globalEulaPreRun(cmd *cobra.Command, q *qliksense.Qliksense) {
|
||||
if isEulaEnforced(cmd.Name()) {
|
||||
if strings.TrimSpace(strings.ToLower(cmd.Flag("acceptEULA").Value.String())) != "yes" {
|
||||
if eulaPreRunHook := eulaPreRunHooks.getValidator(cmd.Name()); eulaPreRunHook != nil {
|
||||
if eulaAccepted, err := eulaPreRunHook(cmd, q); err != nil {
|
||||
panic(err)
|
||||
} else if !eulaAccepted {
|
||||
doEnforceEula()
|
||||
}
|
||||
} else if qConfig, err := qapi.NewQConfigE(q.QliksenseHome); err != nil {
|
||||
doEnforceEula()
|
||||
} else if qcr, err := qConfig.GetCurrentCR(); err != nil || !qcr.IsEULA() {
|
||||
doEnforceEula()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func globalEulaPostRun(cmd *cobra.Command, q *qliksense.Qliksense) {
|
||||
if isEulaEnforced(cmd.Name()) {
|
||||
if err := q.SetEulaAccepted(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isEulaEnforced(commandName string) bool {
|
||||
return eulaEnforced || commandAlwaysRequiresEulaAcceptance(commandName)
|
||||
}
|
||||
|
||||
func doEnforceEula() {
|
||||
fmt.Println(eulaText)
|
||||
fmt.Print(eulaPrompt)
|
||||
answer := readRuneFromTty()
|
||||
if strings.ToLower(answer) != "y" {
|
||||
fmt.Println(eulaErrorInstruction)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func readRuneFromTty() string {
|
||||
t, err := tty.Open()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer t.Close()
|
||||
answer, err := t.ReadString()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return answer
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func fetchCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
c := &cobra.Command{
|
||||
Use: "fetch",
|
||||
Short: "fetch a release from qliksense-k8s repo",
|
||||
Long: `fetch a release from qliksense-k8s repo`,
|
||||
Example: `qliksense fetch <version>`,
|
||||
Args: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 1 {
|
||||
return errors.New("requires a version (i.e. v1.0.0)")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.FetchQK8s(args[0])
|
||||
},
|
||||
}
|
||||
return c
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const defaultVersionsLimit = 10
|
||||
|
||||
func getInstallableVersionsCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
opts := &qliksense.LsRemoteCmdOptions{
|
||||
IncludeBranches: false,
|
||||
Limit: defaultVersionsLimit,
|
||||
}
|
||||
c := &cobra.Command{
|
||||
Use: "get-versions",
|
||||
Short: "list remote/installable versions",
|
||||
Long: `list remote/installable versions`,
|
||||
Example: `qliksense get-versions`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.GetInstallableVersions(opts)
|
||||
},
|
||||
}
|
||||
|
||||
f := c.Flags()
|
||||
f.BoolVarP(&opts.IncludeBranches, "include-branches", "", opts.IncludeBranches, "Include branches")
|
||||
f.IntVarP(&opts.Limit, "limit", "", opts.Limit, "Maximum versions to list (starting with the highest)")
|
||||
return c
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func installCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
opts := &qliksense.InstallCommandOptions{}
|
||||
keepPatchFiles := false
|
||||
c := &cobra.Command{
|
||||
Use: "install",
|
||||
Short: "install a qliksense release",
|
||||
Long: `install a qliksense release`,
|
||||
Example: `qliksense install <version> #if no version provides, expect manifestsRoot is set somewhere in the file system`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
version := ""
|
||||
if len(args) != 0 {
|
||||
version = args[0]
|
||||
}
|
||||
return q.InstallQK8s(version, opts, keepPatchFiles)
|
||||
},
|
||||
}
|
||||
|
||||
f := c.Flags()
|
||||
f.StringVarP(&opts.StorageClass, "storageClass", "s", "", "Storage class for qliksense")
|
||||
f.StringVarP(&opts.MongoDbUri, "mongoDbUri", "m", "", "mongoDbUri for qliksense (i.e. mongodb://qlik-default-mongodb:27017/qliksense?ssl=false)")
|
||||
f.StringVarP(&opts.RotateKeys, "rotateKeys", "r", "", "Rotate JWT keys for qliksense (yes:rotate keys/ no:use exising keys from cluster/ None: use default EJSON_KEY from env")
|
||||
f.BoolVar(&keepPatchFiles, keepPatchFilesFlagName, keepPatchFiles, keepPatchFilesFlagUsage)
|
||||
|
||||
return c
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func loadCrFile(q *qliksense.Qliksense) *cobra.Command {
|
||||
filePath := ""
|
||||
overwriteExistingContext := false
|
||||
c := &cobra.Command{
|
||||
Use: "load",
|
||||
Short: "load a CR a file and create necessary structure for future use",
|
||||
Long: `load a CR a file and create necessary structure for future use`,
|
||||
Example: `qliksense load -f file_name or cat cr_file | qliksense load -f -`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runLoadOrApplyCommandE(cmd, func(reader io.Reader) error {
|
||||
return q.LoadCr(reader, overwriteExistingContext)
|
||||
})
|
||||
},
|
||||
}
|
||||
f := c.Flags()
|
||||
f.StringVarP(&filePath, "file", "f", "", "File to load CR from")
|
||||
c.MarkFlagRequired("file")
|
||||
f.BoolVarP(&overwriteExistingContext, "overwrite", "o", overwriteExistingContext, "Overwrite any existing contexts with the same name")
|
||||
|
||||
eulaPreRunHooks.addValidator(c.Name(), loadOrApplyCommandEulaPreRunHook)
|
||||
return c
|
||||
}
|
||||
|
||||
func getCrFileFromFlag(cmd *cobra.Command, flagName string) (*os.File, error) {
|
||||
filePath := cmd.Flag(flagName).Value.String()
|
||||
if filePath == "-" {
|
||||
if !isInputFromPipe() {
|
||||
return nil, errors.New("No input pipe present")
|
||||
}
|
||||
return os.Stdin, nil
|
||||
}
|
||||
file, e := os.Open(filePath)
|
||||
if e != nil {
|
||||
return nil, errors.Wrapf(e,
|
||||
"unable to read the file %s", filePath)
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func isInputFromPipe() bool {
|
||||
fileInfo, _ := os.Stdin.Stat()
|
||||
return fileInfo.Mode()&os.ModeCharDevice == 0
|
||||
}
|
||||
|
||||
func loadOrApplyCommandEulaPreRunHook(cmd *cobra.Command, q *qliksense.Qliksense) (bool, error) {
|
||||
file, err := getCrFileFromFlag(cmd, "file")
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
if crBytes, err := ioutil.ReadAll(file); err != nil {
|
||||
return false, err
|
||||
} else {
|
||||
eulaPreRunHooks.addPostValidationArtifact("CR", crBytes)
|
||||
return q.IsEulaAcceptedInCrFile(bytes.NewBuffer(crBytes))
|
||||
}
|
||||
}
|
||||
|
||||
func runLoadOrApplyCommandE(cmd *cobra.Command, callBack func(io.Reader) error) error {
|
||||
if crBytes := eulaPreRunHooks.getPostValidationArtifact("CR"); crBytes != nil {
|
||||
return callBack(bytes.NewBuffer(crBytes.([]byte)))
|
||||
} else {
|
||||
file, err := getCrFileFromFlag(cmd, "file")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
return callBack(file)
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var operatorCmd = &cobra.Command{
|
||||
Use: "operator",
|
||||
Short: "Configuration for operator",
|
||||
Long: `Configuration for operator`,
|
||||
}
|
||||
|
||||
/*
|
||||
func operatorViewCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
c := &cobra.Command{
|
||||
Use: "view",
|
||||
Short: "View CRD for operator",
|
||||
Long: `View CRD for operator`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.ViewOperator()
|
||||
},
|
||||
}
|
||||
return c
|
||||
}
|
||||
*/
|
||||
|
||||
func operatorCrdCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
c := &cobra.Command{
|
||||
Use: "crd",
|
||||
Short: "View CRD for operator",
|
||||
Long: `View CRD for operator`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.ViewOperator()
|
||||
},
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func operatorControllerCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
c := &cobra.Command{
|
||||
Use: "controller",
|
||||
Short: "View manifests for operator controller",
|
||||
Long: `View manifests for operator controller`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.ViewOperatorController()
|
||||
},
|
||||
}
|
||||
return c
|
||||
}
|
||||
31
cmd/qliksense/porter.go
Normal file
31
cmd/qliksense/porter.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func porter(q *qliksense.Qliksense) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "porter",
|
||||
Short: "Execute a porter command",
|
||||
DisableFlagParsing: true,
|
||||
RunE: func(cobCmd *cobra.Command, args []string) error {
|
||||
var (
|
||||
err error
|
||||
)
|
||||
if _, err = q.CallPorter(args,
|
||||
func(x string) (out *string) {
|
||||
out = new(string)
|
||||
*out = strings.ReplaceAll(x, "porter", "qliksense porter")
|
||||
fmt.Println(*out)
|
||||
return
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1,350 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/qlik-oss/sense-installer/pkg/preflight"
|
||||
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func preflightCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var preflightCmd = &cobra.Command{
|
||||
Use: "preflight",
|
||||
Short: "perform preflight checks on the cluster",
|
||||
Long: `perform preflight checks on the cluster`,
|
||||
Example: `qliksense preflight <preflight_check_to_run>`,
|
||||
}
|
||||
return preflightCmd
|
||||
}
|
||||
|
||||
func pfDnsCheckCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var preflightDnsCmd = &cobra.Command{
|
||||
Use: "dns",
|
||||
Short: "perform preflight dns check",
|
||||
Long: `perform preflight dns check to check DNS connectivity status in the cluster`,
|
||||
Example: `qliksense preflight dns`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
qp := &preflight.QliksensePreflight{Q: q}
|
||||
|
||||
// Preflight DNS check
|
||||
fmt.Printf("Preflight DNS check\n")
|
||||
fmt.Println("---------------------")
|
||||
namespace, kubeConfigContents, err := preflight.InitPreflight()
|
||||
if err != nil {
|
||||
fmt.Printf("Preflight DNS check FAILED\n")
|
||||
log.Fatal(err)
|
||||
}
|
||||
if namespace == "" {
|
||||
namespace = "default"
|
||||
}
|
||||
if err = qp.CheckDns(namespace, kubeConfigContents); err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Print("Preflight DNS check FAILED\n")
|
||||
log.Fatal()
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return preflightDnsCmd
|
||||
}
|
||||
|
||||
func pfK8sVersionCheckCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var preflightCheckK8sVersionCmd = &cobra.Command{
|
||||
Use: "kube-version",
|
||||
Short: "check kubernetes version",
|
||||
Long: `check minimum valid kubernetes version on the cluster`,
|
||||
Example: `qliksense preflight kube-version`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
qp := &preflight.QliksensePreflight{Q: q}
|
||||
|
||||
// Preflight Kubernetes minimum version check
|
||||
fmt.Printf("Preflight kubernetes minimum version check\n")
|
||||
fmt.Println("------------------------------------------")
|
||||
namespace, kubeConfigContents, err := preflight.InitPreflight()
|
||||
if err != nil {
|
||||
fmt.Printf("Preflight kubernetes minimum version check FAILED\n")
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err = qp.CheckK8sVersion(namespace, kubeConfigContents); err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Printf("Preflight kubernetes minimum version check FAILED\n")
|
||||
log.Fatal()
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return preflightCheckK8sVersionCmd
|
||||
}
|
||||
|
||||
func pfAllChecksCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var mongodbUrl string
|
||||
var preflightAllChecksCmd = &cobra.Command{
|
||||
Use: "all",
|
||||
Short: "perform all checks",
|
||||
Long: `perform all preflight checks on the target cluster`,
|
||||
Example: `qliksense preflight all`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
qp := &preflight.QliksensePreflight{Q: q}
|
||||
|
||||
// Preflight run all checks
|
||||
fmt.Printf("Running all preflight checks\n")
|
||||
namespace, kubeConfigContents, err := preflight.InitPreflight()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Printf("Running preflight check suite has FAILED...\n")
|
||||
log.Fatal()
|
||||
}
|
||||
if namespace == "" {
|
||||
namespace = "default"
|
||||
}
|
||||
qp.RunAllPreflightChecks(namespace, kubeConfigContents, mongodbUrl)
|
||||
return nil
|
||||
|
||||
},
|
||||
}
|
||||
f := preflightAllChecksCmd.Flags()
|
||||
f.StringVarP(&mongodbUrl, "mongodb-url", "", "", "mongodbUrl to try connecting to")
|
||||
return preflightAllChecksCmd
|
||||
}
|
||||
|
||||
func pfDeploymentCheckCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var pfDeploymentCheckCmd = &cobra.Command{
|
||||
Use: "deployment",
|
||||
Short: "perform preflight deploymwnt check",
|
||||
Long: `perform preflight deployment check to ensure that we can create deployments in the cluster`,
|
||||
Example: `qliksense preflight deployment`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
qp := &preflight.QliksensePreflight{Q: q}
|
||||
|
||||
// Preflight deployments check
|
||||
fmt.Printf("Preflight deployment check\n")
|
||||
fmt.Println("--------------------------")
|
||||
namespace, kubeConfigContents, err := preflight.InitPreflight()
|
||||
if err != nil {
|
||||
fmt.Printf("Preflight deployment check FAILED\n")
|
||||
log.Fatal(err)
|
||||
}
|
||||
if namespace == "" {
|
||||
namespace = "default"
|
||||
}
|
||||
if err = qp.CheckDeployment(namespace, kubeConfigContents); err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Print("Preflight deploy check FAILED\n")
|
||||
log.Fatal()
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return pfDeploymentCheckCmd
|
||||
}
|
||||
|
||||
func pfServiceCheckCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var pfServiceCheckCmd = &cobra.Command{
|
||||
Use: "service",
|
||||
Short: "perform preflight service check",
|
||||
Long: `perform preflight service check to ensure that we are able to create services in the cluster`,
|
||||
Example: `qliksense preflight service`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
qp := &preflight.QliksensePreflight{Q: q}
|
||||
|
||||
// Preflight service check
|
||||
fmt.Printf("Preflight service check\n")
|
||||
fmt.Println("-----------------------")
|
||||
namespace, kubeConfigContents, err := preflight.InitPreflight()
|
||||
if err != nil {
|
||||
fmt.Printf("Preflight service check FAILED\n")
|
||||
log.Fatal(err)
|
||||
}
|
||||
if namespace == "" {
|
||||
namespace = "default"
|
||||
}
|
||||
if err = qp.CheckService(namespace, kubeConfigContents); err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Print("Preflight service check FAILED\n")
|
||||
log.Fatal()
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return pfServiceCheckCmd
|
||||
}
|
||||
|
||||
func pfPodCheckCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var pfPodCheckCmd = &cobra.Command{
|
||||
Use: "pod",
|
||||
Short: "perform preflight pod check",
|
||||
Long: `perform preflight pod check to ensure we can create pods in the cluster`,
|
||||
Example: `qliksense preflight pod`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
qp := &preflight.QliksensePreflight{Q: q}
|
||||
|
||||
// Preflight pod check
|
||||
fmt.Printf("Preflight pod check\n")
|
||||
fmt.Println("--------------------")
|
||||
namespace, kubeConfigContents, err := preflight.InitPreflight()
|
||||
if err != nil {
|
||||
fmt.Printf("Preflight pod check FAILED\n")
|
||||
log.Fatal(err)
|
||||
}
|
||||
if namespace == "" {
|
||||
namespace = "default"
|
||||
}
|
||||
if err = qp.CheckPod(namespace, kubeConfigContents); err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Print("Preflight pod check FAILED\n")
|
||||
log.Fatal()
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return pfPodCheckCmd
|
||||
}
|
||||
|
||||
func pfCreateRoleCheckCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var preflightRoleCmd = &cobra.Command{
|
||||
Use: "role",
|
||||
Short: "preflight create role check",
|
||||
Long: `perform preflight role check to ensure we are able to create a role in the cluster`,
|
||||
Example: `qliksense preflight createRole`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
qp := &preflight.QliksensePreflight{Q: q}
|
||||
|
||||
// Preflight role check
|
||||
fmt.Printf("Preflight role check\n")
|
||||
fmt.Println("---------------------------")
|
||||
namespace, _, err := preflight.InitPreflight()
|
||||
if err != nil {
|
||||
fmt.Printf("Preflight role check FAILED\n")
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err = qp.CheckCreateRole(namespace); err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Print("Preflight role FAILED\n")
|
||||
log.Fatal()
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return preflightRoleCmd
|
||||
}
|
||||
|
||||
func pfCreateRoleBindingCheckCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var preflightRoleBindingCmd = &cobra.Command{
|
||||
Use: "rolebinding",
|
||||
Short: "preflight create rolebinding check",
|
||||
Long: `perform preflight rolebinding check to ensure we are able to create a rolebinding in the cluster`,
|
||||
Example: `qliksense preflight rolebinding`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
qp := &preflight.QliksensePreflight{Q: q}
|
||||
|
||||
// Preflight createRoleBinding check
|
||||
fmt.Printf("Preflight rolebinding check\n")
|
||||
fmt.Println("---------------------------")
|
||||
namespace, _, err := preflight.InitPreflight()
|
||||
if err != nil {
|
||||
fmt.Printf("Preflight rolebinding check FAILED\n")
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err = qp.CheckCreateRoleBinding(namespace); err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Print("Preflight rolebinding check FAILED\n")
|
||||
log.Fatal()
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return preflightRoleBindingCmd
|
||||
}
|
||||
|
||||
func pfCreateServiceAccountCheckCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var preflightServiceAccountCmd = &cobra.Command{
|
||||
Use: "serviceaccount",
|
||||
Short: "preflight create ServiceAccount check",
|
||||
Long: `perform preflight serviceaccount check to ensure we are able to create a service account in the cluster`,
|
||||
Example: `qliksense preflight serviceaccount`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
qp := &preflight.QliksensePreflight{Q: q}
|
||||
|
||||
// Preflight createServiceAccount check
|
||||
fmt.Printf("Preflight ServiceAccount check\n")
|
||||
fmt.Println("-------------------------------------")
|
||||
namespace, _, err := preflight.InitPreflight()
|
||||
if err != nil {
|
||||
fmt.Printf("Preflight serviceaccount check FAILED\n")
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err = qp.CheckCreateServiceAccount(namespace); err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Print("Preflight serviceaccount check FAILED\n")
|
||||
log.Fatal()
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return preflightServiceAccountCmd
|
||||
}
|
||||
|
||||
func pfCreateAuthCheckCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var preflightCreateAuthCmd = &cobra.Command{
|
||||
Use: "authcheck",
|
||||
Short: "preflight authcheck",
|
||||
Long: `perform preflight authcheck that combines the role, rolebinding and serviceaccount checks`,
|
||||
Example: `qliksense preflight authcheck`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
qp := &preflight.QliksensePreflight{Q: q}
|
||||
|
||||
// Preflight authcheck
|
||||
fmt.Printf("Preflight authcheck\n")
|
||||
fmt.Println("------------------------")
|
||||
namespace, kubeConfigContents, err := preflight.InitPreflight()
|
||||
if err != nil {
|
||||
fmt.Printf("Preflight authcheck FAILED\n")
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err = qp.CheckCreateRB(namespace, kubeConfigContents); err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Print("Preflight authcheck FAILED\n")
|
||||
log.Fatal()
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return preflightCreateAuthCmd
|
||||
}
|
||||
|
||||
func pfMongoCheckCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
var mongodbUrl string
|
||||
var preflightMongoCmd = &cobra.Command{
|
||||
Use: "mongo",
|
||||
Short: "preflight mongo OR preflight mongo --url=<url>",
|
||||
Long: `perform preflight mongo check to ensure we are able to connect to a mongodb instance in the cluster`,
|
||||
Example: `qliksense preflight mongo OR preflight mongo --url=<url>`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
qp := &preflight.QliksensePreflight{Q: q}
|
||||
|
||||
// Preflight mongo check
|
||||
fmt.Printf("Preflight mongo check\n")
|
||||
fmt.Println("-------------------------------------")
|
||||
namespace, kubeConfigContents, err := preflight.InitPreflight()
|
||||
if err != nil {
|
||||
fmt.Printf("Preflight mongo check FAILED\n")
|
||||
log.Fatal(err)
|
||||
}
|
||||
if namespace == "" {
|
||||
namespace = "default"
|
||||
}
|
||||
if err = qp.CheckMongo(kubeConfigContents, namespace, mongodbUrl); err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Print("Preflight mongo check FAILED\n")
|
||||
log.Fatal()
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
f := preflightMongoCmd.Flags()
|
||||
f.StringVarP(&mongodbUrl, "url", "", "", "mongodbUrl to try connecting to")
|
||||
return preflightMongoCmd
|
||||
}
|
||||
33
cmd/qliksense/pull.go
Normal file
33
cmd/qliksense/pull.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func pullQliksenseImages(q *qliksense.Qliksense) *cobra.Command {
|
||||
var (
|
||||
cmd *cobra.Command
|
||||
opts *aboutOptions
|
||||
)
|
||||
opts = &aboutOptions{}
|
||||
|
||||
cmd = &cobra.Command{
|
||||
Use: "pull",
|
||||
Short: "Pull docke images for offline install",
|
||||
Example: `qliksense pull`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.PullImages(opts.getTagDefaults(args))
|
||||
},
|
||||
}
|
||||
f := cmd.Flags()
|
||||
f.StringVarP(&opts.Version, "version", "v", "latest",
|
||||
"From version of Qlik Sense The images will be pulled")
|
||||
f.StringVarP(&opts.Tag, "tag", "t", "",
|
||||
"Use a bundle in an OCI registry specified by the given tag")
|
||||
f.StringVarP(&opts.File, "file", "f", "",
|
||||
"Path to the porter manifest file. Defaults to the bundle in the current directory.")
|
||||
f.StringVar(&opts.CNABFile, "cnab-file", "",
|
||||
"Path to the CNAB bundle.json file.")
|
||||
return cmd
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func pullQliksenseImages(q *qliksense.Qliksense) *cobra.Command {
|
||||
opts := &aboutCommandOptions{}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "pull",
|
||||
Short: "Pull docker images for offline install",
|
||||
Example: `qliksense pull`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
version, err := getAboutCommandGitRef(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return q.PullImages(version, opts.Profile)
|
||||
},
|
||||
}
|
||||
f := cmd.Flags()
|
||||
f.StringVar(&opts.Profile, "profile", "", "Configuration profile")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func pushQliksenseImages(q *qliksense.Qliksense) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "push",
|
||||
Short: "Push docker images for offline install",
|
||||
Example: `qliksense push`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
if qcr, err := qConfig.GetCurrentCR(); err != nil {
|
||||
return err
|
||||
} else if registry := qcr.GetImageRegistry(); registry == "" {
|
||||
return errors.New("no image registry in config")
|
||||
} else {
|
||||
return q.PushImagesForCurrentCR()
|
||||
}
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
@@ -1,129 +1,267 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
ansi "github.com/mattn/go-colorable"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/qlik-oss/sense-installer/pkg"
|
||||
"github.com/qlik-oss/sense-installer/pkg/api"
|
||||
"github.com/qlik-oss/sense-installer/pkg/preflight"
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/ttacon/chalk"
|
||||
)
|
||||
|
||||
// To run this project in debug mode, run:
|
||||
// To run this project in ddebug mode, run:
|
||||
// export QLIKSENSE_DEBUG=true
|
||||
// qliksense <command>
|
||||
|
||||
const (
|
||||
qlikSenseHomeVar = "QLIKSENSE_HOME"
|
||||
qlikSenseDirVar = ".qliksense"
|
||||
keepPatchFilesFlagName = "keep-config-repo-patches"
|
||||
keepPatchFilesFlagUsage = "Keep config repo patch files (for debugging)"
|
||||
winOS = "porter-windows-amd64.exe"
|
||||
linuxOS = "porter-linux-amd64"
|
||||
macOS = "porter-darwin-amd64"
|
||||
porterHomeVar = "PORTER_HOME"
|
||||
qlikSenseHomeVar = "QLIKSENSE_HOME"
|
||||
qlikSenseDirVar = ".qliksense"
|
||||
mixinDirVar = "mixins"
|
||||
porterRuntime = "porter-runtime"
|
||||
)
|
||||
|
||||
func initAndExecute() error {
|
||||
var (
|
||||
qlikSenseHome string
|
||||
err error
|
||||
porterExe, qlikSenseHome string
|
||||
err error
|
||||
)
|
||||
qlikSenseHome, err = setUpPaths()
|
||||
|
||||
porterExe, qlikSenseHome, err = setUpPaths()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// create dirs and appropriate files for setting up contexts
|
||||
api.LogDebugMessage("QliksenseHomeDir: %s", qlikSenseHome)
|
||||
|
||||
qliksenseClient := qliksense.New(qlikSenseHome)
|
||||
cmd := rootCmd(qliksenseClient)
|
||||
if err := cmd.Execute(); err != nil {
|
||||
//levenstein checks (auto-suggestions)
|
||||
levenstein(cmd)
|
||||
if err = rootCmd(qliksense.New(porterExe, qlikSenseHome)).Execute(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func setUpPaths() (string, error) {
|
||||
func setUpPaths() (string, string, error) {
|
||||
var (
|
||||
homeDir, qlikSenseHome string
|
||||
err error
|
||||
porterExe, homeDir, qlikSenseHome string
|
||||
err error
|
||||
)
|
||||
|
||||
porterExe = "porter"
|
||||
if runtime.GOOS == "windows" {
|
||||
porterExe = porterExe + ".exe"
|
||||
}
|
||||
if qlikSenseHome = os.Getenv(qlikSenseHomeVar); qlikSenseHome == "" {
|
||||
if homeDir, err = homedir.Dir(); err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
if homeDir, err = homedir.Expand(homeDir); err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
qlikSenseHome = filepath.Join(homeDir, qlikSenseDirVar)
|
||||
}
|
||||
os.Setenv(porterHomeVar, qlikSenseHome)
|
||||
|
||||
if err := os.MkdirAll(qlikSenseHome, os.ModePerm); err != nil {
|
||||
return "", err
|
||||
porterExe = filepath.Join(qlikSenseHome, porterExe)
|
||||
return porterExe, qlikSenseHome, nil
|
||||
}
|
||||
|
||||
func installPorter(qlikSenseHome, porterExe string) (string, error) {
|
||||
var (
|
||||
destination string
|
||||
// downloadPorter = true
|
||||
porterDownloadURL string
|
||||
err error
|
||||
)
|
||||
|
||||
// if downloadPorter {
|
||||
os.Mkdir(qlikSenseHome, os.ModePerm)
|
||||
destination = filepath.Join(qlikSenseHome, porterRuntime)
|
||||
// construct url to download porter from
|
||||
porterDownloadURL = constructPorterURL(runtime.GOOS)
|
||||
|
||||
if (runtime.GOOS == "linux" && runtime.GOARCH == "amd64") || runtime.GOOS == "darwin" {
|
||||
if err = downloadFile(porterDownloadURL, destination); err != nil {
|
||||
return "", err
|
||||
}
|
||||
os.Chmod(destination, 0755)
|
||||
if _, err = copy(destination, porterExe); err != nil {
|
||||
return "", err
|
||||
}
|
||||
os.Chmod(porterExe, 0755)
|
||||
} else if runtime.GOOS == "windows" {
|
||||
if err = downloadFile(porterDownloadURL, porterExe); err != nil {
|
||||
return "", err
|
||||
}
|
||||
os.Chmod(porterExe, 0755)
|
||||
}
|
||||
|
||||
return qlikSenseHome, nil
|
||||
return porterExe, nil
|
||||
}
|
||||
|
||||
var versionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Print the version number of qliksense cli",
|
||||
Long: "Print the version number of qliksense cli",
|
||||
Long: `All software has versions. This is Hugo's`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Printf("%s (%s, %s)\n", pkg.Version, pkg.Commit, pkg.CommitDate)
|
||||
},
|
||||
}
|
||||
|
||||
func commandUsesContext(commandName string) bool {
|
||||
return commandName != "" && commandName != "qliksense" && commandName != "help" && commandName != "version"
|
||||
func constructPorterURL(runtimeOS string) string {
|
||||
// FYI: Porter does not support other architectures other than amd64
|
||||
const (
|
||||
porterURLBase = "https://cdn.deislabs.io/porter/"
|
||||
winOS = "porter-windows-amd64.exe"
|
||||
linuxOS = "porter-linux-amd64"
|
||||
macOS = "porter-darwin-amd64"
|
||||
)
|
||||
var url, version string
|
||||
version = retrievePorterVersion()
|
||||
if runtimeOS == "linux" {
|
||||
url = porterURLBase + version + "/" + linuxOS
|
||||
} else if runtimeOS == "windows" {
|
||||
url = porterURLBase + version + "/" + winOS
|
||||
} else if runtimeOS == "darwin" {
|
||||
url = porterURLBase + version + "/" + macOS
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
func getRootCmd(p *qliksense.Qliksense) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
func retrievePorterVersion() string {
|
||||
type apiInfo struct {
|
||||
TagName string `json:"tag_name,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
}
|
||||
const porterRepoURL = "https://api.github.com/repos/deislabs/porter/releases/latest"
|
||||
|
||||
resp, err := http.Get(porterRepoURL)
|
||||
if err != nil {
|
||||
fmt.Printf("Error occurred while retrieving porter version info: %v\n", err)
|
||||
return ""
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
fmt.Printf("response status was not OK while retrieving porter version info\n")
|
||||
return ""
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Printf("Error occurred while reading porter version info: %v\n", err)
|
||||
return ""
|
||||
}
|
||||
result := &apiInfo{}
|
||||
err = json.Unmarshal(body, result)
|
||||
if err != nil {
|
||||
fmt.Printf("Error occurred while unmarshalling porter version info: %v\n", err)
|
||||
return ""
|
||||
}
|
||||
fmt.Printf("Porter Version: %s\n", result.Name)
|
||||
return result.Name
|
||||
}
|
||||
|
||||
func installMixins(porterExe, qlikSenseHome string) (string, error) {
|
||||
var (
|
||||
mixin, mixinOpts string
|
||||
mixinsVar = map[string]string{
|
||||
"kustomize": "-v 0.2-beta-3-0e19ca4 --url https://github.com/donmstewart/porter-kustomize/releases/download",
|
||||
"qliksense": "-v v0.16.0 --url https://github.com/qlik-oss/porter-qliksense/releases/download",
|
||||
"exec": "-v latest",
|
||||
"kubernetes": "-v latest",
|
||||
"helm": "-v latest",
|
||||
"azure": "-v latest",
|
||||
"terraform": "-v latest",
|
||||
"az": "-v latest",
|
||||
"aws": "-v latest",
|
||||
"gcloud": "-v latest",
|
||||
}
|
||||
downloadMixins map[string]string
|
||||
err error
|
||||
)
|
||||
if _, err = os.Stat(filepath.Join(qlikSenseHome, mixinDirVar)); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
downloadMixins = mixinsVar
|
||||
} else {
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
downloadMixins = make(map[string]string)
|
||||
for mixin, mixinOpts = range mixinsVar {
|
||||
if _, err = os.Stat(filepath.Join(qlikSenseHome, mixinDirVar, mixin)); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
downloadMixins[mixin] = mixinOpts
|
||||
} else {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for mixin, mixinOpts = range downloadMixins {
|
||||
if _, err = installMixin(porterExe, mixin, mixinOpts); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
|
||||
func installMixin(porterExe, mixin, mixinOpts string) (string, error) {
|
||||
var cmd *exec.Cmd
|
||||
|
||||
args := []string{"mixin", "install", mixin}
|
||||
if mixinOpts != "" {
|
||||
args = append(args, strings.Fields(mixinOpts)...)
|
||||
}
|
||||
cmd = exec.Command(porterExe, args...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func rootCmd(p *qliksense.Qliksense) *cobra.Command {
|
||||
var (
|
||||
cmd, porterCmd, alias *cobra.Command
|
||||
)
|
||||
|
||||
cmd = &cobra.Command{
|
||||
Use: "qliksense",
|
||||
Short: "Qliksense cli tool",
|
||||
Long: `qliksense cli tool provides functionality to perform operations on qliksense-k8s, qliksense operator, and kubernetes cluster`,
|
||||
Args: cobra.ArbitraryArgs,
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
if commandUsesContext(cmd.Name()) {
|
||||
globalEulaPreRun(cmd, p)
|
||||
if err := p.SetUpQliksenseDefaultContext(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
pf := preflight.NewPreflightConfig(p.QliksenseHome)
|
||||
if err := pf.Initialize(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
globalEulaPostRun(cmd, p)
|
||||
}
|
||||
},
|
||||
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
||||
if commandUsesContext(cmd.Name()) {
|
||||
globalEulaPostRun(cmd, p)
|
||||
}
|
||||
},
|
||||
Long: `qliksense cli tool provides a wrapper around the porter api as well as
|
||||
provides addition functionality`,
|
||||
Args: cobra.ArbitraryArgs,
|
||||
SilenceUsage: true,
|
||||
}
|
||||
origHelpFunc := cmd.HelpFunc()
|
||||
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
|
||||
if !commandUsesContext(cmd.Name()) {
|
||||
cmd.Flags().MarkHidden("acceptEULA")
|
||||
}
|
||||
origHelpFunc(cmd, args)
|
||||
})
|
||||
accept := ""
|
||||
cmd.PersistentFlags().StringVarP(&accept, "acceptEULA", "a", "", "Accept EULA for qliksense")
|
||||
|
||||
cmd.Flags().SetInterspersed(false)
|
||||
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
// For qliksense overrides/commands
|
||||
|
||||
cmd.AddCommand(pullQliksenseImages(p))
|
||||
porterCmd = porter(p)
|
||||
cmd.AddCommand(porterCmd)
|
||||
for _, alias = range buildAliasCommands(porterCmd, p) {
|
||||
cmd.AddCommand(alias)
|
||||
}
|
||||
// add version command
|
||||
cmd.AddCommand(versionCmd)
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -132,92 +270,31 @@ func initConfig() {
|
||||
viper.AutomaticEnv()
|
||||
}
|
||||
|
||||
func rootCmd(p *qliksense.Qliksense) *cobra.Command {
|
||||
cmd := getRootCmd(p)
|
||||
cobra.OnInitialize(initConfig)
|
||||
func downloadFile(url string, filepath string) error {
|
||||
var (
|
||||
out *os.File
|
||||
err error
|
||||
resp *http.Response
|
||||
)
|
||||
logDebugMessage("Porter download link: %s\n", url)
|
||||
// Create the file
|
||||
if out, err = os.Create(filepath); err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
cmd.AddCommand(getInstallableVersionsCmd(p))
|
||||
cmd.AddCommand(pullQliksenseImages(p))
|
||||
cmd.AddCommand(pushQliksenseImages(p))
|
||||
cmd.AddCommand(about(p))
|
||||
// add version command
|
||||
cmd.AddCommand(versionCmd)
|
||||
// Get the data
|
||||
if resp, err = http.Get(url); err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// add operator command
|
||||
cmd.AddCommand(operatorCmd)
|
||||
//operatorCmd.AddCommand(operatorViewCmd(p))
|
||||
operatorCmd.AddCommand(operatorCrdCmd(p))
|
||||
operatorCmd.AddCommand(operatorControllerCmd(p))
|
||||
// Write the body to file
|
||||
if _, err = io.Copy(out, resp.Body); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//add fetch command
|
||||
cmd.AddCommand(fetchCmd(p))
|
||||
|
||||
// add install command
|
||||
cmd.AddCommand(installCmd(p))
|
||||
|
||||
// add config command
|
||||
configCmd := configCmd(p)
|
||||
cmd.AddCommand(configCmd)
|
||||
configCmd.AddCommand(configApplyCmd(p))
|
||||
configCmd.AddCommand(configViewCmd(p))
|
||||
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
||||
|
||||
//add upgrade command
|
||||
cmd.AddCommand(upgradeCmd(p))
|
||||
|
||||
// add the set-context config command as a sub-command to the app config command
|
||||
configCmd.AddCommand(setContextConfigCmd(p))
|
||||
|
||||
// add the set profile/namespace/storageClassName/git-repository config command as a sub-command to the app config command
|
||||
configCmd.AddCommand(setOtherConfigsCmd(p))
|
||||
|
||||
// add the set ### config command as a sub-command to the app config sub-command
|
||||
configCmd.AddCommand(setConfigsCmd(p))
|
||||
|
||||
// add the set ### config command as a sub-command to the app config sub-command
|
||||
configCmd.AddCommand(setSecretsCmd(p))
|
||||
|
||||
// add the list config command as a sub-command to the app config sub-command
|
||||
configCmd.AddCommand(listContextConfigCmd(p))
|
||||
|
||||
// add the delete-context config command as a sub-command to the app config command
|
||||
configCmd.AddCommand(deleteContextConfigCmd(p))
|
||||
|
||||
// add set-image-registry command as a sub-command to the app config sub-command
|
||||
configCmd.AddCommand(setImageRegistryCmd(p))
|
||||
|
||||
// add clean-config-repo-patches command as a sub-command to the app config sub-command
|
||||
configCmd.AddCommand(cleanConfigRepoPatchesCmd(p))
|
||||
|
||||
// open editor for config
|
||||
configCmd.AddCommand(configEditCmd(p))
|
||||
// add uninstall command
|
||||
cmd.AddCommand(uninstallCmd(p))
|
||||
|
||||
// add crds
|
||||
cmd.AddCommand(crdsCmd)
|
||||
crdsCmd.AddCommand(crdsViewCmd(p))
|
||||
crdsCmd.AddCommand(crdsInstallCmd(p))
|
||||
|
||||
// add preflight command
|
||||
preflightCmd := preflightCmd(p)
|
||||
preflightCmd.AddCommand(pfDnsCheckCmd(p))
|
||||
preflightCmd.AddCommand(pfK8sVersionCheckCmd(p))
|
||||
preflightCmd.AddCommand(pfAllChecksCmd(p))
|
||||
preflightCmd.AddCommand(pfMongoCheckCmd(p))
|
||||
preflightCmd.AddCommand(pfDeploymentCheckCmd(p))
|
||||
preflightCmd.AddCommand(pfServiceCheckCmd(p))
|
||||
preflightCmd.AddCommand(pfPodCheckCmd(p))
|
||||
preflightCmd.AddCommand(pfCreateRoleCheckCmd(p))
|
||||
preflightCmd.AddCommand(pfCreateRoleBindingCheckCmd(p))
|
||||
preflightCmd.AddCommand(pfCreateServiceAccountCheckCmd(p))
|
||||
preflightCmd.AddCommand(pfCreateAuthCheckCmd(p))
|
||||
|
||||
cmd.AddCommand(preflightCmd)
|
||||
cmd.AddCommand(loadCrFile(p))
|
||||
cmd.AddCommand((applyCmd(p)))
|
||||
return cmd
|
||||
return nil
|
||||
}
|
||||
|
||||
func copy(src, dst string) (int64, error) {
|
||||
@@ -247,22 +324,3 @@ func copy(src, dst string) (int64, error) {
|
||||
nBytes, err = io.Copy(destination, source)
|
||||
return nBytes, err
|
||||
}
|
||||
|
||||
func levenstein(cmd *cobra.Command) {
|
||||
cmd.SuggestionsMinimumDistance = 2
|
||||
if len(os.Args) > 1 {
|
||||
args := os.Args[1]
|
||||
suggest := cmd.SuggestionsFor(args)
|
||||
if len(suggest) > 0 {
|
||||
arg := []string{}
|
||||
for _, cm := range os.Args {
|
||||
arg = append(arg, cm)
|
||||
}
|
||||
if !strings.EqualFold(arg[1], suggest[0]) {
|
||||
arg[1] = suggest[0]
|
||||
out := ansi.NewColorableStdout()
|
||||
fmt.Fprintln(out, chalk.Green.Color("Did you mean: "), chalk.Bold.TextStyle(strings.Join(arg, " ")), "?")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func uninstallCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
c := &cobra.Command{
|
||||
Use: "uninstall",
|
||||
Short: "Uninstall the deployed qliksense.",
|
||||
Long: `Uninstall the deployed qliksense. By default uninstall the current context`,
|
||||
Example: `qliksense uninstall <context-name>`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) > 0 {
|
||||
return q.UninstallQK8s(args[0])
|
||||
}
|
||||
return q.UninstallQK8s("")
|
||||
},
|
||||
}
|
||||
return c
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func upgradeCmd(q *qliksense.Qliksense) *cobra.Command {
|
||||
keepPatchFiles := false
|
||||
c := &cobra.Command{
|
||||
Use: "upgrade",
|
||||
Short: "upgrade qliksense release",
|
||||
Long: `upgrade qliksense release`,
|
||||
Example: `qliksense upgrade <version>`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return q.UpgradeQK8s(keepPatchFiles)
|
||||
},
|
||||
}
|
||||
|
||||
f := c.Flags()
|
||||
f.BoolVar(&keepPatchFiles, keepPatchFilesFlagName, keepPatchFiles, keepPatchFilesFlagUsage)
|
||||
return c
|
||||
}
|
||||
292
cmd/qliksense/version_checks.go
Normal file
292
cmd/qliksense/version_checks.go
Normal file
@@ -0,0 +1,292 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/qlik-oss/sense-installer/pkg"
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
dependenciesFile = "dependencies.yaml"
|
||||
updateMixin, updateComponent bool
|
||||
currentPorterVersion string
|
||||
mixinURLs = map[string]string{
|
||||
"qliksense": "--url https://github.com/qlik-oss/porter-qliksense/releases/download",
|
||||
"kustomize": "--url https://github.com/donmstewart/porter-kustomize/releases/",
|
||||
}
|
||||
mixinsVar = map[string]string{
|
||||
"kustomize": "-v 0.2-beta-3-0e19ca4 --url https://github.com/donmstewart/porter-kustomize/releases/download",
|
||||
"qliksense": "-v v0.14.0 --url https://github.com/qlik-oss/porter-qliksense/releases/download",
|
||||
"exec": "-v latest",
|
||||
"kubernetes": "-v latest",
|
||||
"helm": "-v latest",
|
||||
"azure": "-v latest",
|
||||
"terraform": "-v latest",
|
||||
"az": "-v latest",
|
||||
"aws": "-v latest",
|
||||
"gcloud": "-v latest",
|
||||
}
|
||||
)
|
||||
|
||||
func checkMinVersion(tag string, q *qliksense.Qliksense) {
|
||||
|
||||
logDebugMessage("Starting version checks\n")
|
||||
|
||||
dependencies := map[string]string{}
|
||||
// check if tag is empty or not
|
||||
var err error
|
||||
if len(strings.TrimSpace(tag)) > 0 {
|
||||
// --tag exists
|
||||
logDebugMessage("Input tag: %s, %s\n", tag, strings.Replace(tag, "bundle", "invocation", 1))
|
||||
// Pull the image and store labels in a map
|
||||
dependencies, err = q.PullImage(strings.Replace(tag, "bundle", "invocation", 1))
|
||||
logDebugMessage("\nDependencies map from the inspected image: %v\n", dependencies)
|
||||
if err != nil {
|
||||
log.Fatalf("unable to pull the requested image: %v", err)
|
||||
}
|
||||
|
||||
} else {
|
||||
// Tag is empty, hence looking for dependenciesFile.yaml, exit if this file is not present
|
||||
if fileExists(dependenciesFile) {
|
||||
// read the dependencies.yaml and store into a map
|
||||
yamlFile, err := ioutil.ReadFile(dependenciesFile)
|
||||
if err != nil {
|
||||
logDebugMessage("Exit: CheckMinVersion()\n")
|
||||
log.Fatalf("Error reading from source: %s\n", err)
|
||||
}
|
||||
err = yaml.Unmarshal(yamlFile, &dependencies)
|
||||
if err != nil {
|
||||
logDebugMessage("Exit: CheckMinVersion()\n")
|
||||
log.Fatalf("Error when parsing from source: %s\n", err)
|
||||
}
|
||||
logDebugMessage("Dependencies map from the given yaml: %v\n", dependencies)
|
||||
}
|
||||
}
|
||||
if len(dependencies) > 0 {
|
||||
for k, v := range dependencies {
|
||||
if strings.Contains(k, ".mixin.") {
|
||||
dependencies[k] = fmt.Sprintf("-v %s", v)
|
||||
}
|
||||
}
|
||||
// CLI check
|
||||
checkCLIVersion(dependencies)
|
||||
// Porter check
|
||||
checkPorterVersion(dependencies, q)
|
||||
// Mixins check
|
||||
checkMixinVersion(dependencies, q)
|
||||
} else {
|
||||
log.Fatalf("Not able to infer dependencies, hence exiting")
|
||||
}
|
||||
logDebugMessage("Completed version checks\n")
|
||||
}
|
||||
|
||||
func checkMixinVersion(dependencies map[string]string, q *qliksense.Qliksense) {
|
||||
var tmp string
|
||||
logDebugMessage("------------ Mixins version check -----------")
|
||||
currentMixinVersions, err := retrieveCurrentInstalledMixinVersions(q)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
for k := range mixinsVar {
|
||||
tmp, _ = dependencies[fmt.Sprintf("org.qlik.operator.mixin.%s.version.min", k)]
|
||||
if tmp == "" {
|
||||
continue
|
||||
}
|
||||
shouldUpdateMixin := false
|
||||
mixinVersion, ok := currentMixinVersions[k]
|
||||
if !ok {
|
||||
shouldUpdateMixin = true
|
||||
} else {
|
||||
shouldUpdateMixin = versionCheck(fmt.Sprintf("Mixin %s", k), mixinVersion, tmp)
|
||||
}
|
||||
// if tmp is not empty and mixin requires download and install
|
||||
if shouldUpdateMixin {
|
||||
fmt.Println("Downloading a newer version of mixin:", k)
|
||||
// download and install the new mixin
|
||||
mURL, ok := mixinURLs[k]
|
||||
if ok {
|
||||
tmp = fmt.Sprintf("%s %s", tmp, mURL)
|
||||
}
|
||||
if _, err = installMixin(q.PorterExe, k, tmp); err != nil {
|
||||
logDebugMessage("Completed version checks\n")
|
||||
log.Fatalf("Error installing mixin %s: %s\n", k, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func checkPorterVersion(dependencies map[string]string, q *qliksense.Qliksense) {
|
||||
// Infer info about the min porter version
|
||||
var porterVersionFromDependencies, tmp string
|
||||
var err error
|
||||
logDebugMessage("------------ Porter version check -----------")
|
||||
tmp, _ = dependencies["org.qlik.operator.cli.porter.version.min"]
|
||||
if len(tmp) != 0 {
|
||||
porterVersionFromDependencies = tmp
|
||||
}
|
||||
logDebugMessage("Porter version from dependencies map: %v\n", porterVersionFromDependencies)
|
||||
|
||||
// check porter version
|
||||
currentPorterVersion, err = determineCurrentPorterVersion(q)
|
||||
if err != nil {
|
||||
log.Println("warning:", err)
|
||||
}
|
||||
logDebugMessage("Current Porter version: %v\n", currentPorterVersion)
|
||||
updateComponent = true
|
||||
if currentPorterVersion != "" {
|
||||
updateComponent = versionCheck("Porter", currentPorterVersion, porterVersionFromDependencies)
|
||||
}
|
||||
if updateComponent {
|
||||
fmt.Println("Downloading a newer version of Porter")
|
||||
// Download and install newer version of porter and mixins
|
||||
q.PorterExe, err = installPorter(q.QliksenseHome, q.PorterExe)
|
||||
if err != nil {
|
||||
logDebugMessage("Completed version checks")
|
||||
log.Fatalf("error installing porter: %v", err)
|
||||
}
|
||||
|
||||
if _, err = installMixins(q.PorterExe, q.QliksenseHome); err != nil {
|
||||
logDebugMessage("Completed version checks")
|
||||
log.Fatalf("error installing mixin: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func checkCLIVersion(dependencies map[string]string) {
|
||||
// Infer info about the minimum cli version
|
||||
var cliVersionFromDependencies, tmp string
|
||||
logDebugMessage("\n------------ CLI version check -----------\n")
|
||||
tmp, _ = dependencies["org.qlik.operator.cli.sense-installer.version.min"]
|
||||
if len(tmp) != 0 {
|
||||
cliVersionFromDependencies = tmp
|
||||
}
|
||||
logDebugMessage("\nCLI version from dependencies map: %v\n", cliVersionFromDependencies)
|
||||
|
||||
// Checking version below
|
||||
updateComponent = versionCheck("CLI", pkg.Version, cliVersionFromDependencies)
|
||||
if updateComponent {
|
||||
log.Fatalf("Please download a newer version of CLI and retry the operation, exiting now.")
|
||||
}
|
||||
}
|
||||
|
||||
func retrieveCurrentInstalledMixinVersions(q *qliksense.Qliksense) (map[string]string, error) {
|
||||
if _, err := os.Stat(filepath.Join(q.QliksenseHome, mixinDirVar)); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
// if path doesnt exist, return empty map, and let porter take care of the rest
|
||||
return map[string]string{}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := map[string]string{}
|
||||
currentInstalledMixinVersions, err := q.CallPorter([]string{"mixins", "list"}, func(x string) (out *string) {
|
||||
out = new(string)
|
||||
*out = strings.ReplaceAll(x, "porter", "qliksense porter")
|
||||
logDebugMessage("%s\n", *out)
|
||||
return
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("Error occurred when retrieving mixins list: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
currentInstalledMixinVersionsArr := strings.Split(currentInstalledMixinVersions, "\n")
|
||||
for _, mix := range currentInstalledMixinVersionsArr {
|
||||
mixRow := strings.Fields(mix)
|
||||
mixRowLen := len(mixRow)
|
||||
if mixRowLen > 0 && mixRow[0] == "Name" {
|
||||
continue
|
||||
}
|
||||
// we handle the case of mixins like `kustomize` where version and author could be empty
|
||||
if mixRowLen >= 2 {
|
||||
_, err := semver.NewVersion(mixRow[1])
|
||||
if err == nil {
|
||||
result[mixRow[0]] = mixRow[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func determineVersion(versionString string) (string, error) {
|
||||
|
||||
versionSlice := strings.Fields(versionString)
|
||||
|
||||
var currentComponentVersionNumber *semver.Version
|
||||
var err error
|
||||
for _, value := range versionSlice {
|
||||
currentComponentVersionNumber, err = semver.NewVersion(value)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
logDebugMessage("Version string: %v\n", currentComponentVersionNumber)
|
||||
if currentComponentVersionNumber != nil {
|
||||
return currentComponentVersionNumber.String(), nil
|
||||
}
|
||||
return "", fmt.Errorf("unable to extract version information")
|
||||
}
|
||||
|
||||
func determineCurrentPorterVersion(q *qliksense.Qliksense) (string, error) {
|
||||
// determine current porter version
|
||||
currentPorterVersion, err := q.CallPorter([]string{"version"}, func(x string) (out *string) {
|
||||
out = new(string)
|
||||
*out = strings.ReplaceAll(x, "porter", "qliksense porter")
|
||||
logDebugMessage(*out)
|
||||
return
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("Error occurred during porter call: %v", err)
|
||||
return "", err
|
||||
}
|
||||
return determineVersion(currentPorterVersion)
|
||||
}
|
||||
|
||||
func versionCheck(component string, currentVersion string, versionFromSourceOfTruth string) bool {
|
||||
|
||||
if strings.HasPrefix(versionFromSourceOfTruth, "-v ") {
|
||||
versionFromSourceOfTruth = strings.Replace(versionFromSourceOfTruth, "-v ", "", 1)
|
||||
}
|
||||
componentVersionFromDependenciesYaml, err := semver.NewVersion(versionFromSourceOfTruth)
|
||||
if err != nil {
|
||||
fmt.Printf("There has been an error parsing version from source of truth: %s\n", err)
|
||||
return true
|
||||
}
|
||||
logDebugMessage("%s version from source of truth: %s\n", component, componentVersionFromDependenciesYaml)
|
||||
|
||||
currentComponentVersion, err := semver.NewVersion(currentVersion)
|
||||
if err != nil {
|
||||
fmt.Printf("There has been an error parsing version from the derived current version: %s\n", err)
|
||||
return true
|
||||
}
|
||||
logDebugMessage("\nCurrently installed %s version: %v\n", component, currentComponentVersion)
|
||||
|
||||
// check component version
|
||||
if currentComponentVersion.LessThan(componentVersionFromDependenciesYaml) {
|
||||
fmt.Printf("\n\nCurrent %s Component version: %s is less than minimum required version:%s\n", component, currentComponentVersion, componentVersionFromDependenciesYaml)
|
||||
return true
|
||||
}
|
||||
logDebugMessage("Current %s version is greater than version from dependencies, upgrade not necessary.\n\n", component)
|
||||
return false
|
||||
}
|
||||
|
||||
func logDebugMessage(strMessage string, args ...interface{}) {
|
||||
if os.Getenv("QLIKSENSE_DEBUG") == "true" {
|
||||
fmt.Printf(strMessage, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func fileExists(filename string) bool {
|
||||
info, err := os.Stat(filename)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return !info.IsDir()
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
# qliksense command reference
|
||||
|
||||
## qliksense apply
|
||||
|
||||
`qliksense apply` command takes input a cr file or input from pipe
|
||||
|
||||
- `qliksense apply -f cr-file.yaml`
|
||||
- `cat cr-file.yaml | qliksense apply -f -`
|
||||
|
||||
the content of `cr-file.yaml` should be something similar
|
||||
|
||||
```yaml
|
||||
apiVersion: qlik.com/v1
|
||||
kind: Qliksense
|
||||
metadata:
|
||||
name: qlik-test
|
||||
labels:
|
||||
version: v0.0.2
|
||||
spec:
|
||||
configs:
|
||||
qliksense:
|
||||
- name: acceptEULA
|
||||
value: "yes"
|
||||
secrets:
|
||||
qliksense:
|
||||
- name: mongoDbUri
|
||||
value: mongodb://qlik-test-mongodb:27017/qliksense?ssl=false
|
||||
profile: docker-desktop
|
||||
rotateKeys: "yes"
|
||||
```
|
||||
|
||||
This will do everything `qliksense load` does and install the qliksense into the cluster.
|
||||
|
||||
## qliksense load
|
||||
|
||||
`qliksense load` command takes input a cr file or input from pipe.
|
||||
|
||||
- `qliksense load -f cr-file.yaml`
|
||||
- `cat cr-file.yaml | qliksense load -f -`
|
||||
|
||||
This will load the CR into `${QLIKSENSE_HOME}` folder, create context structure and set the current context to that CR.
|
||||
This will also encrypt the secrets from CR while writing the CR into the disk.
|
||||
|
||||
## qliksense about
|
||||
|
||||
About action will display inside information regarding [qliksense-k8](https://github.com/qlik-oss/qliksense-k8s) release.
|
||||
|
||||
it will support following flags
|
||||
|
||||
- `qliksense about 1.0.0` display default profile for tag `1.0.0`.
|
||||
- `qliksense about 1.0.0 --profile=docker-desktop`
|
||||
- `qliksense about`
|
||||
- assuming current directory has `manifests/docker-desktop`
|
||||
- or get version information from pull of `qliksense-k8s` `master`
|
||||
|
||||
using other supported commands user might have built the CR into the location `~/.qliksense/myqliksense.yaml`
|
||||
|
||||
```yaml
|
||||
apiVersion: qlik.com/v1
|
||||
kind: QlikSense
|
||||
metadata:
|
||||
name: myqliksense
|
||||
spec:
|
||||
profile: docker-desktop
|
||||
manifestsRoot: /Usr/ddd/my-k8-repo/manifests
|
||||
namespace: myqliksense
|
||||
storageClassName: efs
|
||||
configs:
|
||||
qliksense:
|
||||
- name: acceptEULA
|
||||
value: "yes"
|
||||
secrets:
|
||||
qliksense:
|
||||
- name: mongoDbUri
|
||||
value: "mongo://mongo:3307"
|
||||
- name: messagingPassword
|
||||
valueFromKey: messagingPassword
|
||||
```
|
||||
|
||||
In that case the command would be
|
||||
|
||||
- `qliksense about`
|
||||
- display from `/Usr/ddd/my-k8-repo/manifests/docker-desktop` location
|
||||
- pull from `master` if directory invalid/empty
|
||||
|
||||
|
||||
## qliksense config
|
||||
|
||||
Config action will perform operations on configurations and contexts regarding the [qliksense-k8](https://github.com/qlik-oss/qliksense-k8s) release.
|
||||
|
||||
it will support following commands:
|
||||
|
||||
- `qliksense config apply` - generate the patchs and apply manifests to k8s
|
||||
- `qliksense config list-contexts` - retrieves the contexts and lists them
|
||||
- `qliksense config set` - configure a key value pair into the current context
|
||||
- `qliksense config set-configs` - set configurations into the qliksense context as key-value pairs
|
||||
- `qliksense config set-context` - sets the context in which the Kubernetes cluster and resources live in
|
||||
- `qliksense config set-secrets <service_name>.<attribute>="<value>" --secret=false` - set secrets configurations into the qliksense context as key-value pairs and show encrypted value as part of CR
|
||||
- `qliksense config set-secrets <service_name>.<attribute>="<value>" --secret=true` - set secrets configurations into the qliksense context as key-value pairs and show a key reference to the created Kubernetes secret resource as part of the CR
|
||||
- `qliksense config view` - view the qliksense operator CR
|
||||
- `qliksense config delete-context` - deletes a specific context locally (not in-cluster). Deletes context in spec of `config.yaml` and locally deletes entire folder of specified context (does not delete in-cluster secrets)
|
||||
|
||||
|
||||
the global file that abstracts all the contexts is `config.yaml`, located at: `~/.qliksense/config.yaml`:
|
||||
```yaml
|
||||
apiVersion: config.qlik.com/v1
|
||||
kind: QliksenseConfig
|
||||
metadata:
|
||||
name: QliksenseConfigMetadata
|
||||
spec:
|
||||
contexts:
|
||||
- name: qlik-default
|
||||
crFile: /Users/fff/.qliksense/contexts/qlik-default/qlik-default.yaml
|
||||
- name: myqliksense
|
||||
crFile: /Users/fff/.qliksense/contexts/myqliksense/myqliksense.yaml
|
||||
- name: hello
|
||||
crFile: /Users/fff/.qliksense/contexts/hello/hello.yaml
|
||||
currentContext: hello
|
||||
```
|
||||
@@ -1,97 +0,0 @@
|
||||
# How qliksense cli works
|
||||
|
||||
At the initialization, `qliksense` cli creates few files in the director `~/.qliksene` and it contains following files:
|
||||
|
||||
```console
|
||||
.qliksense
|
||||
├── config.yaml
|
||||
├── contexts
|
||||
│ └── qlik-default
|
||||
│ └── qlik-default.yaml
|
||||
└── ejson
|
||||
└── keys
|
||||
```
|
||||
|
||||
`qlik-default.yaml` is a default CR created with some default values like:
|
||||
|
||||
```yaml
|
||||
apiVersion: qlik.com/v1
|
||||
kind: Qliksense
|
||||
metadata:
|
||||
name: qlik-default
|
||||
spec:
|
||||
profile: docker-desktop
|
||||
secrets:
|
||||
qliksense:
|
||||
- name: mongoDbUri
|
||||
value: mongodb://qlik-default-mongodb:27017/qliksense?ssl=false
|
||||
rotateKeys: "yes"
|
||||
releaseName: qlik-default
|
||||
```
|
||||
|
||||
The `qliksense` cli creates a default qliksense context (different from kubectl context) named `qlik-default` which will be the prefix for all kubernetes resources created by the cli under this context later on.
|
||||
|
||||
New context and configuration can be created by the cli, get available commands using:
|
||||
|
||||
```console
|
||||
qliksense config -h
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
`qliksense` cli works in two modes
|
||||
|
||||
- With a git repo fork/clone of [qliksense-k8s](https://github.com/qlik-oss/qliksense-k8s)
|
||||
- Without git repo
|
||||
|
||||
## Without git repo
|
||||
|
||||
In this mode `qliksense` CLI downloads the specified version from [qliksense-k8s](https://github.com/qlik-oss/qliksense-k8s) and places it in `~/.qliksense/contexts/<context-name>/qlik-k8s` folder.
|
||||
|
||||
The qliksense cli creates a CR for the QlikSense operator and all config operations are peformed to edit the CR.
|
||||
|
||||
`qliksense install` or `qliksense config apply` will generate patches in local file system (i.e `~/.qliksense/contexts/<context-name>/qlik-k8s`) and
|
||||
|
||||
- Install those manifests into the cluster
|
||||
- Create a custom resoruce (CR) for the `qliksene operator`.
|
||||
|
||||
The operator makes the association to the installed resoruces so that when `qliksense uninstall` is performed the operator can delete all kubernetes resources related to QSEoK for the current context.
|
||||
|
||||
## With a git repo
|
||||
|
||||
Create a fork or clone of [qliksense-k8s](https://github.com/qlik-oss/qliksense-k8s) and push it to your git repo/server
|
||||
|
||||
To add your repo into CR, perform the following:
|
||||
|
||||
```bash
|
||||
qliksense config set git.repository="https://github.com/my-org/qliksense-k8s"
|
||||
qliksense config set git.accessToken="<mySecretToken>"
|
||||
```
|
||||
|
||||
When you perform `qliksense install` or `qliksene config apply`, qliksense operator performs these tasks:
|
||||
|
||||
- Download corresponding version of manifests from the your git repo
|
||||
- Generate kustomize patches
|
||||
- Install kubernetes resources
|
||||
- Push generated patches into a new branch in the provided git repo. _Gives you ability to merge patches into your master branch_
|
||||
- Create a CronJob to monitor master branch. Any changes pushed to master branch will be applied into the cluster. _This is a light weight `git-ops` model_
|
||||
|
||||
## GitOps
|
||||
|
||||
To enable gitops, the following section should be in the CR
|
||||
|
||||
```yaml
|
||||
....
|
||||
spec:
|
||||
git:
|
||||
repository: https://github.com/<OWNER>/<REPO>
|
||||
accessToken: "<git-token>"
|
||||
userName: "<git-username>"
|
||||
gitOps:
|
||||
enabled: "yes"
|
||||
schedule: "*/5 * * * *"
|
||||
watchBranch: <myBranch>
|
||||
image: qlik-docker-oss.bintray.io/qliksense-repo-watcher
|
||||
....
|
||||
```
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
# Getting started
|
||||
|
||||
## Requirements
|
||||
|
||||
- Kubernetes cluster (Docker Desktop with enabled Kubernetes)
|
||||
- `kubectl` installed, configured and able to communicate with kubernetes cluster. _`qliksense` CLI uses `kubectl` under the hood to perform operations on cluster_
|
||||
|
||||
## Installing Sense installer
|
||||
|
||||
Download the executable for your platform from [releases page](https://github.com/qlik-oss/sense-installer/releases) and rename it to `qliksense`
|
||||
|
||||
??? tldr "Linux"
|
||||
|
||||
``` bash
|
||||
curl -Lo qliksense https://github.com/qlik-oss/sense-installer/releases/download/v0.7.0/qliksense-linux-amd64
|
||||
chmod +x qliksense
|
||||
sudo mv qliksense /usr/local/bin
|
||||
```
|
||||
|
||||
??? tldr "MacOS"
|
||||
|
||||
``` bash
|
||||
curl -Lo qliksense https://github.com/qlik-oss/sense-installer/releases/download/v0.7.0/qliksense-darwin-amd64
|
||||
chmod +x qliksense
|
||||
sudo mv qliksense /usr/local/bin
|
||||
```
|
||||
|
||||
??? tldr "Windows"
|
||||
Download Windows executable and add it in your `PATH` as `qliksense.exe`
|
||||
|
||||
[https://github.com/qlik-oss/sense-installer/releases/download/v0.7.0/qliksense-windows-amd64.exe](https://github.com/qlik-oss/sense-installer/releases/download/v0.7.0/qliksense-windows-amd64.exe)
|
||||
|
||||
|
||||
|
||||
## Quick start
|
||||
|
||||
- To download the version `v0.0.2` from qliksense-k8s [releases](https://github.com/qlik-oss/qliksense-k8s/releases).
|
||||
|
||||
```shell
|
||||
qliksense fetch v0.0.2
|
||||
```
|
||||
|
||||
- To install CRDs for QSEoK and qliksense operator into the kubernetes cluster.
|
||||
|
||||
```shell
|
||||
qliksense crds install --all
|
||||
```
|
||||
|
||||
- To install QSEoK into a namespace in the kubernetes cluster where `kubectl` is pointing to.
|
||||
|
||||
```shell
|
||||
qliksense install --acceptEULA="yes"
|
||||
```
|
||||
@@ -1,22 +0,0 @@
|
||||
# Overview
|
||||
|
||||
The Qlik Sense installer CLI (`qliksense`) provides an imperative interface to many of the configurations that need to be applied against the declarative structure described in [qliksense-k8s](https://github.com/qlik-oss/qliksense-k8s). This cli facilitates:
|
||||
|
||||
- Installation of QSEoK
|
||||
- Installation of qliksense operator to manage QSEoK
|
||||
- Air gapped installation of QSEoK
|
||||
|
||||
!!! info ""
|
||||
This is a technology preview that uses Qlik modified [kustomize](https://github.com/qlik-oss/kustomize) for kubernetes manifests on [qliksense-k8s](https://github.com/qlik-oss/qliksense-k8s) repository
|
||||
|
||||
!!! info ""
|
||||
See QlikSense [edge releases on qliksense-k8s](https://github.com/qlik-oss/qliksense-k8s/releases) repository
|
||||
|
||||
## Future Direction
|
||||
|
||||
Operations:
|
||||
|
||||
- Expand preflight checks
|
||||
- Backup/restore operations
|
||||
- Fully support airgap installation of QSEoK
|
||||
- Restore unwanted deletion of kubernetes resources
|
||||
@@ -1,253 +0,0 @@
|
||||
##Preflight checks
|
||||
Preflight checks provide pre-installation cluster conformance testing and validation before we install qliksense on the cluster. We gather a suite of conformance tests that can be easily written and run on the target cluster to verify that cluster-specific requirements are met.
|
||||
The suite consists of a set of `collectors` which run the specifications of every test and `analyzers` which analyze the results of every test run by the collector.
|
||||
We support the following tests at the moment as part of preflight checks, and the range of the suite will be expanded in future.
|
||||
|
||||
Run the following command to view help about the commands supported by preflight at any moment:
|
||||
```console
|
||||
$ qliksense preflight
|
||||
perform preflight checks on the cluster
|
||||
|
||||
Usage:
|
||||
qliksense preflight [command]
|
||||
|
||||
Examples:
|
||||
qliksense preflight <preflight_check_to_run>
|
||||
|
||||
Available Commands:
|
||||
all perform all checks
|
||||
dns perform preflight dns check
|
||||
k8s-version check k8s version
|
||||
|
||||
Flags:
|
||||
-h, --help help for preflight
|
||||
```
|
||||
|
||||
### DNS check
|
||||
Run the following command to perform preflight DNS check. We setup a kubernetes deployment and try to reach it as part of establishing DNS connectivity in this check.
|
||||
The expected output should be similar to the one shown below.
|
||||
```console
|
||||
$ qliksense preflight dns
|
||||
|
||||
Preflight DNS check
|
||||
---------------------
|
||||
Created deployment "dep-dns-preflight-check"
|
||||
Created service "svc-dns-pf-check"
|
||||
Created pod: pf-pod-1
|
||||
Fetching pod: pf-pod-1
|
||||
Fetching pod: pf-pod-1
|
||||
Exec-ing into the container...
|
||||
Preflight DNS check: PASSED
|
||||
Completed preflight DNS check
|
||||
Cleaning up resources...
|
||||
Deleted pod: pf-pod-1
|
||||
Deleted service: svc-dns-pf-check
|
||||
Deleted deployment: dep-dns-preflight-check
|
||||
|
||||
```
|
||||
|
||||
### Kubernetes version check
|
||||
We check the version of the target kubernetes cluster and ensure that it falls in the valid range of kubernetes versions that are supported by qliksense.
|
||||
The command to run this check and the expected similar output are as shown below:
|
||||
```console
|
||||
$ qliksense preflight k8s-version
|
||||
|
||||
Preflight kubernetes minimum version check
|
||||
------------------------------------------
|
||||
Kubernetes API Server version: v1.15.5
|
||||
Current K8s Version: 1.15.5
|
||||
Current 1.15.5 is greater than minimum required version:1.11.0, hence good to go
|
||||
Preflight minimum kubernetes version check: PASSED
|
||||
Completed Preflight kubernetes minimum version check
|
||||
|
||||
```
|
||||
|
||||
### Service check
|
||||
We use the commmand below to test if we are able to create a service in the cluster.
|
||||
```console
|
||||
$ qliksense preflight service
|
||||
|
||||
Preflight service check
|
||||
-----------------------
|
||||
|
||||
Preflight service check:
|
||||
Created service "svc-pf-check"
|
||||
Preflight service creation check: PASSED
|
||||
Cleaning up resources...
|
||||
Deleted service: svc-pf-check
|
||||
Completed preflight service check
|
||||
```
|
||||
|
||||
### Deployment check
|
||||
We use the commmand below to test if we are able to create a deployment in the cluster. After the test exexutes, we wait until the created deployment terminates before we exit the command.
|
||||
```console
|
||||
$ qliksense preflight deployment
|
||||
|
||||
Preflight deployment check
|
||||
-----------------------
|
||||
Preflight deployment check:
|
||||
Created deployment "deployment-preflight-check"
|
||||
Preflight Deployment check: PASSED
|
||||
Cleaning up resources...
|
||||
Deleted deployment: deployment-preflight-check
|
||||
Completed preflight deployment check
|
||||
```
|
||||
|
||||
### Pod check
|
||||
We use the commmand below to test if we are able to create a pod in the cluster.
|
||||
```console
|
||||
$ qliksense preflight pod
|
||||
|
||||
Preflight pod check
|
||||
--------------------
|
||||
|
||||
Preflight pod check:
|
||||
Created pod: pod-pf-check
|
||||
Preflight pod creation check: PASSED
|
||||
Cleaning up resources...
|
||||
Deleted pod: pod-pf-check
|
||||
Completed preflight pod check
|
||||
```
|
||||
|
||||
### Create-Role check
|
||||
We use the command below to test if we are able to create a role in the cluster
|
||||
```shell
|
||||
$ qliksense preflight create-role
|
||||
Preflight create-role check
|
||||
---------------------------
|
||||
Preflight create-role check:
|
||||
Created role: role-preflight-check
|
||||
Preflight create-role check: PASSED
|
||||
Cleaning up resources...
|
||||
Deleted role: role-preflight-check
|
||||
|
||||
Completed preflight create-role check
|
||||
```
|
||||
|
||||
### Create-RoleBinding check
|
||||
We use the command below to test if we are able to create a role binding in the cluster
|
||||
```shell
|
||||
$ qliksense preflight createRoleBinding
|
||||
|
||||
Preflight create roleBinding check
|
||||
---------------------------
|
||||
Preflight createRoleBinding check:
|
||||
Created RoleBinding: role-binding-preflight-check
|
||||
Preflight createRoleBinding check: PASSED
|
||||
Cleaning up resources...
|
||||
Deleting RoleBinding: role-binding-preflight-check
|
||||
Deleted RoleBinding: role-binding-preflight-check
|
||||
|
||||
Completed preflight createRoleBinding check
|
||||
```
|
||||
|
||||
### Create-ServiceAccount check
|
||||
We use the command below to test if we are able to create a service account in the cluster
|
||||
```shell
|
||||
$ qliksense preflight createServiceAccount
|
||||
|
||||
Preflight create ServiceAccount check
|
||||
-------------------------------------
|
||||
Preflight createServiceAccount check:
|
||||
Created Service Account: preflight-check-test-serviceaccount
|
||||
Preflight createServiceAccount check: PASSED
|
||||
Cleaning up resources...
|
||||
Deleting ServiceAccount: preflight-check-test-serviceaccount
|
||||
Deleted ServiceAccount: preflight-check-test-serviceaccount
|
||||
|
||||
Completed preflight createServiceAccount check
|
||||
```
|
||||
|
||||
### CreateRB check
|
||||
We use the command below to combine creation of role, role binding, and service account tests
|
||||
```shell
|
||||
$ qliksense preflight createRB
|
||||
|
||||
Preflight createRB check
|
||||
-------------------------------------
|
||||
Preflight create-role check:
|
||||
Created role: role-preflight-check
|
||||
Preflight create-role check: PASSED
|
||||
Cleaning up resources...
|
||||
Deleted role: role-preflight-check
|
||||
|
||||
Completed preflight create-role check
|
||||
|
||||
Preflight create RoleBinding check:
|
||||
Created RoleBinding: role-binding-preflight-check
|
||||
Preflight create RoleBinding check: PASSED
|
||||
Cleaning up resources...
|
||||
Deleted RoleBinding: role-binding-preflight-check
|
||||
|
||||
Completed preflight create RoleBinding check
|
||||
|
||||
Preflight createServiceAccount check:
|
||||
Created Service Account: preflight-check-test-serviceaccount
|
||||
Preflight createServiceAccount check: PASSED
|
||||
Cleaning up resources...
|
||||
Deleted ServiceAccount: preflight-check-test-serviceaccount
|
||||
|
||||
Completed preflight createServiceAccount check
|
||||
Completed preflight CreateRB check
|
||||
```
|
||||
|
||||
### Mongodb check
|
||||
We can check if we are able to connect to an instance of mongodb on the cluster by either supplying the mongodbUri as part of the command or infer it from the current context.
|
||||
|
||||
```shell
|
||||
qliksense preflight mongo --url=<url> OR
|
||||
qliksense preflight mongo
|
||||
|
||||
Preflight mongo check
|
||||
---------------------
|
||||
Preflight mongodb check:
|
||||
Created pod: pf-mongo-pod
|
||||
stdout: MongoDB shell version v4.2.5
|
||||
connecting to: <url>/?compressors=disabled&gssapiServiceName=mongodb
|
||||
Implicit session: session { "id" : UUID("64f639d3-2c93-4894-80f6-ee14acaf56a5") }
|
||||
MongoDB server version: 4.2.5
|
||||
bye
|
||||
stderr:
|
||||
Preflight mongo check: PASSED
|
||||
Deleted pod: pf-mongo-pod
|
||||
Completed preflight mongodb check
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Running all checks
|
||||
Run the command shown below to execute all preflight checks.
|
||||
```console
|
||||
$ qliksense preflight all --mongodb-url=<url> OR
|
||||
$ qliksense preflight all
|
||||
|
||||
Running all preflight checks
|
||||
|
||||
Preflight DNS check
|
||||
-------------------
|
||||
Created deployment "dep-dns-preflight-check"
|
||||
Created service "svc-dns-pf-check"
|
||||
Created pod: pf-pod-1
|
||||
Fetching pod: pf-pod-1
|
||||
Fetching pod: pf-pod-1
|
||||
Exec-ing into the container...
|
||||
Preflight DNS check: PASSED
|
||||
Completed preflight DNS check
|
||||
Cleaning up resources...
|
||||
Deleted pod: pf-pod-1
|
||||
Deleted service: svc-dns-pf-check
|
||||
Deleted deployment: dep-dns-preflight-check
|
||||
|
||||
Preflight kubernetes minimum version check
|
||||
------------------------------------------
|
||||
Kubernetes API Server version: v1.15.5
|
||||
Current K8s Version: 1.15.5
|
||||
Current 1.15.5 is greater than minimum required version:1.11.0, hence good to go
|
||||
Preflight minimum kubernetes version check: PASSED
|
||||
Completed Preflight kubernetes minimum version check
|
||||
...
|
||||
...
|
||||
All preflight checks have PASSED
|
||||
Completed running all preflight checks
|
||||
|
||||
```
|
||||
96
go.mod
96
go.mod
@@ -3,66 +3,68 @@ module github.com/qlik-oss/sense-installer
|
||||
go 1.13
|
||||
|
||||
replace (
|
||||
github.com/Sirupsen/logrus v1.0.5 => github.com/sirupsen/logrus v1.0.5
|
||||
github.com/Sirupsen/logrus v1.3.0 => github.com/Sirupsen/logrus v1.0.6
|
||||
github.com/Sirupsen/logrus v1.4.0 => github.com/sirupsen/logrus v1.0.6
|
||||
// github.com/containerd/containerd v1.3.0-0.20190507210959-7c1e88399ec0 => github.com/containerd/containerd v1.3.2
|
||||
|
||||
github.com/docker/docker => github.com/moby/moby v0.7.3-0.20190826074503-38ab9da00309
|
||||
// github.com/jaguilar/vt100 => github.com/tonistiigi/vt100 v0.0.0-20190402012908-ad4c4a574305
|
||||
// golang.org/x/crypto v0.0.0-20190129210102-0709b304e793 => golang.org/x/crypto v0.0.0-20180904163835-0709b304e793
|
||||
golang.org/x/sys => golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a
|
||||
|
||||
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20191004115801-a2eda9f80ab8
|
||||
k8s.io/client-go => k8s.io/client-go v0.0.0-20191016111102-bec269661e48
|
||||
k8s.io/kubectl => k8s.io/kubectl v0.0.0-20191016120415-2ed914427d51
|
||||
|
||||
sigs.k8s.io/kustomize/api => github.com/qlik-oss/kustomize/api v0.3.3-0.20200402170547-2e8140160c36
|
||||
)
|
||||
|
||||
require (
|
||||
cloud.google.com/go v0.52.0 // indirect
|
||||
cloud.google.com/go/storage v1.5.0 // indirect
|
||||
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
|
||||
github.com/Masterminds/semver/v3 v3.0.3
|
||||
github.com/Shopify/ejson v1.2.1
|
||||
github.com/aws/aws-sdk-go v1.28.9 // indirect
|
||||
github.com/Microsoft/hcsshim v0.8.7 // indirect
|
||||
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d // indirect
|
||||
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 // indirect
|
||||
github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 // indirect
|
||||
github.com/bitly/go-simplejson v0.5.0 // indirect
|
||||
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
|
||||
github.com/bugsnag/bugsnag-go v1.5.3 // indirect
|
||||
github.com/bugsnag/panicwrap v1.2.0 // indirect
|
||||
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
|
||||
github.com/cloudflare/cfssl v1.4.1 // indirect
|
||||
github.com/containerd/containerd v1.3.2 // indirect
|
||||
github.com/containerd/continuity v0.0.0-20191214063359-1097c8bae83b // indirect
|
||||
github.com/containers/image/v5 v5.1.0
|
||||
github.com/docker/cli v0.0.0-20191212191748-ebca1413117a // indirect
|
||||
github.com/docker/docker v1.4.2-0.20190924003213-a8608b5b67c7 // indirect
|
||||
github.com/docker/cli v0.0.0-20191212191748-ebca1413117a
|
||||
github.com/docker/distribution v2.7.1+incompatible
|
||||
github.com/docker/docker v1.4.2-0.20190924003213-a8608b5b67c7
|
||||
github.com/docker/docker-credential-helpers v0.6.3 // indirect
|
||||
github.com/docker/go v1.5.1-1 // indirect
|
||||
github.com/docker/go-connections v0.4.0 // indirect
|
||||
github.com/docker/go-metrics v0.0.1 // indirect
|
||||
github.com/gobuffalo/envy v1.9.0 // indirect
|
||||
github.com/gobuffalo/logger v1.0.3 // indirect
|
||||
github.com/gobuffalo/packd v1.0.0 // indirect
|
||||
github.com/gobuffalo/packr/v2 v2.7.1
|
||||
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
|
||||
github.com/golang/protobuf v1.3.3 // indirect
|
||||
github.com/google/uuid v1.1.1
|
||||
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
|
||||
github.com/gofrs/uuid v3.2.0+incompatible // indirect
|
||||
github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d // indirect
|
||||
github.com/gorilla/mux v1.7.3 // indirect
|
||||
github.com/hashicorp/golang-lru v0.5.4 // indirect
|
||||
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a
|
||||
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect
|
||||
github.com/jinzhu/gorm v1.9.11 // indirect
|
||||
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.4
|
||||
github.com/mattn/go-tty v0.0.3
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
|
||||
github.com/lib/pq v1.2.0 // indirect
|
||||
github.com/mattn/go-sqlite3 v2.0.1+incompatible // indirect
|
||||
github.com/miekg/pkcs11 v1.0.3 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0
|
||||
github.com/morikuni/aec v1.0.0 // indirect
|
||||
github.com/pkg/errors v0.8.1
|
||||
github.com/qlik-oss/k-apis v0.0.34
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
github.com/rogpeppe/go-internal v1.5.2 // indirect
|
||||
github.com/spf13/cobra v0.0.6
|
||||
github.com/opencontainers/image-spec v1.0.1 // indirect
|
||||
github.com/opencontainers/runc v0.1.1 // indirect
|
||||
github.com/sirupsen/logrus v1.4.2 // indirect
|
||||
github.com/spf13/cobra v0.0.5
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/spf13/viper v1.6.1
|
||||
github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31
|
||||
golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 // indirect
|
||||
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a // indirect
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b
|
||||
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
|
||||
golang.org/x/tools v0.0.0-20200312194400-c312e98713c2 // indirect
|
||||
google.golang.org/genproto v0.0.0-20200128133413-58ce757ed39b // indirect
|
||||
google.golang.org/grpc v1.27.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.8
|
||||
gopkg.in/yaml.v3 v3.0.0-20190924164351-c8b7dadae555
|
||||
k8s.io/api v0.17.0
|
||||
k8s.io/apimachinery v0.17.0
|
||||
k8s.io/client-go v11.0.0+incompatible
|
||||
k8s.io/kubectl v0.0.0-20191016120415-2ed914427d51
|
||||
sigs.k8s.io/kustomize/api v0.3.2
|
||||
sigs.k8s.io/yaml v1.1.0
|
||||
github.com/theupdateframework/notary v0.6.1 // indirect
|
||||
github.com/xlab/handysort v0.0.0-20150421192137-fb3537ed64a1 // indirect
|
||||
golang.org/x/crypto v0.0.0-20191202143827-86a70503ff7e // indirect
|
||||
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553
|
||||
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 // indirect
|
||||
google.golang.org/grpc v1.24.0 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
|
||||
gopkg.in/dancannon/gorethink.v3 v3.0.5 // indirect
|
||||
gopkg.in/fatih/pool.v2 v2.0.0 // indirect
|
||||
gopkg.in/gorethink/gorethink.v3 v3.0.5 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.7
|
||||
vbom.ml/util v0.0.0-20180919145318-efcd4e0f9787 // indirect
|
||||
)
|
||||
|
||||
exclude github.com/Azure/go-autorest v12.0.0+incompatible
|
||||
|
||||
24
mkdocs.yml
24
mkdocs.yml
@@ -1,24 +0,0 @@
|
||||
site_name: Qlik Sense installer
|
||||
repo_url: 'https://github.com/qlik-oss/sense-installer'
|
||||
strict: true
|
||||
theme:
|
||||
name: "material"
|
||||
palette:
|
||||
primary: 'green'
|
||||
accent: 'indigo'
|
||||
markdown_extensions:
|
||||
- toc:
|
||||
permalink: true
|
||||
- admonition
|
||||
- codehilite
|
||||
- pymdownx.inlinehilite
|
||||
- pymdownx.superfences
|
||||
- pymdownx.details
|
||||
nav:
|
||||
- Overview: index.md
|
||||
- getting_started.md
|
||||
- command_reference.md
|
||||
- concepts.md
|
||||
- preflight_checks.md
|
||||
- air_gap.md
|
||||
- Releases ⧉: https://github.com/qlik-oss/sense-installer/releases
|
||||
BIN
pkg/.DS_Store
vendored
BIN
pkg/.DS_Store
vendored
Binary file not shown.
541
pkg/api/apis.go
541
pkg/api/apis.go
@@ -1,541 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/qlik-oss/k-apis/pkg/config"
|
||||
|
||||
b64 "encoding/base64"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
)
|
||||
|
||||
const (
|
||||
pushSecretFileName = "image-registry-push-secret.yaml"
|
||||
pullSecretFileName = "image-registry-pull-secret.yaml"
|
||||
qliksenseContextsDirName = "contexts"
|
||||
qliksenseSecretsDirName = "secrets"
|
||||
qliksenseEjsonDirName = "ejson"
|
||||
)
|
||||
|
||||
// NewQConfig create QliksenseConfig object from file ~/.qliksense/config.yaml
|
||||
func NewQConfig(qsHome string) *QliksenseConfig {
|
||||
qc, err := NewQConfigE(qsHome)
|
||||
if err != nil {
|
||||
fmt.Println("yaml unmarshalling error ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return qc
|
||||
}
|
||||
|
||||
func NewQConfigE(qsHome string) (*QliksenseConfig, error) {
|
||||
configFile := filepath.Join(qsHome, "config.yaml")
|
||||
qc := &QliksenseConfig{}
|
||||
|
||||
err := ReadFromFile(qc, configFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
qc.QliksenseHomePath = qsHome
|
||||
return qc, nil
|
||||
}
|
||||
func NewQConfigEmpty(qsHome string) *QliksenseConfig {
|
||||
return &QliksenseConfig{
|
||||
QliksenseHomePath: qsHome,
|
||||
}
|
||||
}
|
||||
|
||||
// GetCR create a QliksenseCR object for a particular context
|
||||
// from file ~/.qliksense/contexts/<contx-name>/<contx-name>.yaml
|
||||
func (qc *QliksenseConfig) GetCR(contextName string) (*QliksenseCR, error) {
|
||||
crFilePath := qc.GetCRFilePath(contextName)
|
||||
if crFilePath == "" {
|
||||
return nil, errors.New("context name " + contextName + " not found")
|
||||
}
|
||||
return qc.GetAndTransformCrObject(crFilePath)
|
||||
}
|
||||
|
||||
// GetCurrentCR create a QliksenseCR object for current context
|
||||
func (qc *QliksenseConfig) GetCurrentCR() (*QliksenseCR, error) {
|
||||
return qc.GetCR(qc.Spec.CurrentContext)
|
||||
}
|
||||
|
||||
// SetCrLocation sets the CR location for a context. Helpful during test
|
||||
func (qc *QliksenseConfig) SetCrLocation(contextName, filePath string) (*QliksenseConfig, error) {
|
||||
tempQc := &QliksenseConfig{}
|
||||
copier.Copy(tempQc, qc)
|
||||
found := false
|
||||
tempQc.Spec.Contexts = []Context{}
|
||||
for _, c := range qc.Spec.Contexts {
|
||||
if c.Name == contextName {
|
||||
c.CrFile = filePath
|
||||
found = true
|
||||
}
|
||||
tempQc.Spec.Contexts = append(tempQc.Spec.Contexts, []Context{c}...)
|
||||
}
|
||||
if found {
|
||||
return tempQc, nil
|
||||
}
|
||||
return nil, errors.New("cannot find the context")
|
||||
}
|
||||
|
||||
// GetCRObject create a qliksense CR object from file
|
||||
func GetCRObject(crfile string) (*QliksenseCR, error) {
|
||||
cr := &QliksenseCR{}
|
||||
err := ReadFromFile(cr, crfile)
|
||||
if err != nil {
|
||||
fmt.Println("cannot unmarshal cr ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cr, nil
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) GetAndTransformCrObject(crfile string) (*QliksenseCR, error) {
|
||||
cr, err := GetCRObject(crfile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cr.Spec.ManifestsRoot != "" && !filepath.IsAbs(cr.Spec.ManifestsRoot) {
|
||||
cr.Spec.ManifestsRoot = filepath.Join(qc.QliksenseHomePath, cr.Spec.ManifestsRoot)
|
||||
}
|
||||
return cr, nil
|
||||
}
|
||||
|
||||
//CreateCRObjectFromString create a QliksenseCR from string content
|
||||
func CreateCRObjectFromString(crContent string) (*QliksenseCR, error) {
|
||||
if crContent == "" {
|
||||
return nil, errors.New("empty string cannot qliksensecr")
|
||||
}
|
||||
cr := &QliksenseCR{}
|
||||
err := ReadFromStream(cr, strings.NewReader(crContent))
|
||||
if err != nil {
|
||||
fmt.Println("cannot unmarshal cr ", err)
|
||||
return nil, err
|
||||
}
|
||||
return cr, nil
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) GetCRFilePath(contextName string) string {
|
||||
crFilePath := ""
|
||||
for _, ctx := range qc.Spec.Contexts {
|
||||
if ctx.Name == contextName {
|
||||
crFilePath = filepath.Join(qc.QliksenseHomePath, ctx.CrFile)
|
||||
break
|
||||
}
|
||||
}
|
||||
return crFilePath
|
||||
}
|
||||
|
||||
func (cr *QliksenseCR) IsRepoExist() bool {
|
||||
if cr.Spec.ManifestsRoot == "" {
|
||||
return false
|
||||
}
|
||||
if _, err := os.Lstat(cr.Spec.ManifestsRoot); err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) IsRepoExist(contextName, version string) bool {
|
||||
if _, err := os.Lstat(qc.BuildRepoPathForContext(contextName, version)); err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) IsRepoExistForCurrent(version string) bool {
|
||||
if _, err := os.Lstat(qc.BuildRepoPath(version)); err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) BuildRepoPath(version string) string {
|
||||
return qc.BuildRepoPathForContext(qc.Spec.CurrentContext, version)
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) BuildRepoPathForContext(contextName, version string) string {
|
||||
return filepath.Join(qc.GetContextPath(contextName), "qlik-k8s", version)
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) BuildCurrentManifestsRoot(version string) string {
|
||||
return qc.BuildRepoPath(version)
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) WriteCR(cr *QliksenseCR) error {
|
||||
crf := qc.GetCRFilePath(cr.GetName())
|
||||
if crf == "" {
|
||||
return errors.New("context name " + cr.GetName() + " not found")
|
||||
}
|
||||
|
||||
return qc.TransformAndWriteCr(cr, crf)
|
||||
}
|
||||
|
||||
//CreateOrWriteCrAndContext create necessary folder structure, update config.yaml and context yaml files
|
||||
func (qc *QliksenseConfig) CreateOrWriteCrAndContext(cr *QliksenseCR) error {
|
||||
if qc.QliksenseHomePath == "" {
|
||||
return errors.New("qliksense home is not set")
|
||||
}
|
||||
crf := qc.GetCRFilePath(cr.GetName())
|
||||
if crf == "" {
|
||||
// create direcotry structure for context
|
||||
cDir := filepath.Join(qc.QliksenseHomePath, "contexts", cr.GetName())
|
||||
if err := os.MkdirAll(cDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
crf = filepath.Join(cDir, cr.GetName()+".yaml")
|
||||
ctx := Context{
|
||||
Name: cr.GetName(),
|
||||
CrFile: "contexts/" + cr.GetName() + "/" + cr.GetName() + ".yaml", //filepath.Join("contexts", cr.GetName(), cr.GetName()+".yaml"),
|
||||
}
|
||||
qc.AddToContexts(ctx)
|
||||
|
||||
if err := qc.Write(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return qc.TransformAndWriteCr(cr, crf)
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) TransformAndWriteCr(cr *QliksenseCR, file string) error {
|
||||
if strings.HasPrefix(cr.Spec.ManifestsRoot, qc.QliksenseHomePath) {
|
||||
cr.Spec.ManifestsRoot = strings.Replace(cr.Spec.ManifestsRoot, qc.QliksenseHomePath+"/", "", 1)
|
||||
cr.Spec.ManifestsRoot = strings.Replace(cr.Spec.ManifestsRoot, qc.QliksenseHomePath+"\\", "", 1)
|
||||
cr.Spec.ManifestsRoot = strings.Replace(cr.Spec.ManifestsRoot, "\\", "/", -1)
|
||||
}
|
||||
if err := WriteToFile(cr, file); err != nil {
|
||||
return err
|
||||
}
|
||||
if cr.Spec.ManifestsRoot != "" {
|
||||
cr.Spec.ManifestsRoot = filepath.Join(qc.QliksenseHomePath, cr.Spec.ManifestsRoot)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (qc *QliksenseConfig) AddToContexts(ctx Context) error {
|
||||
//TODO: additional duplicate check may be added latter
|
||||
qc.Spec.Contexts = append(qc.Spec.Contexts, ctx)
|
||||
|
||||
return nil
|
||||
}
|
||||
func (qc *QliksenseConfig) WriteCurrentContextCR(cr *QliksenseCR) error {
|
||||
return qc.WriteCR(cr)
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) IsContextExist(ctxName string) bool {
|
||||
for _, ct := range qc.Spec.Contexts {
|
||||
if ct.Name == ctxName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) GetCurrentContextDir() (string, error) {
|
||||
if qcr, err := qc.GetCurrentCR(); err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
return filepath.Join(qc.QliksenseHomePath, qliksenseContextsDirName, qcr.GetObjectMeta().GetName()), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) GetCurrentContextSecretsDir() (string, error) {
|
||||
if currentContextDir, err := qc.GetCurrentContextDir(); err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
return filepath.Join(currentContextDir, qliksenseSecretsDirName), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) setDockerConfigJsonSecret(filename string, dockerConfigJsonSecret *DockerConfigJsonSecret) error {
|
||||
if secretsDir, err := qc.GetCurrentContextSecretsDir(); err != nil {
|
||||
return err
|
||||
} else if publicKey, _, err := qc.GetCurrentContextEncryptionKeyPair(); err != nil {
|
||||
return err
|
||||
} else if dockerConfigJsonSecretYaml, err := dockerConfigJsonSecret.ToYaml(publicKey); err != nil {
|
||||
return err
|
||||
} else if err := os.MkdirAll(secretsDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return ioutil.WriteFile(filepath.Join(secretsDir, filename), dockerConfigJsonSecretYaml, os.ModePerm)
|
||||
}
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) SetPushDockerConfigJsonSecret(dockerConfigJsonSecret *DockerConfigJsonSecret) error {
|
||||
return qc.setDockerConfigJsonSecret(pushSecretFileName, dockerConfigJsonSecret)
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) SetPullDockerConfigJsonSecret(dockerConfigJsonSecret *DockerConfigJsonSecret) error {
|
||||
return qc.setDockerConfigJsonSecret(pullSecretFileName, dockerConfigJsonSecret)
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) GetPushDockerConfigJsonSecret() (*DockerConfigJsonSecret, error) {
|
||||
return qc.getDockerConfigJsonSecret(pushSecretFileName)
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) GetPullDockerConfigJsonSecret() (*DockerConfigJsonSecret, error) {
|
||||
return qc.getDockerConfigJsonSecret(pullSecretFileName)
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) DeletePushDockerConfigJsonSecret() error {
|
||||
return qc.deleteDockerConfigJsonSecret(pushSecretFileName)
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) DeletePullDockerConfigJsonSecret() error {
|
||||
return qc.deleteDockerConfigJsonSecret(pullSecretFileName)
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) deleteDockerConfigJsonSecret(name string) error {
|
||||
if secretsDir, err := qc.GetCurrentContextSecretsDir(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return os.Remove(filepath.Join(secretsDir, name))
|
||||
}
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) getDockerConfigJsonSecret(name string) (*DockerConfigJsonSecret, error) {
|
||||
dockerConfigJsonSecret := &DockerConfigJsonSecret{}
|
||||
if secretsDir, err := qc.GetCurrentContextSecretsDir(); err != nil {
|
||||
return nil, err
|
||||
} else if dockerConfigJsonSecretYaml, err := ioutil.ReadFile(filepath.Join(secretsDir, name)); err != nil {
|
||||
return nil, err
|
||||
} else if _, privateKey, err := qc.GetCurrentContextEncryptionKeyPair(); err != nil {
|
||||
return nil, err
|
||||
} else if err := dockerConfigJsonSecret.FromYaml(dockerConfigJsonSecretYaml, privateKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dockerConfigJsonSecret, nil
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) getCurrentContextEncryptionKeyPairLocation() (string, error) {
|
||||
|
||||
if qcr, err := qc.GetCurrentCR(); err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
return qc.getContextEncryptionKeyPairLocation(qcr.GetName())
|
||||
}
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) getContextEncryptionKeyPairLocation(contextName string) (string, error) {
|
||||
// Check env var: QLIKSENSE_KEY_LOCATION to determine location to store keypair
|
||||
var secretKeyPairLocation string
|
||||
if os.Getenv("QLIKSENSE_KEY_LOCATION") != "" {
|
||||
LogDebugMessage("Env variable: QLIKSENSE_KEY_LOCATION= %s", os.Getenv("QLIKSENSE_KEY_LOCATION"))
|
||||
secretKeyPairLocation = os.Getenv("QLIKSENSE_KEY_LOCATION")
|
||||
} else {
|
||||
// QLIKSENSE_KEY_LOCATION has not been set, hence storing key pair in default location:
|
||||
// /.qliksense/secrets/contexts/<current-context>/secrets/
|
||||
secretKeyPairLocation = filepath.Join(qc.QliksenseHomePath, qliksenseSecretsDirName, qliksenseContextsDirName, contextName, qliksenseSecretsDirName)
|
||||
|
||||
}
|
||||
return secretKeyPairLocation, nil
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) GetCurrentContextEjsonKeyDir() (string, error) {
|
||||
if qcr, err := qc.GetCurrentCR(); err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
ejsonKeyDir := filepath.Join(qc.QliksenseHomePath, qliksenseSecretsDirName, qliksenseContextsDirName, qcr.GetObjectMeta().GetName(), qliksenseEjsonDirName)
|
||||
if err := os.MkdirAll(ejsonKeyDir, os.ModePerm); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return ejsonKeyDir, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) GetCurrentContextEncryptionKeyPair() (*rsa.PublicKey, *rsa.PrivateKey, error) {
|
||||
if qcr, err := qc.GetCurrentCR(); err != nil {
|
||||
return nil, nil, err
|
||||
} else {
|
||||
return qc.GetContextEncryptionKeyPair(qcr.GetName())
|
||||
}
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) GetContextEncryptionKeyPair(contextName string) (*rsa.PublicKey, *rsa.PrivateKey, error) {
|
||||
secretKeyPairLocation, err := qc.getContextEncryptionKeyPairLocation(contextName)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
publicKeyFilePath := filepath.Join(secretKeyPairLocation, QliksensePublicKey)
|
||||
privateKeyFilePath := filepath.Join(secretKeyPairLocation, QliksensePrivateKey)
|
||||
// try to create the dir if it doesn't exist
|
||||
if !FileExists(publicKeyFilePath) || !FileExists(privateKeyFilePath) {
|
||||
LogDebugMessage("Qliksense secretKeyLocation dir does not exist, creating it now: %s", secretKeyPairLocation)
|
||||
if err := os.MkdirAll(secretKeyPairLocation, os.ModePerm); err != nil {
|
||||
err = fmt.Errorf("Not able to create %s dir: %v", secretKeyPairLocation, err)
|
||||
log.Println(err)
|
||||
return nil, nil, err
|
||||
}
|
||||
// generating and storing key-pair
|
||||
err1 := GenerateAndStoreSecretKeypair(secretKeyPairLocation)
|
||||
if err1 != nil {
|
||||
err1 = fmt.Errorf("Not able to generate and store key pair for encryption")
|
||||
log.Println(err1)
|
||||
return nil, nil, err1
|
||||
}
|
||||
}
|
||||
|
||||
if publicKeyBytes, err := ReadKeys(publicKeyFilePath); err != nil {
|
||||
LogDebugMessage("Not able to read public key")
|
||||
return nil, nil, err
|
||||
} else if privateKeyBytes, err := ReadKeys(privateKeyFilePath); err != nil {
|
||||
LogDebugMessage("Not able to read private key")
|
||||
return nil, nil, err
|
||||
} else if rsaPublicKey, err := DecodeToPublicKey(publicKeyBytes); err != nil {
|
||||
return nil, nil, err
|
||||
} else if rsaPrivateKey, err := DecodeToPrivateKey(privateKeyBytes); err != nil {
|
||||
return nil, nil, err
|
||||
} else {
|
||||
return rsaPublicKey, rsaPrivateKey, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (cr *QliksenseCR) AddLabelToCr(key, value string) {
|
||||
m := cr.GetObjectMeta().GetLabels()
|
||||
if m == nil {
|
||||
m = make(map[string]string)
|
||||
}
|
||||
m[key] = value
|
||||
cr.GetObjectMeta().SetLabels(m)
|
||||
}
|
||||
|
||||
func (cr *QliksenseCR) GetLabelFromCr(key string) string {
|
||||
return cr.GetObjectMeta().GetLabels()[key]
|
||||
}
|
||||
|
||||
func (cr *QliksenseCR) GetString() (string, error) {
|
||||
out, err := K8sToYaml(cr)
|
||||
if err != nil {
|
||||
fmt.Println("cannot unmarshal cr ", err)
|
||||
return "", err
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
||||
|
||||
func (cr *QliksenseCR) GetImageRegistry() string {
|
||||
for _, nameValues := range cr.Spec.Configs {
|
||||
for _, nameValue := range nameValues {
|
||||
if nameValue.Name == "imageRegistry" {
|
||||
return nameValue.Value
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (cr *QliksenseCR) GetK8sSecretsFolder(qlikSenseHomeDir string) string {
|
||||
return filepath.Join(qlikSenseHomeDir, qliksenseContextsDirName, cr.GetName(), qliksenseSecretsDirName)
|
||||
}
|
||||
|
||||
func (cr *QliksenseCR) IsEULA() bool {
|
||||
for k, nvs := range cr.Spec.Configs {
|
||||
if k == "qliksense" {
|
||||
for _, nv := range nvs {
|
||||
if nv.Name == "acceptEULA" {
|
||||
return nv.Value == "yes"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (cr *QliksenseCR) SetEULA(value string) {
|
||||
cr.Spec.AddToConfigs("qliksense", "acceptEULA", value)
|
||||
}
|
||||
|
||||
// GetCustomCrdsPath get crds path if exist in the profile dir
|
||||
func (cr *QliksenseCR) GetCustomCrdsPath() string {
|
||||
if cr.Spec.ManifestsRoot == "" || cr.Spec.Profile == "" {
|
||||
return ""
|
||||
}
|
||||
crdsPath := filepath.Join(cr.Spec.GetManifestsRoot(), "manifests", cr.Spec.Profile, "crds")
|
||||
if _, err := os.Lstat(crdsPath); err != nil {
|
||||
return ""
|
||||
}
|
||||
return crdsPath
|
||||
}
|
||||
|
||||
// GetDecryptedCr it decrypts all the encrypted value and return a new CR
|
||||
func (qc *QliksenseConfig) GetDecryptedCr(cr *QliksenseCR) (*QliksenseCR, error) {
|
||||
newCr := &QliksenseCR{}
|
||||
copier.Copy(newCr, cr)
|
||||
_, rsaPrivateKey, err := qc.GetCurrentContextEncryptionKeyPair()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
finalSecrets := map[string]config.NameValues{}
|
||||
for k, nvs := range newCr.Spec.Secrets {
|
||||
newNvs := config.NameValues{}
|
||||
for _, nv := range nvs {
|
||||
if nv.Value != "" {
|
||||
b, err := b64.StdEncoding.DecodeString(strings.TrimSpace(nv.Value))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db, err := Decrypt(b, rsaPrivateKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newNvs = append(newNvs, config.NameValue{
|
||||
Name: nv.Name,
|
||||
Value: string(db),
|
||||
})
|
||||
}
|
||||
}
|
||||
finalSecrets[k] = newNvs
|
||||
}
|
||||
newCr.Spec.Secrets = finalSecrets
|
||||
return newCr, nil
|
||||
}
|
||||
|
||||
//Validate validate CR
|
||||
func (cr *QliksenseCR) Validate() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
//CreateContextDirs create context dir structure ~/.qliksense/contexts/contextName
|
||||
func (qc *QliksenseConfig) CreateContextDirs(contextName string) error {
|
||||
return os.MkdirAll(qc.GetContextPath(contextName), os.ModePerm)
|
||||
}
|
||||
|
||||
func (qc *QliksenseConfig) GetContextPath(contextName string) string {
|
||||
return filepath.Join(qc.QliksenseHomePath, qliksenseContextsDirName, contextName)
|
||||
}
|
||||
|
||||
//BuildCrFileAbsolutePath build absolute path for a cr ie. ~/.qliksense/contexts/qlik-defautl/qlik-default.yaml
|
||||
func (qc *QliksenseConfig) BuildCrFileAbsolutePath(contextName string) string {
|
||||
return filepath.Join(qc.GetContextPath(contextName), contextName+".yaml")
|
||||
}
|
||||
|
||||
//BuildCrFilePath build cr file path i.e. contexts/qlik-default/qlik-default.yaml
|
||||
func (qc *QliksenseConfig) BuildCrFilePath(contextName string) string {
|
||||
return filepath.Join(qc.GetContextPath(contextName), contextName+".yaml")
|
||||
}
|
||||
|
||||
//AddToContexts add the context into qc.Spec.Contexts
|
||||
func (qc *QliksenseConfig) AddToContextsRaw(crName, crFile string) {
|
||||
qc.Spec.Contexts = append(qc.Spec.Contexts, []Context{
|
||||
{CrFile: crFile,
|
||||
Name: crName},
|
||||
}...)
|
||||
}
|
||||
|
||||
//SetCurrentContextName set the qc.Spec.CurrentContext
|
||||
func (qc *QliksenseConfig) SetCurrentContextName(name string) {
|
||||
qc.Spec.CurrentContext = name
|
||||
}
|
||||
|
||||
//Write write QliksenseConfig into config.yaml
|
||||
func (qc *QliksenseConfig) Write() error {
|
||||
return WriteToFile(qc, filepath.Join(qc.QliksenseHomePath, "config.yaml"))
|
||||
}
|
||||
@@ -1,225 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const tempPermissionCode os.FileMode = 0777
|
||||
|
||||
func setup() (func(), string) {
|
||||
dir, _ := ioutil.TempDir("", "testing_path")
|
||||
config :=
|
||||
`
|
||||
apiVersion: config.qlik.com/v1
|
||||
kind: QliksenseConfig
|
||||
metadata:
|
||||
name: whatever
|
||||
spec:
|
||||
contexts:
|
||||
- name: contx1
|
||||
crLocation: /Users/mqb/.qliksense/contexts/contx1
|
||||
- name: cotx2
|
||||
crLocation: /root/.qliksense/contexts/cotx2.yaml
|
||||
currentContext: contx1
|
||||
`
|
||||
configFile := filepath.Join(dir, "config.yaml")
|
||||
ioutil.WriteFile(configFile, []byte(config), tempPermissionCode)
|
||||
tearDown := func() {
|
||||
os.RemoveAll(dir)
|
||||
}
|
||||
return tearDown, dir
|
||||
}
|
||||
|
||||
func createCRFile(homeDir string) {
|
||||
cr :=
|
||||
`
|
||||
apiVersion: qlik.com/v1
|
||||
kind: QlikSense
|
||||
metadata:
|
||||
name: contx1
|
||||
labels:
|
||||
version: v1.0.0
|
||||
spec:
|
||||
profile: docker-desktop
|
||||
manifestsRoot: /Users/mqb/.qliksense/contexts/contx1/qlik-k8s/v0.0.1/manifests
|
||||
storageClassName: efs
|
||||
configs:
|
||||
qliksense:
|
||||
- name: acceptEULA
|
||||
value: "yes"
|
||||
secrets:
|
||||
qliksense:
|
||||
- name: mongoDbUri
|
||||
# this is rsa encrypted value, the pub and pri keys are in setuPublicAndPrivateKey() method
|
||||
# actual value: mongodb://qlik-default-mongodb:27017/qliksense?ssl=false
|
||||
value: n/pDi7Z/A3i16cAHFFwMp19/egNKc8WZxm6MKHLT/B1DMv3U6pDXWyXT5fYYDV1wDTO3Vk43yECST1UgZYmMpgUOwgSfGgqTVi2VqS0JQsnwI+Twwhnvha8RJANX8b/XIoSFVWaOgy7+RP35ZkvOqHdCfC2aT8JMIHgBQqqCbsNgimCuRSxi0klR000ic/Tp5PYSz5mD+WLrkPw2FbS0OVBsQ/hIp5GZrmVpvEOZdbT63Sz+n/G4Br6GTv2LkZcU7JBuKQm2wfB+mRjJmJnNrPawLfn2UZ89Rz0BLwIy+6b24/RoIUgoNowfGkJreGiwItGK8fjCcx11oavK/yAo6pYZXCcru46pmHbxxle1OlkdTKkG6EVtJuKjSZXtVmBHZYRFzsR7HnAiXnL7QzSEcS7ieZlQvTmNLfpidJhK199oSbyKREqXGl2S8DzPKM9RLccVbQTy6X8qWimP3MYCnO4K0KoQnNQAgfuV8ZxnvdDecByLDPIpmFMGy0Xm9pUZWxmSoDBq+p5WBI2HdCX2gCYVv5yxS2iBqO5SMKo8iOglHtPI9NIMvloERdN1vZtxSRkY5uDEfrU9ysYwfayEXxvXmdWv0HxlotcgUinP02j7k+OfIapTmY/jGfvF4euyCGRKuJ9JlSD9pIiRdAcekjL6hCxXLJLdajCV4sL/YDo=
|
||||
`
|
||||
ctx1Dir := filepath.Join(homeDir, "contexts", "contx1")
|
||||
crFile := filepath.Join(ctx1Dir, "contx1.yaml")
|
||||
os.MkdirAll(ctx1Dir, tempPermissionCode)
|
||||
ioutil.WriteFile(crFile, []byte(cr), tempPermissionCode)
|
||||
|
||||
}
|
||||
func TestGetCR(t *testing.T) {
|
||||
td, dir := setup()
|
||||
qc := NewQConfig(dir)
|
||||
if qc.Spec.CurrentContext != "contx1" {
|
||||
t.Fail()
|
||||
}
|
||||
// create CR
|
||||
createCRFile(dir)
|
||||
|
||||
crFile := filepath.Join("contexts", "contx1", "contx1.yaml")
|
||||
qct, e := qc.SetCrLocation("contx1", crFile)
|
||||
if e != nil {
|
||||
t.Fail()
|
||||
t.Log(e)
|
||||
}
|
||||
qcr, err := qct.GetCurrentCR()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
t.Log(err)
|
||||
}
|
||||
if qcr.Spec.Profile != "docker-desktop" {
|
||||
t.Fail()
|
||||
}
|
||||
td()
|
||||
}
|
||||
|
||||
func TestGetDecryptedCr(t *testing.T) {
|
||||
td, dir := setup()
|
||||
qc := NewQConfig(dir)
|
||||
if qc.Spec.CurrentContext != "contx1" {
|
||||
t.Fail()
|
||||
}
|
||||
// create CR
|
||||
createCRFile(dir)
|
||||
|
||||
crFile := filepath.Join("contexts", "contx1", "contx1.yaml")
|
||||
qct, e := qc.SetCrLocation("contx1", crFile)
|
||||
if e != nil {
|
||||
t.Fail()
|
||||
t.Log(e)
|
||||
}
|
||||
qcr, err := qct.GetCurrentCR()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
t.Log(err)
|
||||
}
|
||||
|
||||
setuPublicAndPrivateKey(dir)
|
||||
newCr, err := qct.GetDecryptedCr(qcr)
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
t.Log(err)
|
||||
}
|
||||
|
||||
decryptedValue := newCr.Spec.GetFromSecrets("qliksense", "mongoDbUri")
|
||||
orignalValue := qcr.Spec.GetFromSecrets("qliksense", "mongoDbUri")
|
||||
if decryptedValue != "mongodb://qlik-default-mongodb:27017/qliksense?ssl=false" {
|
||||
t.Fail()
|
||||
b, _ := K8sToYaml(newCr)
|
||||
t.Log(b)
|
||||
}
|
||||
if decryptedValue == orignalValue {
|
||||
t.Fail()
|
||||
}
|
||||
td()
|
||||
}
|
||||
func setuPublicAndPrivateKey(homeDir string) ([]byte, []byte, error) {
|
||||
privKeyBytes := []byte(`-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIJJwIBAAKCAgEAwUCimKCidbF3UxEHPy8K+hvhklRB9JYhj5sJy0if4lTVibkK
|
||||
1MrYCykOnmC40pPU9GLY1b8HxAg9tvyRn0YHUxOra6vVQaVcOVJhTM8D18d+lSr3
|
||||
Lp1yiX+UGT4nzWI9+R1CCbwXrqeQVoZs6QZKynEXMkFI9/wNMOwPOvQFOSTuoEoC
|
||||
O+zyTyUWEkNbUq825ELUQdIsjgmlWUOONudxsAr7ESRXW9QTHVh6uWmr3VRKZHby
|
||||
1JdU3I/wjdlGg5M2dDuXy5nQO9w/nYLjJXiw+zzOetZ/+t7/VOkOpNTeJQhwTM1W
|
||||
F7Y2VLetbi9FHgyzHatrduh07+XEiTbgDf3GIx2bp2p6oh0G3N2zpiLcK/aZj8ro
|
||||
uWWydfFfsU3MZ4FfJDP8I6b9awxjmKYqIr6hiPQCJaLBED8mwK+I5evIbnKv6E6u
|
||||
K+BApWA/R7ElragoFYbqQ1VpvntVMtJt9Dy5ZrI+IQARdXD3bb34oh0IPBhClnvv
|
||||
MUc1cWxDoXEX6oJ4I+LzxE87Zkwnan9qOwengolMVKFwPx1o37qrbmrXID21kKt7
|
||||
FL6xN4HxHLkItr1fKzdyWDFRHgASTAWfx5BIwvPuUW0vZHkvO80VyV2L63whVhPn
|
||||
PASmFkbviomrBttYfpr2aGQqF/qR1Nlxe834MFxk1pS9LMa/WnzvFr0gWakCAwEA
|
||||
AQKCAgARSp9B2N2wejibDiL/3E23I1eDqFZedDB8kPrHXbAwqDaTJCN79spt9TaB
|
||||
pVXkQaYEV/Pe7EDdoX8kKGU/QxzUqiXkdHOYdBtUZbKfFMbbP9ZrsnR7j0r4UpoF
|
||||
yDH3hprU93E5PcNAtW2M0GpeT1nR01yn+n908PCdOAIE3GC7RDq1zOl2QzVLL55R
|
||||
9ATv2Q2oTvJ/ETc7XlGVMx4+e2cIwXLFjeLjLI6pSYlxnarrGuetJZeEviWxto9n
|
||||
odFVZI6yx8JFTXX8ZTCr/1IjwDDVyhMPmrHI2Lsv9cqBpSpbVe32cUkKxhsGaYjz
|
||||
GvesQKamOPhco2ATNxPm0yopFlPsGKMfVl0BK0J6BqFh1BvU/SYJmXfnFuUNO3vV
|
||||
4u2Saa0q1iddxV0rXDwIqUfn+S6rwzK0G7y8bH2yvpB2VwiG3TFPnULep4wsefNq
|
||||
Fj92kqFBjacGpQLEEslUY0CMgeZ2+NuBQSUTscP3wBRsottMR6YXJtINdvfHBx+e
|
||||
EcN71z8D00w3mYqIQ7qb4Ml6HOqknunn58g43L9sACMUMTlEBXa9pUnScNYgWBAz
|
||||
W2q2mH37cIydM2JRZPpA8B4yTHt5ugJmChwyNFM7941arjKrebH+6AzLkofGedOP
|
||||
zg+vZQuPEXWs+3MBBnkWoyJW3Y0fbQdjsuQTtnd+7iyoxoBroQKCAQEA4dIiFlIS
|
||||
MDfRhQQWSiDvaw9aneDEJ3uo63ZRH5tm/IynLgtjYgEm/ZxlBCQgqRKLYELBxhu8
|
||||
SaF0uPK8pmpFJt0mIwSlsdeVhuE2obQeKUCczaqrKeaHS3PdWLjTlwph81BGRkHy
|
||||
qfqtNylyyMxrdEbnR51EtsWgFq6anTUAui1Q09JMuMNZRMOzDs1F4gExgD22rc0V
|
||||
c9YQ+jHJRxBGtNKMpMEqc8cvaxBidbItrN9SMTSWog7uYPBuEuaJ6K9vpgyJMOzJ
|
||||
SYcQEFGqgIqIDCg+ABE4d/4YROMKZ1DV/bJCind9brUHSx6XALsF0nC5c1Q9TnUL
|
||||
qI2khOwts4KYKwKCAQEA2xRC6Az97Vkdzu7BjLJ1FKmx4S2nEEgVS12ds82U+5Xf
|
||||
BHKAJnjqlqmmpzzJG+d77IYktz0+mey1QCNkqlm2fhuKs8LZMnpZRf0l8VcoBsUP
|
||||
/xKz7wfiE7RRFZtLJhPp4hhe43GzX5/JFMWMnC6UykwQbj4t1E/GNM/Suqwvg12M
|
||||
wktAJ6nqLgfhjQSO4xWo+nPzcbX+fNtrPCZVrBhYXihhcwRRNImWUCGJ6J4LMdPY
|
||||
Y9Z59qhOvE9cReH/Xw1av46omyiSyAqlgPyZ/kzA2IJSqYCjiQR/2+RD/g13jpcJ
|
||||
jatXLVZ8MJSL5OTS40G/HHTNNpNHbKKh0GOyxBA3ewKCAQBAn8UXhCcmW2L/YPsL
|
||||
/b7mcX9qPP+FmRLvR23R0MQ5M/tH5wRq8I969n3GIJykJeVzB8eybQ+GNslTgEvS
|
||||
iAkAJTubu+G7MkndTqg2wHf9MDtvdA8Fr646Po8yq7oJuHPtkKR7yLWsRUu6xIbP
|
||||
xgheP0hCq1QVxhqZQyCGKrvpi7xc0gsYuPbcAfFFJCOCmPrUi1SzCkTAYJt9LjA+
|
||||
wP6rErIjGBCRD4iXaBn1OqdtmH9KC5WsDP/VCBlIGWeQCly2NVIxiSHVg+xp7yUP
|
||||
IhXq/L05gbQaSsIhPKQmivCiaJg4The8TdwneDqYf+0bmxzHT203/bD3bImPbJNr
|
||||
ksz/AoIBAEwu4Y1cZzkQUmNRd5D7xecnk6ngfEYXKwCIT3zlMrfCSEl9n77BMaKu
|
||||
4Dsr0iuX9eosQ7xM2eYhAG6LYEg05lc4MKWOToVVMpI6E+W3Dz47bPKgiF3I+f8s
|
||||
Jz5CQIG/TwfGvciOE3hfUkec4ua09BzdEqGjkcBQ9XYMBxXPJr6h2379OBQS7FKR
|
||||
fwfQ2/dv4tElXTTfut2kV8gU9Jnh5Wjo1epvR+XjKpg28YQo4W+0YX1magcyRB8L
|
||||
4eSTUIC3XiVa8Jr0IwbZXPBb5xkdi7o+p4w2JahSHjxTRqmj+T1mnHXdbXVgq9Mg
|
||||
9Pzl7cgFZvX4UBx4XtASRf73jITNtt0CggEADH9K+O7FrIOSQly0sMvsRCMtejp3
|
||||
o+MDh1Q+vEg2kEgNXjS4ZFVljUpM2kg1OdUz7feS4dLXUJiIQ8ZWtZPedcq7wjHd
|
||||
02he5+s06l0jPifN3tX1ADfXGpXg5R2fbkrIzakkPP5/RO/aDxIUo7qhklNsVTXO
|
||||
VlGGfWLdk0ekA4upKm02Q1+YOlbIcAicEYYY8K7IffUwnohzKwL9yfuGi1VKTXpE
|
||||
4fzdegsHI03FSqR7V+LvtBpIupQ7RO4kuBmCEyI4E9FVknchg4te4gO3qwd9y0rJ
|
||||
Gu7HNIOrwOHzviI7J6Nd/l9MmeKqklHSgJvko/f5TmiXuQQ8xDZf84rcjQ==
|
||||
-----END RSA PRIVATE KEY-----
|
||||
`)
|
||||
|
||||
publicKeyBytes := []byte(`-----BEGIN RSA PUBLIC KEY-----
|
||||
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAwUCimKCidbF3UxEHPy8K
|
||||
+hvhklRB9JYhj5sJy0if4lTVibkK1MrYCykOnmC40pPU9GLY1b8HxAg9tvyRn0YH
|
||||
UxOra6vVQaVcOVJhTM8D18d+lSr3Lp1yiX+UGT4nzWI9+R1CCbwXrqeQVoZs6QZK
|
||||
ynEXMkFI9/wNMOwPOvQFOSTuoEoCO+zyTyUWEkNbUq825ELUQdIsjgmlWUOONudx
|
||||
sAr7ESRXW9QTHVh6uWmr3VRKZHby1JdU3I/wjdlGg5M2dDuXy5nQO9w/nYLjJXiw
|
||||
+zzOetZ/+t7/VOkOpNTeJQhwTM1WF7Y2VLetbi9FHgyzHatrduh07+XEiTbgDf3G
|
||||
Ix2bp2p6oh0G3N2zpiLcK/aZj8rouWWydfFfsU3MZ4FfJDP8I6b9awxjmKYqIr6h
|
||||
iPQCJaLBED8mwK+I5evIbnKv6E6uK+BApWA/R7ElragoFYbqQ1VpvntVMtJt9Dy5
|
||||
ZrI+IQARdXD3bb34oh0IPBhClnvvMUc1cWxDoXEX6oJ4I+LzxE87Zkwnan9qOwen
|
||||
golMVKFwPx1o37qrbmrXID21kKt7FL6xN4HxHLkItr1fKzdyWDFRHgASTAWfx5BI
|
||||
wvPuUW0vZHkvO80VyV2L63whVhPnPASmFkbviomrBttYfpr2aGQqF/qR1Nlxe834
|
||||
MFxk1pS9LMa/WnzvFr0gWakCAwEAAQ==
|
||||
-----END RSA PUBLIC KEY-----
|
||||
`)
|
||||
secretKeyPairDir := filepath.Join(homeDir, "secrets", "contexts", "contx1", "secrets")
|
||||
if err := os.MkdirAll(secretKeyPairDir, 0777); err != nil {
|
||||
err = fmt.Errorf("Not able to create directories")
|
||||
log.Fatal(err)
|
||||
}
|
||||
os.Setenv("QLIKSENSE_KEY_LOCATION", secretKeyPairDir)
|
||||
|
||||
privKeyFile := filepath.Join(secretKeyPairDir, "qliksensePriv")
|
||||
// construct and write priv key file into secretsDir location
|
||||
err := ioutil.WriteFile(privKeyFile, privKeyBytes, 0777)
|
||||
if err != nil {
|
||||
log.Printf("Error while creating file: %v", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
pubKeyFile := filepath.Join(secretKeyPairDir, "qliksensePub")
|
||||
// construct and write pub key file into secretsDir location
|
||||
err = ioutil.WriteFile(pubKeyFile, publicKeyBytes, 0777)
|
||||
if err != nil {
|
||||
log.Printf("Error while creating file: %v", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
return publicKeyBytes, privKeyBytes, nil
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/qlik-oss/k-apis/pkg/config"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
machine_yaml "k8s.io/apimachinery/pkg/util/yaml"
|
||||
)
|
||||
|
||||
const (
|
||||
QliksenseConfigApiVersion = "v1"
|
||||
QliksenseConfigApiGroup = "config.qlik.com"
|
||||
QliksenseConfigKind = "QliksenseConfig"
|
||||
|
||||
QliksenseApiVersion = "v1"
|
||||
QliksenseKind = "Qliksense"
|
||||
QliksenseGroup = "qlik.com"
|
||||
QliksenseDefaultProfile = "docker-desktop"
|
||||
DefaultRotateKeys = "yes"
|
||||
QliksenseMetadataName = "QliksenseConfigMetadata"
|
||||
DefaultMongoDbUri = "mongodb://qlik-default-mongodb:27017/qliksense?ssl=false"
|
||||
DefaultMongoDbUriKey = "mongoDbUri"
|
||||
)
|
||||
|
||||
// AddCommonConfig adds common configs into CRs
|
||||
func (qliksenseCR *QliksenseCR) AddCommonConfig(contextName string) {
|
||||
qliksenseCR.SetGroupVersionKind(schema.GroupVersionKind{
|
||||
Group: QliksenseGroup,
|
||||
Kind: QliksenseKind,
|
||||
Version: QliksenseApiVersion,
|
||||
})
|
||||
qliksenseCR.SetName(contextName)
|
||||
qliksenseCR.Spec = &config.CRSpec{
|
||||
Profile: QliksenseDefaultProfile,
|
||||
RotateKeys: DefaultRotateKeys,
|
||||
}
|
||||
qliksenseCR.Spec.AddToSecrets("qliksense", DefaultMongoDbUriKey, DefaultMongoDbUri, "")
|
||||
}
|
||||
|
||||
// AddBaseQliksenseConfigs adds configs into config.yaml
|
||||
func (qliksenseConfig *QliksenseConfig) AddBaseQliksenseConfigs(defaultQliksenseContext string) {
|
||||
qliksenseConfig.SetGroupVersionKind(schema.GroupVersionKind{
|
||||
Group: QliksenseConfigApiGroup,
|
||||
Kind: QliksenseConfigKind,
|
||||
Version: QliksenseConfigApiVersion,
|
||||
})
|
||||
qliksenseConfig.SetName(QliksenseMetadataName)
|
||||
if defaultQliksenseContext != "" {
|
||||
if qliksenseConfig.Spec == nil {
|
||||
qliksenseConfig.Spec = &ContextSpec{}
|
||||
}
|
||||
qliksenseConfig.Spec.CurrentContext = defaultQliksenseContext
|
||||
}
|
||||
}
|
||||
|
||||
func (qliksenseConfig *QliksenseConfig) SwitchCurrentCRToVersionAndProfile(version, profile string) error {
|
||||
if qcr, err := qliksenseConfig.GetCurrentCR(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
versionManifestRoot := qliksenseConfig.BuildCurrentManifestsRoot(version)
|
||||
if (qcr.Spec.ManifestsRoot != versionManifestRoot) || (profile != "" && qcr.Spec.Profile != profile) || (qcr.GetLabelFromCr("version") != version) {
|
||||
qcr.Spec.ManifestsRoot = versionManifestRoot
|
||||
if profile != "" {
|
||||
qcr.Spec.Profile = profile
|
||||
}
|
||||
qcr.AddLabelToCr("version", version)
|
||||
if err := qliksenseConfig.WriteCurrentContextCR(qcr); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteToFile (content, targetFile) writes content into specified file
|
||||
func WriteToFile(content interface{}, targetFile string) error {
|
||||
if content == nil || targetFile == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
x, err := K8sToYaml(content)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("An error occurred during marshalling CR: %v", err)
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
// Writing content
|
||||
err = ioutil.WriteFile(targetFile, x, 0644)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
LogDebugMessage("Wrote content into %s", targetFile)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadFromFile (content, targetFile) reads content from specified sourcefile
|
||||
func ReadFromFile(content interface{}, sourceFile string) error {
|
||||
if content == nil || sourceFile == "" {
|
||||
return nil
|
||||
}
|
||||
file, e := os.Open(sourceFile)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
return ReadFromStream(content, file)
|
||||
}
|
||||
|
||||
// ReadFromStream reads from input stream and creat yaml struct of type content
|
||||
func ReadFromStream(content interface{}, reader io.Reader) error {
|
||||
contents, err := ioutil.ReadAll(reader)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("There was an error reading from reader: %v", err)
|
||||
return err
|
||||
}
|
||||
// reading k8s style object
|
||||
// https://stackoverflow.com/questions/44306554/how-to-deserialize-kubernetes-yaml-file
|
||||
dec := machine_yaml.NewYAMLOrJSONDecoder(bytes.NewReader(contents), 10000)
|
||||
return dec.Decode(content)
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/qlik-oss/k-apis/pkg/config"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
var (
|
||||
testDir = "./tests"
|
||||
)
|
||||
|
||||
func TestAddCommonConfig(t *testing.T) {
|
||||
gvk := schema.GroupVersionKind{
|
||||
Group: QliksenseGroup,
|
||||
Kind: QliksenseKind,
|
||||
Version: QliksenseApiVersion,
|
||||
}
|
||||
q := &QliksenseCR{}
|
||||
q.SetName("myqliksense")
|
||||
q.SetGroupVersionKind(gvk)
|
||||
q.Spec = &config.CRSpec{
|
||||
Profile: QliksenseDefaultProfile,
|
||||
RotateKeys: DefaultRotateKeys,
|
||||
Secrets: map[string]config.NameValues{
|
||||
"qliksense": []config.NameValue{{
|
||||
Name: DefaultMongoDbUriKey,
|
||||
Value: DefaultMongoDbUri,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
type args struct {
|
||||
qliksenseCR *QliksenseCR
|
||||
contextName string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *QliksenseCR
|
||||
}{
|
||||
{
|
||||
name: "valid case",
|
||||
args: args{
|
||||
qliksenseCR: &QliksenseCR{},
|
||||
contextName: "myqliksense",
|
||||
},
|
||||
want: q,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.args.qliksenseCR.AddCommonConfig(tt.args.contextName)
|
||||
if !reflect.DeepEqual(tt.args.qliksenseCR, tt.want) {
|
||||
t.Errorf("AddCommonConfig() = %+v, want %+v", tt.args.qliksenseCR, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddBaseQliksenseConfigs(t *testing.T) {
|
||||
gvk := schema.GroupVersionKind{
|
||||
Group: QliksenseConfigApiGroup,
|
||||
Kind: QliksenseConfigKind,
|
||||
Version: QliksenseConfigApiVersion,
|
||||
}
|
||||
qc := &QliksenseConfig{}
|
||||
qc.SetGroupVersionKind(gvk)
|
||||
qc.SetName(QliksenseMetadataName)
|
||||
qc.Spec = &ContextSpec{
|
||||
CurrentContext: "qlik-default",
|
||||
}
|
||||
|
||||
type args struct {
|
||||
qliksenseConfig *QliksenseConfig
|
||||
defaultQliksenseContext string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *QliksenseConfig
|
||||
}{
|
||||
{
|
||||
name: "valid case",
|
||||
args: args{
|
||||
qliksenseConfig: &QliksenseConfig{},
|
||||
defaultQliksenseContext: "qlik-default",
|
||||
},
|
||||
want: qc,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.args.qliksenseConfig.AddBaseQliksenseConfigs(tt.args.defaultQliksenseContext)
|
||||
if !reflect.DeepEqual(tt.args.qliksenseConfig, tt.want) {
|
||||
t.Errorf("AddBaseQliksenseConfigs() = %+v, want %+v", tt.args.qliksenseConfig, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
type k8sDockerConfigJsonMapType struct {
|
||||
Auths map[string]k8sDockerConfigJsonType `json:"auths"`
|
||||
}
|
||||
|
||||
type k8sDockerConfigJsonType struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Email string `json:"email,omitempty"`
|
||||
Auth string `json:"auth"`
|
||||
}
|
||||
|
||||
func (kdcjt *k8sDockerConfigJsonType) GenerateAuth() {
|
||||
kdcjt.Auth = base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", kdcjt.Username, kdcjt.Password)))
|
||||
}
|
||||
|
||||
type DockerConfigJsonSecret struct {
|
||||
Name string
|
||||
Uri string
|
||||
Username string
|
||||
Password string
|
||||
Email string
|
||||
}
|
||||
|
||||
func (d *DockerConfigJsonSecret) ToYaml(encryptionKey *rsa.PublicKey) ([]byte, error) {
|
||||
k8sDockerConfigJson := k8sDockerConfigJsonType{
|
||||
Username: d.Username,
|
||||
Password: d.Password,
|
||||
Email: d.Email,
|
||||
}
|
||||
k8sDockerConfigJson.GenerateAuth()
|
||||
k8sDockerConfigJsonMap := k8sDockerConfigJsonMapType{
|
||||
Auths: map[string]k8sDockerConfigJsonType{
|
||||
d.Uri: k8sDockerConfigJson,
|
||||
},
|
||||
}
|
||||
k8sDockerConfigJsonMapBytes, err := json.Marshal(k8sDockerConfigJsonMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var k8sDockerConfigJsonMapMaybeEncryptedBytes []byte
|
||||
if encryptionKey != nil {
|
||||
if k8sDockerConfigJsonMapMaybeEncryptedBytes, err = Encrypt(k8sDockerConfigJsonMapBytes, encryptionKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
k8sDockerConfigJsonMapMaybeEncryptedBytes = k8sDockerConfigJsonMapBytes
|
||||
}
|
||||
|
||||
k8sSecret := v1.Secret{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "v1",
|
||||
Kind: "Secret",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: d.Name,
|
||||
},
|
||||
Type: v1.SecretTypeDockerConfigJson,
|
||||
Data: map[string][]byte{
|
||||
".dockerconfigjson": k8sDockerConfigJsonMapMaybeEncryptedBytes,
|
||||
},
|
||||
}
|
||||
|
||||
return K8sSecretToYaml(k8sSecret)
|
||||
}
|
||||
|
||||
func (d *DockerConfigJsonSecret) FromYaml(secretBytes []byte, decryptionKey *rsa.PrivateKey) error {
|
||||
k8sDockerConfigJsonMap := k8sDockerConfigJsonMapType{}
|
||||
if k8sSecret, err := K8sSecretFromYaml(secretBytes); err != nil {
|
||||
return err
|
||||
} else if k8sSecret.TypeMeta.Kind != "Secret" {
|
||||
return errors.New("not a Secret kind")
|
||||
} else if k8sSecret.Type != v1.SecretTypeDockerConfigJson {
|
||||
return errors.New("not a kubernetes.io/dockerconfigjson type")
|
||||
} else if k8sDockerConfigJsonMapEncryptedBytes, ok := k8sSecret.Data[".dockerconfigjson"]; !ok {
|
||||
return errors.New("secret data is missing a value for the .dockerconfigjson key")
|
||||
} else if k8sDockerConfigJsonMapBytes, err := Decrypt(k8sDockerConfigJsonMapEncryptedBytes, decryptionKey); err != nil {
|
||||
return errors.New("secret data is missing a value for the .dockerconfigjson key")
|
||||
} else if err := json.Unmarshal(k8sDockerConfigJsonMapBytes, &k8sDockerConfigJsonMap); err != nil {
|
||||
return err
|
||||
} else {
|
||||
d.Name = k8sSecret.ObjectMeta.Name
|
||||
for registry, k8sDockerConfigJson := range k8sDockerConfigJsonMap.Auths {
|
||||
d.Uri = registry
|
||||
d.Username = k8sDockerConfigJson.Username
|
||||
d.Password = k8sDockerConfigJson.Password
|
||||
d.Email = k8sDockerConfigJson.Email
|
||||
break
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func TestDockerConfigJsonSecret(t *testing.T) {
|
||||
dockerConfigJsonSecret := DockerConfigJsonSecret{
|
||||
Name: "some-name",
|
||||
Uri: "some-uri",
|
||||
Username: "some-username",
|
||||
Password: "some-password",
|
||||
Email: "some-email",
|
||||
}
|
||||
dockerConfigJsonSecretFromYaml := DockerConfigJsonSecret{}
|
||||
validYamlMap := map[string]interface{}{}
|
||||
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, RSA_KEY_LENGTH)
|
||||
if err != nil {
|
||||
t.Fatalf("error generating RSA private key: %v\n", err)
|
||||
}
|
||||
|
||||
dockerConfigJsonSecretYamlBytes, err := dockerConfigJsonSecret.ToYaml(&privateKey.PublicKey)
|
||||
dockerConfigJsonMap := map[string]interface{}{}
|
||||
if err != nil {
|
||||
t.Fatalf("error converting secret to yaml: %v", err)
|
||||
} else if err := yaml.Unmarshal(dockerConfigJsonSecretYamlBytes, &validYamlMap); err != nil {
|
||||
t.Fatalf("error unmarshalling yaml string: %v, error: %v", string(dockerConfigJsonSecretYamlBytes), err)
|
||||
} else if validYamlMap["apiVersion"] != "v1" ||
|
||||
validYamlMap["kind"] != "Secret" ||
|
||||
validYamlMap["metadata"].(map[string]interface{})["name"] != dockerConfigJsonSecret.Name ||
|
||||
validYamlMap["type"] != "kubernetes.io/dockerconfigjson" {
|
||||
t.Fatalf("error verifying validity of secret yaml: %v", string(dockerConfigJsonSecretYamlBytes))
|
||||
} else if dockerConfigJsonBytesBase64, ok := validYamlMap["data"].(map[string]interface{})[".dockerconfigjson"]; !ok {
|
||||
t.Fatalf("no .dockerconfigjson data key in the secret yaml: %v", string(dockerConfigJsonSecretYamlBytes))
|
||||
} else if dockerConfigJsonEncryptedBytes, err := base64.StdEncoding.DecodeString(dockerConfigJsonBytesBase64.(string)); err != nil {
|
||||
t.Fatalf("error decoding dockerConfigJsonBytes from base64: %v", err)
|
||||
} else if dockerConfigJsonBytes, err := Decrypt(dockerConfigJsonEncryptedBytes, privateKey); err != nil {
|
||||
t.Fatalf("error decrypting dockerConfigJsonBytes: %v", err)
|
||||
} else if err := json.Unmarshal(dockerConfigJsonBytes, &dockerConfigJsonMap); err != nil {
|
||||
t.Fatalf("error unmarshalling dockerConfigJson from json: %v", err)
|
||||
} else if dockerConfigJson, ok := dockerConfigJsonMap["auths"].(map[string]interface{})[dockerConfigJsonSecret.Uri]; !ok {
|
||||
t.Fatalf("dockerConfigJson map does not contain data for the registry: %v", dockerConfigJsonSecret.Uri)
|
||||
} else if dockerConfigJson.(map[string]interface{})["username"] != dockerConfigJsonSecret.Username ||
|
||||
dockerConfigJson.(map[string]interface{})["password"] != dockerConfigJsonSecret.Password ||
|
||||
dockerConfigJson.(map[string]interface{})["email"] != dockerConfigJsonSecret.Email {
|
||||
t.Fatal("dockerConfigJson map does not contain expected values")
|
||||
} else {
|
||||
authBase64 := dockerConfigJson.(map[string]interface{})["auth"]
|
||||
if auth, err := base64.StdEncoding.DecodeString(authBase64.(string)); err != nil {
|
||||
t.Fatal("error base64 decoding auth value")
|
||||
} else if string(auth) != fmt.Sprintf("%s:%s", dockerConfigJsonSecret.Username, dockerConfigJsonSecret.Password) {
|
||||
t.Fatal("auth value was not what we expected")
|
||||
}
|
||||
}
|
||||
|
||||
t.Logf("dockerConfigJsonSecretYaml: \n%v\n", string(dockerConfigJsonSecretYamlBytes))
|
||||
if err := dockerConfigJsonSecretFromYaml.FromYaml(dockerConfigJsonSecretYamlBytes, privateKey); err != nil {
|
||||
t.Fatalf("error reading secret in from yaml: %v", err)
|
||||
} else if !reflect.DeepEqual(dockerConfigJsonSecret, dockerConfigJsonSecretFromYaml) {
|
||||
t.Fatalf("secret: %v does not equal secret: %v", dockerConfigJsonSecret, dockerConfigJsonSecretFromYaml)
|
||||
}
|
||||
}
|
||||
@@ -1,168 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const (
|
||||
RSA_KEY_LENGTH = 4096
|
||||
|
||||
QliksensePublicKey = "qliksensePub"
|
||||
QliksensePrivateKey = "qliksensePriv"
|
||||
)
|
||||
|
||||
// GenerateAndStoreSecretKeypair generates and stores key pairs
|
||||
func GenerateAndStoreSecretKeypair(secretsPath string) error {
|
||||
LogDebugMessage("%s exists", secretsPath)
|
||||
// creating contexts/qlik-default/secrets/qliksensePub and contexts/qlik-default/secrets/qliksensePriv files
|
||||
publicKeyFilePath := filepath.Join(secretsPath, QliksensePublicKey)
|
||||
privateKeyFilePath := filepath.Join(secretsPath, QliksensePrivateKey)
|
||||
LogDebugMessage("Generating public-private key pair.....")
|
||||
GenerateRSAEncryptionKeys(publicKeyFilePath, privateKeyFilePath)
|
||||
LogDebugMessage("Generated public-private key pairs")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GenerateRSAEncryptionKeys is used to generate a new public-private key pair
|
||||
func GenerateRSAEncryptionKeys(publicKeyFilePath, privateKeyFilePath string) error {
|
||||
LogDebugMessage("Generating new RSA key pair")
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, RSA_KEY_LENGTH)
|
||||
if err != nil {
|
||||
log.Printf("error generating RSA private key: %v\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
privateKeyPEM := EncodePrivateKey(privateKey)
|
||||
if err := writeContentToFile(privateKeyPEM, privateKeyFilePath); err != nil {
|
||||
return err
|
||||
}
|
||||
pubKeyPEM, err2 := EncodePublicKey(&privateKey.PublicKey)
|
||||
if err2 != nil {
|
||||
log.Printf("error occurred when encoding public key: %v\n", err2)
|
||||
return err2
|
||||
}
|
||||
if err := writeContentToFile(pubKeyPEM, publicKeyFilePath); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// writeContentToFile writes keys to a file
|
||||
func writeContentToFile(keyData []byte, fileName string) error {
|
||||
err := ioutil.WriteFile(fileName, keyData, 0600)
|
||||
if err != nil {
|
||||
log.Printf("error writing to file (%s): %v", fileName, err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Encrypt encrypts data with public key
|
||||
func Encrypt(pt []byte, pub *rsa.PublicKey) ([]byte, error) {
|
||||
//hash := sha512.New()
|
||||
//ct, err := rsa.EncryptOAEP(hash, rand.Reader, pub, pt, nil)
|
||||
ct, err := rsa.EncryptPKCS1v15(rand.Reader, pub, pt)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
return ct, nil
|
||||
}
|
||||
|
||||
// Decrypt decrypts data with private key
|
||||
func Decrypt(ct []byte, priv *rsa.PrivateKey) ([]byte, error) {
|
||||
// hash := sha512.New()
|
||||
// plaintext, err := rsa.DecryptOAEP(hash, rand.Reader, priv, ciphertext, nil)
|
||||
pt, err := rsa.DecryptPKCS1v15(rand.Reader, priv, ct)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
return pt, nil
|
||||
}
|
||||
|
||||
// EncodePrivateKey private key to bytes
|
||||
func EncodePrivateKey(priv *rsa.PrivateKey) []byte {
|
||||
privBytes := pem.EncodeToMemory(
|
||||
&pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Bytes: x509.MarshalPKCS1PrivateKey(priv),
|
||||
},
|
||||
)
|
||||
|
||||
return privBytes
|
||||
}
|
||||
|
||||
// EncodePublicKey public key to bytes
|
||||
func EncodePublicKey(pub *rsa.PublicKey) ([]byte, error) {
|
||||
pubASN1, err := x509.MarshalPKIXPublicKey(pub)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pubBytes := pem.EncodeToMemory(&pem.Block{
|
||||
Type: "RSA PUBLIC KEY",
|
||||
Bytes: pubASN1,
|
||||
})
|
||||
|
||||
return pubBytes, nil
|
||||
}
|
||||
|
||||
// DecodeToPrivateKey bytes to private key
|
||||
func DecodeToPrivateKey(priv []byte) (*rsa.PrivateKey, error) {
|
||||
block, _ := pem.Decode(priv)
|
||||
enc := x509.IsEncryptedPEMBlock(block)
|
||||
b := block.Bytes
|
||||
var err error
|
||||
if enc {
|
||||
log.Println("is encrypted pem block")
|
||||
b, err = x509.DecryptPEMBlock(block, nil)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
key, err := x509.ParsePKCS1PrivateKey(b)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// DecodeToPublicKey bytes to public key
|
||||
func DecodeToPublicKey(pub []byte) (*rsa.PublicKey, error) {
|
||||
block, _ := pem.Decode(pub)
|
||||
enc := x509.IsEncryptedPEMBlock(block)
|
||||
b := block.Bytes
|
||||
var err error
|
||||
if enc {
|
||||
log.Println("is encrypted pem block")
|
||||
b, err = x509.DecryptPEMBlock(block, nil)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
iface, err := x509.ParsePKIXPublicKey(b)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
key, ok := iface.(*rsa.PublicKey)
|
||||
if !ok {
|
||||
err := fmt.Errorf("Unable to decode public key")
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_generateRSAEncryptionKeys(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid case",
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := GenerateAndStoreSecretKeypair(os.TempDir()); (err != nil) != tt.wantErr {
|
||||
t.Errorf("generateRSAEncryptionKeys() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_encryption_decryption(t *testing.T) {
|
||||
privKeyBytes := []byte(`-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIJJwIBAAKCAgEAwUCimKCidbF3UxEHPy8K+hvhklRB9JYhj5sJy0if4lTVibkK
|
||||
1MrYCykOnmC40pPU9GLY1b8HxAg9tvyRn0YHUxOra6vVQaVcOVJhTM8D18d+lSr3
|
||||
Lp1yiX+UGT4nzWI9+R1CCbwXrqeQVoZs6QZKynEXMkFI9/wNMOwPOvQFOSTuoEoC
|
||||
O+zyTyUWEkNbUq825ELUQdIsjgmlWUOONudxsAr7ESRXW9QTHVh6uWmr3VRKZHby
|
||||
1JdU3I/wjdlGg5M2dDuXy5nQO9w/nYLjJXiw+zzOetZ/+t7/VOkOpNTeJQhwTM1W
|
||||
F7Y2VLetbi9FHgyzHatrduh07+XEiTbgDf3GIx2bp2p6oh0G3N2zpiLcK/aZj8ro
|
||||
uWWydfFfsU3MZ4FfJDP8I6b9awxjmKYqIr6hiPQCJaLBED8mwK+I5evIbnKv6E6u
|
||||
K+BApWA/R7ElragoFYbqQ1VpvntVMtJt9Dy5ZrI+IQARdXD3bb34oh0IPBhClnvv
|
||||
MUc1cWxDoXEX6oJ4I+LzxE87Zkwnan9qOwengolMVKFwPx1o37qrbmrXID21kKt7
|
||||
FL6xN4HxHLkItr1fKzdyWDFRHgASTAWfx5BIwvPuUW0vZHkvO80VyV2L63whVhPn
|
||||
PASmFkbviomrBttYfpr2aGQqF/qR1Nlxe834MFxk1pS9LMa/WnzvFr0gWakCAwEA
|
||||
AQKCAgARSp9B2N2wejibDiL/3E23I1eDqFZedDB8kPrHXbAwqDaTJCN79spt9TaB
|
||||
pVXkQaYEV/Pe7EDdoX8kKGU/QxzUqiXkdHOYdBtUZbKfFMbbP9ZrsnR7j0r4UpoF
|
||||
yDH3hprU93E5PcNAtW2M0GpeT1nR01yn+n908PCdOAIE3GC7RDq1zOl2QzVLL55R
|
||||
9ATv2Q2oTvJ/ETc7XlGVMx4+e2cIwXLFjeLjLI6pSYlxnarrGuetJZeEviWxto9n
|
||||
odFVZI6yx8JFTXX8ZTCr/1IjwDDVyhMPmrHI2Lsv9cqBpSpbVe32cUkKxhsGaYjz
|
||||
GvesQKamOPhco2ATNxPm0yopFlPsGKMfVl0BK0J6BqFh1BvU/SYJmXfnFuUNO3vV
|
||||
4u2Saa0q1iddxV0rXDwIqUfn+S6rwzK0G7y8bH2yvpB2VwiG3TFPnULep4wsefNq
|
||||
Fj92kqFBjacGpQLEEslUY0CMgeZ2+NuBQSUTscP3wBRsottMR6YXJtINdvfHBx+e
|
||||
EcN71z8D00w3mYqIQ7qb4Ml6HOqknunn58g43L9sACMUMTlEBXa9pUnScNYgWBAz
|
||||
W2q2mH37cIydM2JRZPpA8B4yTHt5ugJmChwyNFM7941arjKrebH+6AzLkofGedOP
|
||||
zg+vZQuPEXWs+3MBBnkWoyJW3Y0fbQdjsuQTtnd+7iyoxoBroQKCAQEA4dIiFlIS
|
||||
MDfRhQQWSiDvaw9aneDEJ3uo63ZRH5tm/IynLgtjYgEm/ZxlBCQgqRKLYELBxhu8
|
||||
SaF0uPK8pmpFJt0mIwSlsdeVhuE2obQeKUCczaqrKeaHS3PdWLjTlwph81BGRkHy
|
||||
qfqtNylyyMxrdEbnR51EtsWgFq6anTUAui1Q09JMuMNZRMOzDs1F4gExgD22rc0V
|
||||
c9YQ+jHJRxBGtNKMpMEqc8cvaxBidbItrN9SMTSWog7uYPBuEuaJ6K9vpgyJMOzJ
|
||||
SYcQEFGqgIqIDCg+ABE4d/4YROMKZ1DV/bJCind9brUHSx6XALsF0nC5c1Q9TnUL
|
||||
qI2khOwts4KYKwKCAQEA2xRC6Az97Vkdzu7BjLJ1FKmx4S2nEEgVS12ds82U+5Xf
|
||||
BHKAJnjqlqmmpzzJG+d77IYktz0+mey1QCNkqlm2fhuKs8LZMnpZRf0l8VcoBsUP
|
||||
/xKz7wfiE7RRFZtLJhPp4hhe43GzX5/JFMWMnC6UykwQbj4t1E/GNM/Suqwvg12M
|
||||
wktAJ6nqLgfhjQSO4xWo+nPzcbX+fNtrPCZVrBhYXihhcwRRNImWUCGJ6J4LMdPY
|
||||
Y9Z59qhOvE9cReH/Xw1av46omyiSyAqlgPyZ/kzA2IJSqYCjiQR/2+RD/g13jpcJ
|
||||
jatXLVZ8MJSL5OTS40G/HHTNNpNHbKKh0GOyxBA3ewKCAQBAn8UXhCcmW2L/YPsL
|
||||
/b7mcX9qPP+FmRLvR23R0MQ5M/tH5wRq8I969n3GIJykJeVzB8eybQ+GNslTgEvS
|
||||
iAkAJTubu+G7MkndTqg2wHf9MDtvdA8Fr646Po8yq7oJuHPtkKR7yLWsRUu6xIbP
|
||||
xgheP0hCq1QVxhqZQyCGKrvpi7xc0gsYuPbcAfFFJCOCmPrUi1SzCkTAYJt9LjA+
|
||||
wP6rErIjGBCRD4iXaBn1OqdtmH9KC5WsDP/VCBlIGWeQCly2NVIxiSHVg+xp7yUP
|
||||
IhXq/L05gbQaSsIhPKQmivCiaJg4The8TdwneDqYf+0bmxzHT203/bD3bImPbJNr
|
||||
ksz/AoIBAEwu4Y1cZzkQUmNRd5D7xecnk6ngfEYXKwCIT3zlMrfCSEl9n77BMaKu
|
||||
4Dsr0iuX9eosQ7xM2eYhAG6LYEg05lc4MKWOToVVMpI6E+W3Dz47bPKgiF3I+f8s
|
||||
Jz5CQIG/TwfGvciOE3hfUkec4ua09BzdEqGjkcBQ9XYMBxXPJr6h2379OBQS7FKR
|
||||
fwfQ2/dv4tElXTTfut2kV8gU9Jnh5Wjo1epvR+XjKpg28YQo4W+0YX1magcyRB8L
|
||||
4eSTUIC3XiVa8Jr0IwbZXPBb5xkdi7o+p4w2JahSHjxTRqmj+T1mnHXdbXVgq9Mg
|
||||
9Pzl7cgFZvX4UBx4XtASRf73jITNtt0CggEADH9K+O7FrIOSQly0sMvsRCMtejp3
|
||||
o+MDh1Q+vEg2kEgNXjS4ZFVljUpM2kg1OdUz7feS4dLXUJiIQ8ZWtZPedcq7wjHd
|
||||
02he5+s06l0jPifN3tX1ADfXGpXg5R2fbkrIzakkPP5/RO/aDxIUo7qhklNsVTXO
|
||||
VlGGfWLdk0ekA4upKm02Q1+YOlbIcAicEYYY8K7IffUwnohzKwL9yfuGi1VKTXpE
|
||||
4fzdegsHI03FSqR7V+LvtBpIupQ7RO4kuBmCEyI4E9FVknchg4te4gO3qwd9y0rJ
|
||||
Gu7HNIOrwOHzviI7J6Nd/l9MmeKqklHSgJvko/f5TmiXuQQ8xDZf84rcjQ==
|
||||
-----END RSA PRIVATE KEY-----
|
||||
`)
|
||||
|
||||
publicKeyBytes := []byte(`-----BEGIN RSA PUBLIC KEY-----
|
||||
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAwUCimKCidbF3UxEHPy8K
|
||||
+hvhklRB9JYhj5sJy0if4lTVibkK1MrYCykOnmC40pPU9GLY1b8HxAg9tvyRn0YH
|
||||
UxOra6vVQaVcOVJhTM8D18d+lSr3Lp1yiX+UGT4nzWI9+R1CCbwXrqeQVoZs6QZK
|
||||
ynEXMkFI9/wNMOwPOvQFOSTuoEoCO+zyTyUWEkNbUq825ELUQdIsjgmlWUOONudx
|
||||
sAr7ESRXW9QTHVh6uWmr3VRKZHby1JdU3I/wjdlGg5M2dDuXy5nQO9w/nYLjJXiw
|
||||
+zzOetZ/+t7/VOkOpNTeJQhwTM1WF7Y2VLetbi9FHgyzHatrduh07+XEiTbgDf3G
|
||||
Ix2bp2p6oh0G3N2zpiLcK/aZj8rouWWydfFfsU3MZ4FfJDP8I6b9awxjmKYqIr6h
|
||||
iPQCJaLBED8mwK+I5evIbnKv6E6uK+BApWA/R7ElragoFYbqQ1VpvntVMtJt9Dy5
|
||||
ZrI+IQARdXD3bb34oh0IPBhClnvvMUc1cWxDoXEX6oJ4I+LzxE87Zkwnan9qOwen
|
||||
golMVKFwPx1o37qrbmrXID21kKt7FL6xN4HxHLkItr1fKzdyWDFRHgASTAWfx5BI
|
||||
wvPuUW0vZHkvO80VyV2L63whVhPnPASmFkbviomrBttYfpr2aGQqF/qR1Nlxe834
|
||||
MFxk1pS9LMa/WnzvFr0gWakCAwEAAQ==
|
||||
-----END RSA PUBLIC KEY-----
|
||||
`)
|
||||
origStr := "Value1234"
|
||||
|
||||
pubKey, err := DecodeToPublicKey(publicKeyBytes)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
privKey, err := DecodeToPrivateKey(privKeyBytes)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
encData, err := Encrypt([]byte(origStr), pubKey)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
encDataStr := base64.StdEncoding.EncodeToString(encData)
|
||||
log.Println("Encoded text:", encDataStr)
|
||||
dec, _ := base64.StdEncoding.DecodeString(encDataStr)
|
||||
data, err := Decrypt(dec, privKey)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
if string(data) != origStr {
|
||||
t.Error("original string and decrypted string don't match")
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
func K8sSecretToYaml(k8sSecret v1.Secret) ([]byte, error) {
|
||||
return K8sToYaml(k8sSecret)
|
||||
}
|
||||
|
||||
func K8sSecretFromYaml(k8sSecretBytes []byte) (v1.Secret, error) {
|
||||
k8sSecret := v1.Secret{}
|
||||
if err := yaml.UnmarshalStrict(k8sSecretBytes, &k8sSecret); err != nil {
|
||||
return k8sSecret, err
|
||||
}
|
||||
return k8sSecret, nil
|
||||
}
|
||||
|
||||
func K8sToYaml(k8sObj interface{}) ([]byte, error) {
|
||||
k8sSecretYamlMap := map[string]interface{}{}
|
||||
if k8sSecretYamlBytes, err := yaml.Marshal(k8sObj); err != nil {
|
||||
return nil, err
|
||||
} else if err := yaml.Unmarshal(k8sSecretYamlBytes, &k8sSecretYamlMap); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
delete(k8sSecretYamlMap["metadata"].(map[string]interface{}), "creationTimestamp")
|
||||
return yaml.Marshal(k8sSecretYamlMap)
|
||||
}
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// KubectlApply create resoruces in the provided namespace,
|
||||
// if namespace="" then use whatever the kubectl default is
|
||||
func KubectlApply(manifests, namespace string) error {
|
||||
return kubectlOperation(manifests, "apply", namespace)
|
||||
}
|
||||
|
||||
// KubectlDelete delete resoruces in the provided namespace,
|
||||
// if namespace="" then use whatever the kubectl default is
|
||||
func KubectlDelete(manifests, namespace string) error {
|
||||
return kubectlOperation(manifests, "delete", namespace)
|
||||
}
|
||||
|
||||
func GetKubectlNamespace() string {
|
||||
namespace := ""
|
||||
cmd := exec.Command("kubectl", "config", "current-context")
|
||||
var out, out2 bytes.Buffer
|
||||
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = os.Stderr
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Printf("kubectl config current-context %q\n", err)
|
||||
return namespace
|
||||
}
|
||||
if out.String() == "" {
|
||||
fmt.Println("kubectl config current-context does not return anything")
|
||||
return namespace
|
||||
}
|
||||
|
||||
cmd = exec.Command("kubectl", "config", "view", "-o", `jsonpath={.contexts[?(@.name == "`+strings.TrimSpace(out.String())+`")].context.namespace}`)
|
||||
cmd.Stdout = &out2
|
||||
cmd.Stderr = os.Stderr
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Printf("kubectl config view failed with %q\n", err)
|
||||
return namespace
|
||||
}
|
||||
namespace = out2.String()
|
||||
return namespace
|
||||
}
|
||||
|
||||
func SetKubectlNamespace(ns string) {
|
||||
cmd := exec.Command("kubectl", "config", "set-context", "--namespace="+ns, "--current")
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Printf("kubectl config set-context --namespace failed with %q\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func kubectlOperation(manifests string, oprName string, namespace string) error {
|
||||
tempYaml, err := ioutil.TempFile("", "")
|
||||
if err != nil {
|
||||
fmt.Println("cannot create file ", err)
|
||||
return err
|
||||
}
|
||||
tempYaml.WriteString(manifests)
|
||||
|
||||
arguments := make([]string, 0)
|
||||
arguments = append(arguments, oprName)
|
||||
arguments = append(arguments, "-f")
|
||||
arguments = append(arguments, tempYaml.Name())
|
||||
|
||||
if oprName == "apply" {
|
||||
arguments = append(arguments, "--validate=false")
|
||||
}
|
||||
if namespace != "" {
|
||||
arguments = append(arguments, "-n")
|
||||
arguments = append(arguments, namespace)
|
||||
}
|
||||
var cmd *exec.Cmd
|
||||
if oprName == "apply" {
|
||||
cmd = exec.Command("kubectl", arguments...)
|
||||
} else {
|
||||
cmd = exec.Command("kubectl", arguments...)
|
||||
}
|
||||
|
||||
sterrBuffer := &bytes.Buffer{}
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = sterrBuffer
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return fmt.Errorf("kubectl %v failed with: %v, %v, temp k8s yaml file:%v\n", oprName, err, sterrBuffer.String(), tempYaml.Name())
|
||||
}
|
||||
os.Remove(tempYaml.Name())
|
||||
return nil
|
||||
}
|
||||
|
||||
func KubectlDirectOps(opr []string, namespace string) (string, error) {
|
||||
arguments := []string{}
|
||||
if namespace != "" {
|
||||
arguments = append(arguments, "-n", namespace)
|
||||
}
|
||||
arguments = append(arguments, opr...)
|
||||
var out bytes.Buffer
|
||||
cmd := exec.Command("kubectl", arguments...)
|
||||
LogDebugMessage("Kubectl command: %s %v\n", "kubectl", arguments)
|
||||
sterrBuffer := &bytes.Buffer{}
|
||||
cmd.Stderr = sterrBuffer
|
||||
cmd.Stdout = &out
|
||||
if err := cmd.Run(); err != nil {
|
||||
return "", fmt.Errorf("kubectl %v failed with: %v, %v\n", opr, err, sterrBuffer.String())
|
||||
}
|
||||
s := out.String()
|
||||
return s, nil
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetKubectlNamespace(t *testing.T) {
|
||||
t.Skip()
|
||||
ns := GetKubectlNamespace()
|
||||
SetKubectlNamespace("tada")
|
||||
got := GetKubectlNamespace()
|
||||
if got != "tada" {
|
||||
t.Log(got)
|
||||
t.Fail()
|
||||
}
|
||||
SetKubectlNamespace(ns)
|
||||
}
|
||||
|
||||
func TestKubectlDirectOps(t *testing.T) {
|
||||
t.Skip()
|
||||
SetKubectlNamespace("test")
|
||||
ns := GetKubectlNamespace()
|
||||
opr := fmt.Sprintf("version")
|
||||
opr1 := strings.Fields(opr)
|
||||
_, err := KubectlDirectOps(opr1, ns)
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
kapi_config "github.com/qlik-oss/k-apis/pkg/config"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
// QliksenseConfig is exported
|
||||
type QliksenseConfig struct {
|
||||
metav1.TypeMeta `json:",inline" yaml:",inline"`
|
||||
metav1.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
|
||||
Spec *ContextSpec `json:"spec" yaml:"spec"`
|
||||
QliksenseHomePath string `json:"-" yaml:"-"`
|
||||
}
|
||||
|
||||
/*type CommonConfig struct {
|
||||
metav1.TypeMeta `json:",inline" yaml:",inline"`
|
||||
metav1.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
|
||||
}
|
||||
*/
|
||||
// QliksenseCR is exported
|
||||
type QliksenseCR struct {
|
||||
kapi_config.KApiCr `json:",inline" yaml:",inline"`
|
||||
}
|
||||
|
||||
// ContextSpec is exported
|
||||
type ContextSpec struct {
|
||||
Contexts []Context `json:"contexts" yaml:"contexts"`
|
||||
CurrentContext string `json:"currentContext" yaml:"currentContext"`
|
||||
}
|
||||
|
||||
// Context is exported
|
||||
type Context struct {
|
||||
Name string `json:"name,omitempty" yaml:"name,omitempty"`
|
||||
CrFile string `json:"crFile,omitempty" yaml:"crFile,omitempty"`
|
||||
}
|
||||
|
||||
// Metadata is exported
|
||||
type Metadata struct {
|
||||
Name string `json:"name,omitempty" yaml:"name,omitempty"`
|
||||
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
|
||||
}
|
||||
|
||||
// ServiceKeyValue holds the combination of service, key and value
|
||||
type ServiceKeyValue struct {
|
||||
SvcName string
|
||||
Key string
|
||||
Value string
|
||||
}
|
||||
267
pkg/api/utils.go
267
pkg/api/utils.go
@@ -1,267 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"archive/zip"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func checkExists(filename string) os.FileInfo {
|
||||
info, err := os.Stat(filename)
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
LogDebugMessage("File exists")
|
||||
return info
|
||||
}
|
||||
|
||||
// FileExists checks if a file exists
|
||||
func FileExists(filename string) bool {
|
||||
if fe := checkExists(filename); fe != nil && !fe.IsDir() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// DirExists checks if a directory exists
|
||||
func DirExists(dirname string) bool {
|
||||
if fe := checkExists(dirname); fe != nil && fe.IsDir() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// LogDebugMessage logs a debug message
|
||||
func LogDebugMessage(strMessage string, args ...interface{}) {
|
||||
if os.Getenv("QLIKSENSE_DEBUG") == "true" {
|
||||
log.Printf(strMessage, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// ReadKeys reads key file from disk
|
||||
func ReadKeys(keyFile string) ([]byte, error) {
|
||||
keybyteArray, err := ioutil.ReadFile(keyFile)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("There was an error reading from file: %s, %v", keyFile, err)
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
return keybyteArray, nil
|
||||
}
|
||||
|
||||
// ProcessConfigArgs processes args and returns an service, key, value slice
|
||||
func ProcessConfigArgs(args []string) ([]*ServiceKeyValue, error) {
|
||||
// prepare received args
|
||||
// split args[0] into key and value
|
||||
if len(args) == 0 {
|
||||
err := fmt.Errorf("No args were provided. Please provide args to configure the current context")
|
||||
return nil, err
|
||||
}
|
||||
resultSvcKV := make([]*ServiceKeyValue, len(args))
|
||||
re1 := regexp.MustCompile(`([\w\-]{1,}).([\w\-]{1,})=("*[\w\-?=_/:0-9\.]+"*)`)
|
||||
for i, arg := range args {
|
||||
LogDebugMessage("Arg received: %s", arg)
|
||||
result := re1.FindStringSubmatch(arg)
|
||||
// check if result array's length is == 4 (index 0 - is the full match & indices 1,2,3- are the fields we need)
|
||||
if len(result) != 4 {
|
||||
err := fmt.Errorf("Please provide valid args for this command")
|
||||
return nil, err
|
||||
}
|
||||
resultSvcKV[i] = &ServiceKeyValue{
|
||||
SvcName: result[1],
|
||||
Key: result[2],
|
||||
Value: strings.ReplaceAll(result[3], `"`, ""),
|
||||
}
|
||||
}
|
||||
return resultSvcKV, nil
|
||||
}
|
||||
|
||||
func ExecuteTaskWithBlinkingStdoutFeedback(task func() (interface{}, error), feedback string) (result interface{}, err error) {
|
||||
taskDone := make(chan bool)
|
||||
go func() {
|
||||
result, err = task()
|
||||
taskDone <- true
|
||||
}()
|
||||
progressOnTicker := time.NewTicker(500 * time.Millisecond)
|
||||
progressOffTicker := time.NewTicker(1000 * time.Millisecond)
|
||||
printProgress := func(on bool) {
|
||||
if on {
|
||||
fmt.Printf("%s\r", feedback)
|
||||
} else {
|
||||
fmt.Printf("%s\r", strings.Repeat(" ", len(feedback)))
|
||||
}
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-taskDone:
|
||||
progressOnTicker.Stop()
|
||||
progressOffTicker.Stop()
|
||||
printProgress(false)
|
||||
return result, err
|
||||
case <-progressOnTicker.C:
|
||||
printProgress(true)
|
||||
case <-progressOffTicker.C:
|
||||
printProgress(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func DownloadFile(url, baseFolder, installerName string) error {
|
||||
var (
|
||||
out *os.File
|
||||
err error
|
||||
resp *http.Response
|
||||
)
|
||||
// Create the file
|
||||
fileName := filepath.Join(baseFolder, installerName)
|
||||
LogDebugMessage("Installer Filename: %s\n", fileName)
|
||||
if out, err = os.Create(fileName); err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
// Get the data
|
||||
if resp, err = http.Get(url); err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
err = fmt.Errorf("unable to download the file from URL: %s, status: %s", url, resp.Status)
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Write the body to file
|
||||
if _, err = io.Copy(out, resp.Body); err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.Chmod(fileName, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ExplodePackage(destination, fileToUntar string) error {
|
||||
LogDebugMessage("Destination: %s\n", destination)
|
||||
LogDebugMessage("fileToUntar: %s\n", fileToUntar)
|
||||
|
||||
if strings.HasSuffix(fileToUntar, "zip") {
|
||||
LogDebugMessage("This is a windows file : %s", fileToUntar)
|
||||
err := UnZipFile(destination, fileToUntar)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
} else if strings.HasSuffix(fileToUntar, "tar.gz") {
|
||||
LogDebugMessage("This is a mac/linux file: %s", fileToUntar)
|
||||
err := UntarGzFile(destination, fileToUntar)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UntarGzFile(destination, fileToUntar string) error {
|
||||
lFile, err := os.Open(fileToUntar)
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "unable to read the local file %s", fileToUntar)
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
gzReader, err := gzip.NewReader(lFile)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "unable to load the file into a gz reader")
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
defer gzReader.Close()
|
||||
|
||||
tarReader := tar.NewReader(gzReader)
|
||||
for {
|
||||
header, err := tarReader.Next()
|
||||
switch {
|
||||
case err == io.EOF:
|
||||
return nil
|
||||
case err != nil:
|
||||
err = errors.Wrap(err, "error during untar")
|
||||
log.Fatal(err)
|
||||
return err
|
||||
case header == nil:
|
||||
continue
|
||||
}
|
||||
|
||||
fileInLoop := filepath.Join(destination, header.Name)
|
||||
switch header.Typeflag {
|
||||
case tar.TypeDir:
|
||||
if _, err := os.Stat(fileInLoop); err != nil {
|
||||
if err := os.MkdirAll(fileInLoop, 0755); err != nil {
|
||||
err = errors.Wrapf(err, "error creating directory %s", fileInLoop)
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
case tar.TypeReg:
|
||||
fileAtLoc, err := os.OpenFile(fileInLoop, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "error opening file %s", fileInLoop)
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := io.Copy(fileAtLoc, tarReader); err != nil {
|
||||
err = errors.Wrapf(err, "error writing file %s", fileInLoop)
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
fileAtLoc.Close()
|
||||
fileAtLoc.Chmod(os.ModePerm)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func UnZipFile(destination, fileToUnzip string) error {
|
||||
zipReader, _ := zip.OpenReader(fileToUnzip)
|
||||
for _, file := range zipReader.Reader.File {
|
||||
|
||||
zippedFile, err := file.Open()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer zippedFile.Close()
|
||||
extractedFilePath := filepath.Join(
|
||||
destination,
|
||||
file.Name,
|
||||
)
|
||||
outputFile, err := os.OpenFile(
|
||||
extractedFilePath,
|
||||
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
|
||||
file.Mode(),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer outputFile.Close()
|
||||
|
||||
_, err = io.Copy(outputFile, zippedFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
LogDebugMessage("File extracted: %s, Extracted file path: %s\n", file.Name, extractedFilePath)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestProcessConfigArgs(t *testing.T) {
|
||||
args := []string{
|
||||
"qliksense.mongodb=mongouri://something?ffall",
|
||||
"test_under.test=value_under",
|
||||
"test-dash.dash-key=value-dash",
|
||||
"test-dot.dot-key=127.0.0.1",
|
||||
"test123.key123=value123",
|
||||
}
|
||||
expectedKeys := []string{"mongodb", "test", "dash-key", "dot-key", "key123"}
|
||||
expectedValue := []string{"mongouri://something?ffall", "value_under", "value-dash", "127.0.0.1", "value123"}
|
||||
exppectedSvc := []string{"qliksense", "test_under", "test-dash", "test-dot", "test123"}
|
||||
sv, err := ProcessConfigArgs(args)
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
t.FailNow()
|
||||
}
|
||||
for _, v := range sv {
|
||||
if !contains(expectedKeys, v.Key) {
|
||||
t.Fail()
|
||||
t.Log("expectd key " + v.Key + " not found")
|
||||
}
|
||||
if !contains(expectedValue, v.Value) {
|
||||
t.Fail()
|
||||
t.Log("expectd Value " + v.Value + " not found")
|
||||
}
|
||||
if !contains(exppectedSvc, v.SvcName) {
|
||||
t.Fail()
|
||||
t.Log("expectd service " + v.SvcName + " not found")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func contains(arr []string, str string) bool {
|
||||
for _, a := range arr {
|
||||
if a == str {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (qp *QliksensePreflight) RunAllPreflightChecks(namespace string, kubeConfigContents []byte, mongodbUrl string) {
|
||||
|
||||
checkCount := 0
|
||||
// Preflight minimum kuberenetes version check
|
||||
fmt.Printf("\nPreflight kubernetes minimum version check\n")
|
||||
fmt.Println("------------------------------------------")
|
||||
if err := qp.CheckK8sVersion(namespace, kubeConfigContents); err != nil {
|
||||
fmt.Printf("Preflight kubernetes minimum version check: FAILED\n")
|
||||
} else {
|
||||
checkCount++
|
||||
}
|
||||
|
||||
// Preflight deployment check
|
||||
fmt.Printf("\nPreflight deployment check\n")
|
||||
fmt.Println("--------------------------")
|
||||
if err := qp.CheckDeployment(namespace, kubeConfigContents); err != nil {
|
||||
fmt.Printf("Preflight deployment check: FAILED\n")
|
||||
} else {
|
||||
checkCount++
|
||||
}
|
||||
|
||||
// Preflight service check
|
||||
fmt.Printf("\nPreflight service check\n")
|
||||
fmt.Println("-----------------------")
|
||||
if err := qp.CheckService(namespace, kubeConfigContents); err != nil {
|
||||
fmt.Printf("Preflight service check: FAILED\n")
|
||||
} else {
|
||||
checkCount++
|
||||
}
|
||||
|
||||
// Preflight pod check
|
||||
fmt.Printf("\nPreflight pod check\n")
|
||||
fmt.Println("-----------------------")
|
||||
if err := qp.CheckPod(namespace, kubeConfigContents); err != nil {
|
||||
fmt.Printf("Preflight pod check: FAILED\n")
|
||||
} else {
|
||||
checkCount++
|
||||
}
|
||||
|
||||
// Preflight role check
|
||||
fmt.Printf("\nPreflight role check\n")
|
||||
fmt.Println("--------------------------")
|
||||
if err := qp.CheckCreateRole(namespace); err != nil {
|
||||
fmt.Printf("Preflight role check: FAILED\n")
|
||||
} else {
|
||||
checkCount++
|
||||
}
|
||||
|
||||
// Preflight rolebinding check
|
||||
fmt.Printf("\nPreflight rolebinding check\n")
|
||||
fmt.Println("---------------------------------")
|
||||
if err := qp.CheckCreateRoleBinding(namespace); err != nil {
|
||||
fmt.Printf("Preflight rolebinding check: FAILED\n")
|
||||
} else {
|
||||
checkCount++
|
||||
}
|
||||
|
||||
// Preflight serviceaccount check
|
||||
fmt.Printf("\nPreflight serviceaccount check\n")
|
||||
fmt.Println("------------------------------------")
|
||||
if err := qp.CheckCreateServiceAccount(namespace); err != nil {
|
||||
fmt.Printf("Preflight serviceaccount check: FAILED\n")
|
||||
} else {
|
||||
checkCount++
|
||||
}
|
||||
|
||||
// Preflight mongo check
|
||||
fmt.Printf("\nPreflight mongo check\n")
|
||||
fmt.Println("---------------------")
|
||||
if err := qp.CheckMongo(kubeConfigContents, namespace, mongodbUrl); err != nil {
|
||||
fmt.Printf("Preflight mongo check: FAILED\n")
|
||||
} else {
|
||||
checkCount++
|
||||
}
|
||||
|
||||
// Preflight DNS check
|
||||
fmt.Printf("\nPreflight DNS check\n")
|
||||
fmt.Println("-------------------")
|
||||
if err := qp.CheckDns(namespace, kubeConfigContents); err != nil {
|
||||
fmt.Printf("Preflight DNS check: FAILED\n")
|
||||
} else {
|
||||
checkCount++
|
||||
}
|
||||
|
||||
if checkCount == 9 {
|
||||
fmt.Printf("\nAll preflight checks have PASSED\n")
|
||||
} else {
|
||||
fmt.Printf("\n1 or more preflight checks have FAILED\n")
|
||||
}
|
||||
fmt.Println("Completed running all preflight checks")
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
func (qp *QliksensePreflight) CheckDeployment(namespace string, kubeConfigContents []byte) error {
|
||||
clientset, _, err := getK8SClientSet(kubeConfigContents, "")
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Kube config error: %v\n", err)
|
||||
fmt.Print(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Deployment check
|
||||
fmt.Printf("Preflight deployment check: \n")
|
||||
err = qp.checkPfDeployment(clientset, namespace, "deployment-preflight-check")
|
||||
if err != nil {
|
||||
fmt.Println("Preflight Deployment check: FAILED")
|
||||
return err
|
||||
}
|
||||
fmt.Println("Completed preflight deployment check")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (qp *QliksensePreflight) CheckService(namespace string, kubeConfigContents []byte) error {
|
||||
clientset, _, err := getK8SClientSet(kubeConfigContents, "")
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to create a kubernetes client: %v\n", err)
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
// Service check
|
||||
fmt.Printf("\nPreflight service check: \n")
|
||||
err = checkPfService(clientset, namespace)
|
||||
if err != nil {
|
||||
fmt.Println("Preflight Service check: FAILED")
|
||||
return err
|
||||
}
|
||||
fmt.Println("Completed preflight service check")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (qp *QliksensePreflight) CheckPod(namespace string, kubeConfigContents []byte) error {
|
||||
clientset, _, err := getK8SClientSet(kubeConfigContents, "")
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to create a kubernetes client: %v\n", err)
|
||||
fmt.Print(err)
|
||||
return err
|
||||
}
|
||||
// Pod check
|
||||
fmt.Printf("\nPreflight pod check: \n")
|
||||
|
||||
err = qp.checkPfPod(clientset, namespace)
|
||||
if err != nil {
|
||||
fmt.Println("Preflight Pod check: FAILED")
|
||||
return err
|
||||
}
|
||||
fmt.Println("Completed preflight pod check")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (qp *QliksensePreflight) checkPfPod(clientset *kubernetes.Clientset, namespace string) error {
|
||||
// create a pod
|
||||
podName := "pod-pf-check"
|
||||
pod, err := createPreflightTestPod(clientset, namespace, podName, qp.GetPreflightConfigObj().GetImageName(nginx))
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to create pod %s - %v\n", podName, err)
|
||||
return err
|
||||
}
|
||||
defer deletePod(clientset, namespace, podName)
|
||||
|
||||
if err := waitForPod(clientset, namespace, pod); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("Preflight pod creation check: PASSED")
|
||||
fmt.Println("Cleaning up resources...")
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkPfService(clientset *kubernetes.Clientset, namespace string) error {
|
||||
// creating service
|
||||
serviceName := "svc-pf-check"
|
||||
pfService, err := createPreflightTestService(clientset, namespace, serviceName)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to create service : %s\n", serviceName)
|
||||
return err
|
||||
}
|
||||
defer deleteService(clientset, namespace, serviceName)
|
||||
_, err = getService(clientset, namespace, pfService.GetName())
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to retrieve service: %s\n", serviceName)
|
||||
return err
|
||||
}
|
||||
fmt.Println("Preflight service creation check: PASSED")
|
||||
fmt.Println("Cleaning up resources...")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (qp *QliksensePreflight) checkPfDeployment(clientset *kubernetes.Clientset, namespace, depName string) error {
|
||||
// check if we are able to create a deployment
|
||||
pfDeployment, err := createPreflightTestDeployment(clientset, namespace, depName, qp.GetPreflightConfigObj().GetImageName(nginx))
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to create deployment: %v\n", err)
|
||||
return err
|
||||
}
|
||||
defer deleteDeployment(clientset, namespace, depName)
|
||||
if err := waitForDeployment(clientset, namespace, pfDeployment); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Preflight Deployment check: PASSED")
|
||||
fmt.Println("Cleaning up resources...")
|
||||
return nil
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
const (
|
||||
nginx = "nginx"
|
||||
netcat = "netcat"
|
||||
)
|
||||
|
||||
func (qp *QliksensePreflight) CheckDns(namespace string, kubeConfigContents []byte) error {
|
||||
clientset, clientConfig, err := getK8SClientSet(kubeConfigContents, "")
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to create a kubernetes client: %v\n", err)
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// creating deployment
|
||||
depName := "dep-dns-preflight-check"
|
||||
dnsDeployment, err := createPreflightTestDeployment(clientset, namespace, depName, qp.GetPreflightConfigObj().GetImageName(nginx))
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to create deployment: %v\n", err)
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
defer deleteDeployment(clientset, namespace, depName)
|
||||
|
||||
if err := waitForDeployment(clientset, namespace, dnsDeployment); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// creating service
|
||||
serviceName := "svc-dns-pf-check"
|
||||
dnsService, err := createPreflightTestService(clientset, namespace, serviceName)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to create service : %s\n", serviceName)
|
||||
return err
|
||||
}
|
||||
defer deleteService(clientset, namespace, serviceName)
|
||||
|
||||
// create a pod
|
||||
podName := "pf-pod-1"
|
||||
dnsPod, err := createPreflightTestPod(clientset, namespace, podName, qp.GetPreflightConfigObj().GetImageName(netcat))
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to create pod : %s\n", podName)
|
||||
return err
|
||||
}
|
||||
defer deletePod(clientset, namespace, podName)
|
||||
|
||||
if err := waitForPod(clientset, namespace, dnsPod); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(dnsPod.Spec.Containers) == 0 {
|
||||
err := fmt.Errorf("error: there are no containers in the pod")
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
api.LogDebugMessage("Exec-ing into the container...")
|
||||
stdout, stderr, err := executeRemoteCommand(clientset, clientConfig, dnsPod.Name, dnsPod.Spec.Containers[0].Name, namespace, []string{"nc", "-z", "-v", "-w 1", dnsService.Name, "80"})
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to execute dns check in the cluster: %v", err)
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if strings.HasSuffix(stdout, "succeeded!") || strings.HasSuffix(stderr, "succeeded!") {
|
||||
fmt.Println("Preflight DNS check: PASSED")
|
||||
} else {
|
||||
fmt.Println("Preflight DNS check: FAILED")
|
||||
}
|
||||
|
||||
fmt.Println("Completed preflight DNS check")
|
||||
fmt.Println("Cleaning up resources...")
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/qlik-oss/sense-installer/pkg/api"
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
const (
|
||||
mongoImage = "mongo"
|
||||
)
|
||||
|
||||
func (qp *QliksensePreflight) CheckMongo(kubeConfigContents []byte, namespace, mongodbUrl string) error {
|
||||
fmt.Printf("Preflight mongodb check: \n")
|
||||
|
||||
if mongodbUrl == "" {
|
||||
// infer mongoDbUrl from currentCR
|
||||
fmt.Println("MongoDbUri is empty, infer from CR")
|
||||
qConfig := qapi.NewQConfig(qp.Q.QliksenseHome)
|
||||
var currentCR *qapi.QliksenseCR
|
||||
|
||||
var err error
|
||||
qConfig.SetNamespace(namespace)
|
||||
currentCR, err = qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to retrieve current CR: %v\n", err)
|
||||
return err
|
||||
}
|
||||
decryptedCR, err := qConfig.GetDecryptedCr(currentCR)
|
||||
mongodbUrl = decryptedCR.Spec.GetFromSecrets("qliksense", "mongoDbUri")
|
||||
}
|
||||
|
||||
fmt.Printf("mongodbUrl: %s\n", mongodbUrl)
|
||||
if err := mongoConnCheck(kubeConfigContents, namespace, mongodbUrl); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Completed preflight mongodb check")
|
||||
return nil
|
||||
}
|
||||
|
||||
func mongoConnCheck(kubeConfigContents []byte, namespace, mongodbUrl string) error {
|
||||
clientset, clientConfig, err := getK8SClientSet(kubeConfigContents, "")
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to create a kubernetes client: %v\n", err)
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
// create a pod
|
||||
podName := "pf-mongo-pod"
|
||||
mongoPod, err := createPreflightTestPod(clientset, namespace, podName, mongoImage)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to create pod : %s\n", podName)
|
||||
fmt.Println("Preflight mongo check: FAILED")
|
||||
return err
|
||||
}
|
||||
defer deletePod(clientset, namespace, podName)
|
||||
|
||||
if err := waitForPod(clientset, namespace, mongoPod); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(mongoPod.Spec.Containers) == 0 {
|
||||
err := fmt.Errorf("error: there are no containers in the pod")
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
api.LogDebugMessage("Exec-ing into the container...")
|
||||
stdout, stderr, err := executeRemoteCommand(clientset, clientConfig, mongoPod.Name, mongoPod.Spec.Containers[0].Name, namespace, []string{"mongo", mongodbUrl})
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to execute mongo check in the cluster: %v", err)
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
api.LogDebugMessage("stdout:", stdout)
|
||||
api.LogDebugMessage("stderr:", stderr)
|
||||
stringToCheck := "Implicit session"
|
||||
if strings.Contains(stdout, stringToCheck) || strings.Contains(stderr, stringToCheck) {
|
||||
fmt.Println("Preflight mongo check: PASSED")
|
||||
} else {
|
||||
fmt.Println("Preflight mongo check: FAILED")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
api "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
type PreflightConfig struct {
|
||||
metav1.TypeMeta `json:",inline" yaml:",inline"`
|
||||
metav1.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
|
||||
Spec *PreflightSpec `json:"spec" yaml:"spec"`
|
||||
QliksenseHomePath string `json:"-" yaml:"-"`
|
||||
}
|
||||
|
||||
type PreflightSpec struct {
|
||||
MinK8sVersion string `json:"minK8sVersion,omitempty" yaml:"minK8sVersion,omitempty"`
|
||||
Images map[string]string `json:"images,omitempty" yaml:"images,omitempty"`
|
||||
}
|
||||
|
||||
//NewPreflightConfigEmpty create empty PreflightConfig object
|
||||
func NewPreflightConfigEmpty(qHome string) *PreflightConfig {
|
||||
p := &PreflightConfig{
|
||||
QliksenseHomePath: qHome,
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "config.qlik.com/v1",
|
||||
Kind: "PreflightConfig",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "PreflightConfigMetadata",
|
||||
},
|
||||
Spec: &PreflightSpec{},
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
//NewPreflightConfig create empty PreflightConfig object if preflit/preflight-config.yaml not exist
|
||||
func NewPreflightConfig(qHome string) *PreflightConfig {
|
||||
p := NewPreflightConfigEmpty(qHome)
|
||||
conFile := p.GetConfigFilePath()
|
||||
if _, err := os.Lstat(conFile); err != nil {
|
||||
return p
|
||||
}
|
||||
p = &PreflightConfig{}
|
||||
if err := api.ReadFromFile(p, conFile); err != nil {
|
||||
return nil
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
//GetConfigFilePath return preflight-config.yaml file path
|
||||
func (p *PreflightConfig) GetConfigFilePath() string {
|
||||
return filepath.Join(p.QliksenseHomePath, "preflight", "preflight-config.yaml")
|
||||
}
|
||||
|
||||
//Write write PreflightConfig object into the ~/.qliksense/preflight/preflight-config.yaml file
|
||||
func (p *PreflightConfig) Write() error {
|
||||
pDir := filepath.Join(p.QliksenseHomePath, "preflight")
|
||||
if err := os.MkdirAll(pDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
return api.WriteToFile(p, p.GetConfigFilePath())
|
||||
}
|
||||
|
||||
func (p *PreflightConfig) AddMinK8sV(version string) {
|
||||
if p.Spec == nil {
|
||||
p.Spec = &PreflightSpec{}
|
||||
}
|
||||
p.Spec.MinK8sVersion = version
|
||||
}
|
||||
|
||||
func (p *PreflightConfig) AddImage(imageFor, imageName string) {
|
||||
if p.Spec.Images == nil {
|
||||
p.Spec.Images = make(map[string]string)
|
||||
}
|
||||
p.Spec.Images[imageFor] = imageName
|
||||
}
|
||||
|
||||
func (p *PreflightConfig) GetImageName(imageFor string) string {
|
||||
if p.Spec.Images == nil {
|
||||
return ""
|
||||
}
|
||||
return p.Spec.Images[imageFor]
|
||||
}
|
||||
func (p *PreflightConfig) GetMinK8sVersion() string {
|
||||
return p.Spec.MinK8sVersion
|
||||
}
|
||||
func (p *PreflightConfig) IsExistOnDisk() bool {
|
||||
if _, err := os.Lstat(p.GetConfigFilePath()); err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *PreflightConfig) GetImageMap() map[string]string {
|
||||
return p.Spec.Images
|
||||
}
|
||||
|
||||
func (p *PreflightConfig) Initialize() error {
|
||||
if p.IsExistOnDisk() {
|
||||
return nil
|
||||
}
|
||||
p.AddMinK8sV("1.15")
|
||||
p.AddImage("nginx", "nginx")
|
||||
p.AddImage("netcat", "subfuzion/netcat")
|
||||
return p.Write()
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
api "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
func Test_Initalize(t *testing.T) {
|
||||
tempDir, err := ioutil.TempDir("", "")
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
t.FailNow()
|
||||
}
|
||||
pf := NewPreflightConfig(tempDir)
|
||||
if err := pf.Initialize(); err != nil {
|
||||
t.Log()
|
||||
t.FailNow()
|
||||
}
|
||||
p := &PreflightConfig{
|
||||
QliksenseHomePath: tempDir,
|
||||
}
|
||||
if err := api.ReadFromFile(p, pf.GetConfigFilePath()); err != nil {
|
||||
t.Log(err)
|
||||
t.FailNow()
|
||||
}
|
||||
if p.GetMinK8sVersion() != "1.15" {
|
||||
t.Log("expected k8 version: 1.15, but got " + p.GetMinK8sVersion())
|
||||
t.Fail()
|
||||
}
|
||||
p.AddImage("test", "testimage")
|
||||
if err := p.Write(); err != nil {
|
||||
t.Log(err)
|
||||
t.Fail()
|
||||
}
|
||||
p2 := NewPreflightConfig(tempDir)
|
||||
if p2.GetImageName("test") != "testimage" {
|
||||
t.Log("expected image name: testimage, got: " + p2.GetImageName("test"))
|
||||
}
|
||||
}
|
||||
@@ -1,642 +0,0 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mitchellh/go-homedir"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
|
||||
"k8s.io/client-go/util/retry"
|
||||
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
"k8s.io/client-go/tools/remotecommand"
|
||||
"k8s.io/kubectl/pkg/scheme"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/qlik-oss/sense-installer/pkg/api"
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
apiv1 "k8s.io/api/core/v1"
|
||||
"k8s.io/api/rbac/v1beta1"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
restclient "k8s.io/client-go/rest"
|
||||
|
||||
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
)
|
||||
|
||||
var gracePeriod int64 = 0
|
||||
|
||||
type QliksensePreflight struct {
|
||||
Q *qliksense.Qliksense
|
||||
}
|
||||
|
||||
func (qp *QliksensePreflight) GetPreflightConfigObj() *PreflightConfig {
|
||||
return NewPreflightConfig(qp.Q.QliksenseHome)
|
||||
}
|
||||
|
||||
func InitPreflight() (string, []byte, error) {
|
||||
api.LogDebugMessage("Reading .kube/config file...")
|
||||
|
||||
homeDir, err := homedir.Dir()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Unable to deduce home dir\n")
|
||||
return "", nil, err
|
||||
}
|
||||
api.LogDebugMessage("Kube config location: %s\n\n", filepath.Join(homeDir, ".kube", "config"))
|
||||
|
||||
kubeConfig := filepath.Join(homeDir, ".kube", "config")
|
||||
kubeConfigContents, err := ioutil.ReadFile(kubeConfig)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Unable to deduce home dir\n")
|
||||
return "", nil, err
|
||||
}
|
||||
// retrieve namespace
|
||||
namespace := api.GetKubectlNamespace()
|
||||
// if namespace comes back empty, we will run checks in the default namespace
|
||||
if namespace == "" {
|
||||
namespace = "default"
|
||||
}
|
||||
api.LogDebugMessage("Namespace: %s\n", namespace)
|
||||
return namespace, kubeConfigContents, nil
|
||||
}
|
||||
|
||||
func initiateK8sOps(opr, namespace string) error {
|
||||
opr1 := strings.Fields(opr)
|
||||
_, err := api.KubectlDirectOps(opr1, namespace)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func int32Ptr(i int32) *int32 { return &i }
|
||||
|
||||
func retryOnError(mf func() error) error {
|
||||
return retry.OnError(wait.Backoff{
|
||||
Duration: 1 * time.Second,
|
||||
Factor: 1,
|
||||
Jitter: 0.1,
|
||||
Steps: 5,
|
||||
}, func(err error) bool {
|
||||
return k8serrors.IsConflict(err) || k8serrors.IsGone(err) || k8serrors.IsServerTimeout(err) ||
|
||||
k8serrors.IsServiceUnavailable(err) || k8serrors.IsTimeout(err) || k8serrors.IsTooManyRequests(err)
|
||||
}, mf)
|
||||
}
|
||||
|
||||
func getK8SClientSet(kubeconfig []byte, contextName string) (*kubernetes.Clientset, *rest.Config, error) {
|
||||
var clientConfig *rest.Config
|
||||
var err error
|
||||
if len(kubeconfig) == 0 {
|
||||
clientConfig, err = rest.InClusterConfig()
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "Unable to load in-cluster kubeconfig")
|
||||
fmt.Println(err)
|
||||
return nil, nil, err
|
||||
}
|
||||
} else {
|
||||
config, err := clientcmd.Load(kubeconfig)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "Unable to load kubeconfig")
|
||||
fmt.Println(err)
|
||||
return nil, nil, err
|
||||
}
|
||||
if contextName != "" {
|
||||
config.CurrentContext = contextName
|
||||
}
|
||||
clientConfig, err = clientcmd.NewDefaultClientConfig(*config, &clientcmd.ConfigOverrides{}).ClientConfig()
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "Unable to create client config from config")
|
||||
fmt.Println(err)
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
clientset, err := kubernetes.NewForConfig(clientConfig)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "Unable to create clientset")
|
||||
fmt.Println(err)
|
||||
return nil, nil, err
|
||||
}
|
||||
return clientset, clientConfig, nil
|
||||
}
|
||||
|
||||
func createPreflightTestDeployment(clientset *kubernetes.Clientset, namespace string, depName string, imageName string) (*appsv1.Deployment, error) {
|
||||
deploymentsClient := clientset.AppsV1().Deployments(namespace)
|
||||
deployment := &appsv1.Deployment{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: depName,
|
||||
},
|
||||
Spec: appsv1.DeploymentSpec{
|
||||
Replicas: int32Ptr(1),
|
||||
Selector: &v1.LabelSelector{
|
||||
MatchLabels: map[string]string{
|
||||
"app": "preflight-check",
|
||||
},
|
||||
},
|
||||
Template: apiv1.PodTemplateSpec{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Labels: map[string]string{
|
||||
"app": "preflight-check",
|
||||
"label": "preflight-check-label",
|
||||
},
|
||||
},
|
||||
Spec: apiv1.PodSpec{
|
||||
Containers: []apiv1.Container{
|
||||
{
|
||||
Name: "dep",
|
||||
Image: imageName,
|
||||
Ports: []apiv1.ContainerPort{
|
||||
{
|
||||
Name: "http",
|
||||
Protocol: apiv1.ProtocolTCP,
|
||||
ContainerPort: 80,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Create Deployment
|
||||
var result *appsv1.Deployment
|
||||
if err := retryOnError(func() (err error) {
|
||||
result, err = deploymentsClient.Create(deployment)
|
||||
return err
|
||||
}); err != nil {
|
||||
err = errors.Wrapf(err, "error: unable to create deployments in the %s namespace", namespace)
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
fmt.Printf("Created deployment %q\n", result.GetObjectMeta().GetName())
|
||||
|
||||
return deployment, nil
|
||||
}
|
||||
|
||||
func getDeployment(clientset *kubernetes.Clientset, namespace, depName string) (*appsv1.Deployment, error) {
|
||||
deploymentsClient := clientset.AppsV1().Deployments(namespace)
|
||||
var deployment *appsv1.Deployment
|
||||
if err := retryOnError(func() (err error) {
|
||||
deployment, err = deploymentsClient.Get(depName, v1.GetOptions{})
|
||||
return err
|
||||
}); err != nil {
|
||||
err = errors.Wrapf(err, "error: unable to get deployments in the %s namespace", namespace)
|
||||
api.LogDebugMessage("%v\n", err)
|
||||
return nil, err
|
||||
}
|
||||
return deployment, nil
|
||||
}
|
||||
|
||||
func deleteDeployment(clientset *kubernetes.Clientset, namespace, name string) error {
|
||||
deploymentsClient := clientset.AppsV1().Deployments(namespace)
|
||||
// Create Deployment
|
||||
deletePolicy := v1.DeletePropagationForeground
|
||||
deleteOptions := v1.DeleteOptions{
|
||||
PropagationPolicy: &deletePolicy,
|
||||
GracePeriodSeconds: &gracePeriod,
|
||||
}
|
||||
|
||||
if err := retryOnError(func() (err error) {
|
||||
return deploymentsClient.Delete(name, &deleteOptions)
|
||||
}); err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
if err := waitForDeploymentToDelete(clientset, namespace, name); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Deleted deployment: %s\n", name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func createPreflightTestService(clientset *kubernetes.Clientset, namespace string, svcName string) (*apiv1.Service, error) {
|
||||
iptr := int32Ptr(80)
|
||||
servicesClient := clientset.CoreV1().Services(namespace)
|
||||
service := &apiv1.Service{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: svcName,
|
||||
Namespace: namespace,
|
||||
Labels: map[string]string{
|
||||
"app": "preflight-check",
|
||||
},
|
||||
},
|
||||
Spec: apiv1.ServiceSpec{
|
||||
Ports: []apiv1.ServicePort{
|
||||
{Name: "port1",
|
||||
Port: *iptr,
|
||||
},
|
||||
},
|
||||
Selector: map[string]string{
|
||||
"app": "preflight-check",
|
||||
},
|
||||
ClusterIP: "",
|
||||
},
|
||||
}
|
||||
var result *apiv1.Service
|
||||
if err := retryOnError(func() (err error) {
|
||||
result, err = servicesClient.Create(service)
|
||||
return err
|
||||
}); err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
fmt.Printf("Created service %q\n", result.GetObjectMeta().GetName())
|
||||
|
||||
return service, nil
|
||||
}
|
||||
|
||||
func getService(clientset *kubernetes.Clientset, namespace, svcName string) (*apiv1.Service, error) {
|
||||
servicesClient := clientset.CoreV1().Services(namespace)
|
||||
var svc *apiv1.Service
|
||||
if err := retryOnError(func() (err error) {
|
||||
svc, err = servicesClient.Get(svcName, v1.GetOptions{})
|
||||
return err
|
||||
}); err != nil {
|
||||
err = errors.Wrapf(err, "unable to get services in the %s namespace", namespace)
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return svc, nil
|
||||
}
|
||||
|
||||
func deleteService(clientset *kubernetes.Clientset, namespace, name string) error {
|
||||
servicesClient := clientset.CoreV1().Services(namespace)
|
||||
// Create Deployment
|
||||
deletePolicy := v1.DeletePropagationForeground
|
||||
deleteOptions := v1.DeleteOptions{
|
||||
PropagationPolicy: &deletePolicy,
|
||||
}
|
||||
if err := retryOnError(func() (err error) {
|
||||
return servicesClient.Delete(name, &deleteOptions)
|
||||
}); err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Deleted service: %s\n", name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func deletePod(clientset *kubernetes.Clientset, namespace, name string) error {
|
||||
|
||||
podsClient := clientset.CoreV1().Pods(namespace)
|
||||
deletePolicy := v1.DeletePropagationForeground
|
||||
deleteOptions := v1.DeleteOptions{
|
||||
PropagationPolicy: &deletePolicy,
|
||||
GracePeriodSeconds: &gracePeriod,
|
||||
}
|
||||
if err := retryOnError(func() (err error) {
|
||||
return podsClient.Delete(name, &deleteOptions)
|
||||
}); err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
if err := waitForPodToDelete(clientset, namespace, name); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Deleted pod: %s\n", name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func createPreflightTestPod(clientset *kubernetes.Clientset, namespace string, podName string, imageName string) (*apiv1.Pod, error) {
|
||||
// build the pod definition we want to deploy
|
||||
pod := &apiv1.Pod{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: podName,
|
||||
Namespace: namespace,
|
||||
Labels: map[string]string{
|
||||
"app": "preflight",
|
||||
},
|
||||
},
|
||||
Spec: apiv1.PodSpec{
|
||||
Containers: []apiv1.Container{
|
||||
{
|
||||
Name: "cnt",
|
||||
Image: imageName,
|
||||
ImagePullPolicy: apiv1.PullIfNotPresent,
|
||||
Command: []string{
|
||||
"sleep",
|
||||
"3600",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// now create the pod in kubernetes cluster using the clientset
|
||||
if err := retryOnError(func() (err error) {
|
||||
pod, err = clientset.CoreV1().Pods(namespace).Create(pod)
|
||||
return err
|
||||
}); err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
fmt.Printf("Created pod: %s\n", pod.Name)
|
||||
return pod, nil
|
||||
}
|
||||
|
||||
func getPod(clientset *kubernetes.Clientset, namespace, podName string) (*apiv1.Pod, error) {
|
||||
api.LogDebugMessage("Fetching pod: %s\n", podName)
|
||||
var pod *apiv1.Pod
|
||||
if err := retryOnError(func() (err error) {
|
||||
pod, err = clientset.CoreV1().Pods(namespace).Get(podName, v1.GetOptions{})
|
||||
return err
|
||||
}); err != nil {
|
||||
api.LogDebugMessage("%v\n", err)
|
||||
return nil, err
|
||||
}
|
||||
return pod, nil
|
||||
}
|
||||
|
||||
func execute(method string, url *url.URL, config *restclient.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool) error {
|
||||
exec, err := remotecommand.NewSPDYExecutor(config, method, url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return exec.Stream(remotecommand.StreamOptions{
|
||||
Stdin: stdin,
|
||||
Stdout: stdout,
|
||||
Stderr: stderr,
|
||||
Tty: tty,
|
||||
})
|
||||
}
|
||||
|
||||
func executeRemoteCommand(clientset *kubernetes.Clientset, config *rest.Config, podName, containerName, namespace string, command []string) (string, string, error) {
|
||||
tty := false
|
||||
req := clientset.CoreV1().RESTClient().Post().
|
||||
Resource("pods").
|
||||
Name(podName).
|
||||
Namespace(namespace).
|
||||
SubResource("exec").
|
||||
Param("container", containerName)
|
||||
req.VersionedParams(&apiv1.PodExecOptions{
|
||||
Container: containerName,
|
||||
Command: command,
|
||||
Stdin: false,
|
||||
Stdout: true,
|
||||
Stderr: true,
|
||||
TTY: tty,
|
||||
}, scheme.ParameterCodec)
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
err := execute("POST", req.URL(), config, nil, &stdout, &stderr, tty)
|
||||
return strings.TrimSpace(stdout.String()), strings.TrimSpace(stderr.String()), err
|
||||
}
|
||||
|
||||
func waitForDeployment(clientset *kubernetes.Clientset, namespace string, pfDeployment *appsv1.Deployment) error {
|
||||
timeout := time.NewTicker(2 * time.Minute)
|
||||
defer timeout.Stop()
|
||||
var d *appsv1.Deployment
|
||||
var err error
|
||||
WAIT:
|
||||
for {
|
||||
d, err = getDeployment(clientset, namespace, pfDeployment.GetName())
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to retrieve deployment: %s\n", pfDeployment.GetName())
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case <-timeout.C:
|
||||
break WAIT
|
||||
default:
|
||||
if int(d.Status.ReadyReplicas) > 0 {
|
||||
break WAIT
|
||||
}
|
||||
}
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
if int(d.Status.ReadyReplicas) == 0 {
|
||||
err = fmt.Errorf("error: deployment took longer than expected to spin up pods")
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func waitForPod(clientset *kubernetes.Clientset, namespace string, pod *apiv1.Pod) error {
|
||||
var err error
|
||||
if len(pod.Spec.Containers) > 0 {
|
||||
timeout := time.NewTicker(2 * time.Minute)
|
||||
defer timeout.Stop()
|
||||
podName := pod.Name
|
||||
OUT:
|
||||
for {
|
||||
pod, err = getPod(clientset, namespace, podName)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error: unable to retrieve %s pod by name", podName)
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case <-timeout.C:
|
||||
break OUT
|
||||
default:
|
||||
if len(pod.Status.ContainerStatuses) > 0 && pod.Status.ContainerStatuses[0].Ready {
|
||||
break OUT
|
||||
}
|
||||
}
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
if len(pod.Status.ContainerStatuses) == 0 || !pod.Status.ContainerStatuses[0].Ready {
|
||||
err = fmt.Errorf("error: container is taking much longer than expected")
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
err = fmt.Errorf("error: there are no containers in the pod")
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
func waitForPodToDelete(clientset *kubernetes.Clientset, namespace, podName string) error {
|
||||
var err error
|
||||
timeout := time.NewTicker(2 * time.Minute)
|
||||
defer timeout.Stop()
|
||||
OUT:
|
||||
for {
|
||||
_, err = getPod(clientset, namespace, podName)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
select {
|
||||
case <-timeout.C:
|
||||
break OUT
|
||||
default:
|
||||
|
||||
}
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
err = fmt.Errorf("error: delete pod is taking unusually long")
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
func waitForDeploymentToDelete(clientset *kubernetes.Clientset, namespace, deploymentName string) error {
|
||||
var err error
|
||||
timeout := time.NewTicker(2 * time.Minute)
|
||||
defer timeout.Stop()
|
||||
OUT:
|
||||
for {
|
||||
_, err = getDeployment(clientset, namespace, deploymentName)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
select {
|
||||
case <-timeout.C:
|
||||
break OUT
|
||||
default:
|
||||
|
||||
}
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
err = fmt.Errorf("error: delete deployment is taking unusually long")
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
func createPfRole(clientset *kubernetes.Clientset, namespace, roleName string) (*v1beta1.Role, error) {
|
||||
// build the role defination we want to create
|
||||
var role *v1beta1.Role
|
||||
roleSpec := &v1beta1.Role{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: roleName,
|
||||
Namespace: namespace,
|
||||
Labels: map[string]string{
|
||||
"app": "preflight",
|
||||
},
|
||||
},
|
||||
Rules: []v1beta1.PolicyRule{},
|
||||
}
|
||||
|
||||
// now create the role in kubernetes cluster using the clientset
|
||||
if err := retryOnError(func() (err error) {
|
||||
role, err = clientset.RbacV1beta1().Roles(namespace).Create(roleSpec)
|
||||
return err
|
||||
}); err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Printf("Created role: %s\n", role.Name)
|
||||
|
||||
return role, nil
|
||||
}
|
||||
|
||||
func deleteRole(clientset *kubernetes.Clientset, namespace string, role *v1beta1.Role) {
|
||||
rolesClient := clientset.RbacV1beta1().Roles(namespace)
|
||||
|
||||
deletePolicy := v1.DeletePropagationForeground
|
||||
deleteOptions := v1.DeleteOptions{
|
||||
PropagationPolicy: &deletePolicy,
|
||||
}
|
||||
err := rolesClient.Delete(role.GetName(), &deleteOptions)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Deleted role: %s\n\n", role.Name)
|
||||
}
|
||||
|
||||
func createPfRoleBinding(clientset *kubernetes.Clientset, namespace, roleBindingName string) (*v1beta1.RoleBinding, error) {
|
||||
var roleBinding *v1beta1.RoleBinding
|
||||
// build the rolebinding defination we want to create
|
||||
roleBindingSpec := &v1beta1.RoleBinding{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: roleBindingName,
|
||||
Namespace: namespace,
|
||||
Labels: map[string]string{
|
||||
"app": "demo",
|
||||
},
|
||||
},
|
||||
Subjects: []v1beta1.Subject{
|
||||
{
|
||||
Kind: "ServiceAccount",
|
||||
APIGroup: "",
|
||||
Name: "preflight-check-subject",
|
||||
Namespace: namespace,
|
||||
},
|
||||
},
|
||||
RoleRef: v1beta1.RoleRef{
|
||||
APIGroup: "",
|
||||
Kind: "Role",
|
||||
Name: "preflight-check-roleref",
|
||||
},
|
||||
}
|
||||
|
||||
// now create the roleBinding in kubernetes cluster using the clientset
|
||||
if err := retryOnError(func() (err error) {
|
||||
roleBinding, err = clientset.RbacV1beta1().RoleBindings(namespace).Create(roleBindingSpec)
|
||||
return err
|
||||
}); err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
fmt.Printf("Created RoleBinding: %s\n", roleBindingSpec.Name)
|
||||
return roleBinding, nil
|
||||
}
|
||||
|
||||
func deleteRoleBinding(clientset *kubernetes.Clientset, namespace string, roleBinding *v1beta1.RoleBinding) {
|
||||
roleBindingClient := clientset.RbacV1beta1().RoleBindings(namespace)
|
||||
|
||||
deletePolicy := v1.DeletePropagationForeground
|
||||
deleteOptions := v1.DeleteOptions{
|
||||
PropagationPolicy: &deletePolicy,
|
||||
}
|
||||
err := roleBindingClient.Delete(roleBinding.GetName(), &deleteOptions)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Deleted RoleBinding: %s\n\n", roleBinding.Name)
|
||||
}
|
||||
|
||||
func createPfServiceAccount(clientset *kubernetes.Clientset, namespace, serviceAccountName string) (*apiv1.ServiceAccount, error) {
|
||||
var serviceAccount *apiv1.ServiceAccount
|
||||
// build the serviceAccount defination we want to create
|
||||
serviceAccountSpec := &apiv1.ServiceAccount{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "preflight-check-test-serviceaccount",
|
||||
Namespace: namespace,
|
||||
Labels: map[string]string{
|
||||
"app": "demo",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// now create the serviceAccount in kubernetes cluster using the clientset
|
||||
if err := retryOnError(func() (err error) {
|
||||
serviceAccount, err = clientset.CoreV1().ServiceAccounts(namespace).Create(serviceAccountSpec)
|
||||
return err
|
||||
}); err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
fmt.Printf("Created Service Account: %s\n", serviceAccountSpec.Name)
|
||||
return serviceAccount, nil
|
||||
}
|
||||
|
||||
func deleteServiceAccount(clientset *kubernetes.Clientset, namespace string, serviceAccount *apiv1.ServiceAccount) {
|
||||
serviceAccountClient := clientset.CoreV1().ServiceAccounts(namespace)
|
||||
|
||||
deletePolicy := v1.DeletePropagationForeground
|
||||
deleteOptions := v1.DeleteOptions{
|
||||
PropagationPolicy: &deletePolicy,
|
||||
}
|
||||
err := serviceAccountClient.Delete(serviceAccount.GetName(), &deleteOptions)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Deleted ServiceAccount: %s\n\n", serviceAccount.Name)
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_initiateK8sOps(t *testing.T) {
|
||||
t.Skip()
|
||||
type args struct {
|
||||
opr string
|
||||
namespace string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid case",
|
||||
args: args{
|
||||
opr: fmt.Sprintf("version"),
|
||||
namespace: "test-ns",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid case",
|
||||
args: args{
|
||||
opr: fmt.Sprintf("versions"),
|
||||
namespace: "test-ns",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := initiateK8sOps(tt.args.opr, tt.args.namespace); (err != nil) != tt.wantErr {
|
||||
t.Errorf("initiateK8sOps() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,148 +0,0 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/qlik-oss/k-apis/pkg/cr"
|
||||
"github.com/qlik-oss/sense-installer/pkg/api"
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
"github.com/qlik-oss/sense-installer/pkg/qliksense"
|
||||
)
|
||||
|
||||
func (qp *QliksensePreflight) CheckCreateRole(namespace string) error {
|
||||
// create a Role
|
||||
fmt.Printf("Preflight role check: \n")
|
||||
err := qp.checkCreateEntity(namespace, "Role")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Completed preflight role check")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (qp *QliksensePreflight) CheckCreateRoleBinding(namespace string) error {
|
||||
// create a RoleBinding
|
||||
fmt.Printf("Preflight rolebinding check: \n")
|
||||
err := qp.checkCreateEntity(namespace, "RoleBinding")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Completed preflight rolebinding check")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (qp *QliksensePreflight) CheckCreateServiceAccount(namespace string) error {
|
||||
// create a service account
|
||||
fmt.Printf("Preflight serviceaccount check: \n")
|
||||
err := qp.checkCreateEntity(namespace, "ServiceAccount")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Completed preflight serviceaccount check")
|
||||
return nil
|
||||
}
|
||||
func (qp *QliksensePreflight) checkCreateEntity(namespace, entityToTest string) error {
|
||||
qConfig := qapi.NewQConfig(qp.Q.QliksenseHome)
|
||||
var currentCR *qapi.QliksenseCR
|
||||
mfroot := ""
|
||||
kusDir := ""
|
||||
var err error
|
||||
currentCR, err = qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to retrieve current CR: %v\n", err)
|
||||
return err
|
||||
}
|
||||
if currentCR.IsRepoExist() {
|
||||
mfroot = currentCR.Spec.GetManifestsRoot()
|
||||
} else if tempDownloadedDir, err := qliksense.DownloadFromGitRepoToTmpDir(qliksense.QLIK_GIT_REPO, "master"); err != nil {
|
||||
fmt.Printf("Unable to Download from git repo to tmp dir: %v\n", err)
|
||||
return err
|
||||
} else {
|
||||
mfroot = tempDownloadedDir
|
||||
}
|
||||
|
||||
if currentCR.Spec.Profile == "" {
|
||||
kusDir = filepath.Join(mfroot, "manifests", "docker-desktop")
|
||||
} else {
|
||||
kusDir = filepath.Join(mfroot, "manifests", currentCR.Spec.Profile)
|
||||
}
|
||||
|
||||
currentCR.SetName("random")
|
||||
currentCR.Spec.RotateKeys = "None"
|
||||
currentCR.Spec.ManifestsRoot = mfroot
|
||||
userHomeDir, err := homedir.Dir()
|
||||
if err != nil {
|
||||
fmt.Printf(`error fetching user's home directory: %v\n`, err)
|
||||
return err
|
||||
}
|
||||
|
||||
cr.GeneratePatches(¤tCR.KApiCr, path.Join(userHomeDir, ".kube", "config"))
|
||||
|
||||
resultYamlString, err := qliksense.ExecuteKustomizeBuild(kusDir)
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to retrieve manifests from executing kustomize: %v\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
sa := qliksense.GetYamlsFromMultiDoc(string(resultYamlString), entityToTest)
|
||||
if sa != "" {
|
||||
// sa = strings.ReplaceAll(sa, "namespace: default\n", fmt.Sprintf("namespace: %s\n", namespace))
|
||||
} else {
|
||||
err := fmt.Errorf("Unable to retrieve yamls to apply on cluster")
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
namespace = "" // namespace is handled when generating the manifests
|
||||
|
||||
defer func() {
|
||||
fmt.Println("Cleaning up resources")
|
||||
api.KubectlDelete(sa, namespace)
|
||||
if err != nil {
|
||||
fmt.Println("Preflight cleanup failed!")
|
||||
}
|
||||
}()
|
||||
|
||||
err = api.KubectlApply(sa, namespace)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Failed to create entity on the cluster: %v", err)
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Preflight %s check: PASSED\n", entityToTest)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (qp *QliksensePreflight) CheckCreateRB(namespace string, kubeConfigContents []byte) error {
|
||||
|
||||
// create a role
|
||||
fmt.Printf("Preflight createRole check: \n")
|
||||
err := qp.checkCreateEntity(namespace, "Role")
|
||||
if err != nil {
|
||||
fmt.Println("Preflight role check: FAILED")
|
||||
}
|
||||
fmt.Printf("Completed preflight role check\n\n")
|
||||
|
||||
// create a roleBinding
|
||||
fmt.Printf("Preflight rolebinding check: \n")
|
||||
err = qp.checkCreateEntity(namespace, "RoleBinding")
|
||||
if err != nil {
|
||||
fmt.Println("Preflight rolebinding check: FAILED")
|
||||
}
|
||||
fmt.Printf("Completed preflight rolebinding check\n\n")
|
||||
|
||||
// create a service account
|
||||
fmt.Printf("Preflight serviceaccount check: \n")
|
||||
err = qp.checkCreateEntity(namespace, "ServiceAccount")
|
||||
if err != nil {
|
||||
fmt.Println("Preflight serviceaccount check: FAILED")
|
||||
}
|
||||
fmt.Printf("Completed preflight serviceaccount check\n\n")
|
||||
|
||||
fmt.Println("Preflight RB check: PASSED")
|
||||
fmt.Println("Completed preflight CreateRB check")
|
||||
return nil
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"k8s.io/apimachinery/pkg/version"
|
||||
)
|
||||
|
||||
func (qp *QliksensePreflight) CheckK8sVersion(namespace string, kubeConfigContents []byte) error {
|
||||
|
||||
var currentVersion *semver.Version
|
||||
|
||||
clientset, _, err := getK8SClientSet(kubeConfigContents, "")
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Unable to create clientset: %v\n", err)
|
||||
return err
|
||||
}
|
||||
var serverVersion *version.Info
|
||||
if err := retryOnError(func() (err error) {
|
||||
serverVersion, err = clientset.ServerVersion()
|
||||
return err
|
||||
}); err != nil {
|
||||
err = fmt.Errorf("Unable to get server version: %v\n", err)
|
||||
//fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Kubernetes API Server version: %s\n", serverVersion.String())
|
||||
|
||||
// Compare K8s version on the cluster with minimum supported k8s version
|
||||
currentVersion, err = semver.NewVersion(serverVersion.String())
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Unable to convert server version into semver version: %v\n", err)
|
||||
//fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
//fmt.Printf("Current K8s Version: %v\n", currentVersion)
|
||||
|
||||
minK8sVersionSemver, err := semver.NewVersion(qp.GetPreflightConfigObj().GetMinK8sVersion())
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Unable to convert minimum Kubernetes version into semver version:%v\n", err)
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if currentVersion.GreaterThan(minK8sVersionSemver) {
|
||||
//fmt.Printf("\n\nCurrent %s Component version: %s is less than minimum required version:%s\n", component, currentComponentVersion, componentVersionFromDependenciesYaml)
|
||||
fmt.Printf("Current %s is greater than minimum required version:%s\n", currentVersion, minK8sVersionSemver)
|
||||
fmt.Println("Preflight minimum kubernetes version check: PASSED")
|
||||
} else {
|
||||
fmt.Printf("Current %s is less than minimum required version:%s\n", currentVersion, minK8sVersionSemver)
|
||||
fmt.Println("Preflight minimum kubernetes version check: FAILED")
|
||||
}
|
||||
fmt.Printf("Completed Preflight kubernetes minimum version check\n")
|
||||
return nil
|
||||
}
|
||||
BIN
pkg/qliksense/.DS_Store
vendored
BIN
pkg/qliksense/.DS_Store
vendored
Binary file not shown.
@@ -1,242 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"sort"
|
||||
|
||||
kapis_git "github.com/qlik-oss/k-apis/pkg/git"
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type patch struct {
|
||||
Target struct {
|
||||
Kind string `yaml:"kind"`
|
||||
LabelSelector string `yaml:"labelSelector"`
|
||||
} `yaml:"target"`
|
||||
Patch string `yaml:"patch"`
|
||||
}
|
||||
|
||||
type selectivePatch struct {
|
||||
APIVersion string `yaml:"apiVersion"`
|
||||
Metadata struct {
|
||||
Name string `yaml:"name"`
|
||||
} `yaml:"metadata"`
|
||||
Enabled bool `yaml:"enabled"`
|
||||
Patches []patch `yaml:"patches"`
|
||||
}
|
||||
|
||||
type helmChart struct {
|
||||
APIVersion string `yaml:"apiVersion"`
|
||||
Kind string `yaml:"kind"`
|
||||
Metadata struct {
|
||||
Name string `yaml:"name"`
|
||||
} `yaml:"metadata"`
|
||||
ReleaseNamespace string `yaml:"releaseNamespace"`
|
||||
ChartHome string `yaml:"chartHome"`
|
||||
ChartRepo string `yaml:"chartRepo"`
|
||||
ChartName string `yaml:"chartName"`
|
||||
ChartVersion string `yaml:"chartVersion"`
|
||||
}
|
||||
|
||||
type VersionOutput struct {
|
||||
QliksenseVersion string `yaml:"qlikSenseVersion"`
|
||||
Images []string `yaml:"images"`
|
||||
}
|
||||
|
||||
type nullWriter struct {
|
||||
}
|
||||
|
||||
func (nw *nullWriter) Write(p []byte) (n int, err error) {
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
const (
|
||||
defaultProfile = "docker-desktop"
|
||||
defaultConfigRepoGitUrl = "https://github.com/qlik-oss/qliksense-k8s"
|
||||
)
|
||||
|
||||
func (q *Qliksense) About(gitRef, profile string) (*VersionOutput, error) {
|
||||
configDirectory, isTemporary, profile, err := q.getConfigDirectory(defaultConfigRepoGitUrl, gitRef, profile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if isTemporary {
|
||||
defer os.RemoveAll(configDirectory)
|
||||
}
|
||||
return q.AboutDir(configDirectory, profile)
|
||||
}
|
||||
|
||||
func (q *Qliksense) AboutDir(configDirectory, profile string) (*VersionOutput, error) {
|
||||
if chartVersion, err := getChartVersion(filepath.Join(configDirectory, "transformers", "qseokversion.yaml"), "qliksense"); err != nil {
|
||||
return nil, err
|
||||
} else if kuzManifest, err := executeKustomizeBuildWithStdoutProgress(filepath.Join(configDirectory, "manifests", profile)); err != nil {
|
||||
return nil, err
|
||||
} else if images, err := getImageList(kuzManifest); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return &VersionOutput{
|
||||
QliksenseVersion: chartVersion,
|
||||
Images: images,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Qliksense) getConfigDirectory(gitUrl, gitRef, profileEntered string) (dir string, isTemporary bool, profile string, err error) {
|
||||
profile = profileEntered
|
||||
if profile == "" {
|
||||
profile = defaultProfile
|
||||
}
|
||||
|
||||
if gitRef != "" {
|
||||
if dir, err = DownloadFromGitRepoToTmpDir(gitUrl, gitRef); err != nil {
|
||||
return "", false, "", err
|
||||
} else {
|
||||
return dir, true, profile, nil
|
||||
}
|
||||
}
|
||||
|
||||
var exists bool
|
||||
exists, dir, err = configExistsInCurrentDirectory(profile)
|
||||
if err != nil {
|
||||
return "", false, "", err
|
||||
} else if exists {
|
||||
return dir, false, profile, nil
|
||||
}
|
||||
|
||||
var profileFromCurrentContext string
|
||||
exists, dir, profileFromCurrentContext, err = q.configExistsInCurrentContext()
|
||||
if err != nil {
|
||||
return "", false, "", err
|
||||
} else if exists {
|
||||
if profileEntered == "" {
|
||||
profile = profileFromCurrentContext
|
||||
}
|
||||
return dir, false, profile, nil
|
||||
}
|
||||
|
||||
if dir, err = DownloadFromGitRepoToTmpDir(gitUrl, "master"); err != nil {
|
||||
return "", false, "", err
|
||||
} else {
|
||||
return dir, true, profile, nil
|
||||
}
|
||||
}
|
||||
|
||||
//DownloadFromGitRepoToTmpDir download git repo to a temporary directory
|
||||
func DownloadFromGitRepoToTmpDir(gitUrl, gitRef string) (string, error) {
|
||||
if tmpDir, err := ioutil.TempDir("", ""); err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
downloadPath := path.Join(tmpDir, "repo")
|
||||
if err := downloadFromGitRepo(gitUrl, gitRef, downloadPath); err != nil {
|
||||
_ = os.RemoveAll(tmpDir)
|
||||
return "", err
|
||||
} else {
|
||||
return downloadPath, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func downloadFromGitRepo(gitUrl, gitRef, destDir string) error {
|
||||
if repo, err := kapis_git.CloneRepository(destDir, gitUrl, nil); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return kapis_git.Checkout(repo, gitRef, "", nil)
|
||||
}
|
||||
}
|
||||
|
||||
func configExistsInCurrentDirectory(profile string) (exists bool, currentDirectory string, err error) {
|
||||
currentDirectory, err = os.Getwd()
|
||||
if err == nil {
|
||||
info, err := os.Stat(path.Join(currentDirectory, "manifests", profile))
|
||||
if err == nil && info.IsDir() {
|
||||
exists = true
|
||||
}
|
||||
}
|
||||
return exists, currentDirectory, err
|
||||
}
|
||||
|
||||
func (q *Qliksense) configExistsInCurrentContext() (exists bool, directory string, profile string, err error) {
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
if currentCr, err := qConfig.GetCurrentCR(); err != nil {
|
||||
return false, "", "", err
|
||||
} else if currentCr.Spec.ManifestsRoot == "" {
|
||||
return false, "", "", nil
|
||||
} else {
|
||||
return true, currentCr.Spec.GetManifestsRoot(), currentCr.Spec.Profile, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getImageList(yamlContent []byte) ([]string, error) {
|
||||
decoder := yaml.NewDecoder(bytes.NewReader(yamlContent))
|
||||
var resource map[string]interface{}
|
||||
imageMap := make(map[string]bool)
|
||||
for {
|
||||
err := decoder.Decode(&resource)
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
return nil, err
|
||||
}
|
||||
break
|
||||
}
|
||||
traverseYamlDecodedMapRecursively(reflect.ValueOf(resource), []string{}, func(path []string, val interface{}) {
|
||||
if len(path) >= 2 && path[len(path)-1] == "image" &&
|
||||
(path[len(path)-2] == "containers" || path[len(path)-2] == "initContainers") {
|
||||
if image, ok := val.(string); ok {
|
||||
imageMap[image] = true
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
var sortedImageList []string
|
||||
for image, _ := range imageMap {
|
||||
sortedImageList = append(sortedImageList, image)
|
||||
}
|
||||
sort.Strings(sortedImageList)
|
||||
return sortedImageList, nil
|
||||
}
|
||||
|
||||
func traverseYamlDecodedMapRecursively(val reflect.Value, path []string, visitorFunc func(path []string, val interface{})) {
|
||||
kind := val.Kind()
|
||||
switch kind {
|
||||
case reflect.Interface:
|
||||
traverseYamlDecodedMapRecursively(val.Elem(), path, visitorFunc)
|
||||
case reflect.Slice:
|
||||
for i := 0; i < val.Len(); i++ {
|
||||
traverseYamlDecodedMapRecursively(val.Index(i), path, visitorFunc)
|
||||
}
|
||||
case reflect.Map:
|
||||
for _, key := range val.MapKeys() {
|
||||
traverseYamlDecodedMapRecursively(val.MapIndex(key), append(path, key.Interface().(string)), visitorFunc)
|
||||
}
|
||||
default:
|
||||
if kind != reflect.Invalid {
|
||||
visitorFunc(path, val.Interface())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getChartVersion(versionFile, chartName string) (string, error) {
|
||||
var patchInst patch
|
||||
var selPatch selectivePatch
|
||||
var chart helmChart
|
||||
|
||||
if bytes, err := ioutil.ReadFile(versionFile); err != nil {
|
||||
return "", err
|
||||
} else if err = yaml.Unmarshal(bytes, &selPatch); err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, patchInst = range selPatch.Patches {
|
||||
if err := yaml.Unmarshal([]byte(patchInst.Patch), &chart); err == nil {
|
||||
if chart.ChartName == chartName {
|
||||
return chart.ChartVersion, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
@@ -1,579 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
func Test_About_getImageList(t *testing.T) {
|
||||
var testCases = []struct {
|
||||
name string
|
||||
k8sYaml string
|
||||
expectedImages []string
|
||||
}{
|
||||
{
|
||||
name: "base",
|
||||
k8sYaml: `
|
||||
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: nginx-deployment-1
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
replicas: 2 # tells deployment to run 2 pods matching the template
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:1.7.9
|
||||
ports:
|
||||
- containerPort: 80
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Secret
|
||||
metadata:
|
||||
creationTimestamp: 2018-11-15T20:46:46Z
|
||||
name: mysecret
|
||||
namespace: default
|
||||
resourceVersion: "7579"
|
||||
uid: 91460ecb-e917-11e8-98f2-025000000001
|
||||
type: Opaque
|
||||
data:
|
||||
username: YWRtaW5pc3RyYXRvcg==
|
||||
---
|
||||
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: nginx-deployment-2
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
replicas: 2 # tells deployment to run 2 pods matching the template
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:1.7.9
|
||||
ports:
|
||||
- containerPort: 80
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: nginx
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
||||
ports:
|
||||
- port: 80
|
||||
name: web
|
||||
clusterIP: None
|
||||
selector:
|
||||
app: nginx
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: StatefulSet
|
||||
metadata:
|
||||
name: web
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx # has to match .spec.template.metadata.labels
|
||||
serviceName: "nginx"
|
||||
replicas: 3 # by default is 1
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx # has to match .spec.selector.matchLabels
|
||||
spec:
|
||||
terminationGracePeriodSeconds: 10
|
||||
containers:
|
||||
- name: nginx
|
||||
image: k8s.gcr.io/nginx-slim:0.8
|
||||
ports:
|
||||
- containerPort: 80
|
||||
name: web
|
||||
volumeMounts:
|
||||
- name: www
|
||||
mountPath: /usr/share/nginx/html
|
||||
volumeClaimTemplates:
|
||||
- metadata:
|
||||
name: www
|
||||
spec:
|
||||
accessModes: [ "ReadWriteOnce" ]
|
||||
storageClassName: "my-storage-class"
|
||||
resources:
|
||||
requests:
|
||||
storage: 1Gi
|
||||
---
|
||||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
metadata:
|
||||
name: pi
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: pi
|
||||
image: perl
|
||||
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
|
||||
restartPolicy: Never
|
||||
backoffLimit: 4
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: init-demo
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx
|
||||
env:
|
||||
- name: FOO
|
||||
value: null
|
||||
ports:
|
||||
- containerPort: 80
|
||||
volumeMounts:
|
||||
- name: workdir
|
||||
mountPath: /usr/share/nginx/html
|
||||
# These containers are run during pod initialization
|
||||
initContainers:
|
||||
- name: install
|
||||
image: busybox
|
||||
command:
|
||||
- wget
|
||||
- "-O"
|
||||
- "/work-dir/index.html"
|
||||
- http://kubernetes.io
|
||||
volumeMounts:
|
||||
- name: workdir
|
||||
mountPath: "/work-dir"
|
||||
dnsPolicy: Default
|
||||
volumes:
|
||||
- name: workdir
|
||||
emptyDir: {}
|
||||
`,
|
||||
expectedImages: []string{"busybox", "k8s.gcr.io/nginx-slim:0.8", "nginx", "nginx:1.7.9", "perl"},
|
||||
},
|
||||
{
|
||||
name: "works for custom resources and CronJobs",
|
||||
k8sYaml: `
|
||||
apiVersion: "qixmanager.qlik.com/v1"
|
||||
kind: "Engine"
|
||||
metadata:
|
||||
name: release-name-engine-reload
|
||||
spec:
|
||||
metadata:
|
||||
labels:
|
||||
qix-engine: qix-engine
|
||||
annotations:
|
||||
prometheus.io/scrape: "true"
|
||||
workloadType: "reload"
|
||||
podSpec:
|
||||
imagePullSecrets:
|
||||
- name: artifactory-docker-secret
|
||||
dnsConfig:
|
||||
options:
|
||||
- name: timeout
|
||||
value: "1"
|
||||
- name: single-request-reopen
|
||||
containers:
|
||||
- name: engine-reload
|
||||
image: another-engine
|
||||
---
|
||||
apiVersion: batch/v1beta1
|
||||
kind: CronJob
|
||||
metadata:
|
||||
name: hello
|
||||
spec:
|
||||
schedule: "*/1 * * * *"
|
||||
jobTemplate:
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: hello
|
||||
image: busybox2
|
||||
args:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- date; echo Hello from the Kubernetes cluster
|
||||
restartPolicy: OnFailure
|
||||
`,
|
||||
expectedImages: []string{"another-engine", "busybox2"},
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
images, err := getImageList([]byte(testCase.k8sYaml))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v\n", err)
|
||||
}
|
||||
if !reflect.DeepEqual(images, testCase.expectedImages) {
|
||||
t.Fatalf("expected %v, but got: %v\n", testCase.expectedImages, images)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_About_getConfigDirectory(t *testing.T) {
|
||||
verifyAsdBranch := func(configDir string) (ok bool, reason string) {
|
||||
tmpDir := os.TempDir()
|
||||
|
||||
if (path.Clean(path.Dir(path.Dir(configDir))) != path.Clean(tmpDir)) || (path.Base(configDir) != "repo") {
|
||||
return false, fmt.Sprintf("expected config directory path: %v to be under: %v and terminate with repo", configDir, tmpDir)
|
||||
}
|
||||
|
||||
if info, err := os.Stat(path.Join(configDir, "asdczxc")); err != nil || !info.Mode().IsRegular() {
|
||||
return false, fmt.Sprintf(`expected to find file: "asdczxc" under directory: %v`, configDir)
|
||||
}
|
||||
return true, ""
|
||||
}
|
||||
|
||||
verifyMasterBranch := func(configDir string) (ok bool, reason string) {
|
||||
tmpDir := os.TempDir()
|
||||
|
||||
if (path.Clean(path.Dir(path.Dir(configDir))) != path.Clean(tmpDir)) || (path.Base(configDir) != "repo") {
|
||||
return false, fmt.Sprintf("expected config directory path: %v to be under: %v and terminate with repo", configDir, tmpDir)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(path.Join(configDir, "asdczxc")); err == nil || !os.IsNotExist(err) {
|
||||
return false, fmt.Sprintf(`expected to NOT find file: "asdczxc"" under directory: %v`, configDir)
|
||||
}
|
||||
|
||||
if info, err := os.Stat(path.Join(configDir, "sad")); err != nil || !info.Mode().IsRegular() {
|
||||
return false, fmt.Sprintf(`expected to find file: "sad"" under directory: %v`, configDir)
|
||||
}
|
||||
return true, ""
|
||||
}
|
||||
|
||||
var testCases = []struct {
|
||||
name string
|
||||
setup func(t *testing.T) (q *Qliksense, gitUrl, gitRef, profileEntered string)
|
||||
verify func(q *Qliksense, configDir string, isTemporary bool, profile string) (ok bool, reason string, err error)
|
||||
cleanup func(q *Qliksense, configDir string) error
|
||||
}{
|
||||
{
|
||||
name: "config in current directory and default profile",
|
||||
setup: func(t *testing.T) (q *Qliksense, gitUrl, gitRef, profileEntered string) {
|
||||
currentDirectory, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("error obtaining current directory: %v\n", err)
|
||||
}
|
||||
|
||||
defaultProfilePath := path.Join(currentDirectory, "manifests", "docker-desktop")
|
||||
err = os.MkdirAll(defaultProfilePath, os.ModePerm)
|
||||
if err != nil {
|
||||
t.Fatalf("error making path: %v, err: %v\n", defaultProfilePath, err)
|
||||
}
|
||||
return &Qliksense{}, "no-clone-for-you", "", ""
|
||||
},
|
||||
verify: func(_ *Qliksense, configDir string, isTemporary bool, profile string) (ok bool, reason string, err error) {
|
||||
currentDirectory, err := os.Getwd()
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
|
||||
if configDir != currentDirectory {
|
||||
return false, fmt.Sprintf("expected config directory: %v to equal current directory: %v", configDir, currentDirectory), nil
|
||||
}
|
||||
|
||||
if isTemporary {
|
||||
return false, "expected isTemporary to be false", nil
|
||||
}
|
||||
|
||||
if profile != "docker-desktop" {
|
||||
return false, fmt.Sprintf("expected profile to be: docker-desktop, but it was: %v", profile), nil
|
||||
}
|
||||
|
||||
return true, "", nil
|
||||
},
|
||||
cleanup: func(_ *Qliksense, configDir string) error {
|
||||
if currentDirectory, err := os.Getwd(); err != nil {
|
||||
return err
|
||||
} else if err := os.RemoveAll(path.Join(currentDirectory, "manifests")); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "config in current directory and profile specified",
|
||||
setup: func(t *testing.T) (q *Qliksense, gitUrl, gitRef, profileEntered string) {
|
||||
currentDirectory, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("error obtaining current directory: %v\n", err)
|
||||
}
|
||||
|
||||
profileEntered = "foo"
|
||||
defaultProfilePath := path.Join(currentDirectory, "manifests", profileEntered)
|
||||
err = os.MkdirAll(defaultProfilePath, os.ModePerm)
|
||||
if err != nil {
|
||||
t.Fatalf("error making path: %v, err: %v\n", defaultProfilePath, err)
|
||||
}
|
||||
return &Qliksense{}, "no-clone-for-you", "", profileEntered
|
||||
},
|
||||
verify: func(_ *Qliksense, configDir string, isTemporary bool, profile string) (ok bool, reason string, err error) {
|
||||
currentDirectory, err := os.Getwd()
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
|
||||
if configDir != currentDirectory {
|
||||
return false, fmt.Sprintf("expected config directory: %v to equal current directory: %v", configDir, currentDirectory), nil
|
||||
}
|
||||
|
||||
if isTemporary {
|
||||
return false, "expected isTemporary to be false", nil
|
||||
}
|
||||
|
||||
if profile != "foo" {
|
||||
return false, fmt.Sprintf("expected profile to be: foo, but it was: %v", profile), nil
|
||||
}
|
||||
|
||||
return true, "", nil
|
||||
},
|
||||
cleanup: func(_ *Qliksense, configDir string) error {
|
||||
if currentDirectory, err := os.Getwd(); err != nil {
|
||||
return err
|
||||
} else if err := os.RemoveAll(path.Join(currentDirectory, "manifests")); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "config downloaded from git based on specific git ref and default profile used",
|
||||
setup: func(t *testing.T) (q *Qliksense, gitUrl, gitRef, profileEntered string) {
|
||||
return &Qliksense{}, "https://github.com/test/HelloWorld", "asd", ""
|
||||
},
|
||||
verify: func(_ *Qliksense, configDir string, isTemporary bool, profile string) (ok bool, reason string, err error) {
|
||||
ok, reason = verifyAsdBranch(configDir)
|
||||
if !ok {
|
||||
return ok, reason, nil
|
||||
}
|
||||
|
||||
if !isTemporary {
|
||||
return false, "expected isTemporary to be true", nil
|
||||
}
|
||||
|
||||
if profile != "docker-desktop" {
|
||||
return false, fmt.Sprintf("expected profile to be: docker-desktop, but it was: %v", profile), nil
|
||||
}
|
||||
|
||||
return true, "", nil
|
||||
},
|
||||
cleanup: func(_ *Qliksense, configDir string) error {
|
||||
tmpDir := os.TempDir()
|
||||
|
||||
if path.Clean(path.Dir(path.Dir(configDir))) == path.Clean(tmpDir) && path.Base(configDir) == "repo" {
|
||||
tmpTmpDir := path.Dir(configDir)
|
||||
if err := os.RemoveAll(tmpTmpDir); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "config downloaded from git based on specific git ref and profile specified",
|
||||
setup: func(t *testing.T) (q *Qliksense, gitUrl, gitRef, profileEntered string) {
|
||||
return &Qliksense{}, "https://github.com/test/HelloWorld", "asd", "foo"
|
||||
},
|
||||
verify: func(_ *Qliksense, configDir string, isTemporary bool, profile string) (ok bool, reason string, err error) {
|
||||
ok, reason = verifyAsdBranch(configDir)
|
||||
if !ok {
|
||||
return ok, reason, nil
|
||||
}
|
||||
|
||||
if !isTemporary {
|
||||
return false, "expected isTemporary to be true", nil
|
||||
}
|
||||
|
||||
if profile != "foo" {
|
||||
return false, fmt.Sprintf("expected profile to be: foo, but it was: %v", profile), nil
|
||||
}
|
||||
|
||||
return true, "", nil
|
||||
},
|
||||
cleanup: func(_ *Qliksense, configDir string) error {
|
||||
tmpDir := os.TempDir()
|
||||
|
||||
if path.Clean(path.Dir(path.Dir(configDir))) == path.Clean(tmpDir) && path.Base(configDir) == "repo" {
|
||||
tmpTmpDir := path.Dir(configDir)
|
||||
if err := os.RemoveAll(tmpTmpDir); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "config downloaded from git from master branch and default profile used",
|
||||
setup: func(t *testing.T) (q *Qliksense, gitUrl, gitRef, profileEntered string) {
|
||||
if qliksenseHome, err := ioutil.TempDir("", ""); err != nil {
|
||||
t.Fatalf("error creating tmp qliksenseHome directory: %v\n", err)
|
||||
return nil, "", "", ""
|
||||
} else {
|
||||
q := &Qliksense{QliksenseHome: qliksenseHome}
|
||||
if err := q.SetUpQliksenseDefaultContext(); err != nil {
|
||||
t.Fatalf("error setting up default context in the tmp dir: %v\n", err)
|
||||
return nil, "", "", ""
|
||||
} else {
|
||||
return q, "https://github.com/test/HelloWorld", "", ""
|
||||
}
|
||||
}
|
||||
},
|
||||
verify: func(_ *Qliksense, configDir string, isTemporary bool, profile string) (ok bool, reason string, err error) {
|
||||
ok, reason = verifyMasterBranch(configDir)
|
||||
if !ok {
|
||||
return ok, reason, nil
|
||||
}
|
||||
|
||||
if !isTemporary {
|
||||
return false, "expected isTemporary to be true", nil
|
||||
}
|
||||
|
||||
if profile != "docker-desktop" {
|
||||
return false, fmt.Sprintf("expected profile to be: docker-desktop, but it was: %v", profile), nil
|
||||
}
|
||||
|
||||
return true, "", nil
|
||||
},
|
||||
cleanup: func(q *Qliksense, configDir string) error {
|
||||
tmpDir := os.TempDir()
|
||||
if path.Clean(path.Dir(path.Dir(configDir))) == path.Clean(tmpDir) && path.Base(configDir) == "repo" {
|
||||
tmpTmpDir := path.Dir(configDir)
|
||||
if err := os.RemoveAll(tmpTmpDir); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := os.RemoveAll(q.QliksenseHome); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "config downloaded from git from master branch and profile specified",
|
||||
setup: func(t *testing.T) (q *Qliksense, gitUrl, gitRef, profileEntered string) {
|
||||
if qliksenseHome, err := ioutil.TempDir("", ""); err != nil {
|
||||
t.Fatalf("error creating tmp qliksenseHome directory: %v\n", err)
|
||||
return nil, "", "", ""
|
||||
} else {
|
||||
q := &Qliksense{QliksenseHome: qliksenseHome}
|
||||
if err := q.SetUpQliksenseDefaultContext(); err != nil {
|
||||
t.Fatalf("error setting up default context in the tmp dir: %v\n", err)
|
||||
return nil, "", "", ""
|
||||
} else {
|
||||
return q, "https://github.com/test/HelloWorld", "", "foo"
|
||||
}
|
||||
}
|
||||
},
|
||||
verify: func(_ *Qliksense, configDir string, isTemporary bool, profile string) (ok bool, reason string, err error) {
|
||||
ok, reason = verifyMasterBranch(configDir)
|
||||
if !ok {
|
||||
return ok, reason, nil
|
||||
}
|
||||
|
||||
if !isTemporary {
|
||||
return false, "expected isTemporary to be true", nil
|
||||
}
|
||||
|
||||
if profile != "foo" {
|
||||
return false, fmt.Sprintf("expected profile to be: foo, but it was: %v", profile), nil
|
||||
}
|
||||
|
||||
return true, "", nil
|
||||
},
|
||||
cleanup: func(q *Qliksense, configDir string) error {
|
||||
tmpDir := os.TempDir()
|
||||
if path.Clean(path.Dir(path.Dir(configDir))) == path.Clean(tmpDir) && path.Base(configDir) == "repo" {
|
||||
tmpTmpDir := path.Dir(configDir)
|
||||
if err := os.RemoveAll(tmpTmpDir); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := os.RemoveAll(q.QliksenseHome); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "config loaded from current context",
|
||||
setup: func(t *testing.T) (q *Qliksense, gitUrl, gitRef, profileEntered string) {
|
||||
if qliksenseHome, err := ioutil.TempDir("", ""); err != nil {
|
||||
t.Fatalf("error creating tmp qliksenseHome directory: %v\n", err)
|
||||
return nil, "", "", ""
|
||||
} else {
|
||||
q := &Qliksense{QliksenseHome: qliksenseHome}
|
||||
if err := q.SetUpQliksenseDefaultContext(); err != nil {
|
||||
t.Fatalf("error setting up default context in the tmp dir: %v\n", err)
|
||||
return nil, "", "", ""
|
||||
} else if err := q.FetchQK8s("master"); err != nil {
|
||||
t.Fatalf("error fetching master config to the tmp dir: %v\n", err)
|
||||
return nil, "", "", ""
|
||||
} else {
|
||||
return q, "no-git-clone-for-you", "", ""
|
||||
}
|
||||
}
|
||||
},
|
||||
verify: func(q *Qliksense, configDir string, isTemporary bool, profile string) (ok bool, reason string, err error) {
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
expectedConfigDir := qConfig.BuildRepoPath("master")
|
||||
|
||||
if configDir != expectedConfigDir {
|
||||
return false, fmt.Sprintf("expected configDir to be %v", expectedConfigDir), nil
|
||||
}
|
||||
|
||||
if isTemporary {
|
||||
return false, "expected isTemporary to be false", nil
|
||||
}
|
||||
|
||||
if profile != "docker-desktop" {
|
||||
return false, fmt.Sprintf("expected profile to be: docker-desktop, but it was: %v", profile), nil
|
||||
}
|
||||
|
||||
return true, "", nil
|
||||
},
|
||||
cleanup: func(q *Qliksense, configDir string) error {
|
||||
if err := os.RemoveAll(q.QliksenseHome); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
q, gitUrl, gitRef, profileEntered := testCase.setup(t)
|
||||
configDirectory, isTemporary, profile, err := q.getConfigDirectory(gitUrl, gitRef, profileEntered)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v\n", err)
|
||||
}
|
||||
|
||||
if ok, reason, err := testCase.verify(q, configDirectory, isTemporary, profile); err != nil {
|
||||
t.Fatalf("unexpected verification error: %v\n", err)
|
||||
} else if !ok {
|
||||
t.Fatalf("verification failed: %v\n", reason)
|
||||
} else if err := testCase.cleanup(q, configDirectory); err != nil {
|
||||
t.Fatalf("unexpected cleanup error: %v\n", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
func (q *Qliksense) ApplyCRFromReader(r io.Reader, opts *InstallCommandOptions, keepPatchFiles, overwriteExistingContext bool) error {
|
||||
if err := q.LoadCr(r, overwriteExistingContext); err != nil {
|
||||
return err
|
||||
}
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
cr, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if IsQliksenseInstalled(cr.GetName()) {
|
||||
// it is needed in case want to upgrade from one version to another
|
||||
if cr.Spec.ManifestsRoot == "" && cr.Spec.Git == nil {
|
||||
if err := q.FetchQK8s(cr.GetLabelFromCr("version")); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return q.UpgradeQK8s(keepPatchFiles)
|
||||
}
|
||||
return q.InstallQK8s(cr.GetLabelFromCr("version"), opts, keepPatchFiles)
|
||||
}
|
||||
|
||||
func IsQliksenseInstalled(crName string) bool {
|
||||
args := []string{
|
||||
"get",
|
||||
"qliksense",
|
||||
crName,
|
||||
"-ogo-template",
|
||||
`--template='{{ .metadata.name}}'`,
|
||||
}
|
||||
_, err := qapi.KubectlDirectOps(args, "")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -1,217 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/go-homedir"
|
||||
|
||||
"github.com/qlik-oss/k-apis/pkg/cr"
|
||||
"github.com/qlik-oss/sense-installer/pkg/api"
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
const (
|
||||
Q_INIT_CRD_PATH = "manifests/base/manifests/qliksense-init"
|
||||
agreementTempalte = `
|
||||
Please read the agreement at https://www.qlik.com/us/legal/license-terms
|
||||
Accept the end user license agreement by providing acceptEULA=yes
|
||||
`
|
||||
)
|
||||
|
||||
func (q *Qliksense) ConfigApplyQK8s() error {
|
||||
|
||||
//get the current context cr
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
qcr, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
fmt.Println("cannot get the current-context cr", err)
|
||||
return err
|
||||
}
|
||||
// check if acceptEULA is yes or not
|
||||
if !qcr.IsEULA() {
|
||||
return errors.New(agreementTempalte + "\nPlease do $ qliksense config set-configs qliksense.acceptEULA=yes\n")
|
||||
}
|
||||
|
||||
// create patch dependent resoruces
|
||||
fmt.Println("Installing resoruces used kuztomize patch")
|
||||
if err := q.createK8sResoruceBeforePatch(qcr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if qcr.Spec.Git.Repository != "" {
|
||||
// fetching and applying manifest will be in the operator controller
|
||||
if dcr, err := qConfig.GetDecryptedCr(qcr); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return q.applyCR(dcr)
|
||||
}
|
||||
}
|
||||
if dcr, err := qConfig.GetDecryptedCr(qcr); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return q.applyConfigToK8s(dcr)
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Qliksense) configEjson() error {
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
if ejsonKeyDir, err := qConfig.GetCurrentContextEjsonKeyDir(); err != nil {
|
||||
return err
|
||||
} else if err := os.Unsetenv("EJSON_KEY"); err != nil {
|
||||
return err
|
||||
} else if err := os.Setenv("EJSON_KEYDIR", ejsonKeyDir); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *Qliksense) applyConfigToK8s(qcr *qapi.QliksenseCR) error {
|
||||
if qcr.Spec.RotateKeys != "None" {
|
||||
if err := q.configEjson(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
userHomeDir, err := homedir.Dir()
|
||||
if err != nil {
|
||||
fmt.Printf(`error fetching user's home directory: %v\n`, err)
|
||||
return err
|
||||
}
|
||||
fmt.Println("Manifests root: " + qcr.Spec.GetManifestsRoot())
|
||||
qcr.SetNamespace(qapi.GetKubectlNamespace())
|
||||
// generate patches
|
||||
cr.GeneratePatches(&qcr.KApiCr, path.Join(userHomeDir, ".kube", "config"))
|
||||
// apply generated manifests
|
||||
profilePath := filepath.Join(qcr.Spec.GetManifestsRoot(), qcr.Spec.GetProfileDir())
|
||||
mByte, err := ExecuteKustomizeBuild(profilePath)
|
||||
if err != nil {
|
||||
fmt.Println("cannot generate manifests for "+profilePath, err)
|
||||
return err
|
||||
}
|
||||
if err = qapi.KubectlApply(string(mByte), qcr.GetNamespace()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *Qliksense) ConfigViewCR() error {
|
||||
//get the current context cr
|
||||
r, err := q.getCurrentCRString()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
oth, err := q.getCurrentCrDependentResourceAsString()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(r + "\n" + oth)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *Qliksense) getCurrentCRString() (string, error) {
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
return q.getCRString(qConfig.Spec.CurrentContext)
|
||||
}
|
||||
|
||||
func (q *Qliksense) getCRString(contextName string) (string, error) {
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
qcr, err := qConfig.GetCR(contextName)
|
||||
if err != nil {
|
||||
fmt.Println("cannot get the context cr", err)
|
||||
return "", err
|
||||
}
|
||||
out, err := qapi.K8sToYaml(qcr)
|
||||
if err != nil {
|
||||
fmt.Println("cannot unmarshal cr ", err)
|
||||
return "", err
|
||||
}
|
||||
return string(out), nil
|
||||
|
||||
}
|
||||
|
||||
func (q *Qliksense) getCurrentCrDependentResourceAsString() (string, error) {
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
qcr, err := qConfig.GetCR(qConfig.Spec.CurrentContext)
|
||||
if err != nil {
|
||||
fmt.Println("cannot get the context cr", err)
|
||||
return "", err
|
||||
}
|
||||
var crString strings.Builder
|
||||
|
||||
for svcName, v := range qcr.Spec.Secrets {
|
||||
for _, item := range v {
|
||||
if item.ValueFrom != nil && item.ValueFrom.SecretKeyRef != nil {
|
||||
secretFilePath := filepath.Join(q.QliksenseHome, QliksenseContextsDir, qcr.GetName(), QliksenseSecretsDir, svcName+".yaml")
|
||||
|
||||
if api.FileExists(secretFilePath) {
|
||||
secretFile, err := ioutil.ReadFile(secretFilePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
crString.WriteString("\n---\n")
|
||||
crString.Write(secretFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
crString.WriteString("\n---\n")
|
||||
return crString.String(), nil
|
||||
}
|
||||
|
||||
func (q *Qliksense) EditCR(contextName string) error {
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
if contextName == "" {
|
||||
cr, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
contextName = cr.GetName()
|
||||
}
|
||||
crFilePath := qConfig.GetCRFilePath(contextName)
|
||||
tempFile, err := ioutil.TempFile("", "*.yaml")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
crContent, err := ioutil.ReadFile(crFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ioutil.WriteFile(tempFile.Name(), crContent, os.ModePerm); err != nil {
|
||||
return nil
|
||||
}
|
||||
cmd := exec.Command(getKubeEditorTool(), tempFile.Name())
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newCr, err := qapi.GetCRObject(tempFile.Name())
|
||||
if err != nil {
|
||||
return errors.New("cannot save the cr. Someting wrong in the file format. It is not saved\n" + err.Error())
|
||||
}
|
||||
oldCr, err := qapi.GetCRObject(crFilePath)
|
||||
|
||||
if oldCr.GetName() != newCr.GetName() {
|
||||
return errors.New("cr name cannot be chagned")
|
||||
}
|
||||
if newCr.Validate() {
|
||||
return qConfig.WriteCR(newCr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getKubeEditorTool() string {
|
||||
editor := os.Getenv("KUBE_EDITOR")
|
||||
if editor == "" {
|
||||
editor = "vim"
|
||||
}
|
||||
return editor
|
||||
}
|
||||
@@ -1,527 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"fmt"
|
||||
|
||||
"github.com/qlik-oss/k-apis/pkg/config"
|
||||
"github.com/robfig/cron/v3"
|
||||
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
b64 "encoding/base64"
|
||||
|
||||
ansi "github.com/mattn/go-colorable"
|
||||
"github.com/qlik-oss/sense-installer/pkg/api"
|
||||
"github.com/ttacon/chalk"
|
||||
_ "gopkg.in/yaml.v2"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
// Below are some constants to support qliksense context setup
|
||||
QliksenseConfigFile = "config.yaml"
|
||||
QliksenseContextsDir = "contexts"
|
||||
DefaultQliksenseContext = "qlik-default"
|
||||
MaxContextNameLength = 17
|
||||
QliksenseSecretsDir = "secrets"
|
||||
|
||||
imageRegistryConfigKey = "imageRegistry"
|
||||
pullSecretName = "artifactory-docker-secret"
|
||||
)
|
||||
|
||||
// SetSecrets - set-secrets <key>=<value> commands
|
||||
func (q *Qliksense) SetSecrets(args []string, isSecretSet bool) error {
|
||||
qConfig := api.NewQConfig(q.QliksenseHome)
|
||||
qliksenseCR, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Metadata name in qliksense CR is the name of the current context
|
||||
api.LogDebugMessage("Current context: %s", qliksenseCR.GetName())
|
||||
rsaPublicKey, _, err := qConfig.GetCurrentContextEncryptionKeyPair()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resultArgs, err := api.ProcessConfigArgs(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, ra := range resultArgs {
|
||||
api.LogDebugMessage("value args to be encrypted: %s", ra.Value)
|
||||
if err := q.processSecret(ra, rsaPublicKey, qliksenseCR, isSecretSet); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// write modified content into context-yaml
|
||||
return qConfig.WriteCR(qliksenseCR)
|
||||
}
|
||||
|
||||
func (q *Qliksense) processSecret(ra *api.ServiceKeyValue, rsaPublicKey *rsa.PublicKey, qliksenseCR *api.QliksenseCR, isSecretSet bool) error {
|
||||
// encrypt value with RSA key pair
|
||||
valueBytes := []byte(ra.Value)
|
||||
cipherText, e2 := api.Encrypt(valueBytes, rsaPublicKey)
|
||||
if e2 != nil {
|
||||
return e2
|
||||
}
|
||||
base64EncodedSecret := b64.StdEncoding.EncodeToString(cipherText)
|
||||
secretName := ""
|
||||
if isSecretSet {
|
||||
secretFolder := qliksenseCR.GetK8sSecretsFolder(q.QliksenseHome)
|
||||
secretFileName := filepath.Join(secretFolder, ra.SvcName+".yaml")
|
||||
|
||||
secretName = fmt.Sprintf("%s-%s-%s", qliksenseCR.GetName(), ra.SvcName, "senseinstaller")
|
||||
api.LogDebugMessage("Constructed secret name: %s", secretName)
|
||||
|
||||
k8sSecret := v1.Secret{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "v1",
|
||||
Kind: "Secret",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: secretName,
|
||||
},
|
||||
Type: v1.SecretTypeOpaque,
|
||||
}
|
||||
|
||||
if !api.DirExists(secretFolder) {
|
||||
if err := os.MkdirAll(secretFolder, os.ModePerm); err != nil {
|
||||
err = fmt.Errorf("Not able to create %s dir: %v", secretFolder, err)
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
// if read from file errors out, we can ignore it here
|
||||
_ = api.ReadFromFile(&k8sSecret, secretFileName)
|
||||
if k8sSecret.Data == nil {
|
||||
k8sSecret.Data = map[string][]byte{}
|
||||
}
|
||||
k8sSecret.Data[ra.Key] = []byte(base64EncodedSecret)
|
||||
|
||||
// Write secret to file
|
||||
k8sSecretBytes, err := api.K8sSecretToYaml(k8sSecret)
|
||||
if err != nil {
|
||||
api.LogDebugMessage("Error while converting K8s secret to yaml")
|
||||
return err
|
||||
}
|
||||
if err = ioutil.WriteFile(secretFileName, k8sSecretBytes, os.ModePerm); err != nil {
|
||||
api.LogDebugMessage("Error while writing K8s secret to file")
|
||||
return err
|
||||
}
|
||||
api.LogDebugMessage("Created a Kubernetes secret")
|
||||
|
||||
// Prepare args to update CR in the next step
|
||||
base64EncodedSecret = ""
|
||||
}
|
||||
|
||||
// write into CR the keyref of the secret
|
||||
qliksenseCR.Spec.AddToSecrets(ra.SvcName, ra.Key, base64EncodedSecret, secretName)
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetConfigs - set-configs <key>=<value> commands
|
||||
func (q *Qliksense) SetConfigs(args []string) error {
|
||||
// retieve current context from config.yaml
|
||||
qConfig := api.NewQConfig(q.QliksenseHome)
|
||||
qliksenseCR, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resultArgs, err := api.ProcessConfigArgs(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, ra := range resultArgs {
|
||||
qliksenseCR.Spec.AddToConfigs(ra.SvcName, ra.Key, ra.Value)
|
||||
}
|
||||
// write modified content into context.yaml
|
||||
return qConfig.WriteCR(qliksenseCR)
|
||||
}
|
||||
|
||||
func caseInsenstiveFieldByName(v reflect.Value, name string) reflect.Value {
|
||||
name = strings.ToLower(name)
|
||||
return v.FieldByNameFunc(func(n string) bool { return strings.ToLower(n) == name })
|
||||
}
|
||||
|
||||
func validateCR(key string, keySub string, value string, crSpec *api.QliksenseCR) (bool, *api.QliksenseCR) {
|
||||
cr := reflect.ValueOf(crSpec.Spec)
|
||||
keyValid := caseInsenstiveFieldByName(reflect.Indirect(cr), key)
|
||||
if !keyValid.IsValid() {
|
||||
//not in main spec
|
||||
fmt.Println(key, "is an invalid key")
|
||||
return false, crSpec
|
||||
} else if keySub == "" {
|
||||
if key == "rotatekeys" {
|
||||
if _, err := validateInput(value); err != nil {
|
||||
return false, crSpec
|
||||
}
|
||||
}
|
||||
}
|
||||
// checks if it is git or gitops
|
||||
if keySub != "" {
|
||||
if !keyValid.IsNil() {
|
||||
if !caseInsenstiveFieldByName(reflect.Indirect(keyValid), keySub).IsValid() {
|
||||
fmt.Println(keySub, "is an invalid key")
|
||||
return false, crSpec
|
||||
} else {
|
||||
// verify gitops enabled and gitops schedule
|
||||
switch keySub {
|
||||
case "schedule":
|
||||
if _, err := cron.ParseStandard(value); err != nil {
|
||||
fmt.Println("Please enter string with standard cron scheduling syntax ")
|
||||
return false, crSpec
|
||||
}
|
||||
case "enabled":
|
||||
if !strings.EqualFold(value, "yes") && !strings.EqualFold(value, "no") {
|
||||
fmt.Println("Please use yes or no for key enabled")
|
||||
return false, crSpec
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
switch key {
|
||||
case "gitops":
|
||||
crSpec.Spec.GitOps = &config.GitOps{}
|
||||
case "git":
|
||||
crSpec.Spec.Git = &config.Repo{}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true, crSpec
|
||||
}
|
||||
|
||||
// SetOtherConfigs - set profile/storageclassname/git.repository/manifestRoot commands
|
||||
func (q *Qliksense) SetOtherConfigs(args []string) error {
|
||||
// retieve current context from config.yaml
|
||||
qConfig := api.NewQConfig(q.QliksenseHome)
|
||||
qliksenseCR, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// modify appropriate fields
|
||||
if len(args) == 0 {
|
||||
err := fmt.Errorf("No args were provided. Please provide args to configure the current context")
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
for _, arg := range args {
|
||||
argsString := strings.Split(arg, "=")
|
||||
key := strings.ToLower(argsString[0])
|
||||
value := argsString[1]
|
||||
// check if key is for git or gitops (sub objects)
|
||||
keySplit := strings.Split(key, ".")
|
||||
key = keySplit[0]
|
||||
keySub := ""
|
||||
|
||||
if len(keySplit) == 2 {
|
||||
keySub = strings.ToLower(keySplit[1])
|
||||
}
|
||||
|
||||
valid := true
|
||||
valid, qliksenseCR = validateCR(key, keySub, value, qliksenseCR)
|
||||
field := caseInsenstiveFieldByName(reflect.Indirect(reflect.ValueOf(qliksenseCR.Spec)), key)
|
||||
if !valid {
|
||||
err := fmt.Errorf("Please enter one of: profile, storageClassName,rotateKeys, manifestRoot, git.repository or gitops arguments to configure the current context")
|
||||
return err
|
||||
} else if strings.EqualFold("", keySub) {
|
||||
// set spec for everything excluding git and gitops
|
||||
if field.CanSet() {
|
||||
field.SetString(value)
|
||||
}
|
||||
} else {
|
||||
// set spec for git or gitops
|
||||
subField := caseInsenstiveFieldByName(reflect.Indirect(field), keySub)
|
||||
if subField.CanSet() {
|
||||
subField.SetString(value)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(chalk.Green.Color("Successfully added to Custom Resource Spec"))
|
||||
}
|
||||
// write modified content into context.yaml
|
||||
return qConfig.WriteCR(qliksenseCR)
|
||||
}
|
||||
|
||||
// SetContextConfig - set the context for qliksense kubernetes resources to live in
|
||||
func (q *Qliksense) SetContextConfig(args []string) error {
|
||||
if len(args) == 1 {
|
||||
err := q.SetUpQliksenseContext(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err := fmt.Errorf("Please provide a name to configure the context with")
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *Qliksense) ListContextConfigs() error {
|
||||
qliksenseConfigFile := filepath.Join(q.QliksenseHome, QliksenseConfigFile)
|
||||
var qliksenseConfig api.QliksenseConfig
|
||||
if err := api.ReadFromFile(&qliksenseConfig, qliksenseConfigFile); err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
out := ansi.NewColorableStdout()
|
||||
w := tabwriter.NewWriter(out, 5, 8, 0, '\t', 0)
|
||||
fmt.Fprintln(w, chalk.Underline.TextStyle("Context Name"), "\t", chalk.Underline.TextStyle("CR File Location"))
|
||||
w.Flush()
|
||||
if len(qliksenseConfig.Spec.Contexts) > 0 {
|
||||
for _, cont := range qliksenseConfig.Spec.Contexts {
|
||||
fmt.Fprintln(w, cont.Name, "\t", qliksenseConfig.GetCRFilePath(cont.Name), "\t")
|
||||
}
|
||||
w.Flush()
|
||||
fmt.Fprintln(out, "")
|
||||
fmt.Fprintln(out, chalk.Bold.TextStyle("Current Context : "), qliksenseConfig.Spec.CurrentContext)
|
||||
} else {
|
||||
fmt.Fprintln(out, "No Contexts Available")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *Qliksense) DeleteContextConfig(args []string) error {
|
||||
if len(args) == 1 {
|
||||
qliksenseConfigFile := filepath.Join(q.QliksenseHome, QliksenseConfigFile)
|
||||
var qliksenseConfig api.QliksenseConfig
|
||||
api.ReadFromFile(&qliksenseConfig, qliksenseConfigFile)
|
||||
out := ansi.NewColorableStdout()
|
||||
switch args[0] {
|
||||
case qliksenseConfig.Spec.CurrentContext:
|
||||
fmt.Fprintln(out, chalk.Yellow.Color("Please switch contexts to be able to delete this context."))
|
||||
err := fmt.Errorf(chalk.Red.Color("Cannot delete current context - %s"), chalk.White.Color(chalk.Bold.TextStyle(qliksenseConfig.Spec.CurrentContext)))
|
||||
return err
|
||||
case DefaultQliksenseContext:
|
||||
err := fmt.Errorf(chalk.Red.Color("Cannot delete default qliksense context"))
|
||||
return err
|
||||
default:
|
||||
qliksenseContextsDir1 := filepath.Join(q.QliksenseHome, QliksenseContextsDir)
|
||||
qliksenseContextFile := filepath.Join(qliksenseContextsDir1, args[0])
|
||||
qliksenseSecretsDir1 := filepath.Join(q.QliksenseHome, QliksenseSecretsDir, QliksenseContextsDir)
|
||||
qliksenseSecretsFile := filepath.Join(qliksenseSecretsDir1, args[0])
|
||||
if err := os.RemoveAll(qliksenseContextFile); err != nil {
|
||||
err = fmt.Errorf("Not able to delete %s dir: %v", qliksenseContextsDir1, err)
|
||||
log.Println(err)
|
||||
return err
|
||||
} else if err := os.RemoveAll(qliksenseSecretsFile); err != nil {
|
||||
err = fmt.Errorf("No Secrets Folder Detected")
|
||||
log.Println(err)
|
||||
return err
|
||||
} else {
|
||||
currentLength := len(qliksenseConfig.Spec.Contexts)
|
||||
if currentLength > 0 {
|
||||
temp := qliksenseConfig.Spec.Contexts
|
||||
qliksenseConfig.Spec.Contexts = nil
|
||||
for _, ctx := range temp {
|
||||
if ctx.Name != args[0] {
|
||||
qliksenseConfig.Spec.Contexts = append(qliksenseConfig.Spec.Contexts, api.Context{
|
||||
Name: ctx.Name,
|
||||
CrFile: ctx.CrFile,
|
||||
})
|
||||
}
|
||||
}
|
||||
newLength := len(qliksenseConfig.Spec.Contexts)
|
||||
if currentLength != newLength {
|
||||
api.WriteToFile(&qliksenseConfig, qliksenseConfigFile)
|
||||
fmt.Fprintln(out, chalk.Yellow.Color(chalk.Underline.TextStyle("Warning: Active resources may still be running in-cluster")))
|
||||
fmt.Fprintln(out, chalk.Green.Color("Successfully deleted context: "), chalk.Bold.TextStyle(args[0]))
|
||||
} else {
|
||||
err := fmt.Errorf(chalk.Red.Color("Context not found"))
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err := fmt.Errorf("Please provide a context as an argument to delete")
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetUpQliksenseDefaultContext - to setup dir structure for default qliksense context
|
||||
func (q *Qliksense) SetUpQliksenseDefaultContext() error {
|
||||
if api.FileExists(filepath.Join(q.QliksenseHome, "config.yaml")) {
|
||||
qliksenseConfig := api.NewQConfig(q.QliksenseHome)
|
||||
if qliksenseConfig.IsContextExist(DefaultQliksenseContext) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return q.SetUpQliksenseContext(DefaultQliksenseContext)
|
||||
}
|
||||
|
||||
// SetUpQliksenseContext - to setup qliksense context
|
||||
func (q *Qliksense) SetUpQliksenseContext(contextName string) error {
|
||||
if contextName == "" {
|
||||
err := fmt.Errorf("Please enter a non-empty context-name")
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
// check the length of the context name entered by the user, it should not exceed 17 chars
|
||||
if len(contextName) > MaxContextNameLength {
|
||||
err := fmt.Errorf("Please enter a context-name with utmost 17 characters")
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
qliksenseConfigFile := filepath.Join(q.QliksenseHome, QliksenseConfigFile)
|
||||
qliksenseConfig := api.NewQConfigEmpty(q.QliksenseHome)
|
||||
|
||||
if !api.FileExists(qliksenseConfigFile) {
|
||||
qliksenseConfig.AddBaseQliksenseConfigs(contextName)
|
||||
} else {
|
||||
if err := api.ReadFromFile(qliksenseConfig, qliksenseConfigFile); err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if qliksenseConfig.IsContextExist(contextName) {
|
||||
qliksenseConfig.Spec.CurrentContext = contextName
|
||||
return qliksenseConfig.Write()
|
||||
}
|
||||
qliksenseCR := &api.QliksenseCR{}
|
||||
qliksenseCR.AddCommonConfig(contextName)
|
||||
qliksenseConfig.Spec.CurrentContext = contextName
|
||||
if err := qliksenseConfig.CreateOrWriteCrAndContext(qliksenseCR); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// set the encrypted default mongo
|
||||
return q.SetSecrets([]string{`qliksense.mongoDbUri="mongodb://qlik-default-mongodb:27017/qliksense?ssl=false"`}, false)
|
||||
}
|
||||
|
||||
func validateInput(input string) (string, error) {
|
||||
var err error
|
||||
validInputs := []string{"yes", "no", "None"}
|
||||
isValid := false
|
||||
for _, elem := range validInputs {
|
||||
if input == elem {
|
||||
isValid = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !isValid {
|
||||
err = fmt.Errorf("Please enter one of: yes, no or None")
|
||||
log.Println(err)
|
||||
|
||||
}
|
||||
return input, err
|
||||
}
|
||||
|
||||
// PrepareK8sSecret decodes and decrypts the secret value in the secret.yaml file and returns a B64encoded string
|
||||
func (q *Qliksense) PrepareK8sSecret(targetFile string) (string, error) {
|
||||
// check if targetFile exists
|
||||
if !api.FileExists(targetFile) {
|
||||
err := fmt.Errorf("Target file does not exist in the path provided")
|
||||
log.Println(err)
|
||||
return "", err
|
||||
}
|
||||
qConfig := api.NewQConfig(q.QliksenseHome)
|
||||
_, rsaPrivateKey, err := qConfig.GetCurrentContextEncryptionKeyPair()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// read the target file
|
||||
k8sSecret, err := readTargetfile(targetFile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// retrieve value from data section
|
||||
k8sSecret1, err := api.K8sSecretFromYaml(k8sSecret)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
dataMap := k8sSecret1.Data
|
||||
var resultMap = make(map[string][]byte)
|
||||
for k, v := range dataMap {
|
||||
ba, err := b64.StdEncoding.DecodeString(string(v))
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Not able to decode message: %v", err)
|
||||
return "", err
|
||||
}
|
||||
decryptedString, err := api.Decrypt(ba, rsaPrivateKey)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Not able to decrypt message: %v", err)
|
||||
return "", err
|
||||
}
|
||||
resultMap[k] = decryptedString
|
||||
}
|
||||
|
||||
// putting the above map back into the k8sSecret struct
|
||||
k8sSecret1.Data = resultMap
|
||||
k8sSecretBytes, err := api.K8sSecretToYaml(k8sSecret1)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(k8sSecretBytes), nil
|
||||
}
|
||||
|
||||
func readTargetfile(targetFile string) ([]byte, error) {
|
||||
k8sSecret, err := ioutil.ReadFile(targetFile)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Unable to read the targetFile")
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
return k8sSecret, nil
|
||||
}
|
||||
|
||||
func (q *Qliksense) SetImageRegistry(registry, pushUsername, pushPassword, pullUsername, pullPassword string) error {
|
||||
qConfig := api.NewQConfig(q.QliksenseHome)
|
||||
qliksenseCR, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if pushUsername != "" {
|
||||
if err := qConfig.SetPushDockerConfigJsonSecret(&api.DockerConfigJsonSecret{
|
||||
Uri: registry,
|
||||
Username: pushUsername,
|
||||
Password: pushPassword,
|
||||
}); err != nil {
|
||||
return err
|
||||
} else if err := qConfig.SetPullDockerConfigJsonSecret(&api.DockerConfigJsonSecret{
|
||||
Name: pullSecretName,
|
||||
Uri: registry,
|
||||
Username: pullUsername,
|
||||
Password: pullPassword,
|
||||
Email: pullUsername,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err := qConfig.DeletePushDockerConfigJsonSecret(); err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
} else if err := qConfig.DeletePullDockerConfigJsonSecret(); err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
qliksenseCR.Spec.AddToConfigs("qliksense", imageRegistryConfigKey, registry)
|
||||
return qConfig.WriteCR(qliksenseCR)
|
||||
}
|
||||
|
||||
func (q *Qliksense) SetEulaAccepted() error {
|
||||
qConfig := api.NewQConfig(q.QliksenseHome)
|
||||
qcr, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !qcr.IsEULA() {
|
||||
qcr.SetEULA("yes")
|
||||
return qConfig.WriteCurrentContextCR(qcr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,938 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
b64 "encoding/base64"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/qlik-oss/k-apis/pkg/config"
|
||||
|
||||
"github.com/qlik-oss/sense-installer/pkg/api"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
const (
|
||||
testDir = "./tests"
|
||||
qlikDefaultContext = "qlik-default"
|
||||
secrets = "secrets"
|
||||
contexts = "contexts"
|
||||
)
|
||||
|
||||
var targetFileStringTemplate = `
|
||||
apiVersion: v1
|
||||
data:
|
||||
mongoDbUri: %s
|
||||
kind: Secret
|
||||
metadata:
|
||||
name: testctx-qliksense-senseinstaller
|
||||
type: Opaque
|
||||
`
|
||||
var encText = "SFpVZ2t5SGsrN2lLQjlTYm9rbFUxSDFRcmVYdUxhTW9MUHlQOGtGditxMEcwZTlIZDl1dVRrV0tEYm5qUURSWFp3dStuNklueGk3anI2c1djSVdsbWlKTHdWQUJwdUg0a1NXd3llMUlMa2oxK3FRSFlMM2dQUExvN1pBYkVDeDROMUVvam12M0t0NmQwbkdhSXlWWEpmWWJUVVFDM1Y4L0ZTVXBVN0JUb0l4OVZWdmlPam5HTHk4RlF2a3RUaHJxWTUvZEh2N3pVUmhiOTc2Q2YwbEovZ3I2L2NwRk9RMUFXVXdodVhrTG9lYjVzNFdtTEZzNldqT3k0bWlKM1J6VllLaWVUSFJ2SE85eDB6dUthanRwSGEzWEZkaE5QNnpySVJJNTRFalUyblVYYUNlYXVnWnZEOUxjdWluOFhFcjExbkFINURCUDAycXhoZk5BejVoMlV2eFNWVmR0aW1QTDBhMVBJTUxGQTgyWUkrQkFOQkhkSUNnZGU5SkxIRFBoTzR6c0llaE1LRmhVQkNoOUhQa3kyRnhTeDJ3YWp3M1UycEsvcFJVZUxDazRUbkhmL25LN3h5ekdpV3dSUFFFZHdsWE5JbUhjVlVPV3gvNWh4WlJCUTZtb3pGYk1HbXR1Mkh5Z3RVV2gzNFYzd1BhS01TNFRsa0hyODFjRjVCWVpxenBFK1pKWnVyLy8zbzJsU0tFMjMxTG1pcGk1K0FqbXZvUVcyWHBocjFNVWJQY1pXUkJFRkkyQXBCM0FhQXFPa0k1MkRqNG43Mko5bCtaMzdydTk1aHk5K1lzY0FxMjZVbExYRlc0S3RUUkRLSjlMNnVmdlIrUUNudER3em5UTFRHUnEwZU5COWt6S0Q4MFlUdXozeHNXK3cxdjlHbDJaMnBZMTZWTCtEV1k9"
|
||||
var decText = "mongodb://qlik-default-mongodb:27017/qliksense?ssl=false"
|
||||
|
||||
func setupTargetFileAndPrivateKey() ([]byte, []byte, string, error) {
|
||||
targetFileString := fmt.Sprintf(targetFileStringTemplate, encText)
|
||||
privKeyBytes := []byte(`-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIJJwIBAAKCAgEApFf3qCQhAr2QLRRZdhLyB8exLjrQiXLr8hwDe0xHSLJX3w7v
|
||||
5z4ujJWrHUulQ2/hvS8uffxMVrp2YqeA4sjy/ku1KqZVQv/WNTdL2v9Z1ewbRnBj
|
||||
DQvmkWDKZ+cP8VPdHGzQ4iM2z6BZ4RQTkdQMKqsVwUsLO9amI2TOny/M696eFRW0
|
||||
pk4+W3QZZRawT0HqJPvKKXKqoO2+62W54rOV8glJi29Do06e4S6CZUl1hBUy0VlL
|
||||
trLlRSOHTois0dF2a9f7+GGgU11MHO6w1k1NesSlfZ0vnrrkW8WFLqewk+Jj+w1x
|
||||
eQnYHOeHjx6zi9f9DC96eSylSB3iJ71NmXcMc0IEZ2LiQIqTL83BLOgMBCsK3FSl
|
||||
GMakQUR2GJ+M0I/selYkRMhid6eOmhlsTNMPbpcTHxZ+ReIzS+5B1X0FZ7RIL+jS
|
||||
L9mFcxmD3dxurrrt/DkLpXcuWdi1s1bpVn3jIQhU0+bgA6hT0k8Kj2f3Q9QnvkHS
|
||||
Eff+XyLvLwQeSsSAcnM+1I7fNSPEo2cq0au6ZtjHcirXmMminAQ6cKW1XrEvJBef
|
||||
HHibtjJjIM4bHH7MKRA5H3km/J4CCwI1VogSTcE05Z6ypAFU2TCrnec9c9VXkRDP
|
||||
94h0GuoG8sdhmQudqvghr/8T7CV+sGRQbdeqrXwanfnGPrjcMVIO/dSOxOUCAwEA
|
||||
AQKCAgA5b2TmJnpC8u0IVCxPz582iNurRHLNFpTPMGsnFCl1hp6fHiFJt7mc+FGt
|
||||
E1rWjqtd6rdc4Gfth40IPXIV0BTcOqk+FpOFrtO2FXU1PDixQqrlmzGCxb324NTc
|
||||
KyyvMpf77yuxXI0zUt8WgmW0eV8nKlOYEhoC96lohTqQ96uuY0bsJ4HS/VVdsN2P
|
||||
Lra/fFHQSw8EHUb0pyIqMoscZ5bn18cUK/Z/hGKSYCbCL0Iavy3bbFHBsBPgbeJD
|
||||
2BBN4953Iiy1Sak2eUy4b9LtkmaZmVAc7mpOFxLn38gD3icgB+bZPoGBw6b7sw71
|
||||
Pc2R+hI9x/oNj0TUR11adhZApBJ9RhBbnSCUt8OUt9U5prNj+9qs8cHJGywtz1da
|
||||
ZT1M6mn2MFSsaOyOlJPzGUzSf4AhI7HiouDpLHtHDqLmc8Vv4rZUqrcFw6kZTCY5
|
||||
564yE8hh/UimOgQr7467/hADHZ0kBsupFEDWRqQ3qTIikHmGhTYZehDrSGL/3BMG
|
||||
rvsFdv0krUHyW4FfHqPN09jfP3LTqd5vVbzRhxcGsoGmP/1kXIDtO8Cp1s/K6Mse
|
||||
tInRCRla8ttZ3CZZ+Vf8HLi/n8XSRfbnMGYi7lVVxnp6kNsTEBgosbdU/r1zbMRJ
|
||||
8mMMHygyugaRLHmOD/8fkWLfyR88cxPH8u9NufTfvJgiFpZboQKCAQEA0uSU6IGZ
|
||||
pXIVZdmDWt4mxpS5T2UYarw3V9z/Isd3kkUU5YrC2XrvPRwmx5Jh9GXl9WENYJAR
|
||||
wH7PaJT0HpBwpxJa1RqHHDSka7DnDcy44oRXyM7e2AmcW8QvcDty/0HPo4oZq4IT
|
||||
m/+ot1R+bIpmJOweGRhVauzxJEUlQyt+kiH/ad8GiOS6LwqFPq42alnUxPQ106wF
|
||||
EZZ2WQdzkyV6tF9aMG18AT1fJsGwNjCLRxJ52t/aEUP5mYwlL2UTT5Acn8KbtrTO
|
||||
fFLAxGuB9LDdT1tGgIpzsXmxAaaeuPvSK4TDFdQyLAUdQJdz0GD9j9ciMPQH3UPe
|
||||
Vjt6qtpfY6QK2wKCAQEAx36Vys5BlQI0TG6qORI0fiOYpLG1GqmdbCNRgBUsMj5T
|
||||
LFe7uSd4qnDvGmns4MdkSSOlpF17bQiWhWKbjKRQpT0U/46zcIT4pWyajXJe+i9H
|
||||
M/DpSRkMq2kGkx6KX2u9L66QBzcxJjtS17amdSpDAfsrvJgOWkxxInzw9n1u6aTe
|
||||
ZjRDXdVX0KjPebEPOaoJToxne21Od3t+47TnDsQPsO1dvvrXX76IfH8cAlD5+0C/
|
||||
b2YvDqWDmh9ICjKShwuDWgi4KjCV5PMHCIxH0FQ2L6mSbwIb9YgGin3wjN3KbWqz
|
||||
dgEu7MeDxEwxZSSg4OstYVLQVgM39G/2ZA4YVJEbPwKCAQAo9FjymhBzb6c2Izp+
|
||||
D/wpvkIKaBCI0cpRlso5P9E5p466UOsr/tKs5GWnhgbdxlgVAebuJKw93KJ8pciO
|
||||
kvA9kbPwBHnOgW6Ytz73kBUrcBX4GixueddSftPTkMfxSB+Bm9UGWHlkZw6lo5P1
|
||||
kh7p9qyVpQMZg7AEoiTtWWn4CQAn2DbVqM17Syi7Fmvc1VsbcG1vkM1fMAAFpAvO
|
||||
vI2Kr6W9F9XoC7oJtb15mI3DnJPrbGNVzQSQzAWAoblRTyQv5kQFBDHBNPTYcCRJ
|
||||
l3sy6P/VAI4dHgvAzVGvjL+w0dRszct8fvXCUGceRWeYYmfyZ8GLN53a0ywsN8Ik
|
||||
gHvXAoIBACee5HEa9bt6bJihgf1DuFk1CKPtB2L8PN+1RAKEMfrolexAoG/tfvGa
|
||||
7GH6l6ks8KX2BnfWeST2h66GHw6Xs8ydjQYUeV7nidqQ70EYbfSSXznZpvt1liaU
|
||||
/VFKx4CcDT7jFIfaVlCZh6KADB9I/XXvRIh4SqF0fSO0XMcXsmeE7watapPAQ2iV
|
||||
nl804yk4tBB9oi/JTcQ9Kr5et2UfW15wRiYf+5ZwaPsQ46cyHfPgsCSXztDB3plF
|
||||
jTE5ShC4IKZJBQqcC6kk+0ifU8P0da6RpxuU96iUE3h9+sB/bCy+/FV7dq5gEbNy
|
||||
znygAbOqAaFKqUXr7bkGY5ELm5lwGFECggEACcyaF9mMqLGghR55ew+cMmdeYdK3
|
||||
meMLi5nrgtbQpVLlz+IV7Vdmrv7lZjeTr4nvU/5miU+p+If14CCFBiSucGq3Kmyp
|
||||
OSM5cNCjDhw8uIDfY2qWCrZ2NSMR3qaAoBAQyQ2ER1IL98TDF/Qui0ZatbPiM4Ns
|
||||
GErhkBZh3MCDSt24yiVKcUB79BxatWB4K7h7y8wqpX4Rj7rpfJMF7wz/I1cgyuCE
|
||||
7XFpRwj7F1B2MmXnvV3KAgAD0EqrJDLeM0vIlDhpOUEaFUkuqmQyeB8qQkWfyXbD
|
||||
jzloS3cNq0MBijB8oixwD2b4dVhBM7z8vQMX6OntN+97luWgO8OIukoYAg==
|
||||
-----END RSA PRIVATE KEY-----
|
||||
`)
|
||||
|
||||
publicKeyBytes := []byte(`-----BEGIN RSA PUBLIC KEY-----
|
||||
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEApFf3qCQhAr2QLRRZdhLy
|
||||
B8exLjrQiXLr8hwDe0xHSLJX3w7v5z4ujJWrHUulQ2/hvS8uffxMVrp2YqeA4sjy
|
||||
/ku1KqZVQv/WNTdL2v9Z1ewbRnBjDQvmkWDKZ+cP8VPdHGzQ4iM2z6BZ4RQTkdQM
|
||||
KqsVwUsLO9amI2TOny/M696eFRW0pk4+W3QZZRawT0HqJPvKKXKqoO2+62W54rOV
|
||||
8glJi29Do06e4S6CZUl1hBUy0VlLtrLlRSOHTois0dF2a9f7+GGgU11MHO6w1k1N
|
||||
esSlfZ0vnrrkW8WFLqewk+Jj+w1xeQnYHOeHjx6zi9f9DC96eSylSB3iJ71NmXcM
|
||||
c0IEZ2LiQIqTL83BLOgMBCsK3FSlGMakQUR2GJ+M0I/selYkRMhid6eOmhlsTNMP
|
||||
bpcTHxZ+ReIzS+5B1X0FZ7RIL+jSL9mFcxmD3dxurrrt/DkLpXcuWdi1s1bpVn3j
|
||||
IQhU0+bgA6hT0k8Kj2f3Q9QnvkHSEff+XyLvLwQeSsSAcnM+1I7fNSPEo2cq0au6
|
||||
ZtjHcirXmMminAQ6cKW1XrEvJBefHHibtjJjIM4bHH7MKRA5H3km/J4CCwI1VogS
|
||||
TcE05Z6ypAFU2TCrnec9c9VXkRDP94h0GuoG8sdhmQudqvghr/8T7CV+sGRQbdeq
|
||||
rXwanfnGPrjcMVIO/dSOxOUCAwEAAQ==
|
||||
-----END RSA PUBLIC KEY-----
|
||||
`)
|
||||
|
||||
targetFile := filepath.Join(testDir, "targetfile.yaml")
|
||||
// tests/config.yaml exists
|
||||
err := ioutil.WriteFile(targetFile, []byte(targetFileString), 0777)
|
||||
if err != nil {
|
||||
log.Printf("Error while creating file: %v", err)
|
||||
return nil, nil, "", err
|
||||
}
|
||||
|
||||
secretKeyPairDir := filepath.Join(testDir, secrets, contexts, qlikDefaultContext, secrets)
|
||||
if err := os.MkdirAll(secretKeyPairDir, 0777); err != nil {
|
||||
err = fmt.Errorf("Not able to create directories")
|
||||
log.Fatal(err)
|
||||
}
|
||||
os.Setenv("QLIKSENSE_KEY_LOCATION", secretKeyPairDir)
|
||||
|
||||
privKeyFile := filepath.Join(secretKeyPairDir, "qliksensePriv")
|
||||
// construct and write priv key file into secretsDir location
|
||||
err = ioutil.WriteFile(privKeyFile, privKeyBytes, 0777)
|
||||
if err != nil {
|
||||
log.Printf("Error while creating file: %v", err)
|
||||
return nil, nil, "", err
|
||||
}
|
||||
pubKeyFile := filepath.Join(secretKeyPairDir, "qliksensePub")
|
||||
api.LogDebugMessage("Test setup - \npub key path: %s\n, priv key path: %s\n", pubKeyFile, privKeyFile)
|
||||
// construct and write pub key file into secretsDir location
|
||||
err = ioutil.WriteFile(pubKeyFile, publicKeyBytes, 0777)
|
||||
if err != nil {
|
||||
log.Printf("Error while creating file: %v", err)
|
||||
return nil, nil, "", err
|
||||
}
|
||||
return publicKeyBytes, privKeyBytes, targetFile, nil
|
||||
}
|
||||
|
||||
func removePrivateKey() {
|
||||
err := os.Remove(filepath.Join(testDir, secrets, contexts, qlikDefaultContext, secrets, "qliksensePriv"))
|
||||
if err != nil {
|
||||
log.Fatalf("Could not delete private key %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func setup() func() {
|
||||
// create tests dir
|
||||
os.RemoveAll(testDir)
|
||||
if err := os.Mkdir(testDir, 0777); err != nil {
|
||||
log.Printf("\nError occurred: %v", err)
|
||||
}
|
||||
config :=
|
||||
`
|
||||
apiVersion: config.qlik.com/v1
|
||||
kind: QliksenseConfig
|
||||
metadata:
|
||||
name: qliksenseConfig
|
||||
spec:
|
||||
contexts:
|
||||
- name: qlik-default
|
||||
crFile: contexts/qlik-default/qlik-default.yaml
|
||||
currentContext: qlik-default
|
||||
`
|
||||
configFile := filepath.Join(testDir, "config.yaml")
|
||||
// tests/config.yaml exists
|
||||
ioutil.WriteFile(configFile, []byte(config), 0777)
|
||||
|
||||
contextYaml :=
|
||||
`
|
||||
apiVersion: qlik.com/v1
|
||||
kind: Qliksense
|
||||
metadata:
|
||||
name: qlik-default
|
||||
spec:
|
||||
profile: docker-desktop
|
||||
rotateKeys: "yes"
|
||||
releaseName: qlik-default
|
||||
`
|
||||
qlikDefaultContext := "qlik-default"
|
||||
// create contexts/qlik-default/ under tests/
|
||||
contexts := "contexts"
|
||||
contextsDir := filepath.Join(testDir, contexts, qlikDefaultContext)
|
||||
if err := os.MkdirAll(contextsDir, 0777); err != nil {
|
||||
err = fmt.Errorf("Not able to create directories")
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
contextFile := filepath.Join(contextsDir, qlikDefaultContext+".yaml")
|
||||
ioutil.WriteFile(contextFile, []byte(contextYaml), 0777)
|
||||
tearDown := func() {
|
||||
os.RemoveAll(testDir)
|
||||
}
|
||||
return tearDown
|
||||
}
|
||||
|
||||
func readCRFile() (*api.QliksenseCR, error) {
|
||||
qlikDefaultContext := "qlik-default"
|
||||
qliksenseCR := &api.QliksenseCR{}
|
||||
contextFileContents, err := ioutil.ReadFile(filepath.Join(testDir, contexts, qlikDefaultContext, qlikDefaultContext+".yaml"))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
err = fmt.Errorf("Not able to read current context info")
|
||||
return nil, err
|
||||
}
|
||||
if err := yaml.Unmarshal(contextFileContents, qliksenseCR); err != nil {
|
||||
err = fmt.Errorf("An error occurred during unmarshalling: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
return qliksenseCR, nil
|
||||
}
|
||||
|
||||
func Test_retrieveCurrentContextInfo(t *testing.T) {
|
||||
|
||||
tearDown := setup()
|
||||
defer tearDown()
|
||||
|
||||
q := &Qliksense{
|
||||
QliksenseHome: testDir,
|
||||
}
|
||||
qConfig := api.NewQConfig(q.QliksenseHome)
|
||||
_, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetUpQliksenseContext(t *testing.T) {
|
||||
type args struct {
|
||||
qlikSenseHome string
|
||||
contextName string
|
||||
isDefaultContext bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid contextname",
|
||||
args: args{
|
||||
qlikSenseHome: testDir,
|
||||
contextName: "testContext1",
|
||||
isDefaultContext: false,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid contextname",
|
||||
args: args{
|
||||
qlikSenseHome: testDir,
|
||||
contextName: "testContext_abcdefgh",
|
||||
isDefaultContext: false,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "empty contextname",
|
||||
args: args{
|
||||
qlikSenseHome: testDir,
|
||||
contextName: "",
|
||||
isDefaultContext: false,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
tearDown := setup()
|
||||
defer tearDown()
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
q := New(tt.args.qlikSenseHome)
|
||||
if err := q.SetUpQliksenseContext(tt.args.contextName); (err != nil) != tt.wantErr {
|
||||
t.Errorf("SetUpQliksenseContext() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetUpQliksenseDefaultContext(t *testing.T) {
|
||||
type args struct {
|
||||
qlikSenseHome string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid case",
|
||||
args: args{
|
||||
qlikSenseHome: testDir,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
tearDown := setup()
|
||||
defer tearDown()
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
q := New(tt.args.qlikSenseHome)
|
||||
if err := q.SetUpQliksenseDefaultContext(); (err != nil) != tt.wantErr {
|
||||
t.Errorf("SetUpQliksenseDefaultContext() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetOtherConfigs(t *testing.T) {
|
||||
type args struct {
|
||||
q *Qliksense
|
||||
args []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid case",
|
||||
args: args{
|
||||
q: &Qliksense{
|
||||
QliksenseHome: testDir,
|
||||
},
|
||||
args: []string{"profile=minikube", "rotateKeys=yes", "storageClassName=efs", "gitops.enabled=yes", "gitops.schedule=30 * * * *", "git.repository=master", "git.username=foo", "git.accesstoken=1234"},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid configs",
|
||||
args: args{
|
||||
q: &Qliksense{
|
||||
QliksenseHome: testDir,
|
||||
},
|
||||
args: []string{"someconfig=somevalue, gitops.schedule=bar", "gitops.enabled=bar", "git.foo=bar", "rotatekeys=bar"},
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "empty configs",
|
||||
args: args{
|
||||
q: &Qliksense{
|
||||
QliksenseHome: testDir,
|
||||
},
|
||||
args: []string{},
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
tearDown := setup()
|
||||
defer tearDown()
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := tt.args.q.SetOtherConfigs(tt.args.args); (err != nil) != tt.wantErr {
|
||||
t.Errorf("SetOtherConfigs() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetConfigs(t *testing.T) {
|
||||
type args struct {
|
||||
q *Qliksense
|
||||
args []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid case",
|
||||
args: args{
|
||||
q: &Qliksense{
|
||||
QliksenseHome: testDir,
|
||||
},
|
||||
args: []string{"qliksense.acceptEULA=\"yes\"", "qliksense.mongoDbUri=\"mongo://mongo:3307\""},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
tearDown := setup()
|
||||
defer tearDown()
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := tt.args.q.SetConfigs(tt.args.args); (err != nil) != tt.wantErr {
|
||||
t.Errorf("SetConfigs() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetImageRegistry(t *testing.T) {
|
||||
getQlikSense := func(tmpQlikSenseHome string) (*Qliksense, error) {
|
||||
if err := ioutil.WriteFile(path.Join(tmpQlikSenseHome, "config.yaml"), []byte(`
|
||||
apiVersion: config.qlik.com/v1
|
||||
kind: QliksenseConfig
|
||||
metadata:
|
||||
name: QliksenseConfigMetadata
|
||||
spec:
|
||||
contexts:
|
||||
- name: qlik-default
|
||||
crFile: contexts/qlik-default/qlik-default.yaml
|
||||
currentContext: qlik-default
|
||||
`), os.ModePerm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defaultContextDir := path.Join(tmpQlikSenseHome, "contexts", "qlik-default")
|
||||
if err := os.MkdirAll(defaultContextDir, os.ModePerm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
version := "foo"
|
||||
manifestsRootDir := fmt.Sprintf("%s/repo/%s", defaultContextDir, version)
|
||||
if err := ioutil.WriteFile(path.Join(defaultContextDir, "qlik-default.yaml"), []byte(fmt.Sprintf(`
|
||||
apiVersion: qlik.com/v1
|
||||
kind: Qliksense
|
||||
metadata:
|
||||
name: qlik-default
|
||||
labels:
|
||||
version: %s
|
||||
spec:
|
||||
profile: docker-desktop
|
||||
manifestsRoot: %s
|
||||
namespace: some-namespace
|
||||
`, version, manifestsRootDir)), os.ModePerm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Qliksense{
|
||||
QliksenseHome: tmpQlikSenseHome,
|
||||
}, nil
|
||||
}
|
||||
testCases := []struct {
|
||||
name string
|
||||
registry string
|
||||
pushUsername string
|
||||
pushPassword string
|
||||
pullUsername string
|
||||
pullPassword string
|
||||
expectSecretsExist bool
|
||||
}{
|
||||
{
|
||||
name: "no auth",
|
||||
registry: "foobar",
|
||||
pushUsername: "",
|
||||
pushPassword: "",
|
||||
pullUsername: "",
|
||||
pullPassword: "",
|
||||
expectSecretsExist: false,
|
||||
},
|
||||
{
|
||||
name: "auth",
|
||||
registry: "foobar",
|
||||
pushUsername: "foo-push",
|
||||
pushPassword: "bar-push",
|
||||
pullUsername: "foo-pull",
|
||||
pullPassword: "bar-pull",
|
||||
expectSecretsExist: true,
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
tmpQlikSenseHome, err := ioutil.TempDir("", "")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpQlikSenseHome)
|
||||
|
||||
q, err := getQlikSense(tmpQlikSenseHome)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if err := q.SetImageRegistry(testCase.registry, testCase.pushUsername, testCase.pushPassword,
|
||||
testCase.pullUsername, testCase.pullPassword); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
qConfig := api.NewQConfig(q.QliksenseHome)
|
||||
if testCase.expectSecretsExist {
|
||||
if pushSecret, err := qConfig.GetPushDockerConfigJsonSecret(); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
} else if pushSecret.Uri != testCase.registry ||
|
||||
pushSecret.Username != testCase.pushUsername || pushSecret.Password != testCase.pushPassword {
|
||||
t.Fatalf("unexpected push secret content: %v", pushSecret)
|
||||
}
|
||||
if pullSecret, err := qConfig.GetPullDockerConfigJsonSecret(); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
} else if pullSecret.Uri != testCase.registry ||
|
||||
pullSecret.Name != "artifactory-docker-secret" ||
|
||||
pullSecret.Username != testCase.pullUsername || pullSecret.Password != testCase.pullPassword {
|
||||
t.Fatalf("unexpected pull secret content: %v", pullSecret)
|
||||
}
|
||||
} else {
|
||||
if _, err := qConfig.GetPushDockerConfigJsonSecret(); err == nil {
|
||||
t.Fatal("unexpected image-registry-push-secret.yaml")
|
||||
} else if _, err := qConfig.GetPullDockerConfigJsonSecret(); err == nil {
|
||||
t.Fatal("unexpected image-registry-pull-secret.yaml")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_PrepareK8sSecret(t *testing.T) {
|
||||
|
||||
type fields struct {
|
||||
QliksenseHome string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want string
|
||||
wantErr bool
|
||||
setup func() (string, func())
|
||||
}{
|
||||
{
|
||||
name: "valid case",
|
||||
fields: fields{
|
||||
QliksenseHome: testDir,
|
||||
},
|
||||
want: fmt.Sprintf(targetFileStringTemplate, base64.StdEncoding.EncodeToString([]byte(decText))),
|
||||
wantErr: false,
|
||||
setup: func() (string, func()) {
|
||||
tearDown := setup()
|
||||
_, _, targetFile, _ := setupTargetFileAndPrivateKey()
|
||||
return targetFile, tearDown
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "private key not supplied should result in decryption error",
|
||||
fields: fields{
|
||||
QliksenseHome: testDir,
|
||||
},
|
||||
want: "",
|
||||
wantErr: true,
|
||||
setup: func() (string, func()) {
|
||||
tearDown := setup()
|
||||
_, _, targetFile, _ := setupTargetFileAndPrivateKey()
|
||||
removePrivateKey()
|
||||
return targetFile, tearDown
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "target file not supplied",
|
||||
fields: fields{
|
||||
QliksenseHome: testDir,
|
||||
},
|
||||
want: "",
|
||||
wantErr: true,
|
||||
setup: func() (string, func()) {
|
||||
tearDown := setup()
|
||||
_, _, _, _ = setupTargetFileAndPrivateKey()
|
||||
removePrivateKey()
|
||||
return "", tearDown
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
targetFile, tearDown := tt.setup()
|
||||
|
||||
q := &Qliksense{
|
||||
QliksenseHome: tt.fields.QliksenseHome,
|
||||
}
|
||||
got, err := q.PrepareK8sSecret(targetFile)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Qliksense.PrepareK8sSecret() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(strings.TrimSpace(got), strings.TrimSpace(tt.want)) {
|
||||
t.Errorf("Qliksense.PrepareK8sSecret() = %v, want %v", got, tt.want)
|
||||
}
|
||||
tearDown()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_ListContextConfigs(t *testing.T) {
|
||||
type fields struct {
|
||||
QliksenseHome string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
wantErr bool
|
||||
setup func() (string, func())
|
||||
}{
|
||||
{
|
||||
name: "valid case",
|
||||
fields: fields{
|
||||
QliksenseHome: testDir,
|
||||
},
|
||||
wantErr: false,
|
||||
setup: func() (string, func()) {
|
||||
tearDown := setup()
|
||||
return "", tearDown
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "config yaml does not exist",
|
||||
fields: fields{
|
||||
QliksenseHome: testDir,
|
||||
},
|
||||
wantErr: true,
|
||||
setup: func() (string, func()) {
|
||||
return "", func() {}
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, tearDown := tt.setup()
|
||||
|
||||
q := &Qliksense{
|
||||
QliksenseHome: tt.fields.QliksenseHome,
|
||||
}
|
||||
if err := q.ListContextConfigs(); (err != nil) != tt.wantErr {
|
||||
t.Errorf("ListContextConfigs() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
tearDown()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_SetSecrets(t *testing.T) {
|
||||
type fields struct {
|
||||
QliksenseHome string
|
||||
}
|
||||
type args struct {
|
||||
args []string
|
||||
isSecretSet bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid secret secrets=false",
|
||||
fields: fields{
|
||||
QliksenseHome: testDir,
|
||||
},
|
||||
args: args{
|
||||
args: []string{"qliksense.mongoDbUri=\"mongodb://qlik-default-mongodb:27017/qliksense?ssl=false\""},
|
||||
isSecretSet: false,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "test1 valid secret secrets=true",
|
||||
fields: fields{
|
||||
QliksenseHome: testDir,
|
||||
},
|
||||
args: args{
|
||||
args: []string{"qliksense.mongoDbUri=\"mongo://mongo:3307\""},
|
||||
isSecretSet: true,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "test2 valid secret secrets=true",
|
||||
fields: fields{
|
||||
QliksenseHome: testDir,
|
||||
},
|
||||
args: args{
|
||||
args: []string{"qliksense.mongoDbUri=\"mongodb://qlik-default-mongodb:27017/qliksense?ssl=false\""},
|
||||
isSecretSet: true,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid secret secrets=false",
|
||||
fields: fields{
|
||||
QliksenseHome: testDir,
|
||||
},
|
||||
args: args{
|
||||
args: []string{},
|
||||
isSecretSet: false,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
tearDown := setup()
|
||||
_, privateKeyBytes, _, err := setupTargetFileAndPrivateKey()
|
||||
if err != nil {
|
||||
t.FailNow()
|
||||
}
|
||||
defer tearDown()
|
||||
|
||||
privKey, err := api.DecodeToPrivateKey(privateKeyBytes)
|
||||
if err != nil {
|
||||
t.FailNow()
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
q := &Qliksense{
|
||||
QliksenseHome: tt.fields.QliksenseHome,
|
||||
}
|
||||
if err := q.SetSecrets(tt.args.args, tt.args.isSecretSet); (err != nil) != tt.wantErr {
|
||||
t.Errorf("SetSecrets() error = %v, wantErr %v", err, tt.wantErr)
|
||||
t.FailNow()
|
||||
}
|
||||
if tt.wantErr || len(tt.args.args) == 0 {
|
||||
return
|
||||
}
|
||||
// VERIFICATION PART BELOW
|
||||
// extract the value for testing
|
||||
testValueArr := strings.SplitN(tt.args.args[0], "=", 2)
|
||||
testValue := strings.ReplaceAll(testValueArr[1], "\"", "")
|
||||
|
||||
qliksenseCR, err := readCRFile()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Not able to read from context file: %v", err)
|
||||
log.Println(err)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
for svcName := range qliksenseCR.Spec.Secrets { // we are sure we only have one service
|
||||
for _, v := range qliksenseCR.Spec.Secrets {
|
||||
for _, item := range v { // we are sure we only have one entry
|
||||
valToBeEncrypted, err := getValueToBeDecodedForSetSecrets(item, qliksenseCR, svcName)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Error occurred while decoding: %v", err)
|
||||
log.Printf("decode error: %v", err)
|
||||
t.FailNow()
|
||||
}
|
||||
decodedValue, err := b64.StdEncoding.DecodeString(valToBeEncrypted)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Error occurred while decoding: %v", err)
|
||||
log.Printf("decode error: %v", err)
|
||||
t.FailNow()
|
||||
}
|
||||
decryptedVal, err := api.Decrypt(decodedValue, privKey)
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Error occurred while testing decryption: %v", err)
|
||||
log.Printf("No Data in Secret: %v", err)
|
||||
t.FailNow()
|
||||
}
|
||||
if string(decryptedVal) != testValue {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func getValueToBeDecodedForSetSecrets(item config.NameValue, qliksenseCR *api.QliksenseCR, svcName string) (string, error) {
|
||||
if item.ValueFrom != nil && item.ValueFrom.SecretKeyRef != nil {
|
||||
// secret=true
|
||||
secretFilePath := filepath.Join(testDir, contexts, qliksenseCR.GetName(), QliksenseSecretsDir, svcName+".yaml")
|
||||
if api.FileExists(secretFilePath) {
|
||||
secretFileContents, err := ioutil.ReadFile(secretFilePath)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("An error occurred during unmarshalling: %v", err)
|
||||
return "", err
|
||||
}
|
||||
k8sSecret, err := api.K8sSecretFromYaml(secretFileContents)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("An error occurred during unmarshalling: %v", err)
|
||||
return "", err
|
||||
}
|
||||
if k8sSecret.Data == nil {
|
||||
err = fmt.Errorf("No Data in Secret: %v", err)
|
||||
return "", err
|
||||
}
|
||||
return string(k8sSecret.Data[item.ValueFrom.SecretKeyRef.Key]), nil
|
||||
}
|
||||
}
|
||||
// secret=false
|
||||
if item.Value != "" {
|
||||
return item.Value, nil
|
||||
}
|
||||
err := fmt.Errorf("Both Value and ValueFrom are empty")
|
||||
return "", err
|
||||
}
|
||||
|
||||
func setupDeleteContext() func() {
|
||||
if err := os.Mkdir(testDir, 0777); err != nil {
|
||||
log.Printf("\nError occurred: %v", err)
|
||||
}
|
||||
config :=
|
||||
`
|
||||
apiVersion: config.qlik.com/v1
|
||||
kind: QliksenseConfig
|
||||
metadata:
|
||||
name: qliksenseConfig
|
||||
spec:
|
||||
contexts:
|
||||
- name: qlik-default
|
||||
crFile: contexts/qlik-default.yaml
|
||||
- name: qlik1
|
||||
crFile: contexts/qlik1.yaml
|
||||
- name: qlik2
|
||||
crFile: contexts/qlik2.yaml
|
||||
currentContext: qlik1
|
||||
`
|
||||
configFile := filepath.Join(testDir, "config.yaml")
|
||||
// tests/config.yaml exists
|
||||
ioutil.WriteFile(configFile, []byte(config), 0777)
|
||||
contextYaml :=
|
||||
`
|
||||
apiVersion: qlik.com/v1
|
||||
kind: Qliksense
|
||||
metadata:
|
||||
name: qlik-default
|
||||
spec:
|
||||
profile: docker-desktop
|
||||
rotateKeys: "yes"
|
||||
releaseName: qlik-default
|
||||
`
|
||||
qlikDefaultContext := "qlik-default"
|
||||
// create contexts/qlik-default/ under tests/
|
||||
contexts := "contexts"
|
||||
contextsDir1 := filepath.Join(testDir, contexts, qlikDefaultContext)
|
||||
if err := os.MkdirAll(contextsDir1, 0777); err != nil {
|
||||
err = fmt.Errorf("Not able to create directories")
|
||||
log.Fatal(err)
|
||||
}
|
||||
contextYaml1 :=
|
||||
`
|
||||
apiVersion: qlik.com/v1
|
||||
kind: Qliksense
|
||||
metadata:
|
||||
name: qlik1
|
||||
spec:
|
||||
profile: docker-desktop
|
||||
rotateKeys: "yes"
|
||||
releaseName: qlik1`
|
||||
|
||||
contextYaml2 :=
|
||||
`
|
||||
apiVersion: qlik.com/v1
|
||||
kind: Qliksense
|
||||
metadata:
|
||||
name: qlik2
|
||||
spec:
|
||||
profile: docker-desktop
|
||||
rotateKeys: "yes"
|
||||
releaseName: qlik2`
|
||||
|
||||
contextsDir := filepath.Join(testDir, contexts, "qlik1")
|
||||
if err := os.MkdirAll(contextsDir, 0777); err != nil {
|
||||
err = fmt.Errorf("Not able to create directories")
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
contextsDir2 := filepath.Join(testDir, contexts, "qlik2")
|
||||
if err := os.MkdirAll(contextsDir2, 0777); err != nil {
|
||||
err = fmt.Errorf("Not able to create directories")
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
contextFile := filepath.Join(contextsDir, "qlik1.yaml")
|
||||
ioutil.WriteFile(contextFile, []byte(contextYaml1), 0777)
|
||||
|
||||
contextFile2 := filepath.Join(contextsDir2, "qlik2.yaml")
|
||||
ioutil.WriteFile(contextFile2, []byte(contextYaml2), 0777)
|
||||
|
||||
contextFile1 := filepath.Join(contextsDir1, "qlik-default.yaml")
|
||||
ioutil.WriteFile(contextFile1, []byte(contextYaml), 0777)
|
||||
|
||||
tearDown := func() {
|
||||
os.RemoveAll(testDir)
|
||||
}
|
||||
return tearDown
|
||||
}
|
||||
|
||||
func TestDeleteContexts(t *testing.T) {
|
||||
type args struct {
|
||||
qlikSenseHome string
|
||||
contextName string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid context",
|
||||
args: args{
|
||||
qlikSenseHome: testDir,
|
||||
contextName: "qlik2",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "default context",
|
||||
args: args{
|
||||
qlikSenseHome: testDir,
|
||||
contextName: "qlik-default",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "non-existent context",
|
||||
args: args{
|
||||
qlikSenseHome: testDir,
|
||||
contextName: "qlik3",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "current context",
|
||||
args: args{
|
||||
qlikSenseHome: testDir,
|
||||
contextName: "qlik1",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
tearDown := setupDeleteContext()
|
||||
defer tearDown()
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
q := New(tt.args.qlikSenseHome)
|
||||
var arg []string
|
||||
arg = append(arg, tt.args.contextName)
|
||||
if err := q.DeleteContextConfig(arg); (err != nil) != tt.wantErr {
|
||||
t.Errorf("DeleteContext() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
type CrdCommandOptions struct {
|
||||
All bool
|
||||
}
|
||||
|
||||
func (q *Qliksense) ViewCrds(opts *CrdCommandOptions) error {
|
||||
//io.WriteString(os.Stdout, q.GetCRDString())
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
qcr, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
fmt.Println("cannot get the current-context cr", err)
|
||||
return err
|
||||
}
|
||||
engineCRD, err := getQliksenseInitCrd(qcr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
customCrd, err := getCustomCrd(qcr)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Println(engineCRD)
|
||||
if customCrd != "" {
|
||||
fmt.Println("---")
|
||||
fmt.Println(customCrd)
|
||||
}
|
||||
|
||||
if opts.All {
|
||||
fmt.Println("---")
|
||||
fmt.Printf("%s", q.GetOperatorCRDString())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *Qliksense) InstallCrds(opts *CrdCommandOptions) error {
|
||||
// install qliksense-init crd
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
qcr, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
fmt.Println("cannot get the current-context cr", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if engineCRD, err := getQliksenseInitCrd(qcr); err != nil {
|
||||
return err
|
||||
} else if err = qapi.KubectlApply(engineCRD, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
if customCrd, err := getCustomCrd(qcr); err != nil {
|
||||
return err
|
||||
} else if customCrd != "" {
|
||||
if err = qapi.KubectlApply(customCrd, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if opts.All { // install opeartor crd
|
||||
if err := qapi.KubectlApply(q.GetOperatorCRDString(), ""); err != nil {
|
||||
fmt.Println("cannot do kubectl apply on opeartor CRD", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getQliksenseInitCrd(qcr *qapi.QliksenseCR) (string, error) {
|
||||
var repoPath string
|
||||
var err error
|
||||
|
||||
if qcr.Spec.GetManifestsRoot() != "" {
|
||||
repoPath = qcr.Spec.GetManifestsRoot()
|
||||
} else {
|
||||
if repoPath, err = DownloadFromGitRepoToTmpDir(defaultConfigRepoGitUrl, "master"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
qInitMsPath := filepath.Join(repoPath, Q_INIT_CRD_PATH)
|
||||
qInitByte, err := ExecuteKustomizeBuild(qInitMsPath)
|
||||
if err != nil {
|
||||
fmt.Println("cannot generate crds for qliksense-init", err)
|
||||
return "", err
|
||||
}
|
||||
return string(qInitByte), nil
|
||||
}
|
||||
|
||||
func getCustomCrd(qcr *qapi.QliksenseCR) (string, error) {
|
||||
crdPath := qcr.GetCustomCrdsPath()
|
||||
if crdPath == "" {
|
||||
return "", nil
|
||||
}
|
||||
qInitByte, err := ExecuteKustomizeBuild(crdPath)
|
||||
if err != nil {
|
||||
fmt.Println("cannot generate custom crds", err)
|
||||
return "", err
|
||||
}
|
||||
return string(qInitByte), nil
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
kapi_config "github.com/qlik-oss/k-apis/pkg/config"
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
func TestGetQliksenseInitCrd(t *testing.T) {
|
||||
someTmpRepoPath, err := DownloadFromGitRepoToTmpDir(defaultConfigRepoGitUrl, "master")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
crdFromContextConfig, err := getQliksenseInitCrd(&qapi.QliksenseCR{
|
||||
KApiCr: kapi_config.KApiCr{
|
||||
Spec: &kapi_config.CRSpec{
|
||||
ManifestsRoot: someTmpRepoPath,
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
crdFromDownloadedConfig, err := getQliksenseInitCrd(&qapi.QliksenseCR{
|
||||
KApiCr: kapi_config.KApiCr{
|
||||
Spec: &kapi_config.CRSpec{
|
||||
ManifestsRoot: "",
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if crdFromContextConfig != crdFromDownloadedConfig {
|
||||
t.Fatalf("expected %v to equal %v, but they didn't", crdFromContextConfig, crdFromDownloadedConfig)
|
||||
}
|
||||
}
|
||||
@@ -1,323 +1,257 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/containers/image/v5/copy"
|
||||
"github.com/containers/image/v5/signature"
|
||||
"github.com/containers/image/v5/transports/alltransports"
|
||||
imageTypes "github.com/containers/image/v5/types"
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
"golang.org/x/net/context"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type imageNameParts struct {
|
||||
name string
|
||||
tag string
|
||||
}
|
||||
|
||||
const (
|
||||
imagesDirName = "images"
|
||||
imageIndexDirName = "index"
|
||||
imageSharedBlobsDirName = "blobs"
|
||||
)
|
||||
|
||||
func (q *Qliksense) PullImages(version, profile string) error {
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
if version != "" {
|
||||
if err := q.FetchQK8s(version); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
qcr, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !qcr.IsRepoExist() {
|
||||
return errors.New("ManifestsRoot not found")
|
||||
}
|
||||
if profile != "" {
|
||||
qcr.Spec.Profile = profile
|
||||
if e := qConfig.WriteCR(qcr); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
return q.PullImagesForCurrentCR()
|
||||
}
|
||||
|
||||
// PullImages ...
|
||||
func (q *Qliksense) PullImagesForCurrentCR() error {
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
qcr, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
version := qcr.GetLabelFromCr("version")
|
||||
profile := qcr.Spec.Profile
|
||||
repoDir := qcr.Spec.ManifestsRoot
|
||||
|
||||
imagesDir, err := setupImagesDir(q.QliksenseHome)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
versionOut, stored, err := q.readOrGenerateVersionOutput(imagesDir, version, repoDir, profile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
images := versionOut.Images
|
||||
if err := q.appendOperatorImages(&images); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, image := range images {
|
||||
if err := pullImage(image, imagesDir); err != nil {
|
||||
fmt.Printf("%v\n", err)
|
||||
return err
|
||||
}
|
||||
fmt.Print("---\n")
|
||||
}
|
||||
|
||||
if version != "" && !stored {
|
||||
if err := q.writeVersionOutput(versionOut, imagesDir, version); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *Qliksense) appendOperatorImages(images *[]string) error {
|
||||
if operatorImages, err := getImageList([]byte(q.GetOperatorControllerString())); err != nil {
|
||||
return err
|
||||
} else {
|
||||
*images = append(*images, operatorImages...)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func pullImage(image, imagesDir string) error {
|
||||
srcRef, err := alltransports.ParseImageName(fmt.Sprintf("docker://%v", image))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nameTag := getImageNameParts(image)
|
||||
targetDir := filepath.Join(imagesDir, imageIndexDirName, nameTag.name, nameTag.tag)
|
||||
if err := os.MkdirAll(targetDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
destRef, err := alltransports.ParseImageName(fmt.Sprintf("oci:%v", targetDir))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
policyContext, err := signature.NewPolicyContext(&signature.Policy{Default: []signature.PolicyRequirement{signature.NewPRInsecureAcceptAnything()}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer policyContext.Destroy()
|
||||
|
||||
fmt.Printf("==> Pulling image from %v\n", srcRef.StringWithinTransport())
|
||||
if _, err := copy.Image(context.Background(), policyContext, destRef, srcRef, ©.Options{
|
||||
ReportWriter: os.Stdout,
|
||||
SourceCtx: &imageTypes.SystemContext{
|
||||
ArchitectureChoice: "amd64",
|
||||
OSChoice: "linux",
|
||||
},
|
||||
DestinationCtx: &imageTypes.SystemContext{
|
||||
OCISharedBlobDirPath: filepath.Join(imagesDir, imageSharedBlobsDirName),
|
||||
},
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TagAndPushImages ...
|
||||
func (q *Qliksense) PushImagesForCurrentCR() error {
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
qcr, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
version := qcr.GetLabelFromCr("version")
|
||||
profile := qcr.Spec.Profile
|
||||
repoDir := qcr.Spec.ManifestsRoot
|
||||
|
||||
dockerConfigJsonSecret, err := qConfig.GetPushDockerConfigJsonSecret()
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
dockerConfigJsonSecret = &qapi.DockerConfigJsonSecret{
|
||||
Uri: qcr.GetImageRegistry(),
|
||||
}
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
imagesDir, err := setupImagesDir(q.QliksenseHome)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
versionOut, stored, err := q.readOrGenerateVersionOutput(imagesDir, version, repoDir, profile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
images := versionOut.Images
|
||||
if err := q.appendOperatorImages(&images); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, image := range images {
|
||||
if err = pushImage(image, imagesDir, dockerConfigJsonSecret); err != nil {
|
||||
fmt.Printf("%v\n", err)
|
||||
return err
|
||||
}
|
||||
fmt.Print("---\n")
|
||||
}
|
||||
|
||||
if version != "" && !stored {
|
||||
if err := q.writeVersionOutput(versionOut, imagesDir, version); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func pushImage(image, imagesDir string, dockerConfigJsonSecret *qapi.DockerConfigJsonSecret) error {
|
||||
imageNameParts := getImageNameParts(image)
|
||||
srcDir := filepath.Join(imagesDir, imageIndexDirName, imageNameParts.name, imageNameParts.tag)
|
||||
if exists, err := directoryExists(srcDir); err != nil {
|
||||
return err
|
||||
} else if !exists {
|
||||
if err := pullImage(image, imagesDir); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
srcRef, err := alltransports.ParseImageName(fmt.Sprintf("oci:%v", srcDir))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newImage := fmt.Sprintf("%v/%v:%v", dockerConfigJsonSecret.Uri, imageNameParts.name, imageNameParts.tag)
|
||||
destRef, err := alltransports.ParseImageName(fmt.Sprintf("docker://%v", newImage))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
policyContext, err := signature.NewPolicyContext(&signature.Policy{Default: []signature.PolicyRequirement{signature.NewPRInsecureAcceptAnything()}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer policyContext.Destroy()
|
||||
|
||||
destinationCtx := &imageTypes.SystemContext{
|
||||
DockerInsecureSkipTLSVerify: imageTypes.OptionalBoolTrue,
|
||||
}
|
||||
if dockerConfigJsonSecret.Username != "" {
|
||||
destinationCtx.DockerAuthConfig = &imageTypes.DockerAuthConfig{
|
||||
Username: dockerConfigJsonSecret.Username,
|
||||
Password: dockerConfigJsonSecret.Password,
|
||||
}
|
||||
}
|
||||
fmt.Printf("==> Pushing image to: %v\n", destRef.StringWithinTransport())
|
||||
if _, err = copy.Image(context.Background(), policyContext, destRef, srcRef, ©.Options{
|
||||
ReportWriter: os.Stdout,
|
||||
SourceCtx: &imageTypes.SystemContext{
|
||||
OCISharedBlobDirPath: filepath.Join(imagesDir, imageSharedBlobsDirName),
|
||||
},
|
||||
DestinationCtx: destinationCtx,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func directoryExists(path string) (exists bool, err error) {
|
||||
if info, err := os.Stat(path); err != nil && os.IsNotExist(err) {
|
||||
exists = false
|
||||
err = nil
|
||||
} else if err != nil && !os.IsNotExist(err) {
|
||||
exists = false
|
||||
} else if err == nil && info.IsDir() {
|
||||
exists = true
|
||||
} else if err == nil && !info.IsDir() {
|
||||
exists = false
|
||||
err = fmt.Errorf("path: %v is occupied by a file instead of a directory", path)
|
||||
}
|
||||
return exists, err
|
||||
}
|
||||
|
||||
func getImageNameParts(image string) imageNameParts {
|
||||
segments := strings.Split(image, "/")
|
||||
nameTag := strings.Split(segments[len(segments)-1], ":")
|
||||
if len(nameTag) < 2 {
|
||||
nameTag = append(nameTag, "latest")
|
||||
}
|
||||
return imageNameParts{
|
||||
name: nameTag[0],
|
||||
tag: nameTag[1],
|
||||
}
|
||||
}
|
||||
|
||||
func setupImagesDir(qliksenseHome string) (string, error) {
|
||||
imagesDir := filepath.Join(qliksenseHome, imagesDirName)
|
||||
|
||||
imageIndexDir := filepath.Join(imagesDir, imageIndexDirName)
|
||||
if err := os.MkdirAll(imageIndexDir, os.ModePerm); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sharedBlobsDir := filepath.Join(imagesDir, imageSharedBlobsDirName)
|
||||
if err := os.MkdirAll(sharedBlobsDir, os.ModePerm); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return imagesDir, nil
|
||||
}
|
||||
|
||||
func (q *Qliksense) readOrGenerateVersionOutput(imagesDir, version, repoDir, profile string) (versionOut *VersionOutput, stored bool, err error) {
|
||||
if version != "" {
|
||||
versionOut, err = q.readVersionOutput(imagesDir, version)
|
||||
if versionOut != nil {
|
||||
stored = true
|
||||
}
|
||||
}
|
||||
if versionOut == nil {
|
||||
if versionOut, err = q.AboutDir(repoDir, profile); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
}
|
||||
return versionOut, stored, nil
|
||||
}
|
||||
|
||||
func (q *Qliksense) readVersionOutput(imagesDir, version string) (*VersionOutput, error) {
|
||||
var versionOut VersionOutput
|
||||
versionFile := filepath.Join(imagesDir, version)
|
||||
if versionOutBytes, err := ioutil.ReadFile(versionFile); err != nil {
|
||||
return nil, err
|
||||
} else if err = yaml.Unmarshal(versionOutBytes, &versionOut); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &versionOut, nil
|
||||
}
|
||||
|
||||
func (q *Qliksense) writeVersionOutput(versionOut *VersionOutput, imagesDir, version string) error {
|
||||
versionFile := filepath.Join(imagesDir, version)
|
||||
if versionOutBytes, err := yaml.Marshal(versionOut); err != nil {
|
||||
return err
|
||||
} else if err = ioutil.WriteFile(versionFile, versionOutBytes, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/docker/cli/cli/command"
|
||||
cliflags "github.com/docker/cli/cli/flags"
|
||||
"github.com/docker/distribution/reference"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/pkg/jsonmessage"
|
||||
"github.com/docker/docker/pkg/term"
|
||||
"github.com/docker/docker/registry"
|
||||
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// Images ...
|
||||
type Images struct {
|
||||
Images []string `yaml:"images"`
|
||||
}
|
||||
|
||||
// PullImages ...
|
||||
func (p *Qliksense) PullImages(args []string) error {
|
||||
var (
|
||||
image string
|
||||
err error
|
||||
yamlVersion string
|
||||
valid bool
|
||||
images Images
|
||||
)
|
||||
println("getting images list...")
|
||||
if yamlVersion, err = p.CallPorter(append([]string{"invoke", "--action", "about"}, args...),
|
||||
func(x string) (out *string) {
|
||||
if strings.HasPrefix(x, "qlikSenseVersion") {
|
||||
valid = true
|
||||
}
|
||||
if strings.HasPrefix(x, "execution") {
|
||||
valid = false
|
||||
}
|
||||
if valid {
|
||||
return &x
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = yaml.Unmarshal([]byte(yamlVersion), &images); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, image = range images.Images {
|
||||
if _, err = p.PullImage(image); err != nil {
|
||||
fmt.Print(err)
|
||||
}
|
||||
println("---")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PullImage ...
|
||||
func (p *Qliksense) PullImage(imageName string) (map[string]string, error) {
|
||||
var (
|
||||
cli *command.DockerCli
|
||||
dockerOutput io.Writer
|
||||
response io.ReadCloser
|
||||
pullOptions types.ImagePullOptions
|
||||
ctx context.Context
|
||||
ref reference.Named
|
||||
repoInfo *registry.RepositoryInfo
|
||||
authConfig types.AuthConfig
|
||||
encodedAuth string
|
||||
termFd uintptr
|
||||
err error
|
||||
)
|
||||
// TODO: Create a real cli config context
|
||||
ctx = context.Background()
|
||||
if cli, err = command.NewDockerCli(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = cli.Initialize(cliflags.NewClientOptions()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if ref, err = reference.ParseNormalizedNamed(imageName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if repoInfo, err = registry.ParseRepositoryInfo(ref); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
authConfig = command.ResolveAuthConfig(ctx, cli, repoInfo.Index)
|
||||
if encodedAuth, err = command.EncodeAuthToBase64(authConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pullOptions = types.ImagePullOptions{
|
||||
RegistryAuth: encodedAuth,
|
||||
}
|
||||
|
||||
if response, err = cli.Client().ImagePull(ctx, imageName, pullOptions); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer response.Close()
|
||||
|
||||
dockerOutput = ioutil.Discard
|
||||
// if b.IsVerbose() {
|
||||
// dockerOutput = b.Out
|
||||
// }
|
||||
dockerOutput = os.Stdout
|
||||
termFd, _ = term.GetFdInfo(dockerOutput)
|
||||
// Setting this to false here because Moby os.Exit(1) all over the place and this fails on WSL (only)
|
||||
// when Term is true.
|
||||
isTerm := false
|
||||
if err = jsonmessage.DisplayJSONMessagesStream(response, dockerOutput, termFd, isTerm, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
inspectData, _, err := cli.Client().ImageInspectWithRaw(ctx, imageName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return inspectData.ContainerConfig.Labels, nil
|
||||
}
|
||||
|
||||
// PullImage ...
|
||||
func (p *Qliksense) TagAndPushImages(registry string) error {
|
||||
var (
|
||||
image string
|
||||
err error
|
||||
yamlVersion string
|
||||
valid bool
|
||||
images Images
|
||||
)
|
||||
|
||||
if yamlVersion, err = p.CallPorter([]string{"invoke", "--action", "about"},
|
||||
func(x string) (out *string) {
|
||||
if strings.HasPrefix(x, "qlikSenseVersion") {
|
||||
valid = true
|
||||
}
|
||||
if strings.HasPrefix(x, "execution") {
|
||||
valid = false
|
||||
}
|
||||
if valid {
|
||||
return &x
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = yaml.Unmarshal([]byte(yamlVersion), &images); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, image = range images.Images {
|
||||
if err = p.TagAndPush(image, registry); err != nil {
|
||||
fmt.Print(err)
|
||||
}
|
||||
println("---")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PullImage ...
|
||||
func (p *Qliksense) TagAndPush(image string, registryName string) error {
|
||||
var (
|
||||
cli *command.DockerCli
|
||||
dockerOutput io.Writer
|
||||
response io.ReadCloser
|
||||
pushOptions types.ImagePushOptions
|
||||
ctx context.Context
|
||||
newName string
|
||||
segments []string
|
||||
imageList []types.ImageSummary
|
||||
imageListOptions types.ImageListOptions
|
||||
filterArgs filters.Args
|
||||
ref reference.Named
|
||||
repoInfo *registry.RepositoryInfo
|
||||
authConfig types.AuthConfig
|
||||
encodedAuth string
|
||||
termFd uintptr
|
||||
err error
|
||||
)
|
||||
// TODO: Create a real cli config context
|
||||
ctx = context.Background()
|
||||
if cli, err = command.NewDockerCli(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = cli.Initialize(cliflags.NewClientOptions()); err != nil {
|
||||
return err
|
||||
}
|
||||
segments = strings.Split(image, "/")
|
||||
if segments[0] == "docker.io" {
|
||||
image = strings.Join(segments[1:], "/")
|
||||
}
|
||||
newName = registryName + "/" + segments[len(segments)-1]
|
||||
|
||||
filterArgs = filters.NewArgs()
|
||||
filterArgs.Add("reference", image)
|
||||
imageListOptions = types.ImageListOptions{
|
||||
Filters: filterArgs,
|
||||
}
|
||||
if imageList, err = cli.Client().ImageList(ctx, imageListOptions); err != nil {
|
||||
return err
|
||||
}
|
||||
if imageList == nil || len(imageList) <= 0 {
|
||||
fmt.Printf("Use `qliksense pull`, to pull %v for an air gap push", newName)
|
||||
return nil
|
||||
}
|
||||
|
||||
if err = cli.Client().ImageTag(ctx, image, newName); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ref, err = reference.ParseNormalizedNamed(image); err != nil {
|
||||
return err
|
||||
}
|
||||
if repoInfo, err = registry.ParseRepositoryInfo(ref); err != nil {
|
||||
return err
|
||||
}
|
||||
authConfig = command.ResolveAuthConfig(ctx, cli, repoInfo.Index)
|
||||
if encodedAuth, err = command.EncodeAuthToBase64(authConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
pushOptions = types.ImagePushOptions{
|
||||
All: true,
|
||||
RegistryAuth: encodedAuth,
|
||||
}
|
||||
|
||||
if response, err = cli.Client().ImagePush(ctx, newName, pushOptions); err != nil {
|
||||
return err
|
||||
}
|
||||
defer response.Close()
|
||||
|
||||
dockerOutput = ioutil.Discard
|
||||
// if b.IsVerbose() {
|
||||
// dockerOutput = b.Out
|
||||
// }
|
||||
dockerOutput = os.Stdout
|
||||
termFd, _ = term.GetFdInfo(dockerOutput)
|
||||
// Setting this to false here because Moby os.Exit(1) all over the place and this fails on WSL (only)
|
||||
// when Term is true.
|
||||
isTerm := false
|
||||
if err = jsonmessage.DisplayJSONMessagesStream(response, dockerOutput, termFd, isTerm, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,556 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
|
||||
"github.com/containers/image/v5/copy"
|
||||
"github.com/containers/image/v5/signature"
|
||||
"github.com/containers/image/v5/transports/alltransports"
|
||||
imageTypes "github.com/containers/image/v5/types"
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/qlik-oss/sense-installer/pkg/api"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
func Test_locateDockerRegistryBinary(t *testing.T) {
|
||||
binary, err := locateDockerRegistryBinary()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
cmd := exec.Command(binary, "--version")
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
t.Logf("output: %v\n", string(out))
|
||||
}
|
||||
|
||||
func Test_getSelfSignedCertAndKey(t *testing.T) {
|
||||
host := "andriy.registry.com"
|
||||
validity := time.Hour * 24 * 365
|
||||
selfSignedCert, key, err := getSelfSignedCertAndKey(host, validity)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
fmt.Print(string(selfSignedCert))
|
||||
fmt.Print(string(key))
|
||||
}
|
||||
|
||||
type clientAuthType byte
|
||||
|
||||
const (
|
||||
clientAuthNotProvided clientAuthType = iota
|
||||
clientAuthProvided
|
||||
clientAuthProvidedButIncorrect
|
||||
)
|
||||
|
||||
func Test_Pull_Push_ImagesForCurrentCR(t *testing.T) {
|
||||
var testCases = []struct {
|
||||
name string
|
||||
registryAuth bool
|
||||
clientAuth clientAuthType
|
||||
expectPushSuccess bool
|
||||
}{
|
||||
{
|
||||
name: "registry does not require auth and we do not provide auth",
|
||||
registryAuth: false,
|
||||
clientAuth: clientAuthNotProvided,
|
||||
expectPushSuccess: true,
|
||||
},
|
||||
{
|
||||
name: "registry does not require auth but we provide auth",
|
||||
registryAuth: false,
|
||||
clientAuth: clientAuthProvided,
|
||||
expectPushSuccess: true,
|
||||
},
|
||||
{
|
||||
name: "registry requires auth but we do not provide auth",
|
||||
registryAuth: true,
|
||||
clientAuth: clientAuthNotProvided,
|
||||
expectPushSuccess: false,
|
||||
},
|
||||
{
|
||||
name: "registry requires auth but we provide wrong auth",
|
||||
registryAuth: true,
|
||||
clientAuth: clientAuthProvidedButIncorrect,
|
||||
expectPushSuccess: false,
|
||||
},
|
||||
{
|
||||
name: "registry requires auth and we provide auth",
|
||||
registryAuth: true,
|
||||
clientAuth: clientAuthProvided,
|
||||
expectPushSuccess: true,
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
registryURI := "127.0.0.1:5555"
|
||||
registry, err := setupRegistryV2At(registryURI, testCase.registryAuth)
|
||||
if registry != nil {
|
||||
defer func() {
|
||||
registry.Close()
|
||||
//fmt.Printf("registry stdout:\n%v\n", registry.stdOutBuffer.String())
|
||||
//fmt.Printf("registry stderr:\n%v\n", registry.stdErrBuffer.String())
|
||||
}()
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error setting up local registry: %v", err)
|
||||
}
|
||||
|
||||
tmpQlikSenseHome, err := ioutil.TempDir("", "tmp-qlik-sense-home-")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error creating tmp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpQlikSenseHome)
|
||||
|
||||
if err := setupQlikSenseHome(t, tmpQlikSenseHome, registry, testCase.clientAuth); err != nil {
|
||||
t.Fatalf("unexpected error setting up qliksense home: %v", err)
|
||||
}
|
||||
q := &Qliksense{
|
||||
QliksenseHome: tmpQlikSenseHome,
|
||||
CrdBox: packr.New("crds", "./crds"),
|
||||
}
|
||||
var versionOut VersionOutput
|
||||
|
||||
if err := q.PullImagesForCurrentCR(); err != nil {
|
||||
t.Fatalf("unexpected pull error: %v", err)
|
||||
} else if versionOutBytes, err := ioutil.ReadFile(path.Join(tmpQlikSenseHome, "images", "foo")); err != nil {
|
||||
t.Fatalf("unexpected error reading version file: %v", err)
|
||||
} else if err = yaml.Unmarshal(versionOutBytes, &versionOut); err != nil {
|
||||
t.Fatalf("unexpected error unmarshalling version file: %v", err)
|
||||
} else if len(versionOut.Images) != 1 || versionOut.Images[0] != "alpine:latest" {
|
||||
t.Fatal(`did not find "alpine:latest"" in the version file`)
|
||||
} else if infos, err := ioutil.ReadDir(path.Join(tmpQlikSenseHome, "images", "index", "alpine", "latest")); err != nil || len(infos) == 0 {
|
||||
t.Fatal("expected images/index/alpine/latest directory to be non-empty")
|
||||
} else if blobInfos, err := ioutil.ReadDir(path.Join(tmpQlikSenseHome, "images", "blobs", "sha256")); err != nil || len(blobInfos) == 0 {
|
||||
t.Fatal("expected images/blobs/sha256 directory to be non-empty")
|
||||
}
|
||||
|
||||
if testCase.expectPushSuccess {
|
||||
if err := q.PushImagesForCurrentCR(); err != nil {
|
||||
t.Fatalf("unexpected push error: %v", err)
|
||||
} else if tmpImagesDir, err := ioutil.TempDir("", "tmp-images-"); err != nil {
|
||||
t.Fatalf("unexpected error creating tmp dir: %v", err)
|
||||
} else if err := testPullImage(fmt.Sprintf("%s/alpine:latest", registryURI), tmpImagesDir, registry); err != nil {
|
||||
t.Fatalf("unexpected error pulling alpine:latest from the local registry: %v", err)
|
||||
} else if infos, err := ioutil.ReadDir(path.Join(tmpImagesDir, "index", "alpine", "latest")); err != nil || len(infos) == 0 {
|
||||
t.Fatal("expected index/alpine/latest directory to be non-empty")
|
||||
} else if blobInfos, err := ioutil.ReadDir(path.Join(tmpImagesDir, "blobs", "sha256")); err != nil || len(blobInfos) == 0 {
|
||||
t.Fatal("expected blobs/sha256 directory to be non-empty")
|
||||
}
|
||||
} else {
|
||||
if err := q.PushImagesForCurrentCR(); err == nil {
|
||||
t.Fatal("unexpected push success")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func setupQlikSenseHome(t *testing.T, tmpQlikSenseHome string, registry *testRegistryV2, clientAuth clientAuthType) error {
|
||||
if err := ioutil.WriteFile(path.Join(tmpQlikSenseHome, "config.yaml"), []byte(`
|
||||
apiVersion: config.qlik.com/v1
|
||||
kind: QliksenseConfig
|
||||
metadata:
|
||||
name: QliksenseConfigMetadata
|
||||
spec:
|
||||
contexts:
|
||||
- name: qlik-default
|
||||
crFile: contexts/qlik-default/qlik-default.yaml
|
||||
currentContext: qlik-default
|
||||
`), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defaultContextDir := path.Join(tmpQlikSenseHome, "contexts", "qlik-default")
|
||||
if err := os.MkdirAll(defaultContextDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
version := "foo"
|
||||
manifestsRootDir := fmt.Sprintf("%s/repo/%s", defaultContextDir, version)
|
||||
if err := ioutil.WriteFile(path.Join(defaultContextDir, "qlik-default.yaml"), []byte(fmt.Sprintf(`
|
||||
apiVersion: qlik.com/v1
|
||||
kind: Qliksense
|
||||
metadata:
|
||||
name: qlik-default
|
||||
labels:
|
||||
version: %s
|
||||
spec:
|
||||
profile: docker-desktop
|
||||
configs:
|
||||
qliksense:
|
||||
- name: imageRegistry
|
||||
value: %s
|
||||
manifestsRoot: %s
|
||||
rotateKeys: "yes"
|
||||
releaseName: qlik-default
|
||||
`, version, registry.url, manifestsRootDir)), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if clientAuth == clientAuthProvided || clientAuth == clientAuthProvidedButIncorrect {
|
||||
if registry.username == "" || clientAuth == clientAuthProvidedButIncorrect {
|
||||
registry.username = "bad"
|
||||
}
|
||||
if registry.password == "" || clientAuth == clientAuthProvidedButIncorrect {
|
||||
registry.password = "worse"
|
||||
}
|
||||
qConfig := api.NewQConfig(tmpQlikSenseHome)
|
||||
if err := qConfig.SetPushDockerConfigJsonSecret(&api.DockerConfigJsonSecret{
|
||||
Uri: registry.url,
|
||||
Username: registry.username,
|
||||
Password: registry.password,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
profileDir := path.Join(manifestsRootDir, "manifests", "docker-desktop")
|
||||
if err := os.MkdirAll(profileDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ioutil.WriteFile(path.Join(profileDir, "kustomization.yaml"), []byte(`
|
||||
resources:
|
||||
- deployment.yaml
|
||||
`), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ioutil.WriteFile(path.Join(profileDir, "deployment.yaml"), []byte(`
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: the-deployment
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: the-container
|
||||
image: alpine:latest
|
||||
`), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
transformersDir := path.Join(manifestsRootDir, "transformers")
|
||||
if err := os.MkdirAll(transformersDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ioutil.WriteFile(path.Join(transformersDir, "qseokversion.yaml"), []byte(`
|
||||
apiVersion: qlik.com/v1
|
||||
kind: SelectivePatch
|
||||
metadata:
|
||||
name: qseokversion
|
||||
enabled: true
|
||||
patches:
|
||||
- target:
|
||||
kind: HelmChart
|
||||
labelSelector: name!=qliksense-init
|
||||
patch: |-
|
||||
chartName: qliksense
|
||||
chartVersion: 1.21.23
|
||||
`), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type testRegistryV2 struct {
|
||||
cmd *exec.Cmd
|
||||
url string
|
||||
dir string
|
||||
username string
|
||||
password string
|
||||
email string
|
||||
stdOutBuffer *bytes.Buffer
|
||||
stdErrBuffer *bytes.Buffer
|
||||
}
|
||||
|
||||
func locateDockerRegistryBinary() (string, error) {
|
||||
if exePath, err := exec.LookPath("docker-registry"); err != nil {
|
||||
if cwd, err := os.Getwd(); err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
return path.Join(cwd, "docker-registry"), nil
|
||||
}
|
||||
} else {
|
||||
return exePath, nil
|
||||
}
|
||||
}
|
||||
|
||||
func setupRegistryV2At(url string, auth bool) (*testRegistryV2, error) {
|
||||
reg, err := newTestRegistryV2At(url, auth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Wait for registry to be ready to serve requests.
|
||||
for i := 0; i != 50; i++ {
|
||||
if err := reg.Ping("http"); err == nil {
|
||||
fmt.Print("registry http ping succeeded\n")
|
||||
break
|
||||
} else {
|
||||
fmt.Printf("registry http ping error: %v\n", err)
|
||||
}
|
||||
if err := reg.Ping("https"); err == nil {
|
||||
fmt.Print("registry https ping succeeded\n")
|
||||
break
|
||||
} else {
|
||||
fmt.Printf("registry https ping error: %v\n", err)
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return reg, errors.New("timeout waiting for test registry to become available")
|
||||
}
|
||||
return reg, nil
|
||||
}
|
||||
|
||||
func newTestRegistryV2At(url string, auth bool) (*testRegistryV2, error) {
|
||||
tmp, err := ioutil.TempDir("", "registry-test-")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
template := `version: 0.1
|
||||
loglevel: info
|
||||
storage:
|
||||
filesystem:
|
||||
rootdirectory: %s
|
||||
delete:
|
||||
enabled: true
|
||||
http:
|
||||
addr: %s
|
||||
%s`
|
||||
var (
|
||||
htpasswd string
|
||||
username string
|
||||
password string
|
||||
email string
|
||||
)
|
||||
var env []string
|
||||
if auth {
|
||||
if certificate, key, err := getSelfSignedCertAndKey("localhost", time.Hour*24); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
certPath := filepath.Join(tmp, "domain.crt")
|
||||
if err := ioutil.WriteFile(certPath, certificate, os.FileMode(0644)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keyPath := filepath.Join(tmp, "domain.key")
|
||||
if err := ioutil.WriteFile(keyPath, key, os.FileMode(0644)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
env = append(env, fmt.Sprintf("REGISTRY_HTTP_TLS_CERTIFICATE=%v", certPath))
|
||||
env = append(env, fmt.Sprintf("REGISTRY_HTTP_TLS_KEY=%v", keyPath))
|
||||
}
|
||||
|
||||
htpasswdPath := filepath.Join(tmp, "htpasswd")
|
||||
userpasswd := "testuser:$2y$05$sBsSqk0OpSD1uTZkHXc4FeJ0Z70wLQdAX/82UiHuQOKbNbBrzs63m"
|
||||
username = "testuser"
|
||||
password = "testpassword"
|
||||
email = "test@test.org"
|
||||
if err := ioutil.WriteFile(htpasswdPath, []byte(userpasswd), os.FileMode(0644)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
htpasswd = fmt.Sprintf(`auth:
|
||||
htpasswd:
|
||||
realm: basic-realm
|
||||
path: %s
|
||||
`, htpasswdPath)
|
||||
}
|
||||
confPath := filepath.Join(tmp, "config.yaml")
|
||||
config, err := os.Create(confPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := fmt.Fprintf(config, template, tmp, url, htpasswd); err != nil {
|
||||
os.RemoveAll(tmp)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dockerRegistryBinaryPath, err := locateDockerRegistryBinary()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cmd := exec.Command(dockerRegistryBinaryPath, "serve", confPath)
|
||||
cmd.Env = env
|
||||
stdOutBuf, stdErrBuf, err := consumeAndLogOutputs(fmt.Sprintf("registry-%s", url), cmd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := cmd.Start(); err != nil {
|
||||
os.RemoveAll(tmp)
|
||||
return nil, err
|
||||
}
|
||||
return &testRegistryV2{
|
||||
cmd: cmd,
|
||||
url: url,
|
||||
dir: tmp,
|
||||
username: username,
|
||||
password: password,
|
||||
email: email,
|
||||
stdOutBuffer: stdOutBuf,
|
||||
stdErrBuffer: stdErrBuf,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *testRegistryV2) Ping(protocol string) error {
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}
|
||||
client := &http.Client{Transport: tr}
|
||||
|
||||
resp, err := client.Get(fmt.Sprintf("%v://%s/v2/", protocol, t.url))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusUnauthorized {
|
||||
return fmt.Errorf("registry ping replied with an unexpected status code %d", resp.StatusCode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *testRegistryV2) Close() {
|
||||
t.cmd.Process.Kill()
|
||||
os.RemoveAll(t.dir)
|
||||
}
|
||||
|
||||
func consumeAndLogOutputStream(id string, f io.ReadCloser) *bytes.Buffer {
|
||||
buff := &bytes.Buffer{}
|
||||
go func() {
|
||||
defer func() {
|
||||
f.Close()
|
||||
fmt.Fprintf(buff, "[%s]: Closed\n", id)
|
||||
}()
|
||||
buf := make([]byte, 1024)
|
||||
for {
|
||||
fmt.Fprintf(buff, "[%s]: waiting\n", id)
|
||||
n, err := f.Read(buf)
|
||||
fmt.Fprintf(buff, "[%s]: got %d,%#v: %s\n", id, n, err, strings.TrimSuffix(string(buf[:n]), "\n"))
|
||||
if n <= 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}()
|
||||
return buff
|
||||
}
|
||||
|
||||
// consumeAndLogOutputs causes all output to stdout and stderr from an *exec.Cmd to be logged to c
|
||||
func consumeAndLogOutputs(id string, cmd *exec.Cmd) (*bytes.Buffer, *bytes.Buffer, error) {
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
stderr, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return consumeAndLogOutputStream(id+" stdout", stdout), consumeAndLogOutputStream(id+" stderr", stderr), nil
|
||||
}
|
||||
|
||||
func getSelfSignedCertAndKey(hostname string, validity time.Duration) (certificate, key []byte, err error) {
|
||||
priv, err := rsa.GenerateKey(rand.Reader, 4096)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
||||
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("ailed to generate serial number: %s", err)
|
||||
}
|
||||
template := x509.Certificate{
|
||||
SerialNumber: serialNumber,
|
||||
Subject: pkix.Name{
|
||||
Organization: []string{"self-signed"},
|
||||
},
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().Add(validity),
|
||||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||
BasicConstraintsValid: true,
|
||||
DNSNames: []string{hostname},
|
||||
}
|
||||
|
||||
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to create certificate: %s", err)
|
||||
}
|
||||
certificate = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
||||
|
||||
privBytes, err := x509.MarshalPKCS8PrivateKey(priv)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("unable to marshal private key: %v", err)
|
||||
}
|
||||
key = pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privBytes})
|
||||
|
||||
return certificate, key, nil
|
||||
}
|
||||
|
||||
func testPullImage(image, imagesDir string, registry *testRegistryV2) error {
|
||||
srcRef, err := alltransports.ParseImageName(fmt.Sprintf("docker://%v", image))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nameTag := getImageNameParts(image)
|
||||
targetDir := filepath.Join(imagesDir, imageIndexDirName, nameTag.name, nameTag.tag)
|
||||
if err := os.MkdirAll(targetDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
destRef, err := alltransports.ParseImageName(fmt.Sprintf("oci:%v", targetDir))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
policyContext, err := signature.NewPolicyContext(&signature.Policy{Default: []signature.PolicyRequirement{signature.NewPRInsecureAcceptAnything()}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer policyContext.Destroy()
|
||||
|
||||
fmt.Printf("==> Test is pulling image from %v\n", srcRef.StringWithinTransport())
|
||||
sourceCtx := &imageTypes.SystemContext{
|
||||
ArchitectureChoice: "amd64",
|
||||
OSChoice: "linux",
|
||||
DockerInsecureSkipTLSVerify: imageTypes.OptionalBoolTrue,
|
||||
}
|
||||
if registry.username != "" {
|
||||
sourceCtx.DockerAuthConfig = &imageTypes.DockerAuthConfig{
|
||||
Username: registry.username,
|
||||
Password: registry.password,
|
||||
}
|
||||
}
|
||||
if _, err := copy.Image(context.Background(), policyContext, destRef, srcRef, ©.Options{
|
||||
ReportWriter: os.Stdout,
|
||||
SourceCtx: sourceCtx,
|
||||
DestinationCtx: &imageTypes.SystemContext{
|
||||
OCISharedBlobDirPath: filepath.Join(imagesDir, imageSharedBlobsDirName),
|
||||
},
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
kapis_git "github.com/qlik-oss/k-apis/pkg/git"
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
const (
|
||||
QLIK_GIT_REPO = "https://github.com/qlik-oss/qliksense-k8s"
|
||||
)
|
||||
|
||||
func (q *Qliksense) FetchQK8s(version string) error {
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
return fetchAndUpdateCR(qConfig, version)
|
||||
}
|
||||
|
||||
func fetchAndUpdateCR(qConfig *qapi.QliksenseConfig, version string) error {
|
||||
qcr, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
fmt.Println("cannot get the current-context cr", err)
|
||||
return err
|
||||
}
|
||||
if qConfig.IsRepoExistForCurrent(version) {
|
||||
return nil
|
||||
}
|
||||
destDir := qConfig.BuildRepoPath(version)
|
||||
fmt.Printf("fetching version [%s] from %s\n", version, QLIK_GIT_REPO)
|
||||
|
||||
if repo, err := kapis_git.CloneRepository(destDir, QLIK_GIT_REPO, nil); err != nil {
|
||||
return err
|
||||
} else if err = kapis_git.Checkout(repo, version, fmt.Sprintf("%v-by-operator-%v", version, uuid.New().String()), nil); err != nil {
|
||||
return err
|
||||
}
|
||||
qcr.Spec.ManifestsRoot = qConfig.BuildCurrentManifestsRoot(version)
|
||||
qcr.AddLabelToCr("version", version)
|
||||
return qConfig.WriteCurrentContextCR(qcr)
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
func TestFetchAndUpdateCR(t *testing.T) {
|
||||
tempHome, _ := ioutil.TempDir("", "")
|
||||
|
||||
q := &Qliksense{
|
||||
QliksenseHome: tempHome,
|
||||
}
|
||||
q.SetUpQliksenseContext("test1")
|
||||
qConfig := qapi.NewQConfig(tempHome)
|
||||
if err := fetchAndUpdateCR(qConfig, "v0.0.2"); err != nil {
|
||||
t.Log(err)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
actualCrFile := filepath.Join(tempHome, "contexts", "test1", "test1.yaml")
|
||||
cr := &qapi.QliksenseCR{}
|
||||
if err := qapi.ReadFromFile(cr, actualCrFile); err != nil {
|
||||
t.Log(err)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if cr.Spec.ManifestsRoot != "contexts/test1/qlik-k8s/v0.0.2" {
|
||||
t.Log("actual path: " + cr.Spec.ManifestsRoot + ", expected path: contexts/test1/qlik-k8s/v0.0.2")
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/qlik-oss/k-apis/pkg/git"
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
type LsRemoteCmdOptions struct {
|
||||
IncludeBranches bool
|
||||
Limit int
|
||||
}
|
||||
|
||||
func (q *Qliksense) GetInstallableVersions(opts *LsRemoteCmdOptions) error {
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
qcr, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var repoPath string
|
||||
if qcr.Spec.GetManifestsRoot() != "" {
|
||||
repoPath = qcr.Spec.GetManifestsRoot()
|
||||
} else {
|
||||
repoPath, err = DownloadFromGitRepoToTmpDir(defaultConfigRepoGitUrl, "master")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
r, err := git.OpenRepository(repoPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
remoteRefsList, err := git.GetRemoteRefs(r, nil,
|
||||
&git.RemoteRefConstraints{
|
||||
Include: true,
|
||||
Sort: true,
|
||||
SortOrder: git.RefSortOrderDescending,
|
||||
},
|
||||
&git.RemoteRefConstraints{
|
||||
Include: opts.IncludeBranches,
|
||||
Sort: true,
|
||||
SortOrder: git.RefSortOrderAscending,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(remoteRefsList) < 1 {
|
||||
return errors.New("cannot find git remote information in the config repository")
|
||||
}
|
||||
|
||||
var originRemoteRefs *git.RemoteRefs
|
||||
for _, remoteRefs := range remoteRefsList {
|
||||
if remoteRefs.Name == "origin" {
|
||||
originRemoteRefs = remoteRefs
|
||||
break
|
||||
}
|
||||
}
|
||||
if originRemoteRefs == nil {
|
||||
return errors.New(`cannot find git remote called "origin" in the config repository`)
|
||||
}
|
||||
|
||||
tags := originRemoteRefs.Tags
|
||||
if len(tags) > opts.Limit {
|
||||
tags = tags[:opts.Limit]
|
||||
}
|
||||
fmt.Print("Versions:\n")
|
||||
for _, tag := range tags {
|
||||
fmt.Printf(" %s\n", tag)
|
||||
}
|
||||
if opts.IncludeBranches {
|
||||
branches := originRemoteRefs.Branches
|
||||
if len(branches) > opts.Limit {
|
||||
branches = branches[:opts.Limit]
|
||||
}
|
||||
fmt.Print("Branches:\n")
|
||||
for _, branch := range branches {
|
||||
fmt.Printf(" %s\n", branch)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,210 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/qlik-oss/k-apis/pkg/config"
|
||||
"sigs.k8s.io/kustomize/api/filesys"
|
||||
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
type InstallCommandOptions struct {
|
||||
StorageClass string
|
||||
MongoDbUri string
|
||||
RotateKeys string
|
||||
}
|
||||
|
||||
func (q *Qliksense) InstallQK8s(version string, opts *InstallCommandOptions, keepPatchFiles bool) error {
|
||||
|
||||
// step1: fetch 1.0.0 # pull down qliksense-k8s@1.0.0
|
||||
// step2: operator view | kubectl apply -f # operator manifest (CRD)
|
||||
// step3: config apply | kubectl apply -f # generates patches (if required) in configuration directory, applies manifest
|
||||
// step4: config view | kubectl apply -f # generates Custom Resource manifest (CR)
|
||||
|
||||
// fetch the version
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
if !keepPatchFiles {
|
||||
defer func() {
|
||||
if err := q.DiscardAllUnstagedChangesFromGitRepo(qConfig); err != nil {
|
||||
fmt.Printf("error removing temporary changes to the config: %v\n", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
qcr, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
fmt.Println("cannot get the current-context cr", err)
|
||||
return err
|
||||
}
|
||||
|
||||
qcr.SetEULA("yes")
|
||||
if opts.MongoDbUri != "" {
|
||||
qcr.Spec.AddToSecrets("qliksense", "mongoDbUri", opts.MongoDbUri, "")
|
||||
}
|
||||
if opts.StorageClass != "" {
|
||||
qcr.Spec.StorageClassName = opts.StorageClass
|
||||
}
|
||||
if opts.RotateKeys != "" {
|
||||
qcr.Spec.RotateKeys = opts.RotateKeys
|
||||
}
|
||||
qConfig.WriteCurrentContextCR(qcr)
|
||||
|
||||
//if the docker pull secret exists on disk, install it in the cluster
|
||||
//if it doesn't exist on disk, remove it in the cluster
|
||||
if err := installOrRemoveImagePullSecret(qConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// check if acceptEULA is yes or not
|
||||
if !qcr.IsEULA() {
|
||||
return errors.New(agreementTempalte + "\n Please do $ qliksense install --acceptEULA=yes\n")
|
||||
}
|
||||
|
||||
//CRD will be installed outside of operator
|
||||
//install operator controller into the namespace
|
||||
fmt.Println("Installing operator controller")
|
||||
operatorControllerString := q.GetOperatorControllerString()
|
||||
if imageRegistry := qcr.GetImageRegistry(); imageRegistry != "" {
|
||||
operatorControllerString, err = kustomizeForImageRegistry(operatorControllerString, pullSecretName,
|
||||
"qlik/qliksense-operator", fmt.Sprintf("%v/qliksense-operator", imageRegistry))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := qapi.KubectlApply(operatorControllerString, ""); err != nil {
|
||||
fmt.Println("cannot do kubectl apply on opeartor controller", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// create patch dependent resoruces
|
||||
fmt.Println("Installing resoruces used kuztomize patch")
|
||||
if err := q.createK8sResoruceBeforePatch(qcr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if qcr.Spec.Git != nil && qcr.Spec.Git.Repository != "" {
|
||||
// fetching and applying manifest will be in the operator controller
|
||||
// get decrypted cr
|
||||
if dcr, err := qConfig.GetDecryptedCr(qcr); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return q.applyCR(dcr)
|
||||
}
|
||||
}
|
||||
if version == "" {
|
||||
version = qcr.GetLabelFromCr("version")
|
||||
}
|
||||
if err := fetchAndUpdateCR(qConfig, version); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
qcr, err = qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
fmt.Println("cannot get the current-context cr", err)
|
||||
return err
|
||||
} else if qcr.Spec.GetManifestsRoot() == "" {
|
||||
return errors.New("cannot get the manifest root. Use qliksense fetch <version> or qliksense set manifestsRoot")
|
||||
}
|
||||
|
||||
// install generated manifests into cluster
|
||||
fmt.Println("Installing generated manifests into cluster")
|
||||
|
||||
if dcr, err := qConfig.GetDecryptedCr(qcr); err != nil {
|
||||
return err
|
||||
} else if err := q.applyConfigToK8s(dcr); err != nil {
|
||||
fmt.Println("cannot do kubectl apply on manifests")
|
||||
return err
|
||||
} else {
|
||||
return q.applyCR(dcr)
|
||||
}
|
||||
}
|
||||
|
||||
func installOrRemoveImagePullSecret(qConfig *qapi.QliksenseConfig) error {
|
||||
if pullDockerConfigJsonSecret, err := qConfig.GetPullDockerConfigJsonSecret(); err == nil {
|
||||
if dockerConfigJsonSecretYaml, err := pullDockerConfigJsonSecret.ToYaml(nil); err != nil {
|
||||
return err
|
||||
} else if err := qapi.KubectlApply(string(dockerConfigJsonSecretYaml), ""); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
deleteDockerConfigJsonSecret := qapi.DockerConfigJsonSecret{
|
||||
Name: pullSecretName,
|
||||
}
|
||||
if deleteDockerConfigJsonSecretYaml, err := deleteDockerConfigJsonSecret.ToYaml(nil); err != nil {
|
||||
return err
|
||||
} else if err := qapi.KubectlDelete(string(deleteDockerConfigJsonSecretYaml), ""); err != nil {
|
||||
qapi.LogDebugMessage("failed deleting %v, error: %v\n", pullSecretName, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func kustomizeForImageRegistry(resources, dockerConfigJsonSecretName, name, newName string) (string, error) {
|
||||
fSys := filesys.MakeFsInMemory()
|
||||
if err := fSys.WriteFile("/resources.yaml", []byte(resources)); err != nil {
|
||||
return "", err
|
||||
} else if err := fSys.WriteFile("/addImagePullSecrets.yaml", []byte(fmt.Sprintf(`
|
||||
apiVersion: builtin
|
||||
kind: PatchTransformer
|
||||
metadata:
|
||||
name: notImportantHere
|
||||
patch: '[{"op": "add", "path": "/spec/template/spec/imagePullSecrets", "value": [{"name": "%v"}]}]'
|
||||
target:
|
||||
name: .*-operator
|
||||
kind: Deployment
|
||||
`, dockerConfigJsonSecretName))); err != nil {
|
||||
return "", err
|
||||
} else if err := fSys.WriteFile("/kustomization.yaml", []byte(fmt.Sprintf(`
|
||||
resources:
|
||||
- resources.yaml
|
||||
transformers:
|
||||
- addImagePullSecrets.yaml
|
||||
images:
|
||||
- name: %s
|
||||
newName: %s
|
||||
`, name, newName))); err != nil {
|
||||
return "", err
|
||||
} else if out, err := executeKustomizeBuildForFileSystem("/", fSys); err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
return string(out), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Qliksense) applyCR(cr *qapi.QliksenseCR) error {
|
||||
// install operator cr into cluster
|
||||
//get the current context cr
|
||||
fmt.Println("Install operator CR into cluster")
|
||||
r, err := cr.GetString()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := qapi.KubectlApply(r, ""); err != nil {
|
||||
fmt.Println("cannot do kubectl apply on operator CR")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *Qliksense) createK8sResoruceBeforePatch(qcr *qapi.QliksenseCR) error {
|
||||
for svc, nvs := range qcr.Spec.Secrets {
|
||||
for _, nv := range nvs {
|
||||
if isK8sSecretNeedToCreate(nv) {
|
||||
fmt.Println(filepath.Join(qcr.GetK8sSecretsFolder(q.QliksenseHome), svc+".yaml"))
|
||||
if secS, err := q.PrepareK8sSecret(filepath.Join(qcr.GetK8sSecretsFolder(q.QliksenseHome), svc+".yaml")); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return qapi.KubectlApply(secS, "")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isK8sSecretNeedToCreate(nv config.NameValue) bool {
|
||||
return nv.ValueFrom != nil
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
func TestCreateK8sResoruceBeforePatch(t *testing.T) {
|
||||
td := setup()
|
||||
sampleCr := `
|
||||
apiVersion: qlik.com/v1
|
||||
kind: Qliksense
|
||||
metadata:
|
||||
name: qlik-test3
|
||||
labels:
|
||||
version: v0.0.2
|
||||
spec:
|
||||
git:
|
||||
repository: https://github.com/ffoysal/qliksense-k8s
|
||||
accessToken: abababababababaab
|
||||
userName: "blblbl"
|
||||
gitOps:
|
||||
enabled: "no"
|
||||
schedule: "*/1 * * * *"
|
||||
watchBranch: pr-branch-db1d26d6
|
||||
image: qlik-docker-oss.bintray.io/qliksense-repo-watcher
|
||||
configs:
|
||||
qliksense:
|
||||
- name: acceptEULA
|
||||
value: "yes"
|
||||
secrets:
|
||||
qliksense:
|
||||
- name: mongoDbUri
|
||||
value: mongodb://qlik-default-mongodb:27017/qliksense?ssl=false
|
||||
profile: docker-desktop
|
||||
rotateKeys: "yes"`
|
||||
|
||||
crFile := filepath.Join(testDir, "install_test.yaml")
|
||||
ioutil.WriteFile(crFile, []byte(sampleCr), 0644)
|
||||
q := New(testDir)
|
||||
file, e := os.Open(crFile)
|
||||
if e != nil {
|
||||
t.Log(e)
|
||||
t.FailNow()
|
||||
}
|
||||
if err := q.LoadCr(file, false); err != nil {
|
||||
t.Log(err)
|
||||
t.FailNow()
|
||||
}
|
||||
qConfig := qapi.NewQConfig(testDir)
|
||||
cr, err := qConfig.GetCR("qlik-test3")
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
t.FailNow()
|
||||
}
|
||||
if err = q.createK8sResoruceBeforePatch(cr); err != nil {
|
||||
t.Log(err)
|
||||
t.FailNow()
|
||||
}
|
||||
td()
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/qlik-oss/sense-installer/pkg/api"
|
||||
|
||||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // don't delete this line ref: https://github.com/kubernetes/client-go/issues/242
|
||||
"sigs.k8s.io/kustomize/api/filesys"
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
"sigs.k8s.io/kustomize/api/krusty"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
)
|
||||
|
||||
//ExecuteKustomizeBuild execute kustomize to the directory and return manifest as byte array
|
||||
func ExecuteKustomizeBuild(directory string) ([]byte, error) {
|
||||
return executeKustomizeBuildForFileSystem(directory, filesys.MakeFsOnDisk())
|
||||
}
|
||||
|
||||
func executeKustomizeBuildForFileSystem(directory string, fSys filesys.FileSystem) ([]byte, error) {
|
||||
log.SetOutput(&nullWriter{})
|
||||
defer func() {
|
||||
log.SetOutput(os.Stderr)
|
||||
}()
|
||||
|
||||
options := &krusty.Options{
|
||||
DoLegacyResourceSort: false,
|
||||
LoadRestrictions: types.LoadRestrictionsNone,
|
||||
DoPrune: false,
|
||||
PluginConfig: konfig.DisabledPluginConfig(),
|
||||
}
|
||||
k := krusty.MakeKustomizer(fSys, options)
|
||||
resMap, err := k.Run(directory)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resMap.AsYaml()
|
||||
}
|
||||
|
||||
func executeKustomizeBuildWithStdoutProgress(path string) (kuzManifest []byte, err error) {
|
||||
result, err := api.ExecuteTaskWithBlinkingStdoutFeedback(func() (interface{}, error) {
|
||||
return ExecuteKustomizeBuild(path)
|
||||
}, "...")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.([]byte), nil
|
||||
}
|
||||
|
||||
//GetYamlsFromMultiDoc filter yaml docs from multiyaml based on kind
|
||||
func GetYamlsFromMultiDoc(multiYaml string, kind string) string {
|
||||
yamlDocs := strings.Split(string(multiYaml), "---")
|
||||
resultDocs := ""
|
||||
for _, doc := range yamlDocs {
|
||||
scanner := bufio.NewScanner(strings.NewReader(doc))
|
||||
for scanner.Scan() {
|
||||
if scanner.Text() == "kind: "+kind {
|
||||
resultDocs = resultDocs + "\n---\n" + doc
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return resultDocs
|
||||
}
|
||||
@@ -1,235 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/Shopify/ejson"
|
||||
"github.com/qlik-oss/k-apis/pkg/config"
|
||||
"github.com/qlik-oss/k-apis/pkg/qust"
|
||||
|
||||
kapis_git "github.com/qlik-oss/k-apis/pkg/git"
|
||||
)
|
||||
|
||||
func Test_ExecuteKustomizeBuild(t *testing.T) {
|
||||
tmpDir, err := ioutil.TempDir("", "")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v\n", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
kustomizationYamlFilePath := path.Join(tmpDir, "kustomization.yaml")
|
||||
kustomizationYaml := `
|
||||
generatorOptions:
|
||||
disableNameSuffixHash: true
|
||||
configMapGenerator:
|
||||
- name: foo-config
|
||||
literals:
|
||||
- foo=bar
|
||||
`
|
||||
err = ioutil.WriteFile(kustomizationYamlFilePath, []byte(kustomizationYaml), os.ModePerm)
|
||||
if err != nil {
|
||||
t.Fatalf("error writing kustomization file to path: %v error: %v\n", kustomizationYamlFilePath, err)
|
||||
}
|
||||
|
||||
result, err := ExecuteKustomizeBuild(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected kustomize error: %v\n", err)
|
||||
}
|
||||
|
||||
expectedK8sYaml := `apiVersion: v1
|
||||
data:
|
||||
foo: bar
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: foo-config
|
||||
`
|
||||
if string(result) != expectedK8sYaml {
|
||||
t.Fatalf("expected k8s yaml: [%v] but got: [%v]\n", expectedK8sYaml, string(result))
|
||||
}
|
||||
}
|
||||
|
||||
func Test_executeKustomizeBuild_onQlikConfig_regenerateKeys(t *testing.T) {
|
||||
tmpDir, err := ioutil.TempDir("", "")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v\n", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
configPath := path.Join(tmpDir, "config")
|
||||
if repo, err := kapis_git.CloneRepository(configPath, defaultConfigRepoGitUrl, nil); err != nil {
|
||||
t.Fatalf("unexpected error: %v\n", err)
|
||||
} else if err := kapis_git.Checkout(repo, "v1.21.23-edge", "", nil); err != nil {
|
||||
t.Fatalf("unexpected error: %v\n", err)
|
||||
}
|
||||
|
||||
cr := &config.CRSpec{
|
||||
ManifestsRoot: configPath,
|
||||
}
|
||||
|
||||
if err := os.Setenv("EJSON_KEYDIR", tmpDir); err != nil {
|
||||
t.Fatalf("unexpected error setting EJSON_KEYDIR environment variable: %v\n", err)
|
||||
}
|
||||
|
||||
if err := os.Unsetenv("EJSON_KEY"); err != nil {
|
||||
t.Fatalf("unexpected error unsetting EJSON_KEY: %v\n", err)
|
||||
}
|
||||
|
||||
generateKeys(cr, "won't-use")
|
||||
|
||||
yamlResources, err := ExecuteKustomizeBuild(path.Join(configPath, "manifests", "base", "resources", "users"))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected kustomize error: %v\n", err)
|
||||
}
|
||||
|
||||
decoder := yaml.NewDecoder(bytes.NewReader(yamlResources))
|
||||
var resource map[string]interface{}
|
||||
keyIdBase64 := ""
|
||||
for {
|
||||
err := decoder.Decode(&resource)
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
t.Fatalf("unexpected yaml decode error: %v\n", err)
|
||||
}
|
||||
break
|
||||
}
|
||||
if resource["kind"].(string) == "Secret" && strings.Contains(resource["metadata"].(map[string]interface{})["name"].(string), "users-secrets-") {
|
||||
keyIdBase64 = resource["data"].(map[string]interface{})["tokenAuthPrivateKeyId"].(string)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
untransformedKeyId := `(( (ds "data").kid ))`
|
||||
if keyIdBase64 == "" {
|
||||
t.Fatalf("expected keyIdBase64 for users secret to be non empty:\n")
|
||||
} else if keyId, err := base64.StdEncoding.DecodeString(keyIdBase64); err != nil {
|
||||
t.Fatalf("unexpected base64 decode error: %v\n", err)
|
||||
} else if string(keyId) == untransformedKeyId {
|
||||
t.Fatalf("unexpected users keyId: %v\n", untransformedKeyId)
|
||||
}
|
||||
}
|
||||
|
||||
func generateKeys(cr *config.CRSpec, defaultKeyDir string) {
|
||||
log.Println("rotating all keys")
|
||||
keyDir := getEjsonKeyDir(defaultKeyDir)
|
||||
if ejsonPublicKey, ejsonPrivateKey, err := ejson.GenerateKeypair(); err != nil {
|
||||
log.Printf("error generating an ejson key pair: %v\n", err)
|
||||
} else if err := qust.GenerateKeys(cr, ejsonPublicKey); err != nil {
|
||||
log.Printf("error generating application keys: %v\n", err)
|
||||
} else if err := os.MkdirAll(keyDir, os.ModePerm); err != nil {
|
||||
log.Printf("error makeing sure private key storage directory: %v exists, error: %v\n", keyDir, err)
|
||||
} else if err := ioutil.WriteFile(path.Join(keyDir, ejsonPublicKey), []byte(ejsonPrivateKey), os.ModePerm); err != nil {
|
||||
log.Printf("error storing ejson private key: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func getEjsonKeyDir(defaultKeyDir string) string {
|
||||
ejsonKeyDir := os.Getenv("EJSON_KEYDIR")
|
||||
if ejsonKeyDir == "" {
|
||||
ejsonKeyDir = defaultKeyDir
|
||||
}
|
||||
return ejsonKeyDir
|
||||
}
|
||||
|
||||
func Test_GetYamlDocKindFromMultiDoc(t *testing.T) {
|
||||
tmpDir, err := ioutil.TempDir("", "")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v\n", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
kustomizationYamlFilePath := path.Join(tmpDir, "kustomization.yaml")
|
||||
testResFileYamlFilePath := path.Join(tmpDir, "test-file.yaml")
|
||||
kustomizationYaml := `
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
resources:
|
||||
- test-file.yaml
|
||||
`
|
||||
testYaml := `
|
||||
apiVersion: v1
|
||||
data:
|
||||
foo: bar
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: foo-config
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
labels:
|
||||
app: qix-sessions
|
||||
chart: qix-sessions-4.0.10
|
||||
heritage: Helm
|
||||
release: qliksense
|
||||
name: qliksense-qix-sessions
|
||||
namespace: default
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1beta1
|
||||
kind: Role
|
||||
metadata:
|
||||
labels:
|
||||
app: chronos
|
||||
chart: chronos-1.5.7
|
||||
heritage: Helm
|
||||
release: qliksense
|
||||
name: qliksense-chronos
|
||||
namespace: default
|
||||
rules:
|
||||
- apiGroups:
|
||||
- ""
|
||||
resources:
|
||||
- endpoints
|
||||
verbs:
|
||||
- get
|
||||
- update
|
||||
`
|
||||
err = ioutil.WriteFile(kustomizationYamlFilePath, []byte(kustomizationYaml), os.ModePerm)
|
||||
if err != nil {
|
||||
t.Fatalf("error writing kustomization file to path: %v error: %v\n", kustomizationYamlFilePath, err)
|
||||
}
|
||||
err = ioutil.WriteFile(testResFileYamlFilePath, []byte(testYaml), os.ModePerm)
|
||||
if err != nil {
|
||||
t.Fatalf("error writing test-file to path: %v error: %v\n", testResFileYamlFilePath, err)
|
||||
}
|
||||
result, err := ExecuteKustomizeBuild(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected kustomize error: %v\n", err)
|
||||
}
|
||||
resultYaml := GetYamlsFromMultiDoc(string(result), "Role")
|
||||
|
||||
expectedK8sYaml := `
|
||||
---
|
||||
|
||||
apiVersion: rbac.authorization.k8s.io/v1beta1
|
||||
kind: Role
|
||||
metadata:
|
||||
labels:
|
||||
app: chronos
|
||||
chart: chronos-1.5.7
|
||||
heritage: Helm
|
||||
release: qliksense
|
||||
name: qliksense-chronos
|
||||
namespace: default
|
||||
rules:
|
||||
- apiGroups:
|
||||
- ""
|
||||
resources:
|
||||
- endpoints
|
||||
verbs:
|
||||
- get
|
||||
- update
|
||||
`
|
||||
if resultYaml != expectedK8sYaml {
|
||||
t.Fatalf("expected k8s yaml: [%v] but got: [%v]\n", expectedK8sYaml, resultYaml)
|
||||
}
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
func (q *Qliksense) LoadCr(reader io.Reader, overwriteExistingContext bool) error {
|
||||
if crBytes, err := ioutil.ReadAll(reader); err != nil {
|
||||
return err
|
||||
} else if crName, err := q.loadCrStringIntoFileSystem(string(crBytes), overwriteExistingContext); err != nil {
|
||||
return err
|
||||
} else {
|
||||
fmt.Println("cr name: [ " + crName + " ] has been loaded")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *Qliksense) IsEulaAcceptedInCrFile(reader io.Reader) (bool, error) {
|
||||
if crBytes, err := ioutil.ReadAll(reader); err != nil {
|
||||
return false, err
|
||||
} else if cr, err := qapi.CreateCRObjectFromString(string(crBytes)); err != nil {
|
||||
return false, err
|
||||
} else {
|
||||
return cr.IsEULA(), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Qliksense) loadCrStringIntoFileSystem(crstr string, overwriteExistingContext bool) (string, error) {
|
||||
cr, err := qapi.CreateCRObjectFromString(crstr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
if qConfig.IsContextExist(cr.GetName()) {
|
||||
if !overwriteExistingContext {
|
||||
return "", errors.New("Context with name: " + cr.GetName() + " already exists. " +
|
||||
"Please delete the existing context first using the delete-context command or specify the --overwrite flag.")
|
||||
}
|
||||
// else if err := os.RemoveAll(qConfig.GetContextPath(cr.GetName())); err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
}
|
||||
if err := qConfig.CreateContextDirs(cr.GetName()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// encrypt the secrets and do base64 then update the CR
|
||||
rsaPublicKey, _, err := qConfig.GetContextEncryptionKeyPair(cr.GetName())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for svc, nvs := range cr.Spec.Secrets {
|
||||
for _, nv := range nvs {
|
||||
if nv.ValueFrom == nil {
|
||||
skv := &qapi.ServiceKeyValue{
|
||||
Key: nv.Name,
|
||||
Value: nv.Value,
|
||||
SvcName: svc,
|
||||
}
|
||||
if err := q.processSecret(skv, rsaPublicKey, cr, false); err != nil {
|
||||
return cr.GetName(), err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// update manifestsRoot in case already exist
|
||||
if existingCr, err := qConfig.GetCR(cr.GetName()); err == nil {
|
||||
// cr exists, so update the manifestsRoot if version exist
|
||||
newV := cr.GetLabelFromCr("version")
|
||||
if strings.HasSuffix(existingCr.Spec.ManifestsRoot, newV) {
|
||||
cr.Spec.ManifestsRoot = existingCr.Spec.ManifestsRoot
|
||||
}
|
||||
}
|
||||
// write to disk
|
||||
if err = qConfig.CreateOrWriteCrAndContext(cr); err != nil {
|
||||
return "", err
|
||||
}
|
||||
qConfig.SetCurrentContextName(cr.GetName())
|
||||
qConfig.Write()
|
||||
|
||||
return cr.GetName(), nil
|
||||
}
|
||||
@@ -1,140 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
func TestLoadCrFile(t *testing.T) {
|
||||
td := setup()
|
||||
setup()
|
||||
sampleCr1 := `
|
||||
apiVersion: qlik.com/v1
|
||||
kind: Qliksense
|
||||
metadata:
|
||||
name: qlik-test
|
||||
labels:
|
||||
version: v0.0.2
|
||||
spec:
|
||||
git:
|
||||
repository: https://github.com/ffoysal/qliksense-k8s
|
||||
accessToken: abababababababaab
|
||||
userName: "blblbl"
|
||||
gitOps:
|
||||
enabled: "no"
|
||||
schedule: "*/1 * * * *"
|
||||
watchBranch: pr-branch-db1d26d6
|
||||
image: qlik-docker-oss.bintray.io/qliksense-repo-watcher
|
||||
configs:
|
||||
qliksense:
|
||||
- name: acceptEULA
|
||||
value: "yes"
|
||||
secrets:
|
||||
qliksense:
|
||||
- name: mongoDbUri
|
||||
value: mongodb://qlik-default-mongodb:27017/qliksense?ssl=false
|
||||
profile: docker-desktop
|
||||
rotateKeys: "yes"`
|
||||
sampleCr2 := `
|
||||
apiVersion: qlik.com/v1
|
||||
kind: Qliksense
|
||||
metadata:
|
||||
name: qlik-test3
|
||||
labels:
|
||||
version: v0.0.2
|
||||
spec:
|
||||
git:
|
||||
repository: https://github.com/ffoysal/qliksense-k8s
|
||||
accessToken: abababababababaab
|
||||
userName: "blblbl"
|
||||
gitOps:
|
||||
enabled: "no"
|
||||
schedule: "*/1 * * * *"
|
||||
watchBranch: pr-branch-db1d26d6
|
||||
image: qlik-docker-oss.bintray.io/qliksense-repo-watcher
|
||||
configs:
|
||||
qliksense:
|
||||
- name: acceptEULA
|
||||
value: "yes"
|
||||
secrets:
|
||||
qliksense:
|
||||
- name: mongoDbUri
|
||||
value: mongodb://qlik-default-mongodb:27017/qliksense?ssl=false
|
||||
profile: docker-desktop
|
||||
rotateKeys: "yes"`
|
||||
|
||||
duplicateCr := `
|
||||
apiVersion: qlik.com/v1
|
||||
kind: Qliksense
|
||||
metadata:
|
||||
name: qlik-default
|
||||
labels:
|
||||
version: v0.0.2
|
||||
spec:
|
||||
git:
|
||||
repository: https://github.com/ffoysal/qliksense-k8s
|
||||
accessToken: abababababababaab
|
||||
userName: "blblbl"`
|
||||
crFile1 := filepath.Join(testDir, "testcr1.yaml")
|
||||
ioutil.WriteFile(crFile1, []byte(sampleCr1), 0644)
|
||||
crFile2 := filepath.Join(testDir, "testcr2.yaml")
|
||||
ioutil.WriteFile(crFile2, []byte(sampleCr2), 0644)
|
||||
|
||||
dupCrFile := filepath.Join(testDir, "dupcr.yaml")
|
||||
ioutil.WriteFile(dupCrFile, []byte(duplicateCr), 0644)
|
||||
|
||||
q := New(testDir)
|
||||
file1, e := os.Open(crFile1)
|
||||
if e != nil {
|
||||
t.Log(e)
|
||||
t.FailNow()
|
||||
}
|
||||
if err := q.LoadCr(file1, false); err != nil {
|
||||
t.Log(err)
|
||||
t.FailNow()
|
||||
}
|
||||
file2, e := os.Open(crFile2)
|
||||
if e != nil {
|
||||
t.Log(e)
|
||||
t.FailNow()
|
||||
}
|
||||
if err := q.LoadCr(file2, false); err != nil {
|
||||
t.Log(err)
|
||||
t.FailNow()
|
||||
}
|
||||
qConfig := qapi.NewQConfig(testDir)
|
||||
cr, err := qConfig.GetCR("qlik-test")
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
t.FailNow()
|
||||
}
|
||||
if cr.GetName() != "qlik-test" {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
cr, err = qConfig.GetCR("qlik-test3")
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
t.FailNow()
|
||||
}
|
||||
if cr.GetName() != "qlik-test3" {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if qConfig.Spec.CurrentContext != "qlik-test3" {
|
||||
t.FailNow()
|
||||
}
|
||||
file, e := os.Open(dupCrFile)
|
||||
if e != nil {
|
||||
t.Log(e)
|
||||
t.FailNow()
|
||||
}
|
||||
if err := q.LoadCr(file, false); err == nil {
|
||||
t.FailNow()
|
||||
}
|
||||
td()
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (q *Qliksense) ViewOperator() error {
|
||||
io.WriteString(os.Stdout, q.GetOperatorCRDString())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *Qliksense) ViewOperatorController() error {
|
||||
io.WriteString(os.Stdout, q.GetOperatorControllerString())
|
||||
return nil
|
||||
}
|
||||
|
||||
// this will return crd,deployment,role, rolebinding,serviceaccount for operator
|
||||
func (q *Qliksense) GetOperatorCRDString() string {
|
||||
result := ""
|
||||
for _, v := range q.getFileList("crd") {
|
||||
result = q.getYamlFromPackrFile(v)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (q *Qliksense) GetOperatorControllerString() string {
|
||||
result := ""
|
||||
for _, v := range q.getFileList("crd-deploy") {
|
||||
result = result + q.getYamlFromPackrFile(v)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (q *Qliksense) getYamlFromPackrFile(packrFile string) string {
|
||||
s, err := q.CrdBox.FindString(packrFile)
|
||||
if err != nil {
|
||||
fmt.Printf("Cannot read file %s", packrFile)
|
||||
}
|
||||
return fmt.Sprintln("#soruce: " + packrFile + "\n\n" + s + "\n---")
|
||||
}
|
||||
func (q *Qliksense) getFileList(resourceType string) []string {
|
||||
var resList []string
|
||||
for _, v := range q.CrdBox.List() {
|
||||
if strings.Contains(v, filepath.Join(resourceType, "")) {
|
||||
resList = append(resList, []string{v}...)
|
||||
}
|
||||
}
|
||||
return resList
|
||||
}
|
||||
59
pkg/qliksense/porter.go
Normal file
59
pkg/qliksense/porter.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// ProcessLine ...
|
||||
type ProcessLine func(string) *string
|
||||
|
||||
// CallPorter ...
|
||||
func (p *Qliksense) CallPorter(args []string, processor ProcessLine) (string, error) {
|
||||
var (
|
||||
outText string
|
||||
cmd *exec.Cmd
|
||||
err error
|
||||
output io.ReadCloser
|
||||
scanner *bufio.Scanner
|
||||
done chan struct{}
|
||||
)
|
||||
cmd = exec.Command(p.PorterExe, args[:]...)
|
||||
if output, err = cmd.StdoutPipe(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
done = make(chan struct{})
|
||||
scanner = bufio.NewScanner(output)
|
||||
go func() {
|
||||
for scanner.Scan() {
|
||||
var text string
|
||||
var newText *string
|
||||
text = scanner.Text()
|
||||
if processor != nil {
|
||||
newText = processor(text)
|
||||
if newText != nil {
|
||||
outText = outText + fmt.Sprintln(*newText)
|
||||
}
|
||||
} else {
|
||||
outText = outText + fmt.Sprintln(text)
|
||||
}
|
||||
}
|
||||
done <- struct{}{}
|
||||
}()
|
||||
if err = cmd.Start(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
<-done
|
||||
if err = cmd.Wait(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err = scanner.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return outText, nil
|
||||
}
|
||||
@@ -1,22 +1,15 @@
|
||||
//go:generate packr2
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
)
|
||||
|
||||
// Qliksense is the logic behind the qliksense client
|
||||
type Qliksense struct {
|
||||
QliksenseHome string
|
||||
CrdBox *packr.Box ``
|
||||
}
|
||||
|
||||
// New qliksense client, initialized with useful defaults.
|
||||
func New(qliksenseHome string) *Qliksense {
|
||||
qliksenseClient := &Qliksense{
|
||||
QliksenseHome: qliksenseHome,
|
||||
CrdBox: packr.New("crds", "./crds"),
|
||||
}
|
||||
|
||||
return qliksenseClient
|
||||
}
|
||||
package qliksense
|
||||
|
||||
// Qliksense is the logic behind the qliksense client
|
||||
type Qliksense struct {
|
||||
PorterExe string
|
||||
QliksenseHome string
|
||||
}
|
||||
|
||||
// New qliksense client, initialized with useful defaults.
|
||||
func New(porterExe, qliksenseHome string) *Qliksense {
|
||||
return &Qliksense{
|
||||
PorterExe: porterExe,
|
||||
QliksenseHome: qliksenseHome,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
kapis_git "github.com/qlik-oss/k-apis/pkg/git"
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
func (q *Qliksense) DiscardAllUnstagedChangesFromGitRepo(qConfig *qapi.QliksenseConfig) error {
|
||||
if qcr, err := qConfig.GetCurrentCR(); err != nil {
|
||||
return err
|
||||
} else if version := qcr.GetLabelFromCr("version"); version == "" {
|
||||
return errors.New("version label is not set in CR")
|
||||
} else if qcr.Spec.ManifestsRoot == qConfig.BuildRepoPath(version) {
|
||||
if repo, err := kapis_git.OpenRepository(qcr.Spec.ManifestsRoot); err != nil {
|
||||
return err
|
||||
} else if err = kapis_git.DiscardAllUnstagedChanges(repo); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
func (q *Qliksense) UninstallQK8s(contextName string) error {
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
if contextName == "" {
|
||||
contextName = qConfig.Spec.CurrentContext
|
||||
} else if !qConfig.IsContextExist(contextName) {
|
||||
return errors.New("context name [ " + contextName + " ] not found")
|
||||
}
|
||||
str, err := q.getCRString(contextName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return qapi.KubectlDelete(str, "")
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package qliksense
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
qapi "github.com/qlik-oss/sense-installer/pkg/api"
|
||||
)
|
||||
|
||||
func (q *Qliksense) UpgradeQK8s(keepPatchFiles bool) error {
|
||||
|
||||
// step1: get CR
|
||||
// step2: run kustomize
|
||||
// step3: run kubectl apply
|
||||
|
||||
// fetch the version
|
||||
qConfig := qapi.NewQConfig(q.QliksenseHome)
|
||||
if !keepPatchFiles {
|
||||
defer func() {
|
||||
if err := q.DiscardAllUnstagedChangesFromGitRepo(qConfig); err != nil {
|
||||
fmt.Printf("error removing temporary changes to the config: %v\n", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
qcr, err := qConfig.GetCurrentCR()
|
||||
if err != nil {
|
||||
fmt.Println("cannot get the current-context cr", err)
|
||||
return err
|
||||
}
|
||||
qcr.Spec.RotateKeys = "no"
|
||||
|
||||
dcr, err := qConfig.GetDecryptedCr(qcr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if dcr.Spec.Git != nil && dcr.Spec.Git.Repository != "" {
|
||||
// fetching and applying manifest will be in the operator controller
|
||||
// get decrypted cr
|
||||
return q.applyCR(dcr)
|
||||
}
|
||||
err = q.applyConfigToK8s(dcr)
|
||||
if err != nil {
|
||||
fmt.Println("cannot do kubectl apply on manifests")
|
||||
return err
|
||||
}
|
||||
return q.applyCR(dcr)
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package pkg
|
||||
|
||||
// These are build-time values, set during an official release
|
||||
var (
|
||||
Commit string
|
||||
Version string
|
||||
CommitDate string
|
||||
)
|
||||
package pkg
|
||||
|
||||
// These are build-time values, set during an official release
|
||||
var (
|
||||
Commit string
|
||||
Version string
|
||||
CommitDate string
|
||||
)
|
||||
|
||||
39
test/Dockerfile.tmpl
Normal file
39
test/Dockerfile.tmpl
Normal file
@@ -0,0 +1,39 @@
|
||||
FROM quay.io/deis/lightweight-docker-go:v0.2.0
|
||||
FROM debian:stretch as base
|
||||
# This is a template Dockerfile for the bundle's invocation image
|
||||
# You can customize it to use different base images, install tools and copy configuration files.
|
||||
#
|
||||
# Porter will use it as a template and append lines to it for the mixins
|
||||
# and to set the CMD appropriately for the CNAB specification.
|
||||
#
|
||||
# Add the following line to porter.yaml to instruct Porter to use this template
|
||||
# dockerfile: Dockerfile.tmpl
|
||||
|
||||
# You can control where the mixin's Dockerfile lines are inserted into this file by moving "# PORTER_MIXINS" line
|
||||
# another location in this file. If you remove that line, the mixins generated content is appended to this file.
|
||||
# PORTER_MIXINS
|
||||
|
||||
COPY --from=0 /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
||||
ARG BUNDLE_DIR
|
||||
ARG HELM_VERSION=v2.16.0
|
||||
RUN apt-get update && \
|
||||
apt-get install -y curl && \
|
||||
curl -o helm.tgz https://get.helm.sh/helm-$HELM_VERSION-linux-amd64.tar.gz && \
|
||||
tar -xzf helm.tgz && \
|
||||
mv linux-amd64/helm /usr/local/bin && \
|
||||
rm helm.tgz
|
||||
RUN helm init --client-only
|
||||
|
||||
# CI job will update this version and make a PR
|
||||
ARG QSEOK_VERSION=1.12.89
|
||||
# This is likely not needed
|
||||
ARG QLIKSENSE_INIT_VERSION=1.1.0
|
||||
|
||||
RUN mkdir -p /tmp/.chartcache
|
||||
RUN helm repo add qlik-edge https://qlik.bintray.com/edge
|
||||
RUN helm fetch qlik-edge/qliksense-init --version $QLIKSENSE_INIT_VERSION --untar -d /tmp/.chartcache/
|
||||
# This is now done by qliksense mixin
|
||||
#RUN helm fetch qlik-edge/qliksense --version $QSEOK_VERSION --untar -d /tmp/.chartcache/
|
||||
|
||||
# Use the BUNDLE_DIR build argument to copy files into the bundle
|
||||
COPY . $BUNDLE_DIR
|
||||
42
test/porter.yaml
Normal file
42
test/porter.yaml
Normal file
@@ -0,0 +1,42 @@
|
||||
name: test-version
|
||||
version: 0.1.0
|
||||
description: "An example Porter bundle demonstrating exec mixin outputs"
|
||||
invocationImage: qlik/test-version:0.1.0
|
||||
tag: qlik/test-version-bundle:0.1.0
|
||||
|
||||
dockerfile: Dockerfile.tmpl
|
||||
|
||||
mixins:
|
||||
- exec
|
||||
- qliksense
|
||||
|
||||
customActions:
|
||||
about:
|
||||
description: "About QLiksense"
|
||||
stateless: true
|
||||
modifies: true
|
||||
|
||||
install:
|
||||
- exec:
|
||||
description: "Get the qliksense version"
|
||||
command: bash
|
||||
arguments:
|
||||
- -c
|
||||
- "echo 1.2.13"
|
||||
outputs:
|
||||
- name: version
|
||||
regex: '.*'
|
||||
uninstall:
|
||||
- exec:
|
||||
description: "Get the qliksense version"
|
||||
command: bash
|
||||
arguments:
|
||||
- -c
|
||||
- "echo 1.2.13"
|
||||
outputs:
|
||||
- name: version
|
||||
regex: '.*'
|
||||
about:
|
||||
- qliksense:
|
||||
description: "Use bundled version by default"
|
||||
version: "bundled"
|
||||
Reference in New Issue
Block a user