<p>I am totally new to Go and the Kubernetes library k8s.io (<a href="https://github.com/kubernetes/client-go" rel="nofollow noreferrer">https://github.com/kubernetes/client-go</a>) and try to figure out how to get a specific secret.</p>
<p>I have a kind of observer which watches changes of <code>Secrets</code>.
I am iterating through a Secretlist within a specific namespace. That works, I also can filter them by a while loop. But I do not know how to <strong>get and search a Secret in a different namespace</strong> which should be available in this loop.</p>
<p>I need a secret named XXX in namespace &quot;my-namespace&quot;
(I know that the following line does not exist, it should only outline the idea what I am looking for) I come from Ruby, so I searched for something like this :</p>
<pre><code>var myKubeSecret = kubernetes.V1().Secrets(&quot;my-namespace&quot;).Find(&quot;XXX&quot;)
</code></pre>
<p>Exists like the function like that one above?</p>
<p>This is what I have: this observes all my secrets in namespace &quot;default&quot;. Which works. That example was taken from a Code that does something similar I was searching for, and I try to modify now.:</p>
<pre><code>import (

    v1 &quot;k8s.io/api/core/v1&quot;
    metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;
    kubeinformers &quot;k8s.io/client-go/informers&quot;
    &quot;k8s.io/client-go/kubernetes&quot;
    &quot;k8s.io/client-go/tools/cache&quot;
    &quot;k8s.io/client-go/tools/clientcmd&quot;
    &quot;sigs.k8s.io/yaml&quot;
)
// a lot of code
// ....
// ...
// ..
// .

if data, err := ioutil.ReadFile(&quot;/var/run/secrets/kubernetes.io/serviceaccount/namespace&quot;); err == nil {
        fmt.Println(&quot;data&quot;,data)
    }
    // listen for new secrets
    factory := kubeinformers.NewSharedInformerFactoryWithOptions(clientsetCore, 0, kubeinformers.WithNamespace(namespace()))
    informer := factory.Core().V1().Secrets().Informer()
    secrets := factory.Core().V1().Secrets().Lister()

var myKubeSecret string // will hold my secret

informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
  AddFunc: func(new interface{}) {
    // get the secret
    var cpSecret = new.(*v1.Secret).DeepCopy()
        if mySecret.GetName() == &quot;argocd-credentials&quot; {
                var cpData = *&amp;cpSecret.Data
                for k, v := range cpData {
                    clusterIP = kubeConfig.Clusters[0].Cluster.Server

                    fmt.Println(&quot;cpData k:&quot;, k, &quot;v:&quot;, v)
                    switch k {
                    case &quot;authToken&quot;:
                        fmt.Println(&quot;authToken:&quot;, v)

                    // ### HERE SHOULD BE THE VALUE OF A
                    // ### SECRET NAMED XXX in ns my-namespace 
                    myKubeSecret = // ### should a bearerToken string
                    }
                }
            }
      }
}
</code></pre>
<p>I hope you get the idea..</p>
<p><em>Please also tell me which <code>import</code> libray is needed, if any.</em></p>

Jan
<p>as mentioned above, <code>secret</code> object resides in a namespace. They can only be referenced by pods in that same namespace.</p>
<p><a href="https://stackoverflow.com/questions/46297949/sharing-secret-across-namespaces">Sharing secret across namespaces</a></p>
<p>if you want to use the secret in multiple namespaces, copy the same secret into the desired namespaces.</p>
<p>example case</p>
<ul>
<li>kubernetes secret: test-secret-1</li>
<li>namespace from: testns1</li>
<li>namespace to: testns2</li>
</ul>
<ol>
<li>Using pipe &quot;|&quot; operator</li>
</ol>
<pre class="lang-bash prettyprint-override"><code>kubectl get secret test-secret-1 --namespace=testns1 -oyaml | grep -v ^\s*namespace:\s' |kubectl apply --namespace=testns2 -f -
</code></pre>
<ol start="2">
<li>Using sed command</li>
</ol>
<pre class="lang-bash prettyprint-override"><code>kubectl get secret test-secret-1 -n testns1 -o yaml | sed s/&quot;namespace: testns1&quot;/&quot;namespace: testns2&quot;/| kubectl 
apply -n testns2 -f -
</code></pre>
<ol start="3">
<li>Export kubernetes secret to yaml and apply secret</li>
</ol>
<pre class="lang-bash prettyprint-override"><code>kubectl get secret test-secret-1 -n testns1 -o yaml
</code></pre>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
data:
  password: dGVzdFBAc3N3b3Jk
  username: dGVzdC11c2Vy
kind: Secret
metadata:
  creationTimestamp: &quot;2021-11-11T21:21:02Z&quot;
  name: test-secret-1
  namespace: testns1 # change namespace to testns2
  resourceVersion: &quot;307939&quot;
  uid: 6a8d9a6d-9648-4a39-a362-150e682c9a42
type: Opaque
</code></pre>
<p><a href="https://jhooq.com/kubernetes-share-secrets-namespaces/" rel="nofollow noreferrer">https://jhooq.com/kubernetes-share-secrets-namespaces/</a></p>
