Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor handling of Kubeconfig for KubeVirt #1301

Merged
merged 1 commit into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/kubevirt-machinedeployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ spec:
cloudProviderSpec:
auth:
kubeconfig:
value: '<< KUBECONFIG >>'
# Can also be set via the env var 'KUBEVIRT_KUBECONFIG' on the machine-controller
# If specified directly, this value should be a base64 encoded kubeconfig in either yaml or json format.
value: "<< KUBECONFIG >>"
virtualMachine:
template:
cpus: "1"
Expand All @@ -44,7 +46,7 @@ spec:
type: "" # Allowed values: "", "soft", "hard"
key: "foo"
values:
- bar
- bar
# Can also be `centos`, must align with he configured registryImage above
operatingSystem: "ubuntu"
operatingSystemSpec:
Expand Down
35 changes: 29 additions & 6 deletions pkg/cloudprovider/provider/kubevirt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package kubevirt

import (
"context"
"encoding/base64"
"errors"
"fmt"
"net/url"
Expand Down Expand Up @@ -199,10 +200,36 @@ func (p *provider) getConfig(provSpec clusterv1alpha1.ProviderSpec) (*Config, *p
}

config := Config{}
config.Kubeconfig, err = p.configVarResolver.GetConfigVarStringValueOrEnv(rawConfig.Auth.Kubeconfig, "KUBEVIRT_KUBECONFIG")

// Kubeconfig was specified directly in the Machine/MachineDeployment CR. In this case we need to ensure that the value is base64 encoded.
if rawConfig.Auth.Kubeconfig.Value != "" {
val, err := base64.StdEncoding.DecodeString(rawConfig.Auth.Kubeconfig.Value)
if err != nil {
// An error here means that this is not a valid base64 string
// We can be more explicit here with the error for visibility. Webhook will return this error if we hit this scenario.
return nil, nil, fmt.Errorf("failed to decode base64 encoded kubeconfig. Expected value is a base64 encoded Kubeconfig in JSON or YAML format: %w", err)
}
config.Kubeconfig = string(val)
} else {
// Environment variable or secret reference was used for providing the value of kubeconfig
// We have to be lenient in this case and allow unencoded values as well.
config.Kubeconfig, err = p.configVarResolver.GetConfigVarStringValueOrEnv(rawConfig.Auth.Kubeconfig, "KUBEVIRT_KUBECONFIG")
if err != nil {
return nil, nil, fmt.Errorf(`failed to get value of "kubeconfig" field: %w`, err)
}
val, err := base64.StdEncoding.DecodeString(config.Kubeconfig)
// We intentionally ignore errors here with an assumption that an unencoded YAML or JSON must have been passed on
// in this case.
if err == nil {
config.Kubeconfig = string(val)
}
}

config.RestConfig, err = clientcmd.RESTConfigFromKubeConfig([]byte(config.Kubeconfig))
if err != nil {
return nil, nil, fmt.Errorf(`failed to get value of "kubeconfig" field: %w`, err)
return nil, nil, fmt.Errorf("failed to decode kubeconfig: %w", err)
}

config.CPUs, err = p.configVarResolver.GetConfigVarStringValue(rawConfig.VirtualMachine.Template.CPUs)
if err != nil {
return nil, nil, fmt.Errorf(`failed to get value of "cpus" field: %w`, err)
Expand Down Expand Up @@ -232,10 +259,6 @@ func (p *provider) getConfig(provSpec clusterv1alpha1.ProviderSpec) (*Config, *p
if err != nil {
return nil, nil, fmt.Errorf(`failed to get value of "storageClassName" field: %w`, err)
}
config.RestConfig, err = clientcmd.RESTConfigFromKubeConfig([]byte(config.Kubeconfig))
if err != nil {
return nil, nil, fmt.Errorf("failed to decode kubeconfig: %w", err)
}
config.FlavorName, err = p.configVarResolver.GetConfigVarStringValue(rawConfig.VirtualMachine.Flavor.Name)
if err != nil {
return nil, nil, fmt.Errorf(`failed to get value of "flavor.name" field: %w`, err)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/provisioning/all_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func addCAToDeployment(ctx context.Context, client ctrlruntimeclient.Client, nam
func TestKubevirtProvisioningE2E(t *testing.T) {
t.Parallel()

kubevirtKubeconfig := os.Getenv("KUBEVIRT_E2E_TESTS_KUBECONFIG_JSON")
kubevirtKubeconfig := os.Getenv("KUBEVIRT_E2E_TESTS_KUBECONFIG")

if kubevirtKubeconfig == "" {
t.Fatalf("Unable to run kubevirt tests, KUBEVIRT_E2E_TESTS_KUBECONFIG must be set")
Expand Down