-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9266bb7
commit ad52882
Showing
6 changed files
with
226 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package connection | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/flanksource/duty/context" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
// +kubebuilder:object:generate=true | ||
type EKSConnection struct { | ||
AWSConnection `json:",inline" yaml:",inline"` | ||
|
||
Cluster string `json:"cluster"` | ||
} | ||
|
||
func (t *EKSConnection) Populate(ctx ConnectionContext) error { | ||
return t.AWSConnection.Populate(ctx) | ||
} | ||
|
||
func (t *EKSConnection) KubernetesClient(ctx context.Context) (kubernetes.Interface, *rest.Config, error) { | ||
awsConfig, err := t.AWSConnection.Client(ctx) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
eksEndpoint, ca, err := eksClusterDetails(ctx, t.Cluster, awsConfig) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
token, err := getEKSToken(ctx, t.Cluster, awsConfig) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to get token for EKS: %w", err) | ||
} | ||
|
||
restConfig := &rest.Config{ | ||
Host: eksEndpoint, | ||
BearerToken: token, | ||
TLSClientConfig: rest.TLSClientConfig{ | ||
CAData: ca, | ||
}, | ||
} | ||
|
||
clientset, err := kubernetes.NewForConfig(restConfig) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return clientset, restConfig, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package connection | ||
|
||
import ( | ||
"crypto/tls" | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/flanksource/duty/context" | ||
container "google.golang.org/api/container/v1" | ||
"google.golang.org/api/option" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
// +kubebuilder:object:generate=true | ||
type GKEConnection struct { | ||
GCPConnection `json:",inline" yaml:",inline"` | ||
|
||
ProjectID string `json:"projectID"` | ||
Zone string `json:"zone"` | ||
Cluster string `json:"cluster"` | ||
} | ||
|
||
func (t *GKEConnection) Populate(ctx ConnectionContext) error { | ||
return t.GCPConnection.HydrateConnection(ctx) | ||
} | ||
|
||
func (t *GKEConnection) Validate() *GKEConnection { | ||
if t == nil { | ||
return &GKEConnection{} | ||
} | ||
|
||
return t | ||
} | ||
|
||
func (t *GKEConnection) Client(ctx context.Context) (*container.Service, error) { | ||
t = t.Validate() | ||
|
||
var clientOpts []option.ClientOption | ||
|
||
if t.Endpoint != "" { | ||
clientOpts = append(clientOpts, option.WithEndpoint(t.Endpoint)) | ||
} | ||
|
||
if t.SkipTLSVerify { | ||
insecureHTTPClient := &http.Client{ | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
}, | ||
} | ||
|
||
clientOpts = append(clientOpts, option.WithHTTPClient(insecureHTTPClient)) | ||
} | ||
|
||
if t.Credentials != nil && !t.Credentials.IsEmpty() { | ||
credential, err := ctx.GetEnvValueFromCache(*t.Credentials, ctx.GetNamespace()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
clientOpts = append(clientOpts, option.WithCredentialsJSON([]byte(credential))) | ||
} else { | ||
clientOpts = append(clientOpts, option.WithoutAuthentication()) | ||
} | ||
|
||
svc, err := container.NewService(ctx, clientOpts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return svc, nil | ||
} | ||
|
||
func (t *GKEConnection) KubernetesClient(ctx context.Context) (kubernetes.Interface, *rest.Config, error) { | ||
containerService, err := t.Client(ctx) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
clusterName := fmt.Sprintf("projects/%s/locations/%s/clusters/%s", t.ProjectID, t.Zone, t.Cluster) | ||
cluster, err := containerService.Projects.Locations.Clusters.Get(clusterName).Context(ctx).Do() | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to get cluster: %w", err) | ||
} | ||
|
||
creds, err := t.GCPConnection.Creds(ctx, container.CloudPlatformScope) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
tokenSource := creds.TokenSource | ||
token, err := tokenSource.Token() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
ca, err := base64.URLEncoding.DecodeString(cluster.MasterAuth.ClusterCaCertificate) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("unable to presign URL: %v", err) | ||
} | ||
|
||
restConfig := &rest.Config{ | ||
Host: "https://" + cluster.Endpoint, | ||
BearerToken: token.AccessToken, | ||
TLSClientConfig: rest.TLSClientConfig{ | ||
CAData: ca, | ||
}, | ||
} | ||
|
||
clientset, err := kubernetes.NewForConfig(restConfig) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return clientset, restConfig, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.