Skip to content

Commit

Permalink
Add VirtualMachineWebConsoleRequest in web-console-validator
Browse files Browse the repository at this point in the history
  • Loading branch information
dilyar85 committed Dec 10, 2024
1 parent 3de1130 commit 7970b1c
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 153 deletions.
30 changes: 27 additions & 3 deletions cmd/web-console-validator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"os"
"strconv"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
klog "k8s.io/klog/v2"
"k8s.io/klog/v2/textlogger"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"

vmopv1a1 "github.com/vmware-tanzu/vm-operator/api/v1alpha1"
vmopv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha3"
"github.com/vmware-tanzu/vm-operator/pkg"
"github.com/vmware-tanzu/vm-operator/pkg/webconsolevalidation"
)
Expand Down Expand Up @@ -56,12 +58,34 @@ func main() {

flag.Parse()

restConfig, err := rest.InClusterConfig()
if err != nil {
logger.Error(err, "Failed to get Kubernetes in-cluster config")
os.Exit(1)
}

scheme := runtime.NewScheme()
if err := vmopv1.AddToScheme(scheme); err != nil {
logger.Error(err, "Failed to add vm-operator v1alpha3 scheme")
os.Exit(1)
}
// NOTE: In v1a1 this CRD has a different name - WebConsoleRequest - so this
// is still required until we stop supporting v1a1.
if err := vmopv1a1.AddToScheme(scheme); err != nil {
logger.Error(err, "Failed to add vm-operator v1alpha1 scheme")
os.Exit(1)
}

client, err := ctrlclient.New(restConfig, ctrlclient.Options{Scheme: scheme})
if err != nil {
logger.Error(err, "Failed to initialize controller-runtime client")
os.Exit(1)
}

server, err := webconsolevalidation.NewServer(
":"+strconv.Itoa(*serverPort),
*serverPath,
rest.InClusterConfig,
vmopv1a1.AddToScheme,
ctrlclient.New,
client,
)
if err != nil {
logger.Error(err, "Failed to initialize web-console validation server")
Expand Down
5 changes: 2 additions & 3 deletions controllers/virtualmachinewebconsolerequest/controllers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 VMware, Inc. All Rights Reserved.
// Copyright (c) 2023-2024 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package virtualmachinewebconsolerequest
Expand All @@ -7,13 +7,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest/v1alpha1"
"github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest/v1alpha2"
pkgctx "github.com/vmware-tanzu/vm-operator/pkg/context"
)

// AddToManager adds the controller to the provided manager.
func AddToManager(ctx *pkgctx.ControllerManagerContext, mgr manager.Manager) error {
if err := v1alpha2.AddToManager(ctx, mgr); err != nil {
if err := addToManager(ctx, mgr); err != nil {
return err
}
// NOTE: In v1a1 this CRD has a different name - WebConsoleRequest - so this is
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2022-2024 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package v1alpha2
package virtualmachinewebconsolerequest

import (
"context"
Expand Down Expand Up @@ -32,8 +32,8 @@ const (
UUIDLabelKey = "vmoperator.vmware.com/webconsolerequest-uuid"
)

// AddToManager adds this package's controller to the provided manager.
func AddToManager(ctx *pkgctx.ControllerManagerContext, mgr manager.Manager) error {
// addToManager adds this package's controller to the provided manager.
func addToManager(ctx *pkgctx.ControllerManagerContext, mgr manager.Manager) error {
var (
controlledType = &vmopv1.VirtualMachineWebConsoleRequest{}
controlledTypeName = reflect.TypeOf(controlledType).Elem().Name()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2022-2024 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package v1alpha2_test
package virtualmachinewebconsolerequest_test

import (
"context"
Expand All @@ -16,7 +16,7 @@ import (
"k8s.io/apimachinery/pkg/types"

vmopv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha3"
webconsolerequest "github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest/v1alpha2"
"github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest"
"github.com/vmware-tanzu/vm-operator/pkg/constants/testlabels"
proxyaddr "github.com/vmware-tanzu/vm-operator/pkg/util/kube/proxyaddr"
"github.com/vmware-tanzu/vm-operator/test/builder"
Expand Down Expand Up @@ -143,8 +143,8 @@ func intgTestsReconcile() {

Expect(wcr.Status.ProxyAddr).To(Equal("192.168.0.1"))
Expect(wcr.Status.Response).To(Equal(ticket))
Expect(wcr.Status.ExpiryTime.Time).To(BeTemporally("~", time.Now(), webconsolerequest.DefaultExpiryTime))
Expect(wcr.Labels).To(HaveKeyWithValue(webconsolerequest.UUIDLabelKey, string(wcr.UID)))
Expect(wcr.Status.ExpiryTime.Time).To(BeTemporally("~", time.Now(), virtualmachinewebconsolerequest.DefaultExpiryTime))
Expect(wcr.Labels).To(HaveKeyWithValue(virtualmachinewebconsolerequest.UUIDLabelKey, string(wcr.UID)))
})
})
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2022 VMware, Inc. All Rights Reserved.
// Copyright (c) 2022-2024 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package v1alpha2_test
package virtualmachinewebconsolerequest_test

import (
"testing"
Expand All @@ -10,7 +10,7 @@ import (

ctrlmgr "sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest/v1alpha2"
"github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest"
pkgcfg "github.com/vmware-tanzu/vm-operator/pkg/config"
pkgctx "github.com/vmware-tanzu/vm-operator/pkg/context"
providerfake "github.com/vmware-tanzu/vm-operator/pkg/providers/fake"
Expand All @@ -21,7 +21,7 @@ var intgFakeVMProvider = providerfake.NewVMProvider()

var suite = builder.NewTestSuiteForControllerWithContext(
pkgcfg.NewContextWithDefaultConfig(),
v1alpha2.AddToManager,
virtualmachinewebconsolerequest.AddToManager,
func(ctx *pkgctx.ControllerManagerContext, _ ctrlmgr.Manager) error {
ctx.VMProvider = intgFakeVMProvider
return nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2022-2024 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package v1alpha2_test
package virtualmachinewebconsolerequest_test

import (
"context"
Expand All @@ -15,7 +15,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

vmopv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha3"
webconsolerequest "github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest/v1alpha2"
"github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest"
appv1a1 "github.com/vmware-tanzu/vm-operator/external/appplatform/api/v1alpha1"
pkgcfg "github.com/vmware-tanzu/vm-operator/pkg/config"
"github.com/vmware-tanzu/vm-operator/pkg/constants/testlabels"
Expand All @@ -40,7 +40,7 @@ func unitTestsReconcile() {
ctx *builder.UnitTestContextForController
fakeVMProvider *providerfake.VMProvider

reconciler *webconsolerequest.Reconciler
reconciler *virtualmachinewebconsolerequest.Reconciler
wcrCtx *pkgctx.WebConsoleRequestContextV1
wcr *vmopv1.VirtualMachineWebConsoleRequest
vm *vmopv1.VirtualMachine
Expand Down Expand Up @@ -84,7 +84,7 @@ func unitTestsReconcile() {

JustBeforeEach(func() {
ctx = suite.NewUnitTestContextForController(initObjects...)
reconciler = webconsolerequest.NewReconciler(
reconciler = virtualmachinewebconsolerequest.NewReconciler(
ctx,
ctx.Client,
ctx.Logger,
Expand Down Expand Up @@ -130,9 +130,9 @@ func unitTestsReconcile() {

Expect(wcrCtx.WebConsoleRequest.Status.ProxyAddr).To(Equal("dummy-proxy-ip"))
Expect(wcrCtx.WebConsoleRequest.Status.Response).To(Equal(ticket))
Expect(wcrCtx.WebConsoleRequest.Status.ExpiryTime.Time).To(BeTemporally("~", time.Now(), webconsolerequest.DefaultExpiryTime))
Expect(wcrCtx.WebConsoleRequest.Status.ExpiryTime.Time).To(BeTemporally("~", time.Now(), virtualmachinewebconsolerequest.DefaultExpiryTime))
// Checking the label key only because UID will not be set to a resource during unit test.
Expect(wcrCtx.WebConsoleRequest.Labels).To(HaveKey(webconsolerequest.UUIDLabelKey))
Expect(wcrCtx.WebConsoleRequest.Labels).To(HaveKey(virtualmachinewebconsolerequest.UUIDLabelKey))
})
})

Expand Down
44 changes: 21 additions & 23 deletions pkg/webconsolevalidation/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,27 @@ import (
"net/http"
"time"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"

vmopv1a1 "github.com/vmware-tanzu/vm-operator/api/v1alpha1"
"github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest/v1alpha1"
vmopv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha3"
)

const UUIDLabelKey = "vmoperator.vmware.com/webconsolerequest-uuid"

// Server represents a web console validation server.
type Server struct {
Addr, Path string
KubeClient ctrlclient.Client
}

// NewServer creates a new web console validation server.
func NewServer(
addr, path string,
inClusterConfigFunc func() (*rest.Config, error),
addToSchemeFunc func(*runtime.Scheme) error,
newClientFunc func(*rest.Config, ctrlclient.Options) (ctrlclient.Client, error)) (*Server, error) {
func NewServer(addr, path string, client ctrlclient.Client) (*Server, error) {
if addr == "" || path == "" {
return nil, errors.New("server addr and path cannot be empty")
}

// Init Kubernetes client.
restConfig, err := inClusterConfigFunc()
if err != nil {
return nil, err
}
scheme := runtime.NewScheme()
if err := addToSchemeFunc(scheme); err != nil {
return nil, err
}
client, err := newClientFunc(restConfig, ctrlclient.Options{Scheme: scheme})
if err != nil {
return nil, err
}

return &Server{
Addr: addr,
Path: path,
Expand Down Expand Up @@ -107,10 +89,26 @@ func isResourceFound(
uuid, namespace string,
kubeClient ctrlclient.Client) (bool, error) {
labelSelector := ctrlclient.MatchingLabels{
v1alpha1.UUIDLabelKey: uuid,
UUIDLabelKey: uuid,
}

// TODO: Use an Informer to avoid hitting the API server for every request.
vmwcrObjectList := &vmopv1.VirtualMachineWebConsoleRequestList{}
if err := kubeClient.List(
ctx,
vmwcrObjectList,
ctrlclient.InNamespace(namespace),
labelSelector,
); err != nil {
return false, err
}

if len(vmwcrObjectList.Items) > 0 {
return true, nil
}

// NOTE: In v1a1 this CRD has a different name - WebConsoleRequest - so this
// is still required until we stop supporting v1a1.
wcrObjectList := &vmopv1a1.WebConsoleRequestList{}
if err := kubeClient.List(
ctx,
Expand Down
Loading

0 comments on commit 7970b1c

Please sign in to comment.