github.com/qsunny/k8s@v0.0.0-20220101153623-e6dca256d5bf/examples-master/staging/cockroachdb/demo.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2016 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  set -euo pipefail
    18  
    19  function sql() {
    20    # TODO(knz): Why does the more idiomatic read from stdin not produce any
    21    # output?
    22    kubectl exec "cockroachdb-${1}" -- /cockroach/cockroach sql \
    23        --host "cockroachdb-${1}.cockroachdb" \
    24        --insecure \
    25        -e "$(cat /dev/stdin)"
    26  }
    27  
    28  function kill() {
    29    ! kubectl exec -t "cockroachdb-${1}" -- /bin/bash -c "while true; do kill 1; done" &> /dev/null
    30  }
    31  
    32  # Create database on second node (idempotently for convenience).
    33  cat <<EOF | sql 1
    34  CREATE DATABASE IF NOT EXISTS foo;
    35  CREATE TABLE IF NOT EXISTS foo.bar (k STRING PRIMARY KEY, v STRING);
    36  UPSERT INTO foo.bar VALUES ('Kuber', 'netes'), ('Cockroach', 'DB');
    37  EOF
    38  
    39  # Kill the node we just created the table on.
    40  kill 1
    41  
    42  # Read the data from all other nodes (we could also read from the one we just
    43  # killed, but it's awkward to wait for it to respawn).
    44  for i in 0 2 3 4; do
    45    cat <<EOF | sql "${i}"
    46  SELECT CONCAT(k, v) FROM foo.bar;
    47  EOF
    48  done