-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #378 from matprig/fix/325
Make kubeconfig attributes available as resource output
- Loading branch information
Showing
26 changed files
with
11,617 additions
and
44 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
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 |
---|---|---|
@@ -1,14 +1,74 @@ | ||
package ovh | ||
|
||
import "fmt" | ||
import ( | ||
"fmt" | ||
|
||
// getKubeconfig call the kubeconfig endpoint to retreive the kube config file | ||
func getKubeconfig(config *Config, serviceName string, kubeID string) (*string, error) { | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// KubectlConfig is a struct to store the kubeconfig file | ||
// Same as https://github.com/kubernetes/kops/blob/2e84499741471ba67582aa0ba6fa3f2e3bdbe3e8/pkg/kubeconfig/config.go#L19 but with yaml format | ||
type KubectlConfig struct { | ||
Kind string `json:"kind" yaml:"kind"` | ||
ApiVersion string `json:"apiVersion" yaml:"apiVersion"` | ||
CurrentContext string `json:"current-context" yaml:"current-context"` | ||
Clusters []*KubectlClusterWithName `json:"clusters" yaml:"clusters"` | ||
Contexts []*KubectlContextWithName `json:"contexts" yaml:"contexts"` | ||
Users []*KubectlUserWithName `json:"users" yaml:"users"` | ||
Raw *string `json:"-" yaml:"-"` | ||
} | ||
|
||
type KubectlClusterWithName struct { | ||
Name string `json:"name" yaml:"name"` | ||
Cluster KubectlCluster `json:"cluster" yaml:"cluster"` | ||
} | ||
|
||
type KubectlCluster struct { | ||
Server string `json:"server,omitempty" yaml:"server,omitempty"` | ||
CertificateAuthorityData string `json:"certificate-authority-data,omitempty" yaml:"certificate-authority-data,omitempty"` | ||
} | ||
|
||
type KubectlContextWithName struct { | ||
Name string `json:"name" yaml:"name"` | ||
Context KubectlContext `json:"context" yaml:"context"` | ||
} | ||
|
||
type KubectlContext struct { | ||
Cluster string `json:"cluster" yaml:"cluster"` | ||
User string `json:"user" yaml:"user"` | ||
} | ||
|
||
type KubectlUserWithName struct { | ||
Name string `json:"name" yaml:"name"` | ||
User KubectlUser `json:"user" yaml:"user"` | ||
} | ||
|
||
type KubectlUser struct { | ||
ClientCertificateData string `json:"client-certificate-data,omitempty" yaml:"client-certificate-data,omitempty"` | ||
ClientKeyData string `json:"client-key-data,omitempty" yaml:"client-key-data,omitempty"` | ||
Password string `json:"password,omitempty" yaml:"password,omitempty"` | ||
Username string `json:"username,omitempty" yaml:"username,omitempty"` | ||
Token string `json:"token,omitempty" yaml:"token,omitempty"` | ||
} | ||
|
||
// getKubeconfig call the kubeconfig endpoint to retrieve the kube config file | ||
func getKubeconfig(config *Config, serviceName string, kubeID string) (*KubectlConfig, error) { | ||
kubeconfigRaw := CloudProjectKubeKubeConfigResponse{} | ||
endpoint := fmt.Sprintf("/cloud/project/%s/kube/%s/kubeconfig", serviceName, kubeID) | ||
err := config.OVHClient.Post(endpoint, nil, &kubeconfigRaw) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &kubeconfigRaw.Content, nil | ||
|
||
return parseKubeconfig(&kubeconfigRaw) | ||
} | ||
|
||
func parseKubeconfig(kubeconfigRaw *CloudProjectKubeKubeConfigResponse) (*KubectlConfig, error) { | ||
var kubeconfig KubectlConfig | ||
if err := yaml.Unmarshal([]byte(kubeconfigRaw.Content), &kubeconfig); err != nil { | ||
return nil, err | ||
} | ||
|
||
kubeconfig.Raw = &kubeconfigRaw.Content | ||
return &kubeconfig, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package ovh | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_parseKubeconfig(t *testing.T) { | ||
type args struct { | ||
kubeconfigRaw *CloudProjectKubeKubeConfigResponse | ||
} | ||
|
||
expectedKubeconfigRaw := `apiVersion: v1 | ||
clusters: | ||
- cluster: | ||
certificate-authority-data: Zm9vCg== | ||
server: https://foo.bar | ||
name: foo | ||
contexts: | ||
- context: | ||
cluster: foo | ||
user: kubernetes-admin-foo | ||
name: kubernetes-admin@foo | ||
current-context: kubernetes-admin@foo | ||
kind: Config | ||
preferences: {} | ||
users: | ||
- name: kubernetes-admin-foo | ||
user: | ||
client-certificate-data: Zm9vCg== | ||
client-key-data: Zm9vCg== | ||
` | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want *KubectlConfig | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "expected kubeconfig content", | ||
args: args{ | ||
kubeconfigRaw: &CloudProjectKubeKubeConfigResponse{ | ||
Content: expectedKubeconfigRaw, | ||
}, | ||
}, | ||
want: &KubectlConfig{ | ||
Kind: "Config", | ||
ApiVersion: "v1", | ||
CurrentContext: "kubernetes-admin@foo", | ||
Clusters: []*KubectlClusterWithName{ | ||
{ | ||
Name: "foo", | ||
Cluster: KubectlCluster{Server: "https://foo.bar", CertificateAuthorityData: "Zm9vCg=="}, | ||
}, | ||
}, | ||
Contexts: []*KubectlContextWithName{ | ||
{ | ||
Name: "kubernetes-admin@foo", | ||
Context: KubectlContext{Cluster: "foo", User: "kubernetes-admin-foo"}, | ||
}, | ||
}, | ||
Users: []*KubectlUserWithName{ | ||
{ | ||
Name: "kubernetes-admin-foo", | ||
User: KubectlUser{ClientCertificateData: "Zm9vCg==", ClientKeyData: "Zm9vCg=="}}, | ||
}, | ||
Raw: &expectedKubeconfigRaw, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := parseKubeconfig(tt.args.kubeconfigRaw) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("parseKubeconfig() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("parseKubeconfig() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.