Skip to content

Commit

Permalink
Update client-go to 10.0.0 and refactor dynamic client usage
Browse files Browse the repository at this point in the history
- Use the updated dynamic client in the new client-go library to
simplify usage and avoid passing around so many variables
- Fix some buggy tests
  • Loading branch information
lblackstone committed Jan 17, 2019
1 parent 098bf43 commit 6a3c89f
Show file tree
Hide file tree
Showing 32 changed files with 1,360 additions and 1,308 deletions.
802 changes: 278 additions & 524 deletions Gopkg.lock

Large diffs are not rendered by default.

76 changes: 41 additions & 35 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,59 @@ required = ["k8s.io/kubernetes/pkg/kubectl/cmd/util"]
name = "github.com/pulumi/pulumi"
branch = "master"

[[override]]
name = "github.com/ugorji/go"
revision = "8c0409fcbb70099c748d71f714529204975f6c3f"

[[override]]
name = "github.com/cbroglie/mustache"
revision = "v1.0.0"

# ---------------------------------------------------------------------------
# NOTE: the k8s.io dependencies, (including go-autorest) are highly sensitive
# to change! They are pinned at versions where (1) they compile (nb.,
# surprisingly challenging) and (2) their quirks are well-understood. cf.,
# [here][1], [here][2], and [here][3].
#
# [1]: https://medium.com/@andy.goldstein/upgrading-kubernetes-client-go-from-v4-to-v5-bbd5025fe381
# [2]: https://medium.com/@vladimirvivien/using-gos-dep-to-organize-your-kubernetes-client-go-dependencies-509ddc766ed3
# [3]: https://blog.heptio.com/straighten-out-your-kubernetes-client-go-dependencies-heptioprotip-8baeed46fe7d
# ---------------------------------------------------------------------------
# Kubernetes packages
#

[[constraint]]
name = "k8s.io/api"
revision = "kubernetes-1.10.2"
name = "k8s.io/kubernetes"
version = "1.13.1"

[[constraint]]
name = "k8s.io/client-go"
version = "10.0.0"

[[constraint]]
name = "k8s.io/apimachinery"
version = "kubernetes-1.10.2"
version = "kubernetes-1.13.0"

[[constraint]]
name = "k8s.io/api"
revision = "kubernetes-1.13.0"

[[override]]
name = "k8s.io/apiserver"
version = "kubernetes-1.10.2"
name = "sigs.k8s.io/kustomize"
version = "1.0.11"

[[constraint]]
name = "k8s.io/client-go"
version = "7.0.0"
#
# Cloud provider packages
#

[[override]]
name = "github.com/aws/aws-sdk-go"
version = "1.13.12"

#
# Third party packages
#

[[constraint]]
name = "k8s.io/kubernetes"
version = "v1.10.2"
name = "github.com/cbroglie/mustache"
version = "v1.0.0"

[[override]]
name = "github.com/russross/blackfriday"
version = "v1.5"
name = "github.com/json-iterator/go"
version = "1.1.5"

# ---------------------------------------------------------------------------
# End sensitive k8s.io dependencies.
# ---------------------------------------------------------------------------
[[override]]
name = "github.com/satori/go.uuid"
version = "1.2.0"

[[override]]
name = "github.com/spf13/cobra"
version = "0.0.3"

[[override]]
name = "github.com/spf13/pflag"
version = "1.0.2"

[[constraint]]
name = "github.com/yudai/gojsondiff"
revision = "9209d1532c51cabe0439993586a71c207b09a0ac"
5 changes: 2 additions & 3 deletions examples/provider/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.

