Skip to content

Commit

Permalink
feat: support aws and other OIDC authentication methods kubeconfig (K…
Browse files Browse the repository at this point in the history
  • Loading branch information
CirillaQL committed Jul 31, 2024
1 parent ff640be commit b102d0d
Show file tree
Hide file tree
Showing 11 changed files with 529 additions and 138 deletions.
40 changes: 31 additions & 9 deletions pkg/infra/multicluster/multicluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
metrics "k8s.io/metrics/pkg/client/clientset/versioned"
metricsv1beta1 "k8s.io/metrics/pkg/client/clientset/versioned/typed/metrics/v1beta1"
)
Expand Down Expand Up @@ -164,15 +165,36 @@ func NewConfigFromCluster(c *clusterv1beta1.Cluster) (*restclient.Config, error)
case clusterv1beta1.CredentialTypeX509Certificate:
cfg.CertData = c.Spec.Access.Credential.X509.Certificate
cfg.KeyData = c.Spec.Access.Credential.X509.PrivateKey
case clusterv1beta1.CredentialTypeOIDC:
var env []clientcmdapi.ExecEnvVar
for _, envValue := range c.Spec.Access.Credential.ExecConfig.Env {
tempEnv := clientcmdapi.ExecEnvVar{
Name: envValue.Name,
Value: envValue.Value,
}
env = append(env, tempEnv)
}
cfg.ExecProvider = &clientcmdapi.ExecConfig{
Command: c.Spec.Access.Credential.ExecConfig.Command,
Args: c.Spec.Access.Credential.ExecConfig.Args,
Env: env,
APIVersion: c.Spec.Access.Credential.ExecConfig.APIVersion,
InstallHint: c.Spec.Access.Credential.ExecConfig.InstallHint,
ProvideClusterInfo: c.Spec.Access.Credential.ExecConfig.ProvideClusterInfo,
InteractiveMode: clientcmdapi.ExecInteractiveMode(c.Spec.Access.Credential.ExecConfig.InteractiveMode),
}
}
// ServerName should be set to an empty string when using ExecConfig
if c.Spec.Access.Credential.Type != clusterv1beta1.CredentialTypeOIDC {
u, err := url.Parse(c.Spec.Access.Endpoint)
if err != nil {
return nil, err
}
host, _, err := net.SplitHostPort(u.Host)
if err != nil {
return nil, err
}
cfg.ServerName = host // apiserver may listen on SNI cert
}
u, err := url.Parse(c.Spec.Access.Endpoint)
if err != nil {
return nil, err
}
host, _, err := net.SplitHostPort(u.Host)
if err != nil {
return nil, err
}
cfg.ServerName = host // apiserver may listen on SNI cert
return cfg, nil
}
21 changes: 19 additions & 2 deletions pkg/kubernetes/apis/cluster/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type CredentialType string
const (
CredentialTypeServiceAccountToken CredentialType = "ServiceAccountToken"
CredentialTypeX509Certificate CredentialType = "X509Certificate"
CredentialTypeOIDC CredentialType = "OIDC"
)

// +genclient
Expand Down Expand Up @@ -74,15 +75,31 @@ type ClusterAccess struct {
type ClusterAccessCredential struct {
Type CredentialType `json:"type"`
// +optional
ServiceAccountToken string `json:"serviceAccountToken,omitempty"`
X509 *X509 `json:"x509,omitempty"`
ServiceAccountToken string `json:"serviceAccountToken,omitempty"`
X509 *X509 `json:"x509,omitempty"`
ExecConfig *ExecConfig `json:"execConfig,omitempty"`
}

type X509 struct {
Certificate []byte `json:"certificate"`
PrivateKey []byte `json:"privateKey"`
}

type ExecEnvVar struct {
Name string `json:"name"`
Value string `json:"value"`
}

type ExecConfig struct {
Command string `json:"command"`
Args []string `json:"args"`
Env []ExecEnvVar `json:"env"`
APIVersion string `json:"apiVersion,omitempty"`
InstallHint string `json:"installHint,omitempty"`
ProvideClusterInfo bool `json:"provideClusterInfo"`
InteractiveMode string `json:"interactiveMode,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type ClusterProxyOptions struct {
Expand Down
18 changes: 18 additions & 0 deletions pkg/kubernetes/apis/cluster/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type CredentialType string
const (
CredentialTypeServiceAccountToken CredentialType = "ServiceAccountToken"
CredentialTypeX509Certificate CredentialType = "X509Certificate"
CredentialTypeOIDC CredentialType = "OIDC"
)

// +genclient
Expand Down Expand Up @@ -81,13 +82,30 @@ type ClusterAccessCredential struct {
ServiceAccountToken string `json:"serviceAccountToken,omitempty"`
// +optional
X509 *X509 `json:"x509,omitempty"`
// +optional
ExecConfig *ExecConfig `json:"execConfig,omitempty"`
}

type X509 struct {
Certificate []byte `json:"certificate"`
PrivateKey []byte `json:"privateKey"`
}

type ExecEnvVar struct {
Name string `json:"name"`
Value string `json:"value"`
}

type ExecConfig struct {
Command string `json:"command"`
Args []string `json:"args"`
Env []ExecEnvVar `json:"env"`
APIVersion string `json:"apiVersion,omitempty"`
InstallHint string `json:"installHint,omitempty"`
ProvideClusterInfo bool `json:"provideClusterInfo"`
InteractiveMode string `json:"interactiveMode,omitempty"`
}

// +k8s:conversion-gen:explicit-from=net/url.Values
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

Expand Down
76 changes: 76 additions & 0 deletions pkg/kubernetes/apis/cluster/v1beta1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions pkg/kubernetes/apis/cluster/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions pkg/kubernetes/apis/cluster/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b102d0d

Please sign in to comment.