feat: local cluster allocation, configuration and teardown

This commit is contained in:
Luke Kingland 2021-02-12 11:12:52 +09:00
parent 80e366b142
commit 9c499b69c4
No known key found for this signature in database
GPG Key ID: 4896F75BAF2E1966
3 changed files with 314 additions and 0 deletions

125
hack/allocate.sh Executable file
View File

@ -0,0 +1,125 @@
#!/usr/bin/env bash
# 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
#
# https://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.
#
# Allocate a local kind cluster with Knative and Kourier installed.
#
set -o errexit
set -o nounset
set -o pipefail
main() {
local serving_version=v0.20.0
local eventing_version=v0.20.0
local kourier_version=v0.20.0
local em=$(tput bold)$(tput setaf 2)
local me=$(tput sgr0)
echo "${em}Allocating...${me}"
cluster
serving
eventing
networking
registry
next_steps
echo "${em}DONE${me}"
}
cluster() {
echo "${em}① Cluster${me}"
cat <<EOF | kind create cluster --wait=60s --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
extraPortMappings:
- containerPort: 30080
hostPort: 80
listenAddress: "127.0.0.1"
- containerPort: 30443
hostPort: 443
listenAddress: "127.0.0.1"
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:5000"]
endpoint = ["http://kind-registry:5000"]
EOF
}
serving() {
echo "${em}② Knative Serving${me}"
kubectl apply --filename https://github.com/knative/serving/releases/download/$serving_version/serving-crds.yaml
sleep 2
curl -L -s https://github.com/knative/serving/releases/download/$serving_version/serving-core.yaml | yq 'del(.spec.template.spec.containers[]?.resources)' -y | yq 'del(.metadata.annotations."knative.dev/example-checksum")' -y | kubectl apply -f -
}
eventing() {
echo "${em}③ Knative Eventing${me}"
kubectl apply --filename https://github.com/knative/eventing/releases/download/$eventing_version/eventing-crds.yaml
sleep 2
curl -L -s https://github.com/knative/eventing/releases/download/$eventing_version/eventing-core.yaml | yq 'del(.spec.template.spec.containers[]?.resources)' -y | yq 'del(.metadata.annotations."knative.dev/example-checksum")' -y | kubectl apply -f -
curl -L -s https://github.com/knative/eventing/releases/download/$eventing_version/in-memory-channel.yaml | yq 'del(.spec.template.spec.containers[]?.resources)' -y | yq 'del(.metadata.annotations."knative.dev/example-checksum")' -y | kubectl apply -f -
curl -L -s https://github.com/knative/eventing/releases/download/$eventing_version/mt-channel-broker.yaml | yq 'del(.spec.template.spec.containers[]?.resources)' -y | yq 'del(.metadata.annotations."knative.dev/example-checksum")' -y | kubectl apply -f -
}
networking() {
echo "${em}④ Kourier Networking${me}"
kubectl apply --filename https://github.com/knative-sandbox/net-kourier/releases/download/$kourier_version/kourier.yaml
kubectl patch configmap/config-network \
--namespace knative-serving \
--type merge \
--patch '{"data":{"ingress.class":"kourier.ingress.networking.knative.dev"}}'
}
registry() {
# see https://kind.sigs.k8s.io/docs/user/local-registry/
echo "${em}⑤ Container Registry${me}"
docker run -d --restart=always -p "5000:5000" --name "kind-registry" registry:2
docker network connect "kind" "kind-registry"
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
name: local-registry-hosting
namespace: kube-public
data:
localRegistryHosting.v1: |
host: "localhost:5000"
help: "https://kind.sigs.k8s.io/docs/user/local-registry/"
EOF
}
next_steps() {
local red=$(tput bold)$(tput setaf 1)
echo "${em}⑥ Configure Registry${me}"
echo "Please ${red}manually add 'kind-registry' to your local hosts${me} file:"
echo " echo \"127.0.0.1 kind-registry\" | sudo tee --append /etc/hosts"
echo "Please ${red}manually set registry as insecure${me} in the docker daemon config (/etc/docker/daemon.json on linux or ~/.docker/daemon.json on OSX):
{
\"insecure-registries\": [ \"kind-registry:5000\" ],
}"
}
main "$@"

