Skip to content

Commit

Permalink
Resolving compilation errors for k8s 1.21.x client
Browse files Browse the repository at this point in the history
* remove "replace" statement for go-autorest

With SOPS upgraded, "go mod tidy" now fails until this is removed.

* Resolve client-go issue from upgrade to 1.20

kubernetes/client-go#914 (comment)

* register deployments type

Signed-off-by: Kingdon Barrett <kingdon@weave.works>
  • Loading branch information
Kingdon Barrett committed Aug 17, 2021
1 parent 5700253 commit 3687686
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 67 deletions.
9 changes: 5 additions & 4 deletions cmd/fluxctl/portforward.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"strings"

Expand All @@ -14,11 +15,11 @@ import (

// Attempt to create PortForwards to fluxes that match the label selectors until a Flux
// is found or an error is returned.
func tryPortforwards(context string, ns string, selectors ...metav1.LabelSelector) (p *portforward.PortForward, err error) {
func tryPortforwards(kubeConfigContext string, ns string, selectors ...metav1.LabelSelector) (p *portforward.PortForward, err error) {
message := fmt.Sprintf("No pod found in namespace %q using the following selectors:", ns)

for _, selector := range selectors {
p, err = tryPortforward(context, ns, selector)
p, err = tryPortforward(context.TODO(), kubeConfigContext, ns, selector)
if err == nil {
return
}
Expand All @@ -39,7 +40,7 @@ func tryPortforwards(context string, ns string, selectors ...metav1.LabelSelecto
}

// Attempt to create a portforward in the namespace for the provided LabelSelector
func tryPortforward(kubeConfigContext string, ns string, selector metav1.LabelSelector) (*portforward.PortForward, error) {
func tryPortforward(ctx context.Context, kubeConfigContext string, ns string, selector metav1.LabelSelector) (*portforward.PortForward, error) {
portforwarder := &portforward.PortForward{
Namespace: ns,
Labels: selector,
Expand All @@ -66,7 +67,7 @@ func tryPortforward(kubeConfigContext string, ns string, selector metav1.LabelSe
return portforwarder, errors.Wrap(err, "Could not create kubernetes client")
}

err = portforwarder.Start()
err = portforwarder.Start(context.TODO())
if err != nil {
return portforwarder, err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/cluster/kubernetes/cached_disco.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kubernetes

import (
"context"
"fmt"
"sync"
"time"
Expand Down Expand Up @@ -67,10 +68,10 @@ func MakeCachedDiscovery(d discovery.DiscoveryInterface, c crd.Interface, shutdo
crdClient := c.ApiextensionsV1().CustomResourceDefinitions()
lw := &toolscache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return crdClient.List(options)
return crdClient.List(context.TODO(), options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return crdClient.Watch(options)
return crdClient.Watch(context.TODO(), options)
},
}
handle := toolscache.ResourceEventHandlerFuncs{
Expand Down
3 changes: 2 additions & 1 deletion pkg/cluster/kubernetes/cached_disco_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kubernetes

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -69,7 +70,7 @@ func TestCachedDiscovery(t *testing.T) {
coreClient.Fake.Resources = append(apiResources, updatedAPI)

// Provoke the cached discovery client into invalidating
_, err = crdClient.ApiextensionsV1().CustomResourceDefinitions().Update(myCRD)
_, err = crdClient.ApiextensionsV1().CustomResourceDefinitions().Update(context.TODO(), myCRD, metav1.UpdateOptions{})
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/kubernetes/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func mergeCredentials(log func(...interface{}) error,
saName = "default"
}

sa, err := client.CoreV1().ServiceAccounts(namespace).Get(saName, meta_v1.GetOptions{})
sa, err := client.CoreV1().ServiceAccounts(namespace).Get(context.TODO(), saName, meta_v1.GetOptions{})
if err == nil {
for _, ips := range sa.ImagePullSecrets {
imagePullSecrets = append(imagePullSecrets, ips.Name)
Expand All @@ -73,7 +73,7 @@ func mergeCredentials(log func(...interface{}) error,
continue
}

secret, err := client.CoreV1().Secrets(namespace).Get(name, meta_v1.GetOptions{})
secret, err := client.CoreV1().Secrets(namespace).Get(context.TODO(), name, meta_v1.GetOptions{})
if err != nil {
log("err", errors.Wrapf(err, "getting secret %q from namespace %q", name, namespace))
imagePullSecretCache[namespacedSecretName] = registry.NoCredentials()
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (c *Cluster) Export(ctx context.Context) ([]byte, error) {
defer encoder.Close()

for _, ns := range namespaces {
namespace, err := c.client.CoreV1().Namespaces().Get(ns, meta_v1.GetOptions{})
namespace, err := c.client.CoreV1().Namespaces().Get(ctx, ns, meta_v1.GetOptions{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -311,7 +311,7 @@ func (c *Cluster) getAllowedAndExistingNamespaces(ctx context.Context) ([]string
if err := ctx.Err(); err != nil {
return nil, err
}
ns, err := c.client.CoreV1().Namespaces().Get(name, meta_v1.GetOptions{})
ns, err := c.client.CoreV1().Namespaces().Get(ctx, name, meta_v1.GetOptions{})
switch {
case err == nil:
c.updateLoggedAllowedNS(name, false) // reset, so if the namespace goes away we'll log it again
Expand Down
18 changes: 9 additions & 9 deletions pkg/cluster/kubernetes/resourcekinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (dk *deploymentKind) getWorkload(ctx context.Context, c *Cluster, namespace
if err := ctx.Err(); err != nil {
return workload{}, err
}
deployment, err := c.client.AppsV1().Deployments(namespace).Get(name, meta_v1.GetOptions{})
deployment, err := c.client.AppsV1().Deployments(namespace).Get(context.TODO(), name, meta_v1.GetOptions{})
if err != nil {
return workload{}, err
}
Expand All @@ -128,7 +128,7 @@ func (dk *deploymentKind) getWorkloads(ctx context.Context, c *Cluster, namespac
if err := ctx.Err(); err != nil {
return nil, err
}
deployments, err := c.client.AppsV1().Deployments(namespace).List(meta_v1.ListOptions{})
deployments, err := c.client.AppsV1().Deployments(namespace).List(context.TODO(), meta_v1.ListOptions{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -199,7 +199,7 @@ func (dk *daemonSetKind) getWorkload(ctx context.Context, c *Cluster, namespace,
if err := ctx.Err(); err != nil {
return workload{}, err
}
daemonSet, err := c.client.AppsV1().DaemonSets(namespace).Get(name, meta_v1.GetOptions{})
daemonSet, err := c.client.AppsV1().DaemonSets(namespace).Get(context.TODO(), name, meta_v1.GetOptions{})
if err != nil {
return workload{}, err
}
Expand All @@ -211,7 +211,7 @@ func (dk *daemonSetKind) getWorkloads(ctx context.Context, c *Cluster, namespace
if err := ctx.Err(); err != nil {
return nil, err
}
daemonSets, err := c.client.AppsV1().DaemonSets(namespace).List(meta_v1.ListOptions{})
daemonSets, err := c.client.AppsV1().DaemonSets(namespace).List(context.TODO(), meta_v1.ListOptions{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -266,7 +266,7 @@ func (dk *statefulSetKind) getWorkload(ctx context.Context, c *Cluster, namespac
if err := ctx.Err(); err != nil {
return workload{}, err
}
statefulSet, err := c.client.AppsV1().StatefulSets(namespace).Get(name, meta_v1.GetOptions{})
statefulSet, err := c.client.AppsV1().StatefulSets(namespace).Get(context.TODO(), name, meta_v1.GetOptions{})
if err != nil {
return workload{}, err
}
Expand All @@ -278,7 +278,7 @@ func (dk *statefulSetKind) getWorkloads(ctx context.Context, c *Cluster, namespa
if err := ctx.Err(); err != nil {
return nil, err
}
statefulSets, err := c.client.AppsV1().StatefulSets(namespace).List(meta_v1.ListOptions{})
statefulSets, err := c.client.AppsV1().StatefulSets(namespace).List(context.TODO(), meta_v1.ListOptions{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -365,7 +365,7 @@ func (dk *cronJobKind) getWorkload(ctx context.Context, c *Cluster, namespace, n
if err := ctx.Err(); err != nil {
return workload{}, err
}
cronJob, err := c.client.BatchV1beta1().CronJobs(namespace).Get(name, meta_v1.GetOptions{})
cronJob, err := c.client.BatchV1beta1().CronJobs(namespace).Get(context.TODO(), name, meta_v1.GetOptions{})
if err != nil {
return workload{}, err
}
Expand All @@ -377,7 +377,7 @@ func (dk *cronJobKind) getWorkloads(ctx context.Context, c *Cluster, namespace s
if err := ctx.Err(); err != nil {
return nil, err
}
cronJobs, err := c.client.BatchV1beta1().CronJobs(namespace).List(meta_v1.ListOptions{})
cronJobs, err := c.client.BatchV1beta1().CronJobs(namespace).List(context.TODO(), meta_v1.ListOptions{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -437,7 +437,7 @@ func (hr *helmReleaseKind) getWorkloads(ctx context.Context, c *Cluster, namespa
}

func makeHelmReleaseStableWorkload(helmRelease *hr_v1.HelmRelease) workload {
containers := createK8sHRContainers(helmRelease.ObjectMeta.Annotations, helmRelease.Spec.Values.Data)
containers := createK8sHRContainers(helmRelease.ObjectMeta.Annotations, helmRelease.GetValues())

podTemplate := apiv1.PodTemplateSpec{
ObjectMeta: helmRelease.ObjectMeta,
Expand Down
6 changes: 4 additions & 2 deletions pkg/cluster/kubernetes/sshkeyring.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package kubernetes

import (
"context"
"encoding/base64"
"encoding/json"
"os"
"path/filepath"
"sync"

"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/typed/core/v1"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"

"github.com/fluxcd/flux/pkg/ssh"
)
Expand Down Expand Up @@ -129,7 +131,7 @@ func (skr *sshKeyRing) Regenerate() error {
return err
}

_, err = skr.SecretAPI.Patch(skr.SecretName, types.StrategicMergePatchType, jsonPatch)
_, err = skr.SecretAPI.Patch(context.TODO(), skr.SecretName, types.StrategicMergePatchType, jsonPatch, metav1.PatchOptions{})
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/cluster/kubernetes/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/ryanuber/go-glob"
"io"
"os/exec"
"sort"
"strings"
"time"

"github.com/ryanuber/go-glob"

"github.com/go-kit/kit/log"
"github.com/imdario/mergo"
"github.com/pkg/errors"
Expand Down Expand Up @@ -336,7 +337,7 @@ func (c *Cluster) listAllowedResources(
if !namespaced {
// The resource is not namespaced, everything is allowed
resourceClient := c.client.dynamicClient.Resource(gvr)
data, err := resourceClient.List(options)
data, err := resourceClient.List(context.TODO(), options)
if err != nil {
return nil, err
}
Expand All @@ -350,7 +351,7 @@ func (c *Cluster) listAllowedResources(
}
var result []unstructured.Unstructured
for _, ns := range namespaces {
data, err := c.client.dynamicClient.Resource(gvr).Namespace(ns).List(options)
data, err := c.client.dynamicClient.Resource(gvr).Namespace(ns).List(context.TODO(), options)
if err != nil {
return result, err
}
Expand Down
Loading

0 comments on commit 3687686

Please sign in to comment.