From d653ae39d17c0adae0ea6edc5767f51111e05e63 Mon Sep 17 00:00:00 2001
From: Jessica Yuen <im.jessicayuen@gmail.com>
Date: Thu, 25 Jan 2018 15:48:18 -0800
Subject: [PATCH] Client-go v4 code changes

Signed-off-by: Jessica Yuen <im.jessicayuen@gmail.com>
---
 docs/cli-reference/ks_apply.md    |  3 ++-
 docs/cli-reference/ks_delete.md   |  3 ++-
 docs/cli-reference/ks_env.md      |  3 ++-
 docs/cli-reference/ks_env_add.md  |  3 ++-
 docs/cli-reference/ks_env_list.md |  3 ++-
 docs/cli-reference/ks_env_rm.md   |  3 ++-
 docs/cli-reference/ks_env_set.md  |  3 ++-
 docs/cli-reference/ks_init.md     |  3 ++-
 docs/cli-reference/ks_validate.md |  3 ++-
 pkg/kubecfg/apply.go              |  2 +-
 pkg/kubecfg/diff.go               |  3 ++-
 utils/acquire.go                  |  4 ++--
 utils/client.go                   | 21 ++++++++++++++++++++-
 utils/openapi.go                  |  2 +-
 utils/openapi_test.go             |  2 +-
 15 files changed, 45 insertions(+), 16 deletions(-)

diff --git a/docs/cli-reference/ks_apply.md b/docs/cli-reference/ks_apply.md
index a50c871c..155f443f 100644
--- a/docs/cli-reference/ks_apply.md
+++ b/docs/cli-reference/ks_apply.md
@@ -66,7 +66,8 @@ ks apply dev -c guestbook-ui -c nginx-depl --create false
 
 ```
       --as string                      Username to impersonate for the operation
-      --certificate-authority string   Path to a cert. file for the certificate authority
+      --as-group stringArray           Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
+      --certificate-authority string   Path to a cert file for the certificate authority
       --client-certificate string      Path to a client certificate file for TLS
       --client-key string              Path to a client key file for TLS
       --cluster string                 The name of the kubeconfig cluster to use
diff --git a/docs/cli-reference/ks_delete.md b/docs/cli-reference/ks_delete.md
index 577bd6e9..830f83a1 100644
--- a/docs/cli-reference/ks_delete.md
+++ b/docs/cli-reference/ks_delete.md
@@ -45,7 +45,8 @@ ks delete --kubeconfig=./kubeconfig -c nginx
 
 ```
       --as string                      Username to impersonate for the operation
-      --certificate-authority string   Path to a cert. file for the certificate authority
+      --as-group stringArray           Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
+      --certificate-authority string   Path to a cert file for the certificate authority
       --client-certificate string      Path to a client certificate file for TLS
       --client-key string              Path to a client key file for TLS
       --cluster string                 The name of the kubeconfig cluster to use
diff --git a/docs/cli-reference/ks_env.md b/docs/cli-reference/ks_env.md
index 77ee3628..d3bc8a41 100644
--- a/docs/cli-reference/ks_env.md
+++ b/docs/cli-reference/ks_env.md
@@ -50,7 +50,8 @@ ks env
 
 ```
       --as string                      Username to impersonate for the operation
-      --certificate-authority string   Path to a cert. file for the certificate authority
+      --as-group stringArray           Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
+      --certificate-authority string   Path to a cert file for the certificate authority
       --client-certificate string      Path to a client certificate file for TLS
       --client-key string              Path to a client key file for TLS
       --cluster string                 The name of the kubeconfig cluster to use
diff --git a/docs/cli-reference/ks_env_add.md b/docs/cli-reference/ks_env_add.md
index d0d65d25..ae25c800 100644
--- a/docs/cli-reference/ks_env_add.md
+++ b/docs/cli-reference/ks_env_add.md
@@ -73,7 +73,8 @@ ks env add prod --server=https://ksonnet-1.us-west.elb.amazonaws.com
 
 ```
       --as string                      Username to impersonate for the operation
-      --certificate-authority string   Path to a cert. file for the certificate authority
+      --as-group stringArray           Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
+      --certificate-authority string   Path to a cert file for the certificate authority
       --client-certificate string      Path to a client certificate file for TLS
       --client-key string              Path to a client key file for TLS
       --cluster string                 The name of the kubeconfig cluster to use
