{"id":23,"date":"2022-05-24T16:45:18","date_gmt":"2022-05-24T21:45:18","guid":{"rendered":"https:\/\/jcshort.net\/?p=23"},"modified":"2022-05-26T16:13:58","modified_gmt":"2022-05-26T21:13:58","slug":"context-specific-k8s-cluster-and-namespace","status":"publish","type":"post","link":"https:\/\/jcshort.net\/?p=23","title":{"rendered":"Context-specific K8s cluster and namespace"},"content":{"rendered":"\n<p>In the last post, I shared my <a href=\"https:\/\/jcshort.net\/?p=19\" data-type=\"post\" data-id=\"19\">context-specific direnv+asdf config<\/a> for managing interpreters and tools.  Since writing that, I attempted to add kubernetes contexts to the mix, and found only dubious advice from other folks advocating wrapper scripts and other oddities.<\/p>\n\n\n\n<p>It&#8217;s poorly documented and somewhat counterintuitive, but <code>kubectl<\/code> supports multiple configuration files in the <code>KUBECONFIG<\/code> environment variable.  Unlike most other unix-like applications however, it treats those configuration files almost exactly backwards &#8211; the first definition wins.  But it&#8217;s enough to get the job done.<\/p>\n\n\n\n<p>First, we create an overlay configuration file, with the minimum amount duplicated from <code>~\/.kube\/config<\/code>.<\/p>\n\n\n\n<p>Here&#8217;s the contents of <code>~\/.kube\/overlay\/project-cluster-namespace<\/code>. Name and path are completely arbitrary, but I do like to be organized.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"yaml\" class=\"language-yaml\">contexts:\n- context:\n    cluster: gke_cluster_name\n    namespace: my-application-namespace\n    user: gke_authentication_bits\n  name: my-favorite-context\ncurrent-context: my-favorite-context<\/code><\/pre>\n\n\n\n<p>From there, one need only add a <code>KUBECONFIG<\/code> environment variable to <code>.envrc<\/code> as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">export KUBECONFIG=\"${HOME}\/.kube\/overlay\/project-cluster-namespace:${HOME}\/.kube\/config\"<\/code><\/pre>\n\n\n\n<p>Entering the directory with direnv hooks active will yield the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">direnv: loading ~\/projects\/my-application\ndirenv: export +KUBECONFIG<\/code><\/pre>\n\n\n\n<p>The result is a context-specific cluster and namespace without having to deal with clunky <code>kubectl<\/code> wrapper scripts or extra files in the workspace, and no side effects for other sessions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last post, I shared my context-specific direnv+asdf config for managing interpreters and tools. Since writing that, I attempted to add kubernetes contexts to the mix, and found only dubious advice from other folks advocating wrapper scripts and other oddities. It&#8217;s poorly documented and somewhat counterintuitive, but kubectl supports multiple configuration files in the [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[1],"tags":[],"class_list":{"0":"post-23","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-tech","7":"entry"},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"Jason Short","author_link":"https:\/\/jcshort.net\/?author=3"},"_links":{"self":[{"href":"https:\/\/jcshort.net\/index.php?rest_route=\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jcshort.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jcshort.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jcshort.net\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/jcshort.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=23"}],"version-history":[{"count":0,"href":"https:\/\/jcshort.net\/index.php?rest_route=\/wp\/v2\/posts\/23\/revisions"}],"wp:attachment":[{"href":"https:\/\/jcshort.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jcshort.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jcshort.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}