Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Mar 7, 2019
1 parent a998855 commit d774a26
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 122 deletions.
51 changes: 13 additions & 38 deletions pkg/addons/default/aws_node.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
package defaultaddons

import (
"fmt"

"github.com/kris-nova/logger"
"github.com/pkg/errors"
"github.com/weaveworks/eksctl/pkg/eks"

apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions/resource"
"k8s.io/client-go/kubernetes"
// "k8s.io/apimachinery/pkg/api/meta"
// "k8s.io/cli-runtime/pkg/genericclioptions/resource"
// "k8s.io/client-go/kubernetes/scheme"
)

const (
AWSNode = "aws-node"
)

func UpdateAWSNode(clientSet *kubernetes.Clientset, dryRun bool) error {
_, err := clientSet.Apps().DaemonSets(metav1.NamespaceSystem).Get(AWSNode, metav1.GetOptions{})
func UpdateAWSNode(rawClient *eks.RawClient, dryRun bool) error {
_, err := rawClient.ClientSet.Apps().DaemonSets(metav1.NamespaceSystem).Get(AWSNode, metav1.GetOptions{})
if err != nil {
if apierrs.IsNotFound(err) {
logger.Warning("%q was not found", AWSNode)
Expand All @@ -35,38 +29,19 @@ func UpdateAWSNode(clientSet *kubernetes.Clientset, dryRun bool) error {
return err
}

// client := clientSet.RESTClient().Put().Namespace(metav1.NamespaceSystem)

// client := clientSet.RESTClient()
// mapper := meta.NewDefaultRESTMapper(scheme.Scheme)
// client, err := NewClientHelper(clientSet)
// if err != nil {
// return err
// }

for _, runtimeObj := range list.Items {
gvk := runtimeObj.Object.GetObjectKind().GroupVersionKind()
obj, ok := runtimeObj.Object.(metav1.Object)
if !ok {
return fmt.Errorf("cannot conver object of type %T to metav1.Object", runtimeObj)
for _, rawObj := range list.Items {
r, info, err := NewResourceClient(rawClient, rawObj)
if err != nil {
return err
}

logger.Info("%s:%s.%s/%s", obj.GetNamespace(), gvk.Kind, gvk.Group, obj.GetName())

// fqKind := schema.FromAPIVersionAndKind(gvk.Version, gvk.Kind)
// RESTMapping(fqKind.GroupKind(), fqKind.Version)

r, err := resource.NewHelper(client, mapper).Get(obj.GetNamespace(), obj.GetName(), false)
logger.Debug("r = %#v", r)
res, err := r.Get(info.Namespace, info.Name, false)
logger.Debug("res = %#v", res)
logger.Debug("err = %#v", err)

// logger.Info("client.APIVersion = %#v", client.APIVersion())
// req := client.Get().
// NamespaceIfScoped(obj.GetNamespace(), obj.GetNamespace() != "").
// //Resource(gvk.Kind + "." + gvk.Group).
// Resource(gvk.Kind).
// Name(obj.GetName())
// // req := client.Resource().Body(obj.Object)
// logger.Debug("req = %#v", req)
// res := req.Do()
// logger.Debug("res = %#v", res)
// logger.Debug("res.Error = %#v", res.Error())
}

return nil
Expand Down
82 changes: 82 additions & 0 deletions pkg/addons/default/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package defaultaddons

import (
"bytes"
"fmt"
"io"
"strings"

"github.com/kris-nova/logger"
"github.com/pkg/errors"
"github.com/weaveworks/eksctl/pkg/eks"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/cli-runtime/pkg/genericclioptions/resource"
"k8s.io/client-go/kubernetes/scheme"

apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
Expand Down Expand Up @@ -77,3 +81,81 @@ func listAppendFlattened(components *metav1.List, component runtime.RawExtension
}
return listAppendFlattened(components, runtime.RawExtension{Object: obj})
}

// type ClientHelper struct {
// client restclient.Interface
// mapper meta.RESTMapper
// }

// func NewClientHelper(clientSet *kubernetes.Clientset) (*ClientHelper, error) {
// // mapper := restmapper.NewDeferredDiscoveryRESTMapper(cacheddiscovery.NewMemCacheClient(clientSet.Discovery()))

// apiGroupResources, err := restmapper.GetAPIGroupResources(clientSet.Discovery())
// if err != nil {
// return nil, errors.Wrap(err, "getting list of API resources for REST client")
// }

// for i, r := range apiGroupResources {
// logger.Debug("apiGroupResources[%d] = %#v", i, *r)
// }

// c := &ClientHelper{
// client: clientSet.RESTClient(),
// mapper: restmapper.NewDiscoveryRESTMapper(apiGroupResources),
// }

// return c, nil
// }

// func (c *ClientHelper) NewResource(rawObj runtime.RawExtension) (*resource.Helper, *resource.Info, error) {
// gvk := rawObj.Object.GetObjectKind().GroupVersionKind()
// // fqk := schema.FromAPIVersionAndKind(gvk.Version, gvk.Kind)
// // mapping, err := c.mapper.RESTMapping(fqk.GroupKind(), fqk.GroupVersion().Version)
// mapping, err := c.mapper.RESTMapping(gvk.GroupKind(), gvk.GroupVersion().Version)

// if err != nil {
// return nil, nil, errors.Wrap(err, "constructing REST client mapping")
// }

// obj, ok := rawObj.Object.(metav1.Object)
// if !ok {
// return nil, nil, fmt.Errorf("cannot conver object of type %T to metav1.Object", rawObj.Object)
// }

// info := &resource.Info{
// Client: c.client,
// Mapping: mapping,
// Name: obj.GetName(),
// Namespace: obj.GetNamespace(),
// Object: rawObj.Object,
// }
// logger.Info("%s:%s.%s/%s", info.Namespace, gvk.Kind, gvk.Group, info.Name)

// return resource.NewHelper(c.client, mapping), info, nil
// }

func NewResourceClient(rawClient *eks.RawClient, rawObj runtime.RawExtension) (*resource.Helper, *resource.Info, error) {
gvk := rawObj.Object.GetObjectKind().GroupVersionKind()
// fqk := schema.FromAPIVersionAndKind(gvk.Version, gvk.Kind)
// mapping, err := c.mapper.RESTMapping(fqk.GroupKind(), fqk.GroupVersion().Version)
helper, err := rawClient.NewFor(gvk)
if err != nil {
return nil, nil, err
}

obj, ok := rawObj.Object.(metav1.Object)
if !ok {
return nil, nil, fmt.Errorf("cannot conver object of type %T to metav1.Object", rawObj.Object)
}

info := &resource.Info{
// Client: c.client,
// Mapping: mapping,
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
Object: rawObj.Object,
}
logger.Info("%s:%s.%s/%s", info.Namespace, gvk.Kind, gvk.Group, info.Name)

return helper, info, nil
}
6 changes: 3 additions & 3 deletions pkg/ctl/create/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,13 @@ func doCreateCluster(p *api.ProviderConfig, cfg *api.ClusterConfig, nameArg stri
var kubeconfigContextName string

if writeKubeconfig {
clientConfig, err := ctl.NewClientConfig(cfg, false)
client, err := ctl.NewClient(cfg, false)
if err != nil {
return err
}
kubeconfigContextName = clientConfig.ContextName
kubeconfigContextName = client.ContextName

kubeconfigPath, err = kubeconfig.Write(kubeconfigPath, *clientConfig.Client, setContext)
kubeconfigPath, err = kubeconfig.Write(kubeconfigPath, *client.Config, setContext)
if err != nil {
return errors.Wrap(err, "writing kubeconfig")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/ctl/utils/update_aws_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ func doUpdateAWSNode(p *api.ProviderConfig, cfg *api.ClusterConfig, nameArg stri
return errors.Wrapf(err, "getting credentials for cluster %q", meta.Name)
}

clientSet, err := ctl.NewStdClientSet(cfg)
rawClient, err := ctl.NewRawClient(cfg)
if err != nil {
return err
}

return defaultaddons.UpdateAWSNode(clientSet, false)
return defaultaddons.UpdateAWSNode(rawClient, false)
}
4 changes: 2 additions & 2 deletions pkg/ctl/utils/write_kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ func doWriteKubeconfigCmd(p *api.ProviderConfig, cfg *api.ClusterConfig, nameArg
return err
}

config, err := ctl.NewClientConfig(cfg, false)
client, err := ctl.NewClient(cfg, false)
if err != nil {
return err
}

filename, err := kubeconfig.Write(writeKubeconfigOutputPath, *config.Client, writeKubeconfigSetContext)
filename, err := kubeconfig.Write(writeKubeconfigOutputPath, *client.Config, writeKubeconfigSetContext)
if err != nil {
return errors.Wrap(err, "writing kubeconfig")
}
Expand Down
77 changes: 0 additions & 77 deletions pkg/eks/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/sts"

"github.com/kubernetes-sigs/aws-iam-authenticator/pkg/token"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/kops/pkg/pki"

api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha4"
"github.com/weaveworks/eksctl/pkg/utils"
"github.com/weaveworks/eksctl/pkg/utils/kubeconfig"
)

func (c *ClusterProvider) getKeyPairName(clusterName string, ng *api.NodeGroup, fingerprint *string) string {
Expand Down Expand Up @@ -150,74 +144,3 @@ func (c *ClusterProvider) getUsername() string {
}
return "iam-root-account"
}

// ClientConfig stores information about the client config
type ClientConfig struct {
Client *clientcmdapi.Config
ContextName string
}

// NewClientConfig creates a new client config, if withEmbeddedToken is true
// it will embed the STS token, otherwise it will use authenticator exec plugin
// and ensures that AWS_PROFILE environment variable gets set also
func (c *ClusterProvider) NewClientConfig(spec *api.ClusterConfig, withEmbeddedToken bool) (*ClientConfig, error) {
client, _, contextName := kubeconfig.New(spec, c.getUsername(), "")

config := &ClientConfig{
Client: client,
ContextName: contextName,
}

if withEmbeddedToken {
if err := config.useEmbeddedToken(spec, c.Provider.STS().(*sts.STS)); err != nil {
return nil, err
}
} else {
kubeconfig.AppendAuthenticator(config.Client, spec, utils.DetectAuthenticator(), c.Provider.Profile())
}

return config, nil
}

func (c *ClientConfig) useEmbeddedToken(spec *api.ClusterConfig, sts *sts.STS) error {
gen, err := token.NewGenerator(true)
if err != nil {
return errors.Wrap(err, "could not get token generator")
}

tok, err := gen.GetWithSTS(spec.Metadata.Name, sts)
if err != nil {
return errors.Wrap(err, "could not get token")
}

c.Client.AuthInfos[c.ContextName].Token = tok
return nil
}

// NewClientSet creates a new API client
func (c *ClientConfig) NewClientSet() (*kubernetes.Clientset, error) {
clientConfig, err := clientcmd.NewDefaultClientConfig(*c.Client, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to create API client configuration from client config")
}

client, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
return nil, errors.Wrap(err, "failed to create API client")
}
return client, nil
}

// NewStdClientSet creates a new API client in one go with an embedded STS token, this is most commonly used option
func (c *ClusterProvider) NewStdClientSet(spec *api.ClusterConfig) (*kubernetes.Clientset, error) {
clientConfig, err := c.NewClientConfig(spec, true)
if err != nil {
return nil, errors.Wrap(err, "creating Kubernetes client config with embedded token")
}

clientSet, err := clientConfig.NewClientSet()
if err != nil {
return nil, errors.Wrap(err, "creating Kubernetes client")
}
return clientSet, nil
}
Loading

0 comments on commit d774a26

Please sign in to comment.