Skip to content

Commit

Permalink
Merge pull request #23 from ibuildthecloud/master
Browse files Browse the repository at this point in the history
Add kubeconfig loader helper code
  • Loading branch information
ibuildthecloud authored Jun 12, 2019
2 parents 4202dbf + 2282c85 commit 51afeff
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 70 deletions.
10 changes: 8 additions & 2 deletions pkg/apply/desiredset_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"sync"

gvk2 "github.com/rancher/wrangler/pkg/gvk"

"github.com/pkg/errors"
"github.com/rancher/wrangler/pkg/apply/injectors"
"github.com/rancher/wrangler/pkg/objectset"
Expand Down Expand Up @@ -165,10 +167,14 @@ func (o *desiredSet) getLabelsAndAnnotations() (map[string]string, map[string]st
}

if o.owner != nil {
annotations[LabelGVK] = o.owner.GetObjectKind().GroupVersionKind().String()
gvk, err := gvk2.Get(o.owner)
if err != nil {
return nil, nil, err
}
annotations[LabelGVK] = gvk.String()
metadata, err := meta.Accessor(o.owner)
if err != nil {
return nil, nil, fmt.Errorf("failed to get metadata for %s", o.owner.GetObjectKind().GroupVersionKind())
return nil, nil, fmt.Errorf("failed to get metadata for %s", gvk)
}
annotations[LabelName] = metadata.GetName()
annotations[LabelNamespace] = metadata.GetNamespace()
Expand Down
12 changes: 8 additions & 4 deletions pkg/apply/desiredset_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,19 @@ func getOriginal(obj v1.Object) ([]byte, error) {
return []byte("{}"), nil
}

mapObj := &unstructured.Unstructured{}
err := json.Unmarshal(original, mapObj)
mapObj := map[string]interface{}{}
err := json.Unmarshal(original, &mapObj)
if err != nil {
return nil, err
}

removeCreationTimestamp(mapObj.Object)
removeCreationTimestamp(mapObj)

objCopy, err := prepareObjectForCreate(mapObj)
u := &unstructured.Unstructured{
Object: mapObj,
}

objCopy, err := prepareObjectForCreate(u)
if err != nil {
return nil, err
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/controller-gen/generators/factory_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generators
import (
"fmt"
"io"
"path/filepath"

args2 "github.com/rancher/wrangler/pkg/controller-gen/args"
"k8s.io/gengo/args"
Expand Down Expand Up @@ -38,7 +39,9 @@ func (f *factory) Imports(*generator.Context) []string {
"k8s.io/apimachinery/pkg/runtime/schema",
"k8s.io/client-go/rest",
GenericPackage,
AllSchemes,
fmt.Sprintf("clientset \"%s\"", group.ClientSetPackage),
fmt.Sprintf("scheme \"%s\"", filepath.Join(group.ClientSetPackage, "scheme")),
fmt.Sprintf("informers \"%s\"", group.InformersPackage),
}
}
Expand All @@ -61,6 +64,10 @@ func (f *factory) Init(c *generator.Context, w io.Writer) error {
}

var factoryBody = `
func init() {
scheme.AddToScheme(schemes.All)
}
type Factory struct {
synced bool
informerFactory informers.SharedInformerFactory
Expand Down
1 change: 1 addition & 0 deletions pkg/controller-gen/generators/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

const (
GenericPackage = "github.com/rancher/wrangler/pkg/generic"
AllSchemes = "github.com/rancher/wrangler/pkg/schemes"
)

func groupPath(group string) string {
Expand Down
1 change: 0 additions & 1 deletion pkg/generic/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func NewController(name string, informer cache.SharedIndexInformer, workqueue wo
workqueue: workqueue,
}

logrus.Info("Setting up event handlers")
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.handleObject,
UpdateFunc: func(old, new interface{}) {
Expand Down
27 changes: 27 additions & 0 deletions pkg/gvk/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gvk

import (
"fmt"

"github.com/rancher/wrangler/pkg/schemes"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func Get(obj runtime.Object) (schema.GroupVersionKind, error) {
gvk := obj.GetObjectKind().GroupVersionKind()
if gvk.Kind != "" {
return gvk, nil
}

gvks, _, err := schemes.All.ObjectKinds(obj)
if err != nil {
return schema.GroupVersionKind{}, err
}

if len(gvks) == 0 {
return schema.GroupVersionKind{}, fmt.Errorf("failed to find gvk for %v", obj.GetObjectKind())
}

return gvks[0], nil
}
39 changes: 39 additions & 0 deletions pkg/kubeconfig/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package kubeconfig

import (
"io"
"os"
"path/filepath"

"k8s.io/client-go/tools/clientcmd"
)

func GetNonInteractiveClientConfig(kubeConfig string) clientcmd.ClientConfig {
return GetClientConfig(kubeConfig, nil)
}

func GetInteractiveClientConfig(kubeConfig string) clientcmd.ClientConfig {
return GetClientConfig(kubeConfig, os.Stdin)
}

func GetClientConfig(kubeConfig string, reader io.Reader) clientcmd.ClientConfig {
loadingRules := GetLoadingRules(kubeConfig)
overrides := &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults}
return clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, reader)
}

func GetLoadingRules(kubeConfig string) *clientcmd.ClientConfigLoadingRules {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
if kubeConfig != "" {
loadingRules.ExplicitPath = kubeConfig
}

homeDir, err := os.UserHomeDir()
if err == nil {
loadingRules.Precedence = append(loadingRules.Precedence, filepath.Join(homeDir, ".kube", "k3s.yaml"))
}

loadingRules.Precedence = append(loadingRules.Precedence, "/etc/rancher/k3s/k3s.yaml")
return loadingRules
}
89 changes: 26 additions & 63 deletions pkg/objectset/objectset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"reflect"
"sort"

"github.com/rancher/wrangler/pkg/gvk"

"github.com/rancher/wrangler/pkg/merr"
"k8s.io/apimachinery/pkg/api/meta"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -33,59 +35,50 @@ func (o ObjectKey) String() string {

type ObjectByGVK map[schema.GroupVersionKind]map[ObjectKey]runtime.Object

func (o ObjectByGVK) Add(obj runtime.Object) error {
func (o ObjectByGVK) Add(obj runtime.Object) (schema.GroupVersionKind, error) {
metadata, err := meta.Accessor(obj)
if err != nil {
return err
return schema.GroupVersionKind{}, err
}

gvk, err := gvk.Get(obj)
if err != nil {
return schema.GroupVersionKind{}, err
}

objs := o[obj.GetObjectKind().GroupVersionKind()]
objs := o[gvk]
if objs == nil {
objs = map[ObjectKey]runtime.Object{}
o[obj.GetObjectKind().GroupVersionKind()] = objs
o[gvk] = objs
}

objs[ObjectKey{
Namespace: metadata.GetNamespace(),
Name: metadata.GetName(),
}] = obj

return nil
return gvk, nil
}

type ObjectSet struct {
errs []error
objects ObjectByGVK
nsed map[schema.GroupVersionKind]bool
inputs []runtime.Object
order []runtime.Object
errs []error
objects ObjectByGVK
order []runtime.Object
gvkOrder []schema.GroupVersionKind
gvkSeen map[schema.GroupVersionKind]bool
}

func NewObjectSet() *ObjectSet {
return &ObjectSet{
nsed: map[schema.GroupVersionKind]bool{},
objects: ObjectByGVK{},
gvkSeen: map[schema.GroupVersionKind]bool{},
}
}

func (o *ObjectSet) Inputs() []runtime.Object {
return o.inputs
}

func (o *ObjectSet) ObjectsByGVK() ObjectByGVK {
return o.objects
}

func (o *ObjectSet) AddInput(objs ...runtime.Object) *ObjectSet {
for _, obj := range objs {
if obj == nil || reflect.ValueOf(obj).IsNil() {
continue
}
o.inputs = append(o.inputs, obj)
}
return o
}

func (o *ObjectSet) Add(objs ...runtime.Object) *ObjectSet {
for _, obj := range objs {
o.add(obj)
Expand All @@ -98,34 +91,17 @@ func (o *ObjectSet) add(obj runtime.Object) {
return
}

gvk := obj.GetObjectKind().GroupVersionKind()

metadata, err := meta.Accessor(obj)
gvk, err := o.objects.Add(obj)
if err != nil {
o.err(fmt.Errorf("failed to get metadata for %s", gvk))
return
}

name := metadata.GetName()
if name == "" {
o.err(fmt.Errorf("%s is missing name", gvk))
return
}

namespace := metadata.GetNamespace()
nsed, ok := o.nsed[gvk]
if ok && nsed != (namespace != "") {
o.err(fmt.Errorf("got %s objects that are both namespaced and not namespaced", gvk))
return
}
o.nsed[gvk] = namespace != ""

if err := o.objects.Add(obj); err != nil {
o.err(fmt.Errorf("failed to get metadata for %s", gvk))
o.err(fmt.Errorf("failed to add %v", obj))
return
}

o.order = append(o.order, obj)
if !o.gvkSeen[gvk] {
o.gvkSeen[gvk] = true
o.gvkOrder = append(o.gvkOrder, gvk)
}
}

func (o *ObjectSet) err(err error) error {
Expand All @@ -146,31 +122,18 @@ func (o *ObjectSet) Len() int {
}

func (o *ObjectSet) GVKOrder(known ...schema.GroupVersionKind) []schema.GroupVersionKind {
seen := map[schema.GroupVersionKind]bool{}
var gvkOrder []schema.GroupVersionKind

for _, obj := range o.order {
if seen[obj.GetObjectKind().GroupVersionKind()] {
continue
}
seen[obj.GetObjectKind().GroupVersionKind()] = true
gvkOrder = append(gvkOrder, obj.GetObjectKind().GroupVersionKind())
}

var rest []schema.GroupVersionKind

for _, gvk := range known {
if seen[gvk] {
if o.gvkSeen[gvk] {
continue
}

seen[gvk] = true
rest = append(rest, gvk)
}

sort.Slice(rest, func(i, j int) bool {
return rest[i].String() < rest[j].String()
})

return append(gvkOrder, rest...)
return append(o.gvkOrder, rest...)
}
7 changes: 7 additions & 0 deletions pkg/schemes/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package schemes

import (
"k8s.io/apimachinery/pkg/runtime"
)

var All = runtime.NewScheme()

0 comments on commit 51afeff

Please sign in to comment.