import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
import * as fs from "fs";
import * as os from "os";
Expand All @@ -20,7 +19,7 @@ const nginxcontainer = new k8s.core.v1.Pod("nginx", {
containers: [{
image: "nginx:1.7.9",
name: "nginx",
ports: [{ containerPort: 80 }],
ports: [{containerPort: 80}],
}],
},
}, { provider: myk8s });
}, {provider: myk8s});
73 changes: 32 additions & 41 deletions pkg/await/apps_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (

"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/pulumi/pulumi-kubernetes/pkg/client"
"github.com/pulumi/pulumi-kubernetes/pkg/clients"
"github.com/pulumi/pulumi-kubernetes/pkg/openapi"
"github.com/pulumi/pulumi/pkg/diag"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
)
Expand Down Expand Up @@ -137,15 +136,15 @@ func (dia *deploymentInitAwaiter) Await() error {
// corresponding ReplicaSet), and therefore there is no rollout to mark as "Progressing".
//

replicaSetClient, podClient, pvcClient, err := dia.makeClients()
deploymentClient, replicaSetClient, podClient, pvcClient, err := dia.makeClients()
if err != nil {
return err
}

// Create Deployment watcher.
deploymentWatcher, err := dia.config.clientForResource.Watch(metav1.ListOptions{})
deploymentWatcher, err := deploymentClient.Watch(metav1.ListOptions{})
if err != nil {
return errors.Wrapf(err, "Could not set up watch for Deployment object %q",
return errors.Wrapf(err, "could not set up watch for Deployment object %q",
dia.config.currentInputs.GetName())
}
defer deploymentWatcher.Stop()
Expand Down Expand Up @@ -185,13 +184,13 @@ func (dia *deploymentInitAwaiter) Await() error {

func (dia *deploymentInitAwaiter) Read() error {
// Get clients needed to retrieve live versions of relevant Deployments, ReplicaSets, and Pods.
replicaSetClient, podClient, pvcClient, err := dia.makeClients()
deploymentClient, replicaSetClient, podClient, pvcClient, err := dia.makeClients()
if err != nil {
return err
}

// Get live versions of Deployment, ReplicaSets, and Pods.
deployment, err := dia.config.clientForResource.Get(dia.config.currentInputs.GetName(),
deployment, err := deploymentClient.Get(dia.config.currentInputs.GetName(),
metav1.GetOptions{})
if err != nil {
// IMPORTANT: Do not wrap this error! If this is a 404, the provider need to know so that it
Expand Down Expand Up @@ -226,8 +225,7 @@ func (dia *deploymentInitAwaiter) Read() error {
pvcList = &unstructured.UnstructuredList{Items: []unstructured.Unstructured{}}
}

return dia.read(deployment, rsList.(*unstructured.UnstructuredList),
podList.(*unstructured.UnstructuredList), pvcList.(*unstructured.UnstructuredList))
return dia.read(deployment, rsList, podList, pvcList)
}

func (dia *deploymentInitAwaiter) read(
Expand Down Expand Up @@ -650,13 +648,13 @@ func (dia *deploymentInitAwaiter) aggregatePodErrors() ([]string, []string) {
}
}

scheduleErrors := []string{}
scheduleErrors := make([]string, 0)
for message, count := range scheduleErrorCounts {
message = fmt.Sprintf("%d Pods failed to schedule because: %s", count, message)
scheduleErrors = append(scheduleErrors, message)
}

containerErrors := []string{}
containerErrors := make([]string, 0)
for message, count := range containerErrorCounts {
message = fmt.Sprintf("%d Pods failed to run because: %s", count, message)
containerErrors = append(containerErrors, message)
Expand All @@ -670,7 +668,7 @@ func (dia *deploymentInitAwaiter) getFailedPersistentValueClaims() []string {
return nil
}

failed := []string{}
failed := make([]string, 0)
for _, pvc := range dia.pvcs {
phase, _ := openapi.Pluck(pvc.Object, "status", "phase")
if phase != statusBound {
Expand All @@ -681,7 +679,7 @@ func (dia *deploymentInitAwaiter) getFailedPersistentValueClaims() []string {
}

func (dia *deploymentInitAwaiter) errorMessages() []string {
messages := []string{}
messages := make([]string, 0)
for _, message := range dia.deploymentErrors {
messages = append(messages, message)
}
Expand Down Expand Up @@ -725,43 +723,36 @@ func (dia *deploymentInitAwaiter) errorMessages() []string {
}

func (dia *deploymentInitAwaiter) makeClients() (
replicaSetClient, podClient, pvcClient dynamic.ResourceInterface, err error,
deploymentClient, replicaSetClient, podClient, pvcClient dynamic.ResourceInterface, err error,
) {
replicaSetClient, err = client.FromGVK(dia.config.pool, dia.config.disco,
schema.GroupVersionKind{
Group: "extensions",
Version: "v1beta1",
Kind: "ReplicaSet",
}, dia.config.currentInputs.GetNamespace())
deploymentClient, err = clients.ResourceClient(
clients.Deployment, dia.config.currentInputs.GetNamespace(), dia.config.clientSet)
if err != nil {
return nil, nil, nil, errors.Wrapf(err,
"Could not make client to watch ReplicaSets associated with Deployment %q",
err = errors.Wrapf(err, "Could not make client to watch Deployment %q",
dia.config.currentInputs.GetName())
return
}

podClient, err = client.FromGVK(dia.config.pool, dia.config.disco,
schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "Pod",
}, dia.config.currentInputs.GetNamespace())
replicaSetClient, err = clients.ResourceClient(
clients.ReplicaSet, dia.config.currentInputs.GetNamespace(), dia.config.clientSet)
if err != nil {
return nil, nil, nil, errors.Wrapf(err,
"Could not make client to watch Pods associated with Deployment %q",
err = errors.Wrapf(err, "Could not make client to watch ReplicaSets associated with Deployment %q",
dia.config.currentInputs.GetName())
return
}

pvcClient, err = client.FromGVK(dia.config.pool, dia.config.disco,
schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "PersistentVolumeClaim",
}, dia.config.currentInputs.GetNamespace())
podClient, err = clients.ResourceClient(
clients.Pod, dia.config.currentInputs.GetNamespace(), dia.config.clientSet)
if err != nil {
return nil, nil, nil, errors.Wrapf(err,
"Could not make client to watch PersistentVolumeClaims associated with Deployment %q",
err = errors.Wrapf(err, "Could not make client to watch Pods associated with Deployment %q",
dia.config.currentInputs.GetName())
return
}
pvcClient, err = clients.ResourceClient(
clients.PersistentVolumeClaim, dia.config.currentInputs.GetNamespace(), dia.config.clientSet)
if err != nil {
err = errors.Wrapf(err, "Could not make client to watch PVCs associated with Deployment %q",
dia.config.currentInputs.GetName())
return
}

return replicaSetClient, podClient, pvcClient, nil
return
}
38 changes: 20 additions & 18 deletions pkg/await/apps_statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (

"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/pulumi/pulumi-kubernetes/pkg/client"
"github.com/pulumi/pulumi-kubernetes/pkg/clients"
"github.com/pulumi/pulumi-kubernetes/pkg/openapi"
"github.com/pulumi/pulumi/pkg/diag"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
)
Expand Down Expand Up @@ -130,18 +129,18 @@ func makeStatefulSetInitAwaiter(c updateAwaitConfig) *statefulsetInitAwaiter {
// 2. The value of `.status.updateRevision` matches `.status.currentRevision`.
func (sia *statefulsetInitAwaiter) Await() error {

podClient, err := sia.makeClients()
statefulSetClient, podClient, err := sia.makeClients()
if err != nil {
return err
}

// Create Deployment watcher.
statefulsetWatcher, err := sia.config.clientForResource.Watch(metav1.ListOptions{})
statefulSetWatcher, err := statefulSetClient.Watch(metav1.ListOptions{})
if err != nil {
return errors.Wrapf(err, "Could not set up watch for StatefulSet object %q",
sia.config.currentInputs.GetName())
}
defer statefulsetWatcher.Stop()
defer statefulSetWatcher.Stop()

// Create Pod watcher.
podWatcher, err := podClient.Watch(metav1.ListOptions{})
Expand All @@ -155,18 +154,18 @@ func (sia *statefulsetInitAwaiter) Await() error {
period := time.NewTicker(10 * time.Second)
defer period.Stop()

return sia.await(statefulsetWatcher, podWatcher, time.After(5*time.Minute), period.C)
return sia.await(statefulSetWatcher, podWatcher, time.After(5*time.Minute), period.C)
}

func (sia *statefulsetInitAwaiter) Read() error {
// Get clients needed to retrieve live versions of relevant Deployments, ReplicaSets, and Pods.
podClient, err := sia.makeClients()
statefulSetClient, podClient, err := sia.makeClients()
if err != nil {
return err
}

// Get live versions of StatefulSet and Pods.
statefulset, err := sia.config.clientForResource.Get(sia.config.currentInputs.GetName(),
statefulset, err := statefulSetClient.Get(sia.config.currentInputs.GetName(),
metav1.GetOptions{})
if err != nil {
// IMPORTANT: Do not wrap this error! If this is a 404, the provider need to know so that it
Expand All @@ -187,7 +186,7 @@ func (sia *statefulsetInitAwaiter) Read() error {
podList = &unstructured.UnstructuredList{Items: []unstructured.Unstructured{}}
}

return sia.read(statefulset, podList.(*unstructured.UnstructuredList))
return sia.read(statefulset, podList)
}

// read is a helper companion to `Read` designed to make it easy to test this module.
Expand Down Expand Up @@ -449,19 +448,22 @@ func (sia *statefulsetInitAwaiter) errorMessages() []string {
}

func (sia *statefulsetInitAwaiter) makeClients() (
podClient dynamic.ResourceInterface, err error,
statefulSetClient, podClient dynamic.ResourceInterface, err error,
) {
podClient, err = client.FromGVK(sia.config.pool, sia.config.disco,
schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "Pod",
}, sia.config.currentInputs.GetNamespace())
statefulSetClient, err = clients.ResourceClient(
clients.StatefulSet, sia.config.currentInputs.GetNamespace(), sia.config.clientSet)
if err != nil {
return nil, errors.Wrapf(err,
return nil, nil, errors.Wrapf(err,
"Could not make client to watch StatefulSet %q",
sia.config.currentInputs.GetName())
}
podClient, err = clients.ResourceClient(
clients.Pod, sia.config.currentInputs.GetNamespace(), sia.config.clientSet)
if err != nil {
return nil, nil, errors.Wrapf(err,
"Could not make client to watch Pods associated with StatefulSet %q",
sia.config.currentInputs.GetName())
}

return podClient, nil
return statefulSetClient, podClient, nil
}
Loading

0 comments on commit 6a3c89f

Please sign in to comment.