Kubernetes Go operator use secret data

5/12/2020

Is it possible to read and use the data values of a Kubernetes secret from within a Kubernetes go operator? Specifically I need the reconcile function to be able to make a call out to a private github. The authorization token is to be stored in a k8s secret. Thus I need to be able to load the specified secret and extract the token from the secret data.

I am able to get the secret:

secret := &corev1.Secret{}
err = r.client.Get(context.TODO(), secretNamespaceName, secret)

reqLogger.Info("Secret", "object", secret, "data", secret.Data)

The output shows the correct secret data:

"data":{"Token":"<the token string>"}

I was expecting that I could then just use the secret.Data["Token"] as a string in the github request Authorization header:

reqLogger.Info(string(secret.Data["Token"]))
req.Header.Add("Authorization", "Token" + " " + string(secret.Data["Token"]))

This isn't working and the log shows the Token string as a non readable series of unicode chars.

Is there some decode step or similar that I am missing here or is this even possible?

-- Kerry Gunn
go
kubernetes
operator-sdk

0 Answers