github.com/IBM-Blockchain/fabric-operator@v1.0.4/sample-network/scripts/kind.sh (about) 1 #!/bin/bash 2 # 3 # Copyright contributors to the Hyperledger Fabric Operator project 4 # 5 # SPDX-License-Identifier: Apache-2.0 6 # 7 # Licensed under the Apache License, Version 2.0 (the "License"); 8 # you may not use this file except in compliance with the License. 9 # You may obtain a copy of the License at: 10 # 11 # http://www.apache.org/licenses/LICENSE-2.0 12 # 13 # Unless required by applicable law or agreed to in writing, software 14 # distributed under the License is distributed on an "AS IS" BASIS, 15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 # See the License for the specific language governing permissions and 17 # limitations under the License. 18 # 19 20 function kind_create() { 21 push_fn "Creating cluster \"${CLUSTER_NAME}\"" 22 23 # prevent the next kind cluster from using the previous Fabric network's enrollments. 24 rm -rf $PWD/temp 25 26 # todo: always delete? Maybe return no-op if the cluster already exists? 27 kind delete cluster --name $CLUSTER_NAME 28 29 local reg_name=${LOCAL_REGISTRY_NAME} 30 local reg_port=${LOCAL_REGISTRY_PORT} 31 local ingress_http_port=${NGINX_HTTP_PORT} 32 local ingress_https_port=${NGINX_HTTPS_PORT} 33 34 # the 'ipvs'proxy mode permits better HA abilities 35 36 cat <<EOF | kind create cluster --name $CLUSTER_NAME --image $CLUSTER_IMAGE --config=- 37 --- 38 kind: Cluster 39 apiVersion: kind.x-k8s.io/v1alpha4 40 nodes: 41 - role: control-plane 42 kubeadmConfigPatches: 43 - | 44 kind: InitConfiguration 45 nodeRegistration: 46 kubeletExtraArgs: 47 node-labels: "ingress-ready=true" 48 extraPortMappings: 49 - containerPort: 80 50 hostPort: ${ingress_http_port} 51 protocol: TCP 52 - containerPort: 443 53 hostPort: ${ingress_https_port} 54 protocol: TCP 55 #networking: 56 # kubeProxyMode: "ipvs" 57 58 # create a cluster with the local registry enabled in containerd 59 containerdConfigPatches: 60 - |- 61 [plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${reg_port}"] 62 endpoint = ["http://${reg_name}:${reg_port}"] 63 64 EOF 65 66 for node in $(kind get nodes); 67 do 68 docker exec "$node" sysctl net.ipv4.conf.all.route_localnet=1; 69 done 70 71 pop_fn 72 } 73 74 function launch_docker_registry() { 75 push_fn "Launching container registry \"${LOCAL_REGISTRY_NAME}\" at localhost:${LOCAL_REGISTRY_PORT}" 76 77 # create registry container unless it already exists 78 local reg_name=${LOCAL_REGISTRY_NAME} 79 local reg_port=${LOCAL_REGISTRY_PORT} 80 local reg_interface=${LOCAL_REGISTRY_INTERFACE} 81 82 running="$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)" 83 if [ "${running}" != 'true' ]; then 84 docker run \ 85 --detach \ 86 --restart always \ 87 --name "${reg_name}" \ 88 --publish "${reg_interface}:${reg_port}:5000" \ 89 registry:2 90 fi 91 92 # connect the registry to the cluster network 93 # (the network may already be connected) 94 docker network connect "kind" "${reg_name}" || true 95 96 # Document the local registry 97 # https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry 98 cat <<EOF | kubectl apply -f - 99 --- 100 apiVersion: v1 101 kind: ConfigMap 102 metadata: 103 name: local-registry-hosting 104 namespace: kube-public 105 data: 106 localRegistryHosting.v1: | 107 host: "localhost:${reg_port}" 108 help: "https://kind.sigs.k8s.io/docs/user/local-registry/" 109 EOF 110 111 pop_fn 112 } 113 114 function stop_docker_registry() { 115 push_fn "Deleting container registry \"${LOCAL_REGISTRY_NAME}\" at localhost:${LOCAL_REGISTRY_PORT}" 116 117 docker kill kind-registry || true 118 docker rm kind-registry || true 119 120 pop_fn 121 } 122 123 function kind_delete() { 124 push_fn "Deleting KIND cluster ${CLUSTER_NAME}" 125 126 kind delete cluster --name $CLUSTER_NAME 127 128 pop_fn 2 129 } 130 131 function kind_init() { 132 set -o errexit 133 134 kind_create 135 136 if [ "${USE_LOCAL_REGISTRY}" == true ]; then 137 launch_docker_registry 138 fi 139 } 140 141 function kind_unkind() { 142 143 kind_delete 144 145 if [ "${USE_LOCAL_REGISTRY}" == true ]; then 146 stop_docker_registry 147 fi 148 }