Skip to content

Commit 08cf42d

Browse files
authored
Merge pull request #20 from gruntwork-io/yori-fix-ingress
Fix Ingress bugs
2 parents 2dac325 + 1c66254 commit 08cf42d

File tree

7 files changed

+77
-65
lines changed

7 files changed

+77
-65
lines changed

.circleci/config.yml

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
defaults: &defaults
2-
machine: true
2+
machine:
3+
enabled: true
4+
image: "ubuntu-1604:201903-01"
35
environment:
46
GRUNTWORK_INSTALLER_VERSION: v0.0.21
57
TERRATEST_LOG_PARSER_VERSION: v0.13.13
68
HELM_VERSION: v2.12.2
7-
MODULE_CI_VERSION: v0.13.3
9+
MODULE_CI_VERSION: v0.13.12
810
TERRAFORM_VERSION: NONE
911
TERRAGRUNT_VERSION: NONE
1012
PACKER_VERSION: NONE
1113
GOLANG_VERSION: 1.11.2
12-
K8S_VERSION: v1.10.0 # Same as EKS
1314
KUBECONFIG: /home/circleci/.kube/config
14-
MINIKUBE_VERSION: v0.28.2 # See https://github.com/kubernetes/minikube/issues/2704
15-
MINIKUBE_WANTUPDATENOTIFICATION: "false"
16-
MINIKUBE_WANTREPORTERRORPROMPT: "false"
17-
MINIKUBE_HOME: /home/circleci
18-
CHANGE_MINIKUBE_NONE_USER: "true"
1915

2016

2117
install_helm_client: &install_helm_client
@@ -28,55 +24,12 @@ install_helm_client: &install_helm_client
2824
sudo mv linux-amd64/helm /usr/local/bin/
2925
3026
31-
deploy_tiller: &deploy_tiller
32-
name: deploy tiller
33-
command: |
34-
# Grant the default service account cluster admin rights so helm works
35-
kubectl create clusterrolebinding default-service-account-cluster-admin-binding --clusterrole cluster-admin --serviceaccount kube-system:default
36-
# Deploy Tiller
37-
helm init --wait
38-
39-
40-
# Install and setup minikube
41-
# https://github.com/kubernetes/minikube#linux-continuous-integration-without-vm-support
42-
setup_minikube: &setup_minikube
43-
name: install kubectl and minikube
44-
command: |
45-
# install kubectl
46-
curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/${K8S_VERSION}/bin/linux/amd64/kubectl
47-
chmod +x kubectl
48-
sudo mv kubectl /usr/local/bin/
49-
mkdir -p ${HOME}/.kube
50-
touch ${HOME}/.kube/config
51-
52-
# Install minikube
53-
curl -Lo minikube https://github.com/kubernetes/minikube/releases/download/${MINIKUBE_VERSION}/minikube-linux-amd64
54-
chmod +x minikube
55-
sudo mv minikube /usr/local/bin/
56-
57-
# start minikube and wait for it
58-
# Ignore warnings on minikube start command, as it will log a warning due to using deprecated localkube
59-
# bootstrapper. However, the localkube bootstrapper is necessary to run on CircleCI which doesn't have
60-
# systemd.
61-
sudo -E minikube start --vm-driver=none --kubernetes-version=${K8S_VERSION} --bootstrapper=localkube --extra-config=apiserver.Authorization.Mode=RBAC
62-
# this for loop waits until kubectl can access the api server that Minikube has created
63-
$(
64-
for i in {1..150}; do # timeout for 5 minutes
65-
kubectl get po &> /dev/null
66-
if [ $? -ne 1 ]; then
67-
break
68-
fi
69-
sleep 2
70-
done
71-
true
72-
)
73-
74-
7527
install_gruntwork_utils: &install_gruntwork_utils
7628
name: install gruntwork utils
7729
command: |
7830
curl -Ls https://raw.githubusercontent.com/gruntwork-io/gruntwork-installer/master/bootstrap-gruntwork-installer.sh | bash /dev/stdin --version "${GRUNTWORK_INSTALLER_VERSION}"
7931
gruntwork-install --module-name "gruntwork-module-circleci-helpers" --repo "https://github.com/gruntwork-io/module-ci" --tag "${MODULE_CI_VERSION}"
32+
gruntwork-install --module-name "kubernetes-circleci-helpers" --repo "https://github.com/gruntwork-io/module-ci" --tag "${MODULE_CI_VERSION}"
8033
gruntwork-install --binary-name "terratest_log_parser" --repo "https://github.com/gruntwork-io/terratest" --tag "${TERRATEST_LOG_PARSER_VERSION}"
8134
configure-environment-for-gruntwork-module \
8235
--circle-ci-2-machine-executor \
@@ -131,13 +84,14 @@ jobs:
13184
<<: *install_gruntwork_utils
13285

