k8s-backend - Initial context support for library update

Signed-off-by: Jared Ledvina <jared.ledvina@datadoghq.com>
This commit is contained in:
Jared Ledvina 2021-05-07 15:03:42 -04:00 committed by James Bardin
parent ac52f5135a
commit 61a5808680
3 changed files with 24 additions and 17 deletions

View File

@ -1,6 +1,7 @@
package kubernetes package kubernetes
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"sort" "sort"
@ -21,6 +22,7 @@ func (b *Backend) Workspaces() ([]string, error) {
} }
secrets, err := secretClient.List( secrets, err := secretClient.List(
context.Background(),
metav1.ListOptions{ metav1.ListOptions{
LabelSelector: tfstateKey + "=true", LabelSelector: tfstateKey + "=true",
}, },

View File

@ -1,6 +1,7 @@
package kubernetes package kubernetes
import ( import (
"context"
"fmt" "fmt"
"math/rand" "math/rand"
"os" "os"
@ -124,6 +125,7 @@ func TestBackendLocksSoak(t *testing.T) {
} }
func cleanupK8sResources(t *testing.T) { func cleanupK8sResources(t *testing.T) {
ctx := context.Background()
// Get a backend to use the k8s client // Get a backend to use the k8s client
b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
"secret_suffix": secretSuffix, "secret_suffix": secretSuffix,
@ -138,13 +140,13 @@ func cleanupK8sResources(t *testing.T) {
// Delete secrets // Delete secrets
opts := metav1.ListOptions{LabelSelector: tfstateKey + "=true"} opts := metav1.ListOptions{LabelSelector: tfstateKey + "=true"}
secrets, err := sClient.List(opts) secrets, err := sClient.List(ctx, opts)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
delProp := metav1.DeletePropagationBackground delProp := metav1.DeletePropagationBackground
delOps := &metav1.DeleteOptions{PropagationPolicy: &delProp} delOps := metav1.DeleteOptions{PropagationPolicy: &delProp}
var errs []error var errs []error
for _, secret := range secrets.Items { for _, secret := range secrets.Items {
@ -155,7 +157,7 @@ func cleanupK8sResources(t *testing.T) {
} }
if key == secretSuffix { if key == secretSuffix {
err = sClient.Delete(secret.GetName(), delOps) err = sClient.Delete(ctx, secret.GetName(), delOps)
if err != nil { if err != nil {
errs = append(errs, err) errs = append(errs, err)
} }
@ -168,7 +170,7 @@ func cleanupK8sResources(t *testing.T) {
} }
// Delete leases // Delete leases
leases, err := leaseClient.List(opts) leases, err := leaseClient.List(ctx, opts)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -181,7 +183,7 @@ func cleanupK8sResources(t *testing.T) {
} }
if key == secretSuffix { if key == secretSuffix {
err = leaseClient.Delete(lease.GetName(), delOps) err = leaseClient.Delete(ctx, lease.GetName(), delOps)
if err != nil { if err != nil {
errs = append(errs, err) errs = append(errs, err)
} }

View File

@ -3,6 +3,7 @@ package kubernetes
import ( import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"context"
"crypto/md5" "crypto/md5"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
@ -46,7 +47,7 @@ func (c *RemoteClient) Get() (payload *remote.Payload, err error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
secret, err := c.kubernetesSecretClient.Get(secretName, metav1.GetOptions{}) secret, err := c.kubernetesSecretClient.Get(context.Background(), secretName, metav1.GetOptions{})
if err != nil { if err != nil {
if k8serrors.IsNotFound(err) { if k8serrors.IsNotFound(err) {
return nil, nil return nil, nil
@ -78,6 +79,7 @@ func (c *RemoteClient) Get() (payload *remote.Payload, err error) {
} }
func (c *RemoteClient) Put(data []byte) error { func (c *RemoteClient) Put(data []byte) error {
ctx := context.Background()
secretName, err := c.createSecretName() secretName, err := c.createSecretName()
if err != nil { if err != nil {
return err return err
@ -105,14 +107,14 @@ func (c *RemoteClient) Put(data []byte) error {
}, },
} }
secret, err = c.kubernetesSecretClient.Create(secret, metav1.CreateOptions{}) secret, err = c.kubernetesSecretClient.Create(ctx, secret, metav1.CreateOptions{})
if err != nil { if err != nil {
return err return err
} }
} }
setState(secret, payload) setState(secret, payload)
_, err = c.kubernetesSecretClient.Update(secret, metav1.UpdateOptions{}) _, err = c.kubernetesSecretClient.Update(ctx, secret, metav1.UpdateOptions{})
return err return err
} }
@ -145,6 +147,7 @@ func (c *RemoteClient) Delete() error {
} }
func (c *RemoteClient) Lock(info *statemgr.LockInfo) (string, error) { func (c *RemoteClient) Lock(info *statemgr.LockInfo) (string, error) {
ctx := context.Background()
leaseName, err := c.createLeaseName() leaseName, err := c.createLeaseName()
if err != nil { if err != nil {
return "", err return "", err
@ -170,7 +173,7 @@ func (c *RemoteClient) Lock(info *statemgr.LockInfo) (string, error) {
}, },
} }
_, err = c.kubernetesLeaseClient.Create(lease) _, err = c.kubernetesLeaseClient.Create(ctx, lease, metav1.CreateOptions{})
if err != nil { if err != nil {
return "", err return "", err
} else { } else {
@ -197,7 +200,7 @@ func (c *RemoteClient) Lock(info *statemgr.LockInfo) (string, error) {
lease.Spec.HolderIdentity = pointer.StringPtr(info.ID) lease.Spec.HolderIdentity = pointer.StringPtr(info.ID)
setLockInfo(lease, info.Marshal()) setLockInfo(lease, info.Marshal())
_, err = c.kubernetesLeaseClient.Update(lease) _, err = c.kubernetesLeaseClient.Update(ctx, lease, metav1.UpdateOptions{})
if err != nil { if err != nil {
return "", err return "", err
} }
@ -234,7 +237,7 @@ func (c *RemoteClient) Unlock(id string) error {
lease.Spec.HolderIdentity = nil lease.Spec.HolderIdentity = nil
removeLockInfo(lease) removeLockInfo(lease)
_, err = c.kubernetesLeaseClient.Update(lease) _, err = c.kubernetesLeaseClient.Update(context.Background(), lease, metav1.UpdateOptions{})
if err != nil { if err != nil {
lockErr.Err = err lockErr.Err = err
return lockErr return lockErr
@ -276,11 +279,11 @@ func (c *RemoteClient) getLabels() map[string]string {
} }
func (c *RemoteClient) getSecret(name string) (*unstructured.Unstructured, error) { func (c *RemoteClient) getSecret(name string) (*unstructured.Unstructured, error) {
return c.kubernetesSecretClient.Get(name, metav1.GetOptions{}) return c.kubernetesSecretClient.Get(context.Background(), name, metav1.GetOptions{})
} }
func (c *RemoteClient) getLease(name string) (*coordinationv1.Lease, error) { func (c *RemoteClient) getLease(name string) (*coordinationv1.Lease, error) {
return c.kubernetesLeaseClient.Get(name, metav1.GetOptions{}) return c.kubernetesLeaseClient.Get(context.Background(), name, metav1.GetOptions{})
} }
func (c *RemoteClient) deleteSecret(name string) error { func (c *RemoteClient) deleteSecret(name string) error {
@ -296,8 +299,8 @@ func (c *RemoteClient) deleteSecret(name string) error {
} }
delProp := metav1.DeletePropagationBackground delProp := metav1.DeletePropagationBackground
delOps := &metav1.DeleteOptions{PropagationPolicy: &delProp} delOps := metav1.DeleteOptions{PropagationPolicy: &delProp}
return c.kubernetesSecretClient.Delete(name, delOps) return c.kubernetesSecretClient.Delete(context.Background(), name, delOps)
} }
func (c *RemoteClient) deleteLease(name string) error { func (c *RemoteClient) deleteLease(name string) error {
@ -313,8 +316,8 @@ func (c *RemoteClient) deleteLease(name string) error {
} }
delProp := metav1.DeletePropagationBackground delProp := metav1.DeletePropagationBackground
delOps := &metav1.DeleteOptions{PropagationPolicy: &delProp} delOps := metav1.DeleteOptions{PropagationPolicy: &delProp}
return c.kubernetesLeaseClient.Delete(name, delOps) return c.kubernetesLeaseClient.Delete(context.Background(), name, delOps)
} }
func (c *RemoteClient) createSecretName() (string, error) { func (c *RemoteClient) createSecretName() (string, error) {