running kubectl create command from inside a shell script

4/18/2017

I am trying to call kubectl create -n $test_namespace -f <absolute-path-to-tea-rc-file>.yaml from a shell script.

#!/bin/bash

current_dir=$(pwd)
echo "current_dir is $current_dir"
parentdir="$(dirname "$current_dir")"
echo $parentdir
kubectl create -n $test_namespace -f $parentdir/deployment/common/tea-rc.yaml

It gives error as below:

Bhagyashrees-MacBook-Pro:execution_controller bhagyashree$ sh test.sh 
current_dir is /Users/bhagyashree/Documents/FDrepo/armada-ingress-ctl-test/execution_controller
/Users/bhagyashree/Documents/FDrepo/armada-ingress-ctl-test
error: unknown command "/Users/bhagyashree/Documents/FDrepo/armada-ingress-ctl-test/deployment/common/tea-rc.yaml"
See 'kubectl create -h' for help and examples.

the same command works when it is executed from a terminal. kubectl create -n testnamespace -f /Users/bhagyashree/Documents/FDrepo/armada-ingress-ctl-test/deployment/common/tea-rc.yaml

What I am i missing here?

-- Bhagyashree
kubernetes

1 Answer

4/18/2017

I think it's because the variable $test_namespace has not been set.

If you change the last line to echo "kubectl create -n $test_namespace -f $parentdir/deployment/common/tea-rc.yaml" you'll see what it's trying to run, it will look like kubctl create -n -f /path/to/dir/deployment/common/tea-rc.yaml. The Namespace can not be left blank.

You could also try adding a line like echo $test_namespace to check.

-- Simon I
Source: StackOverflow