13386
- run:
134-
<<: *setup_minikube
87+
command: setup-minikube
13588

13689
- run:
13790
<<: *install_helm_client
13891

13992
- run:
140-
<<: *deploy_tiller
93+
name: deploy tiller
94+
command: helm init --wait
14195

14296
- run:
14397
name: run tests

charts/k8s-service/templates/ingress.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ We declare some variables defined on the Values. These are reused in `with` and
1010
{{- $fullName := include "k8s-service.fullname" . -}}
1111
{{- $ingressPath := .Values.ingress.path -}}
1212
{{- $servicePort := .Values.ingress.servicePort -}}
13-
apiVersion: extensions/v1
13+
apiVersion: extensions/v1beta1
1414
kind: Ingress
1515
metadata:
1616
name: {{ $fullName }}
@@ -22,7 +22,7 @@ metadata:
2222
helm.sh/chart: {{ include "k8s-service.chart" . }}
2323
app.kubernetes.io/instance: {{ .Release.Name }}
2424
app.kubernetes.io/managed-by: {{ .Release.Service }}
25-
{{- if .Values.service.annotations }}
25+
{{- if .Values.ingress.annotations }}
2626
{{- with .Values.ingress.annotations }}
2727
annotations:
2828
{{ toYaml . | indent 4 }}
@@ -36,6 +36,7 @@ spec:
3636
{{- end }}
3737
{{- end }}
3838
rules:
39+
{{- if .Values.ingress.hosts }}
3940
{{- range .Values.ingress.hosts }}
4041
- host: {{ . | quote }}
4142
http:
@@ -45,4 +46,13 @@ spec:
4546
serviceName: {{ $fullName }}
4647
servicePort: {{ $servicePort }}
4748
{{- end }}
49+
{{- else }}
50+
- http:
51+
paths:
52+
- path: {{ $ingressPath }}
53+
backend:
54+
serviceName: {{ $fullName }}
55+
servicePort: {{ $servicePort }}
56+
57+
{{- end }}
4858
{{- end }}

charts/k8s-service/values.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ service:
172172
# - tls (list[map]) : Sets up TLS termination on the ingress rule. Each item is a separate TLS
173173
# rule that maps to one or more hosts specified in this ingress rule. This
174174
# is injected directly in to the resource yaml.
175-
# - hosts (list[string]) (required) : Sets up the host routes for the ingress resource. There will be a routing
176-
# rule for each host defined in this list.
175+
# - hosts (list[string]) : Sets up the host routes for the ingress resource. There will be a routing
176+
# rule for each host defined in this list. If empty, will match all hosts.
177177
# - path (string) (required) : The url path to match to route to the Service.
178178
# - servicePort (int|string) (required) : The port (as a number) or the name of the port on the Service to route to.
179179
#

test/Gopkg.lock

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/Gopkg.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
[[constraint]]
2929
name = "github.com/gruntwork-io/terratest"
30-
version = "0.13.28"
30+
version = "0.14.5"
3131

3232
[prune]
3333
go-tests = true

