Enhanced OpenShift CLI: More Stories
The enhanced OpenShift CLI can help you manage a large amount of OpenShift clusters securely and efficiently. It is an open source project I created on GitHub.
As the third post of enhanced oc series, in this post, I will share with you more stories regarding the use of enhanced oc.
Organize Cluster Contexts Hierarchically
In a real project, it is common that you have many clusters to manage and each cluster has its own access data. It is a good idea to use the enhanced oc and context alias to switch among these clusters efficiently. However, with more and more clusters added, the context list can be increasingly long. In such case, a more efficient way to manage these clusters is to organize the cluster contexts hierarchically.
This is natively supported by gopass because in the secret store maintained by gopass, each secret data lives in its own file and related secret data can be put in the same directory. For example, if you have three clusters and want to save the contexts for them, you can use the
path/to/your/context format to name the context alias for the clusters.
$ oc login -s https://api.foo.example.com -c dev-env/cluster-foo
$ oc login -s https://api.bar.example.com -c dev-env/cluster-bar
$ oc login -s https://api.baz.example.com -c dev-env/cluster-baz
Then your cluster contexts will be organized hierarchically in the secret store and the directory structure maps to how you name the context alias. This can be seen with a very straightforward view by running
$ gopass ls
When you switch among these clusters, use the
path/to/your/context alias to refer to the target cluster that you want to access:
$ oc login -c dev-env/cluster-foo
$ oc login -c dev-env/cluster-bar
$ oc login -c dev-env/cluster-baz
Choose Among Multiple Clusters
To organize the cluster contexts hierarchically allows you to manage large amount of clusters efficiently. You can category them into different groups for different purposes and switch among them quickly. With more clusters added and the hierarchy expending, you may also find it is not very trivial to input the full context alias of each cluster when you login.
The enhanced oc supports partial input for context alias when you run
oc login. For example, if you put all clusters for development in category
dev-env, you can just input the first few characters such as
dev-env when you specify the context alias. If there are more than one results that match your input, a numbered list will be presented. You can enter a number to choose one option in the list:
$ oc login -c dev
Read context 'dev-env/cluster-bar' from secret store...
Context loaded successfully.
Login successful.You have access to 59 projects, the list has been suppressed. You can list all projects with 'oc projects'Using project "default".
The partial input of context alias and the numbered list can help you quickly choose one cluster among multiple options. Moreover, if you get fzf installed, an interactive command-line filter and fuzzy finder, you will be able to use typeahead and fuzzy search when run enhanced oc to select a context. There is no additional setup needed after you install fzf. The enhanced oc can auto-detect the existence of fzf and enable the corresponding function. For how to install fzf, please refer to the fzf documentation.
Customize Shell Prompt
For better use experience, you can install kube-ps1, which is a script that lets you add the current cluster context and namespace to the shell prompt. It is very helpful if you have many clusters to manage and need to switch among them from time to time. By looking at shell prompt from the command line, you can quickly know the cluster context that you are working with, so as to avoid any operation performed against wrong cluster.
The enhanced oc can auto-detect the existence of kube-ps1 and work with it seamlessly without any additional setup after you get kube-ps1 installed. It will customize the shell prompt, replace the full cluster context name, which is usually much longer and more machine readable, with the context alias, which is much shorter and more human readable. For how to install kube-ps1, please refer to the kube-ps1 documentation.
In this post, you have learned more usage stories on enhanced OpenShift CLI, e.g. to organize cluster contexts hierarchically, switch context among multiple clusters using partial input and fuzzy search, customize shell prompt to reflect the cluster context.
In next post, I will introduce another import feature supported by enhanced oc: how to share your cluster access information with your team members.
The enhanced OpenShift CLI is an open source project I created on GitHub. If you like it, you can consider to give star to it. You can also learn more on this project by reading its online documents. Any contributions such as bug report and code submission are very welcome.