config.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package local
  2. import (
  3. "github.com/aws/aws-sdk-go/service/iam"
  4. "github.com/porter-dev/porter/cli/cmd/providers/aws"
  5. "github.com/porter-dev/porter/internal/kubernetes/local"
  6. "k8s.io/client-go/kubernetes"
  7. "k8s.io/client-go/tools/clientcmd"
  8. "github.com/aws/aws-sdk-go/aws/session"
  9. )
  10. // NewDefaultAgent returns an agent using Application Default Credentials. If these are not
  11. // set and the gcloud utility is installed on the machine, this will spawn a setup process
  12. // to link these credentials.
  13. func NewDefaultAgent(kubeconfigPath string, contextName string) (*aws.Agent, error) {
  14. // (1) Construct a local clientset from the AWS context, and use the eksctl authconfigmap package
  15. // to read the current identities of the config map, to make sure user has access. Save the created
  16. // clientset.
  17. rawBytes, err := local.GetKubeconfigFromHost(kubeconfigPath, []string{contextName})
  18. if err != nil {
  19. return nil, err
  20. }
  21. conf, err := clientcmd.NewClientConfigFromBytes(rawBytes)
  22. rawConf, err := conf.RawConfig()
  23. conf = clientcmd.NewDefaultClientConfig(rawConf, &clientcmd.ConfigOverrides{
  24. CurrentContext: contextName,
  25. })
  26. restConf, err := conf.ClientConfig()
  27. if err != nil {
  28. return nil, err
  29. }
  30. clientset, err := kubernetes.NewForConfig(restConf)
  31. if err != nil {
  32. return nil, err
  33. }
  34. sess := session.Must(session.NewSession())
  35. iamSvc := iam.New(sess)
  36. // Return a new agent with AWS session and clientset
  37. return &aws.Agent{
  38. Session: sess,
  39. IAMService: iamSvc,
  40. Clientset: clientset,
  41. }, nil
  42. }