test/k8s_service_example_test_helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func verifyServiceAvailable(
104104
// Now hit the service endpoint to verify it is accessible
105105
// Refresh service object in memory
106106
service = *k8s.GetService(t, kubectlOptions, service.Name)
107-
serviceEndpoint := k8s.GetServiceEndpoint(t, &service, 80)
107+
serviceEndpoint := k8s.GetServiceEndpoint(t, kubectlOptions, &service, 80)
108108
http_helper.HttpGetWithRetryWithCustomValidation(
109109
t,
110110
fmt.Sprintf("http://%s", serviceEndpoint),

test/k8s_service_nginx_example_test.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@ import (
1212
"testing"
1313

1414
"github.com/gruntwork-io/terratest/modules/helm"
15+
"github.com/gruntwork-io/terratest/modules/http-helper"
1516
"github.com/gruntwork-io/terratest/modules/k8s"
1617
"github.com/gruntwork-io/terratest/modules/random"
1718
"github.com/stretchr/testify/require"
19+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1820
)
1921

2022
// Test that:
2123
//
2224
// 1. We can deploy the example
2325
// 2. The deployment succeeds without errors
2426
// 3. We can open a port forward to one of the Pods and access nginx
25-
// 4. We can access ngix via the service endpoint
27+
// 4. We can access nginx via the service endpoint
28+
// 5. We can access nginx via the ingress endpoint
2629
func TestK8SServiceNginxExample(t *testing.T) {
2730
t.Parallel()
2831

@@ -45,16 +48,58 @@ func TestK8SServiceNginxExample(t *testing.T) {
4548
options := &helm.Options{
4649
KubectlOptions: kubectlOptions,
4750
ValuesFiles: []string{filepath.Join(examplePath, "values.yaml")},
51+
SetValues: map[string]string{
52+
"ingress.enabled": "true",
53+
"ingress.path": "/app",
54+
"ingress.servicePort": "http",
55+
"ingress.annotations.kubernetes\\.io/ingress\\.class": "nginx",
56+
"ingress.annotations.nginx\\.ingress\\.kubernetes\\.io/rewrite-target": "/",
57+
},
4858
}
4959
defer helm.Delete(t, options, releaseName, true)
5060
helm.Install(t, options, helmChartPath, releaseName)
5161

5262
verifyPodsCreatedSuccessfully(t, kubectlOptions, "nginx", releaseName)
5363
verifyAllPodsAvailable(t, kubectlOptions, "nginx", releaseName, nginxValidationFunction)
5464
verifyServiceAvailable(t, kubectlOptions, "nginx", releaseName, nginxValidationFunction)
65+
verifyIngressAvailable(t, kubectlOptions, "nginx", releaseName, nginxValidationFunction)
5566
}
5667

5768
// nginxValidationFunction checks that we get a 200 response with the nginx welcome page.
5869
func nginxValidationFunction(statusCode int, body string) bool {
5970
return statusCode == 200 && strings.Contains(body, "Welcome to nginx")
6071
}
72+
73+
func verifyIngressAvailable(
74+
t *testing.T,
75+
kubectlOptions *k8s.KubectlOptions,
76+
appName string,
77+
releaseName string,
78+
validationFunction func(int, string) bool,
79+
) {
80+
// Get the service and wait until it is available
81+
filters := metav1.ListOptions{
82+
LabelSelector: fmt.Sprintf("app.kubernetes.io/name=%s,app.kubernetes.io/instance=%s", appName, releaseName),
83+
}
84+
ingresses := k8s.ListIngresses(t, kubectlOptions, filters)
85+
require.Equal(t, len(ingresses), 1)
86+
ingressName := ingresses[0].Name
87+
k8s.WaitUntilIngressAvailable(
88+
t,
89+
kubectlOptions,
90+
ingressName,
91+
WaitTimerRetries,
92+
WaitTimerSleep,
93+
)
94+
95+
// Now hit the service endpoint to verify it is accessible
96+
ingress := k8s.GetIngress(t, kubectlOptions, ingressName)
97+
ingressEndpoint := ingress.Status.LoadBalancer.Ingress[0].IP
98+
http_helper.HttpGetWithRetryWithCustomValidation(
99+
t,
100+
fmt.Sprintf("http://%s/app", ingressEndpoint),
101+
WaitTimerRetries,
102+
WaitTimerSleep,
103+
validationFunction,
104+
)
105+
}

0 commit comments

Comments
 (0)