|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved. |
| 3 | +# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl. |
| 4 | +# |
| 5 | +# Description |
| 6 | +# This sample script creates a Kubernetes secret for WebLogic domain admin credentials. |
| 7 | +# |
| 8 | +# The following pre-requisites must be handled prior to running this script: |
| 9 | +# * The kubernetes namespace must already be created |
| 10 | +# |
| 11 | + |
| 12 | +script="${BASH_SOURCE[0]}" |
| 13 | + |
| 14 | +# |
| 15 | +# Function to exit and print an error message |
| 16 | +# $1 - text of message |
| 17 | +function fail { |
| 18 | + echo [ERROR] $* |
| 19 | + exit 1 |
| 20 | +} |
| 21 | + |
| 22 | +# Try to execute kubectl to see whether kubectl is available |
| 23 | +function validateKubectlAvailable { |
| 24 | + if ! [ -x "$(command -v kubectl)" ]; then |
| 25 | + fail "kubectl is not installed" |
| 26 | + fi |
| 27 | +} |
| 28 | + |
| 29 | +function usage { |
| 30 | + echo usage: ${script} -u username -p password [-d domainUID] [-n namespace] [-h] |
| 31 | + echo " -u username, must be specified." |
| 32 | + echo " -p password, must be specified." |
| 33 | + echo " -n namespace, optional." |
| 34 | + echo " -d domainUID, optional." |
| 35 | + echo " -h Help" |
| 36 | + exit $1 |
| 37 | +} |
| 38 | + |
| 39 | +# |
| 40 | +# Parse the command line options |
| 41 | +# |
| 42 | +domainUID=domain1 |
| 43 | +namespace=default |
| 44 | +while getopts "hu:p:n:d:" opt; do |
| 45 | + case $opt in |
| 46 | + u) username="${OPTARG}" |
| 47 | + ;; |
| 48 | + p) password="${OPTARG}" |
| 49 | + ;; |
| 50 | + n) namespace="${OPTARG}" |
| 51 | + ;; |
| 52 | + d) domainUID="${OPTARG}" |
| 53 | + ;; |
| 54 | + h) usage 0 |
| 55 | + ;; |
| 56 | + *) usage 1 |
| 57 | + ;; |
| 58 | + esac |
| 59 | +done |
| 60 | +secretName=$domainUID-weblogic-credentials |
| 61 | + |
| 62 | +if [ -z ${username} ]; then |
| 63 | + echo "${script}: -u must be specified." |
| 64 | + missingRequiredOption="true" |
| 65 | +fi |
| 66 | + |
| 67 | +if [ -z ${password} ]; then |
| 68 | + echo "${script}: -p must be specified." |
| 69 | + missingRequiredOption="true" |
| 70 | +fi |
| 71 | + |
| 72 | +if [ "${missingRequiredOption}" == "true" ]; then |
| 73 | + usage 1 |
| 74 | +fi |
| 75 | + |
| 76 | +# check and see if the secret already exists |
| 77 | +result=`kubectl get secret ${secretName} -n ${namespace} --ignore-not-found=true | grep ${secretName} | wc | awk ' { print $1; }'` |
| 78 | +if [ "${result:=Error}" != "0" ]; then |
| 79 | + fail "The secret ${secretName} already exists in namespace ${namespace}." |
| 80 | +fi |
| 81 | + |
| 82 | +# create the secret |
| 83 | +kubectl -n $namespace create secret generic $secretName \ |
| 84 | + --from-literal=username=$username \ |
| 85 | + --from-literal=password=$password |
| 86 | + |
| 87 | +# label the secret with domainUID |
| 88 | +kubectl label secret ${secretName} -n $namespace weblogic.domainUID=$domainUID weblogic.domainName=$domainUID |
| 89 | + |
| 90 | +# Verify the secret exists |
| 91 | +SECRET=`kubectl get secret ${secretName} -n ${namespace} | grep ${secretName} | wc | awk ' { print $1; }'` |
| 92 | +if [ "${SECRET}" != "1" ]; then |
| 93 | + fail "The secret ${secretName} was not found in namespace ${namespace}" |
| 94 | +fi |
| 95 | + |
| 96 | +echo "The secret ${secretName} has been successfully created in namespace ${namespace}" |
0 commit comments