diff --git a/docs/cli-reference/ks_env_list.md b/docs/cli-reference/ks_env_list.md
index d06d0c7e..061c0d2a 100644
--- a/docs/cli-reference/ks_env_list.md
+++ b/docs/cli-reference/ks_env_list.md
@@ -27,7 +27,8 @@ ks env list
 
 ```
       --as string                      Username to impersonate for the operation
-      --certificate-authority string   Path to a cert. file for the certificate authority
+      --as-group stringArray           Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
+      --certificate-authority string   Path to a cert file for the certificate authority
       --client-certificate string      Path to a client certificate file for TLS
       --client-key string              Path to a client key file for TLS
       --cluster string                 The name of the kubeconfig cluster to use
diff --git a/docs/cli-reference/ks_env_rm.md b/docs/cli-reference/ks_env_rm.md
index 1be73908..4b8b825c 100644
--- a/docs/cli-reference/ks_env_rm.md
+++ b/docs/cli-reference/ks_env_rm.md
@@ -40,7 +40,8 @@ ks env rm us-west/staging
 
 ```
       --as string                      Username to impersonate for the operation
-      --certificate-authority string   Path to a cert. file for the certificate authority
+      --as-group stringArray           Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
+      --certificate-authority string   Path to a cert file for the certificate authority
       --client-certificate string      Path to a client certificate file for TLS
       --client-key string              Path to a client key file for TLS
       --cluster string                 The name of the kubeconfig cluster to use
diff --git a/docs/cli-reference/ks_env_set.md b/docs/cli-reference/ks_env_set.md
index 9999cc1a..d5d00aa2 100644
--- a/docs/cli-reference/ks_env_set.md
+++ b/docs/cli-reference/ks_env_set.md
@@ -52,7 +52,8 @@ ks env set us-west/staging --context=staging-west
 
 ```
       --as string                      Username to impersonate for the operation
-      --certificate-authority string   Path to a cert. file for the certificate authority
+      --as-group stringArray           Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
+      --certificate-authority string   Path to a cert file for the certificate authority
       --client-certificate string      Path to a client certificate file for TLS
       --client-key string              Path to a client key file for TLS
       --cluster string                 The name of the kubeconfig cluster to use
diff --git a/docs/cli-reference/ks_init.md b/docs/cli-reference/ks_init.md
index e14fae00..21bde859 100644
--- a/docs/cli-reference/ks_init.md
+++ b/docs/cli-reference/ks_init.md
@@ -84,7 +84,8 @@ ks init app-name --dir=custom-location
 ```
       --api-spec string                Manually specified Kubernetes API version. The corresponding OpenAPI spec is used to generate ksonnet's Kubernetes libraries (default "version:v1.7.0")
       --as string                      Username to impersonate for the operation
-      --certificate-authority string   Path to a cert. file for the certificate authority
+      --as-group stringArray           Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
+      --certificate-authority string   Path to a cert file for the certificate authority
       --client-certificate string      Path to a client certificate file for TLS
       --client-key string              Path to a client key file for TLS
       --cluster string                 The name of the kubeconfig cluster to use
