Skip to content

Commit

Permalink
Update go libraries (#97)
Browse files Browse the repository at this point in the history
* Update go libraries

Updated kubernetes libraries to 1.28.6
Updated kubevirt go-client to 1.1.1
Updated containerized data importer to 1.58.1

Switched virtcli from using the one provided by kubevirt to
a generated one using gen-client. This allows us to be independent
of the kubernetes version used by kubevirt. Also generate go-client
for containerized data importer to be independent of their kubernetes
version as well.

Signed-off-by: Alexander Wels <[email protected]>

* Fix generating clients to install gen-client
Fix linter errors

Signed-off-by: Alexander Wels <[email protected]>

* Switch port-forward to using virtclt binary instead of
of having it as a library. No need to maintain the library
this way.

Moved port-forward to the before suite and after suite, so
it will only have to forward the ports once for the whole test
suite.

Added function that finds a free port to use for port-forwarding.

Signed-off-by: Alexander Wels <[email protected]>

* Addressed review comments.

- Removed kubevirt go-client and vendoring.
- Removed unused function

Signed-off-by: Alexander Wels <[email protected]>

---------

Signed-off-by: Alexander Wels <[email protected]>
  • Loading branch information
awels authored Feb 8, 2024
1 parent cd33114 commit 7aa580f
Show file tree
Hide file tree
Showing 1,724 changed files with 147,353 additions and 110,747 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*.so
*.dylib
kubevirt-csi-driver

*.code-workspace
#k8s e2e suite
.kvcluster-kubeconfig-e2e
capk.pem
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ include $(addprefix ./vendor/github.com/openshift/build-machinery-go/make/, \
# You can list all codegen related variables by:
# $ make -n --print-data-base | grep ^CODEGEN
.PHONY: image-build
image-build:
image-build: generate
source ./hack/cri-bin.sh && \
$$CRI_BIN build -t $(IMAGE_REF) --build-arg git_sha=$(SHA) .

Expand Down Expand Up @@ -113,3 +113,7 @@ e2e-test: build-e2e-test ## run e2e tests
.PHONY: sanity-test
sanity-test:
./hack/run-sanity.sh

.PHONY: generate
generate:
./hack/generate_clients.sh
29 changes: 4 additions & 25 deletions cmd/kubevirt-csi-driver/kubevirt-csi-driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"context"
"flag"
"fmt"
"math/rand"
"os"
"strings"
"time"

"gopkg.in/yaml.v2"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -37,30 +35,11 @@ var (
)

func init() {
err := flag.Set("logtostderr", "true")
if err != nil {
panic(fmt.Sprintf("can't set the logtostderr flags; %s", err.Error()))
}
// make glog and klog coexist
klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
klog.InitFlags(klogFlags)

// Sync the glog and klog flags.
flag.CommandLine.Visit(func(f1 *flag.Flag) {
f2 := klogFlags.Lookup(f1.Name)
if f2 != nil {
value := f1.Value.String()
err = f2.Value.Set(value)
if err != nil {
panic(fmt.Sprintf("can't set the %s flags; %s", f1.Name, err.Error()))
}
}
})
klog.InitFlags(nil)
}

func main() {
flag.Parse()
rand.Seed(time.Now().UnixNano())
handle()
os.Exit(0)
}
Expand All @@ -71,7 +50,7 @@ func handle() {
var identityClientset *kubernetes.Clientset

if service.VendorVersion == "" {
klog.Fatalf("VendorVersion must be set at compile time")
klog.Fatal("VendorVersion must be set at compile time")
}
klog.V(2).Infof("Driver vendor %v %v", service.VendorName, service.VendorVersion)

Expand Down Expand Up @@ -130,7 +109,7 @@ func handle() {

infraClusterLabelsMap := parseLabels()
var storageClassEnforcement util.StorageClassEnforcement
//prase yaml
//parse yaml
if infraStorageClassEnforcement == "" {
storageClassEnforcement = util.StorageClassEnforcement{
AllowAll: true,
Expand All @@ -139,7 +118,7 @@ func handle() {
} else {
err := yaml.Unmarshal([]byte(infraStorageClassEnforcement), &storageClassEnforcement)
if err != nil {
klog.Fatalf("Failed to parse infra-storage-class-enforcement", err)
klog.Fatalf("Failed to parse infra-storage-class-enforcement %v", err)
}
}

Expand Down
81 changes: 32 additions & 49 deletions e2e/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ package e2e_test

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"
"strconv"
"strings"

"github.com/golang/glog"
. "github.com/onsi/gomega"
kubevirtv1 "kubevirt.io/api/core/v1"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/klog/v2"
)

// RunCmd function executes a command, and returns STDOUT and STDERR bytes
Expand All @@ -42,16 +41,18 @@ type tenantClusterAccess struct {
namespace string
tenantKubeconfigFile string
isForwarding bool
tenantApiport int
}

func newTenantClusterAccess(namespace string, tenantKubeconfigFile string) tenantClusterAccess {
func newTenantClusterAccess(namespace, tenantKubeconfigFile string, apiPort int) tenantClusterAccess {
return tenantClusterAccess{
namespace: namespace,
tenantKubeconfigFile: tenantKubeconfigFile,
tenantApiport: apiPort,
}
}

func (t *tenantClusterAccess) generateClient() (*kubernetes.Clientset, error) {
func (t *tenantClusterAccess) generateTenantClient() (*kubernetes.Clientset, error) {
overrides := &clientcmd.ConfigOverrides{}
if _, err := os.Stat(t.tenantKubeconfigFile); errors.Is(err, os.ErrNotExist) {
localPort := t.listener.Addr().(*net.TCPAddr).Port
Expand All @@ -74,7 +75,6 @@ func (t *tenantClusterAccess) generateClient() (*kubernetes.Clientset, error) {
if err != nil {
return nil, err
}

return kubernetes.NewForConfig(restConfig)
}

Expand All @@ -96,36 +96,12 @@ func (t *tenantClusterAccess) startForwardingTenantAPI() error {
return err
}

vmiName, err := t.findControlPlaneVMIName()
if err != nil {
return err
}

t.isForwarding = true
go t.waitForConnection(vmiName, t.namespace)
go t.waitForConnection()

return nil
}

func (t *tenantClusterAccess) findControlPlaneVMIName() (string, error) {
vmiList, err := virtClient.VirtualMachineInstance(t.namespace).List(context.TODO(), &metav1.ListOptions{})
if err != nil {
return "", err
}

var chosenVMI *kubevirtv1.VirtualMachineInstance
for _, vmi := range vmiList.Items {
if strings.Contains(vmi.Name, "-control-plane") {
chosenVMI = &vmi
break
}
}
if chosenVMI == nil {
return "", fmt.Errorf("Couldn't find controlplane vmi in namespace %s", t.namespace)
}
return chosenVMI.Name, nil
}

func (t *tenantClusterAccess) stopForwardingTenantAPI() error {
if !t.isForwarding {
return nil
Expand All @@ -134,25 +110,24 @@ func (t *tenantClusterAccess) stopForwardingTenantAPI() error {
return t.listener.Close()
}

func (t *tenantClusterAccess) waitForConnection(name, namespace string) {
for {
conn, err := t.listener.Accept()
if err != nil {
glog.Errorln("error accepting connection:", err)
return
}
stream, err := virtClient.VirtualMachineInstance(namespace).PortForward(name, 6443, "tcp")
if err != nil {
glog.Errorf("can't access vmi %s/%s: %v", namespace, name, err)
return
}
go t.handleConnection(conn, stream.AsConn())
func (t *tenantClusterAccess) waitForConnection() {
conn, err := t.listener.Accept()
if err != nil {
klog.Errorln("error accepting connection:", err)
return
}

proxy, err := net.Dial("tcp", net.JoinHostPort("localhost", strconv.Itoa(t.tenantApiport)))
if err != nil {
klog.Errorf("unable to connect to local port-forward: %v", err)
return
}
go t.handleConnection(conn, proxy)
}

// handleConnection copies data between the local connection and the stream to
// the remote server.
func (t *tenantClusterAccess) handleConnection(local, remote net.Conn) {
// the remote server. It closes the local and remote connections when done.
func (t *tenantClusterAccess) handleConnection(local, remote io.ReadWriteCloser) {
defer local.Close()
defer remote.Close()
errs := make(chan error, 2)
Expand All @@ -170,17 +145,25 @@ func (t *tenantClusterAccess) handleConnection(local, remote net.Conn) {

func (t *tenantClusterAccess) handleConnectionError(err error) {
if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
glog.Errorf("error handling portForward connection: %v", err)
klog.Errorf("error handling portForward connection: %v", err)
}
}

func generateInfraClient() (*kubernetes.Clientset, error) {
func generateInfraRestConfig() (*rest.Config, error) {
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: InfraKubeConfig}, &clientcmd.ConfigOverrides{})
restConfig, err := clientConfig.ClientConfig()
if err != nil {
return nil, err
}
return restConfig, nil
}

func generateInfraClient() (*kubernetes.Clientset, error) {
restConfig, err := generateInfraRestConfig()
if err != nil {
return nil, err
}

return kubernetes.NewForConfig(restConfig)
}
43 changes: 31 additions & 12 deletions e2e/create-pvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"

"k8s.io/client-go/tools/clientcmd"
kubecli "kubevirt.io/csi-driver/pkg/generated/kubevirt/client-go/clientset/versioned"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/spf13/pflag"
Expand All @@ -20,10 +23,26 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/kubernetes"
"kubevirt.io/client-go/kubecli"
)

var virtClient kubecli.KubevirtClient
var virtClient *kubecli.Clientset

func defaultInfraClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig

flags.StringVar(&loadingRules.ExplicitPath, "infra-kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.")

overrides := &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults}

flagNames := clientcmd.RecommendedConfigOverrideFlags("")
flagNames.ClusterOverrideFlags.APIServer.ShortName = "s"

clientcmd.BindOverrideFlags(overrides, flags, flagNames)
clientConfig := clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, os.Stdin)

return clientConfig
}

var _ = Describe("CreatePVC", func() {

Expand All @@ -32,7 +51,7 @@ var _ = Describe("CreatePVC", func() {
var tmpDir string
var tenantClient *kubernetes.Clientset
var infraClient *kubernetes.Clientset
var tenantKubeconfigFile string
var infraKubeconfigFile string
var tenantAccessor tenantClusterAccess
var namespace string

Expand All @@ -43,20 +62,22 @@ var _ = Describe("CreatePVC", func() {
tmpDir, err = os.MkdirTemp(WorkingDir, "pvc-creation-tests")
Expect(err).ToNot(HaveOccurred())

tenantKubeconfigFile = filepath.Join(tmpDir, "tenant-kubeconfig.yaml")
infraKubeconfigFile = filepath.Join(tmpDir, "infra-kubeconfig.yaml")

clientConfig := kubecli.DefaultClientConfig(&pflag.FlagSet{})
virtClient, err = kubecli.GetKubevirtClientFromClientConfig(clientConfig)
clientConfig := defaultInfraClientConfig(&pflag.FlagSet{})
cfg, err := clientConfig.ClientConfig()
Expect(err).ToNot(HaveOccurred())
virtClient = kubecli.NewForConfigOrDie(cfg)
Expect(err).ToNot(HaveOccurred())

tenantAccessor = newTenantClusterAccess("kvcluster", tenantKubeconfigFile)
tenantAccessor = newTenantClusterAccess("kvcluster", infraKubeconfigFile, tenantApiPort)

err = tenantAccessor.startForwardingTenantAPI()
Expect(err).ToNot(HaveOccurred())
} else {
tenantAccessor = newTenantClusterAccess(InfraClusterNamespace, TenantKubeConfig)
tenantAccessor = newTenantClusterAccess(InfraClusterNamespace, TenantKubeConfig, tenantApiPort)
}
tenantClient, err = tenantAccessor.generateClient()
tenantClient, err = tenantAccessor.generateTenantClient()
Expect(err).ToNot(HaveOccurred())

namespace = "e2e-test-create-pvc-" + rand.String(6)
Expand All @@ -75,9 +96,7 @@ var _ = Describe("CreatePVC", func() {

AfterEach(func() {
_ = tenantClient.CoreV1().Namespaces().Delete(context.Background(), namespace, metav1.DeleteOptions{})
if len(TenantKubeConfig) == 0 {
_ = tenantAccessor.stopForwardingTenantAPI()
}
_ = tenantAccessor.stopForwardingTenantAPI()
_ = os.RemoveAll(tmpDir)
})

Expand Down
Loading

0 comments on commit 7aa580f

Please sign in to comment.