Home > Software engineering >  Grep exact match for Kubernetes namespace (non-files)
Grep exact match for Kubernetes namespace (non-files)

Time:02-21

Background: I have two deployments in my k8s cluster called (as an example) the following:

foo-bar
foo-bar-asd

In my pipeline, I want to implement logic which checks if there's any deployments named "foo-bar" already deployed.

How do I construct my command to only fetch/output "foo-bar", and not "foo-bar-asd" ?

I've tried toying around with regex, '^foo-bar$' etc without any success, found through googling/similar stackoverflow-questions.

My current command (which fetches both):

kubectl -n my-namespace get deployments | grep foo-bar 

Thank you in advance for any help!

CodePudding user response:

grep command can also exclude certain string. You can run something like

kubectl -n my-namespace get deployments | grep foo-bar | grep -v foo-bar-asd

This should show you what you are looking for.

CodePudding user response:

My version of grep has a -w switch, which I can use for filtering "whole words only", so in your case it would be:

grep -w "foo-bar"

Good luck

  • Related