diff --git a/docs/cli-reference/ks_validate.md b/docs/cli-reference/ks_validate.md
index cc71c47e..a931c18d 100644
--- a/docs/cli-reference/ks_validate.md
+++ b/docs/cli-reference/ks_validate.md
@@ -50,7 +50,8 @@ ksonnet validate prod -c redis
 
 ```
       --as string                      Username to impersonate for the operation
-      --certificate-authority string   Path to a cert. file for the certificate authority
+      --as-group stringArray           Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
+      --certificate-authority string   Path to a cert file for the certificate authority
       --client-certificate string      Path to a client certificate file for TLS
       --client-key string              Path to a client key file for TLS
       --cluster string                 The name of the kubeconfig cluster to use
diff --git a/pkg/kubecfg/apply.go b/pkg/kubecfg/apply.go
index 0fb8a659..86badd53 100644
--- a/pkg/kubecfg/apply.go
+++ b/pkg/kubecfg/apply.go
@@ -83,7 +83,7 @@ func (c ApplyCmd) Run(apiObjects []*unstructured.Unstructured, wd metadata.AbsPa
 			newobj, err = rc.Patch(obj.GetName(), types.MergePatchType, asPatch)
 			log.Debugf("Patch(%s) returned (%v, %v)", obj.GetName(), newobj, err)
 		} else {
-			newobj, err = rc.Get(obj.GetName())
+			newobj, err = rc.Get(obj.GetName(), metav1.GetOptions{})
 		}
 		if c.Create && errors.IsNotFound(err) {
 			log.Info(" Creating non-existent ", desc, dryRunText)
diff --git a/pkg/kubecfg/diff.go b/pkg/kubecfg/diff.go
index e7b2c119..f6ebb541 100644
--- a/pkg/kubecfg/diff.go
+++ b/pkg/kubecfg/diff.go
@@ -26,6 +26,7 @@ import (
 	"github.com/yudai/gojsondiff"
 	"github.com/yudai/gojsondiff/formatter"
 	"k8s.io/apimachinery/pkg/api/errors"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 	"k8s.io/client-go/discovery"
 	"k8s.io/client-go/dynamic"
@@ -216,7 +217,7 @@ func getLiveObjs(client *Client) ([]*unstructured.Unstructured, map[string]*unst
 			return nil, nil, err
 		}
 
-		liveObj, err := client.Get(obj.GetName())
+		liveObj, err := client.Get(obj.GetName(), metav1.GetOptions{})
 		if err != nil && errors.IsNotFound(err) {
 			log.Debugf("%s doesn't exist on the server", desc)
 			continue
diff --git a/utils/acquire.go b/utils/acquire.go
index dd93e1f7..7b662ad7 100644
--- a/utils/acquire.go
+++ b/utils/acquire.go
@@ -173,8 +173,8 @@ func FlattenToV1(objs []runtime.Object) []*unstructured.Unstructured {
 	for _, obj := range objs {
 		switch o := obj.(type) {
 		case *unstructured.UnstructuredList:
-			for _, item := range o.Items {
-				ret = append(ret, item)
+			for i := range o.Items {
+				ret = append(ret, &o.Items[i])
 			}
 		case *unstructured.Unstructured:
 			ret = append(ret, o)
diff --git a/utils/client.go b/utils/client.go
index 9b89363b..db67a1b4 100644
--- a/utils/client.go
+++ b/utils/client.go
@@ -19,7 +19,8 @@ import (
 	"fmt"
 	"sync"
 
-	"github.com/emicklei/go-restful/swagger"
+	"github.com/emicklei/go-restful-swagger12"
+	"github.com/go-openapi/spec"
 	log "github.com/sirupsen/logrus"
 	"k8s.io/apimachinery/pkg/api/meta"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -37,6 +38,7 @@ type memcachedDiscoveryClient struct {
 	servergroups    *metav1.APIGroupList
 	serverresources map[string]*metav1.APIResourceList
 	schemas         map[string]*swagger.ApiDeclaration
+	schema          *spec.Swagger
 }
 
 // NewMemcachedDiscoveryClient creates a new DiscoveryClient that
@@ -123,6 +125,23 @@ func (c *memcachedDiscoveryClient) SwaggerSchema(version schema.GroupVersion) (*
 	return schema, nil
 }
 
+func (c *memcachedDiscoveryClient) OpenAPISchema() (*spec.Swagger, error) {
+	c.lock.Lock()
+	defer c.lock.Unlock()
+
+	if c.schema != nil {
+		return c.schema, nil
+	}
+
+	schema, err := c.cl.OpenAPISchema()
+	if err != nil {
+		return nil, err
+	}
+
+	c.schema = schema
+	return schema, nil
+}
+
 var _ discovery.CachedDiscoveryInterface = &memcachedDiscoveryClient{}
 
 // ClientForResource returns the ResourceClient for a given object
diff --git a/utils/openapi.go b/utils/openapi.go
index 007904a1..368f01a5 100644
--- a/utils/openapi.go
+++ b/utils/openapi.go
@@ -20,7 +20,7 @@ import (
 	"reflect"
 	"regexp"
 
-	"github.com/emicklei/go-restful/swagger"
+	swagger "github.com/emicklei/go-restful-swagger12"
 	log "github.com/sirupsen/logrus"
 	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 	"k8s.io/apimachinery/pkg/runtime/schema"
diff --git a/utils/openapi_test.go b/utils/openapi_test.go
index 0307a565..17af0d5c 100644
--- a/utils/openapi_test.go
+++ b/utils/openapi_test.go
@@ -24,7 +24,7 @@ import (
 	"strings"
 	"testing"
 
-	"github.com/emicklei/go-restful/swagger"
+	swagger "github.com/emicklei/go-restful-swagger12"
 	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 	"k8s.io/apimachinery/pkg/runtime/schema"
 	utilerrors "k8s.io/apimachinery/pkg/util/errors"
-- 
GitLab