mirror of https://github.com/k3s-io/k3s
Browse Source
* test: add make commands and dependencies Signed-off-by: Francisco <francisco.moral@suse.com> * fix: fix issue on logic for using external dbs and dependencies Signed-off-by: Francisco <francisco.moral@suse.com> --------- Signed-off-by: Francisco <francisco.moral@suse.com>pull/7256/head
fmoral2
2 years ago
committed by
GitHub
16 changed files with 771 additions and 352 deletions
@ -0,0 +1,80 @@
|
||||
linters: |
||||
enable: |
||||
- gofmt |
||||
- govet |
||||
- revive |
||||
- gosec |
||||
- megacheck |
||||
- misspell |
||||
- unparam |
||||
- exportloopref |
||||
- nlreturn |
||||
- nestif |
||||
- dupl |
||||
- gci |
||||
- ginkgolinter |
||||
|
||||
linters-settings: |
||||
govet: |
||||
check-shadowing: true |
||||
check-tests: true |
||||
|
||||
nestif: |
||||
min-complexity: 4 |
||||
|
||||
revive: |
||||
confidence: 0.8 |
||||
severity: warning |
||||
ignore-generated-header: true |
||||
rules: |
||||
- name: line-length-limit |
||||
arguments: [100] |
||||
- name: cognitive-complexity |
||||
arguments: [10] |
||||
- name: empty-lines |
||||
- name: empty-block |
||||
- name: bare-return |
||||
- name: blank-imports |
||||
- name: confusing-naming |
||||
- name: confusing-results |
||||
- name: context-as-argument |
||||
- name: duplicated-imports |
||||
- name: early-return |
||||
- name: empty-block |
||||
- name: empty-lines |
||||
- name: error-naming |
||||
- name: error-return |
||||
- name: error-strings |
||||
- name: errorf |
||||
- name: exported |
||||
- name: flag-parameter |
||||
- name: get-return |
||||
- name: if-return |
||||
- name: increment-decrement |
||||
- name: indent-error-flow |
||||
- name: import-shadowing |
||||
- name: modifies-parameter |
||||
- name: modifies-value-receiver |
||||
- name: range |
||||
- name: range-val-in-closure |
||||
- name: range-val-address |
||||
- name: receiver-naming |
||||
- name: string-of-int |
||||
- name: struct-tag |
||||
- name: superfluous-else |
||||
- name: time-naming |
||||
- name: var-declaration |
||||
- name: unconditional-recursion |
||||
- name: unexported-naming |
||||
- name: unexported-return |
||||
- name: unhandled-error |
||||
arguments: ["fmt.Printf", "builder.WriteString"] |
||||
- name: unnecessary-stmt |
||||
- name: unreachable-code |
||||
- name: unused-parameter |
||||
- name: unused-receiver |
||||
|
||||
issues: |
||||
exclude-rules: |
||||
- linters: [typecheck] |
||||
text: "command-line-arguments" |
@ -1,26 +1,27 @@
|
||||
FROM golang:alpine |
||||
|
||||
ARG TERRAFORM_VERSION=0.12.10 |
||||
ENV TERRAFORM_VERSION=$TERRAFORM_VERSION |
||||
|
||||
ARG TF_VERSION=1.4.0 |
||||
ENV TERRAFORM_VERSION $TF_VERSION |
||||
|
||||
RUN apk update && \ |
||||
apk upgrade --update-cache --available && \ |
||||
apk add curl git jq bash openssh unzip gcc g++ make ca-certificates && \ |
||||
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" && \ |
||||
apk add --no-cache curl git jq bash openssh unzip gcc g++ make ca-certificates && \ |
||||
if [ "$(uname -m)" = "aarch64" ]; then \ |
||||
KUBE_ARCH="linux/arm64" && \ |
||||
TF_ARCH="linux_arm64"; \ |
||||
else \ |
||||
KUBE_ARCH="linux/amd64" && \ |
||||
TF_ARCH="linux_amd64"; \ |
||||
fi && \ |
||||
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/${KUBE_ARCH}/kubectl" && \ |
||||
chmod +x ./kubectl && \ |
||||
mv ./kubectl /usr/local/bin && \ |
||||
mkdir tmp && \ |
||||
curl "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip" -o tmp/terraform.zip && \ |
||||
curl "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_${TF_ARCH}.zip" -o tmp/terraform.zip && \ |
||||
unzip tmp/terraform.zip -d /usr/local/bin && \ |
||||
chmod +x /usr/local/bin/terraform && \ |
||||
rm -rf tmp |
||||
|
||||
WORKDIR $GOPATH/src/github.com/k3s-io/k3s |
||||
|
||||
COPY . . |
||||
RUN go get github.com/gruntwork-io/terratest/modules/terraform |
||||
RUN go get -u github.com/onsi/gomega |
||||
RUN go get -u github.com/onsi/ginkgo/v2 |
||||
RUN go get -u golang.org/x/crypto/... |
||||
RUN go get -u github.com/Thatooine/go-test-html-report |
||||
COPY . . |
@ -0,0 +1,75 @@
|
||||
|
||||
##========================= Terraform Tests =========================#
|
||||
include ./config.mk |
||||
|
||||
TAGNAME ?= default
|
||||
tf-up: |
||||
@cd ../.. && docker build . -q -f ./tests/terraform/Dockerfile.build -t k3s-tf-${TAGNAME}
|
||||
|
||||
.PHONY: tf-run |
||||
tf-run: |
||||
@docker run -d --name k3s-tf-test${IMGNAME} -t \
|
||||
-e AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" \
|
||||
-e AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" \
|
||||
-v ${ACCESS_KEY_LOCAL}:/go/src/github.com/k3s-io/k3s/tests/terraform/modules/k3scluster/config/.ssh/aws_key.pem \
|
||||
k3s-tf-${TAGNAME} sh -c 'cd ./tests/terraform ; \
|
||||
if [ -n "${ARGNAME}" ]; then \
|
||||
go test -v -timeout=45m \
|
||||
./${TESTDIR}/... \
|
||||
-"${ARGNAME}"="${ARGVALUE}"; \
|
||||
elif [ -z "${TESTDIR}" ]; then \
|
||||
go test -v -timeout=45m \
|
||||
./createcluster/...; \
|
||||
else \
|
||||
go test -v -timeout=45m \
|
||||
./${TESTDIR}/...; \
|
||||
fi'
|
||||
|
||||
.PHONY: tf-logs |
||||
tf-logs: |
||||
@docker logs -f k3s-tf-test${IMGNAME}
|
||||
|
||||
.PHONY: tf-down |
||||
tf-down: |
||||
@echo "Removing containers and images"
|
||||
@docker stop $$(docker ps -a -q --filter="name=k3s-tf*")
|
||||
@docker rm $$(docker ps -a -q --filter="name=k3s-tf*") ; \
|
||||
docker rmi --force $$(docker images -q --filter="reference=k3s-tf*")
|
||||
|
||||
tf-clean: |
||||
@./scripts/delete_resources.sh
|
||||
|
||||
.PHONY: tf-complete |
||||
tf-complete: tf-clean tf-down tf-remove-state tf-up tf-run |
||||
|
||||
|
||||
#========================= Run terraform tests locally =========================#
|
||||
|
||||
.PHONY: tf-create |
||||
tf-create: |
||||
@go test -timeout=45m -v ./createcluster/...
|
||||
|
||||
.PHONY: tf-upgrade |
||||
tf-upgrade: |
||||
@go test -timeout=45m -v ./upgradecluster/... -${ARGNAME}=${ARGVALUE}
|
||||
|
||||
.PHONY: tf-remove-state |
||||
tf-remove-state: |
||||
@rm -rf ./modules/k3scluster/.terraform
|
||||
@rm -rf ./modules/k3scluster/.terraform.lock.hcl ./modules/k3scluster/terraform.tfstate ./modules/k3scluster/terraform.tfstate.backup
|
||||
|
||||
.PHONY: tf-test-suite |
||||
tf-test-suite: |
||||
@make tf-remove-state && make tf-create ; sleep 5 && \
|
||||
make tf-remove-state && make tf-upgrade ${ARGNAME}=${ARGVALUE}
|
||||
|
||||
.PHONY: tf-test-suite-same-cluster |
||||
tf-test-suite-same-cluster: |
||||
@make tf-create ; sleep 5 && make v ${ARGNAME}=${ARGVALUE}
|
||||
|
||||
|
||||
#========================= TestCode Static Quality Check =========================#
|
||||
.PHONY: vet-lint ## Run locally only inside Tests package
|
||||
vet-lint: |
||||
@echo "Running go vet and lint"
|
||||
@go vet ./${TESTDIR} && golangci-lint run --tests
|
@ -0,0 +1,13 @@
|
||||
SHELL := /bin/bash
|
||||
|
||||
LOCAL_TFVARS_PATH := modules/k3scluster/config/local.tfvars
|
||||
|
||||
ifeq ($(wildcard ${LOCAL_TFVARS_PATH}),) |
||||
RESOURCE_NAME :=
|
||||
else |
||||
export RESOURCE_NAME := $(shell sed -n 's/resource_name *= *"\([^"]*\)"/\1/p' ${LOCAL_TFVARS_PATH})
|
||||
endif |
||||
|
||||
export ACCESS_KEY_LOCAL |
||||
export AWS_ACCESS_KEY_ID |
||||
export AWS_SECRET_ACCESS_KEY |
@ -0,0 +1,101 @@
|
||||
#!/bin/bash |
||||
|
||||
#Get resource name from tfvarslocal && change name to make more sense in this context |
||||
RESOURCE_NAME=$(grep resource_name <modules/k3scluster/config/local.tfvars | cut -d= -f2 | tr -d ' "') |
||||
NAME_PREFIX="$RESOURCE_NAME" |
||||
|
||||
#Terminate the instances |
||||
echo "Terminating resources for $NAME_PREFIX if still up and running" |
||||
# shellcheck disable=SC2046 |
||||
aws ec2 terminate-instances --instance-ids $(aws ec2 describe-instances \ |
||||
--filters "Name=tag:Name,Values=${NAME_PREFIX}*" \ |
||||
"Name=instance-state-name,Values=running" --query \ |
||||
'Reservations[].Instances[].InstanceId' --output text) > /dev/null 2>&1 |
||||
|
||||
|
||||
#Search for DB instances and delete them |
||||
INSTANCES=$(aws rds describe-db-instances --query "DBInstances[?starts_with(DBInstanceIdentifier, |
||||
'${NAME_PREFIX}')].DBInstanceIdentifier" --output text 2> /dev/null) |
||||
for instance in $INSTANCES; do |
||||
aws rds delete-db-instance --db-instance-identifier "$instance" --skip-final-snapshot > /dev/null 2>&1 |
||||
done |
||||
|
||||
|
||||
#Search for DB clusters and delete them |
||||
CLUSTERS=$(aws rds describe-db-clusters --query "DBClusters[?starts_with(DBClusterIdentifier, |
||||
'${NAME_PREFIX}')].DBClusterIdentifier" --output text 2> /dev/null) |
||||
for cluster in $CLUSTERS; do |
||||
aws rds delete-db-cluster --db-cluster-identifier "$cluster" --skip-final-snapshot > /dev/null 2>&1 |
||||
aws rds wait db-cluster-deleted --db-cluster-identifier "$cluster" |
||||
done |
||||
|
||||
|
||||
#Get the list of load balancer ARNs |
||||
LB_ARN_LIST=$(aws elbv2 describe-load-balancers \ |
||||
--query "LoadBalancers[?starts_with(LoadBalancerName, '${NAME_PREFIX}') && Type=='network'].LoadBalancerArn" \ |
||||
--output text) |
||||
|
||||
|
||||
#Loop through the load balancer ARNs and delete the load balancers |
||||
for LB_ARN in $LB_ARN_LIST; do |
||||
echo "Deleting load balancer $LB_ARN" |
||||
aws elbv2 delete-load-balancer --load-balancer-arn "$LB_ARN" |
||||
done |
||||
|
||||
#Get the list of target group ARNs |
||||
TG_ARN_LIST=$(aws elbv2 describe-target-groups \ |
||||
--query "TargetGroups[?starts_with(TargetGroupName, '${NAME_PREFIX}') && Protocol=='TCP'].TargetGroupArn" \ |
||||
--output text) |
||||
|
||||
|
||||
#Loop through the target group ARNs and delete the target groups |
||||
for TG_ARN in $TG_ARN_LIST; do |
||||
echo "Deleting target group $TG_ARN" |
||||
aws elbv2 delete-target-group --target-group-arn "$TG_ARN" |
||||
done |
||||
|
||||
|
||||
#Get the ID and recordName with lower case of the hosted zone that contains the Route 53 record sets |
||||
NAME_PREFIX_LOWER=$(echo "$NAME_PREFIX" | tr '[:upper:]' '[:lower:]') |
||||
R53_ZONE_ID=$(aws route53 list-hosted-zones-by-name --dns-name "${NAME_PREFIX}." \ |
||||
--query "HostedZones[0].Id" --output text) |
||||
R53_RECORD=$(aws route53 list-resource-record-sets \ |
||||
--hosted-zone-id "${R53_ZONE_ID}" \ |
||||
--query "ResourceRecordSets[?starts_with(Name, '${NAME_PREFIX_LOWER}.') && Type == 'CNAME'].Name" \ |
||||
--output text) |
||||
|
||||
|
||||
#Get ResourceRecord Value |
||||
RECORD_VALUE=$(aws route53 list-resource-record-sets \ |
||||
--hosted-zone-id "${R53_ZONE_ID}" \ |
||||
--query "ResourceRecordSets[?starts_with(Name, '${NAME_PREFIX_LOWER}.') \ |
||||
&& Type == 'CNAME'].ResourceRecords[0].Value" --output text) |
||||
|
||||
|
||||
#Delete Route53 record |
||||
if [[ "$R53_RECORD" == "${NAME_PREFIX_LOWER}."* ]]; then |
||||
echo "Deleting Route53 record ${R53_RECORD}" |
||||
CHANGE_STATUS=$(aws route53 change-resource-record-sets --hosted-zone-id "${R53_ZONE_ID}" \ |
||||
--change-batch '{"Changes": [ |
||||
{ |
||||
"Action": "DELETE", |
||||
"ResourceRecordSet": { |
||||
"Name": "'"${R53_RECORD}"'", |
||||
"Type": "CNAME", |
||||
"TTL": 300, |
||||
"ResourceRecords": [ |
||||
{ |
||||
"Value": "'"${RECORD_VALUE}"'" |
||||
} |
||||
] |
||||
} |
||||
} |
||||
] |
||||
}') |
||||
STATUS_ID=$(echo "$CHANGE_STATUS" | jq -r '.ChangeInfo.Id') |
||||
#Get status from the change |
||||
aws route53 wait resource-record-sets-changed --id "$STATUS_ID" |
||||
echo "Successfully deleted Route53 record ${R53_RECORD}: status: ${STATUS_ID}" |
||||
else |
||||
echo "No Route53 record found" |
||||
fi |
Loading…
Reference in new issue