167
hack/configure.sh Executable file
View File

@ -0,0 +1,167 @@
#!/usr/bin/env bash
# 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
#
# https://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.
#
# Configures the current cluster for use with Functions
#
set -o errexit
set -o nounset
set -o pipefail
DEFAULT_NAMESPACE=func
show_help() {
cat << EOF
Configure a local cluster for use with Functions.
Usage: $(basename "$0") <options>
-h, --help Display help
-n, --namespace The namespace to use for Functions (default: $DEFAULT_NAMESPACE)
EOF
}
main() {
local namespace="$DEFAULT_NAMESPACE"
local em=$(tput bold)$(tput setaf 2)
local me=$(tput sgr0)
parse_command_line "$@"
echo "${em}Configuring...${me}"
namespace
network
kourier_nodeport
default_domain
sleep 10
kubectl --namespace kourier-system get service kourier
echo "${em}DONE${me}"
}
parse_command_line() {
while :; do
case "${1:-}" in
-h|--help)
show_help
exit
;;
-n|--namespace)
if [[ -n "${2:-}" ]]; then
namespace="$2"
shift
else
echo "ERROR: '-n|--namespace' cannot be empty." >&2
show_help
exit 1
fi
;;
*)
break
;;
esac
done
}
namespace() {
echo "${em}① Namespace${me}"
kubectl create namespace "$namespace"
kubectl label namespace "$namespace" knative-eventing-injection=enabled
}
network() {
echo "${em}② Network${me}"
echo "Registering Kourier as ingress"
echo "Enabling subdomains"
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
name: config-network
namespace: knative-serving
data:
# Use Kourier for the networking layer
ingress.class: kourier.ingress.networking.knative.dev
# If there exists an annotation 'func.subdomain' on the service, use it
# instead of the default name.namespace
domainTemplate: |-
{{if index .Annotations "func.subdomain" -}}
{{- index .Annotations "func.subdomain" -}}
{{else -}}
{{- .Name}}.{{.Namespace -}}
{{end -}}
.{{.Domain}}
EOF
}
kourier_nodeport() {
echo "${em}③ Nodeport${me}"
echo 'Setting Kourier service to type NodePort'
# Patch for changing kourier to a NodePort for installations where a
# LoadBalancer is not available (for example local Kind clusters)
# kubectl patch -n kourier-system services/kourier -p "$(cat configure-kourier-nodeport.yaml)"
kubectl patch services/kourier \
--namespace kourier-system \
--type merge \
--patch '{
"spec": {
"ports": [
{
"name": "http2",
"nodePort": 30080,
"port": 80,
"protocol": "TCP",
"targetPort": 8080
},
{
"name": "https",
"nodePort": 30443,
"port": 443,
"protocol": "TCP",
"targetPort": 8443
}
],
"type": "NodePort"
}
}'
}
default_domain() {
echo "${em}④ Default Domains${me}"
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
name: config-domain
namespace: knative-serving
data:
example.com: |
selector:
func.domain: "example.com"
example.org: |
selector:
func.domain: "example.org"
# Default is local only.
svc.cluster.local: ""
EOF
}
main "$@"

22
hack/delete.sh Executable file
View File

@ -0,0 +1,22 @@
# Create a local kind cluster with
# Knative Serving, and Kourier networking installed.
# Suitable for use locally during development.
# CI/CD uses the very similar knative-kind action
main() {
local green=$(tput bold)$(tput setaf 2)
local red=$(tput bold)$(tput setaf 2)
local reset=$(tput sgr0)
echo "${green}Deleting Cluster, Registry and Network${reset}"
kind delete cluster --name "kind"
docker stop kind-registry && docker rm kind-registry
docker network rm kind
echo "${red}NOTE:${reset} The following changes have not been undone:"
echo " - Manual etc/hosts entry for kind-registry"
echo " - Manual docker config registering kind-registry as insecure"
echo " - Downloaded container images were not removed"
echo "${green}DONE${reset}"
}
main