Skip to content

Commit

Permalink
Add visibility API integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Feb 1, 2024
1 parent a65e70c commit 79cd7fc
Show file tree
Hide file tree
Showing 4 changed files with 585 additions and 11 deletions.
3 changes: 2 additions & 1 deletion pkg/visibility/api/rest/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"sigs.k8s.io/kueue/apis/visibility/v1alpha1"
"sigs.k8s.io/kueue/pkg/util/priority"
"sigs.k8s.io/kueue/pkg/workload"
)

Expand All @@ -39,7 +40,7 @@ func newPendingWorkload(wlInfo *workload.Info, positionInLq int32, positionInCq
CreationTimestamp: wlInfo.Obj.CreationTimestamp,
},
PositionInClusterQueue: int32(positionInCq),
Priority: *wlInfo.Obj.Spec.Priority,
Priority: priority.Priority(wlInfo.Obj),
LocalQueueName: wlInfo.Obj.Spec.QueueName,
PositionInLocalQueue: positionInLq,
}
Expand Down
28 changes: 18 additions & 10 deletions pkg/visibility/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ import (
"net"
"strings"

"sigs.k8s.io/kueue/apis/visibility/v1alpha1"
generatedopenapi "sigs.k8s.io/kueue/apis/visibility/v1alpha1/openapi"
"sigs.k8s.io/kueue/pkg/queue"
"sigs.k8s.io/kueue/pkg/visibility/api"

openapinamer "k8s.io/apiserver/pkg/endpoints/openapi"
genericapiserver "k8s.io/apiserver/pkg/server"
genericoptions "k8s.io/apiserver/pkg/server/options"
"k8s.io/client-go/pkg/version"
_ "k8s.io/component-base/metrics/prometheus/restclient" // for client-go metrics registration
ctrl "sigs.k8s.io/controller-runtime"

"sigs.k8s.io/kueue/apis/visibility/v1alpha1"
generatedopenapi "sigs.k8s.io/kueue/apis/visibility/v1alpha1/openapi"
"sigs.k8s.io/kueue/pkg/queue"
"sigs.k8s.io/kueue/pkg/visibility/api"
)

var (
Expand All @@ -47,12 +47,11 @@ type server struct {

// CreateAndStartVisibilityServer creates visibility server injecting KueueManager and starts it
func CreateAndStartVisibilityServer(kueueMgr *queue.Manager, ctx context.Context) {
config := newVisibilityServerConfig()
if err := applyVisibilityServerOptions(config); err != nil {
setupLog.Error(err, "Unable to apply VisibilityServerOptions")
}
CreateAndStartVisibilityServerForConfig(CreateVisibilityServerConfig(), kueueMgr, ctx)
}

visibilityServer, err := config.Complete().New("visibility-server", genericapiserver.NewEmptyDelegate())
func CreateAndStartVisibilityServerForConfig(config genericapiserver.CompletedConfig, kueueMgr *queue.Manager, ctx context.Context) {
visibilityServer, err := config.New("visibility-server", genericapiserver.NewEmptyDelegate())
if err != nil {
setupLog.Error(err, "Unable to create visibility server")
}
Expand All @@ -67,6 +66,15 @@ func CreateAndStartVisibilityServer(kueueMgr *queue.Manager, ctx context.Context
}
}

func CreateVisibilityServerConfig() genericapiserver.CompletedConfig {
config := newVisibilityServerConfig()
if err := applyVisibilityServerOptions(config); err != nil {
setupLog.Error(err, "Unable to apply VisibilityServerOptions")
}

return config.Complete()
}

func applyVisibilityServerOptions(config *genericapiserver.RecommendedConfig) error {
o := genericoptions.NewRecommendedOptions("", api.Codecs.LegacyCodec(v1alpha1.SchemeGroupVersion))
o.Etcd = nil
Expand Down
116 changes: 116 additions & 0 deletions test/integration/visibility/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
Copyright The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package visibility

import (
"context"
"path/filepath"
"testing"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/kueue/pkg/constants"
"sigs.k8s.io/kueue/pkg/scheduler"

config "sigs.k8s.io/kueue/apis/config/v1beta1"
kueueclientset "sigs.k8s.io/kueue/client-go/clientset/versioned"
visibilityv1alpha1 "sigs.k8s.io/kueue/client-go/clientset/versioned/typed/visibility/v1alpha1"
"sigs.k8s.io/kueue/pkg/cache"
"sigs.k8s.io/kueue/pkg/controller/core"
"sigs.k8s.io/kueue/pkg/controller/core/indexer"
"sigs.k8s.io/kueue/pkg/queue"
"sigs.k8s.io/kueue/pkg/visibility"
"sigs.k8s.io/kueue/pkg/webhooks"
"sigs.k8s.io/kueue/test/integration/framework"
// +kubebuilder:scaffold:imports
)

var (
cfg *rest.Config
k8sClient client.Client
visibilityClient visibilityv1alpha1.VisibilityV1alpha1Interface
fwk *framework.Framework
ctx context.Context
)

func TestAPIs(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t,
"Visibility Suite",
)
}

var _ = ginkgo.BeforeSuite(func() {
fwk = &framework.Framework{
CRDPath: filepath.Join("..", "..", "..", "config", "components", "crd", "bases"),
WebhookPath: filepath.Join("..", "..", "..", "config", "components", "webhook"),
}

cfg = fwk.Init()
serverCfg := visibility.CreateVisibilityServerConfig()

kueueClient, err := kueueclientset.NewForConfig(serverCfg.LoopbackClientConfig)
gomega.Expect(err).ToNot(gomega.HaveOccurred(), "unable to create kueue clientset")

visibilityClient = kueueClient.VisibilityV1alpha1()

ctx, k8sClient = fwk.RunManager(cfg, func(mgr manager.Manager, ctx context.Context) {
err := indexer.Setup(ctx, mgr.GetFieldIndexer())
gomega.Expect(err).NotTo(gomega.HaveOccurred())

failedWebhook, err := webhooks.Setup(mgr)
gomega.Expect(err).ToNot(gomega.HaveOccurred(), "webhook", failedWebhook)

cCache := cache.New(mgr.GetClient())
queues := queue.NewManager(mgr.GetClient(), cCache)

configuration := &config.Configuration{}
mgr.GetScheme().Default(configuration)

configuration.QueueVisibility = &config.QueueVisibility{
UpdateIntervalSeconds: 2,
ClusterQueues: &config.ClusterQueueVisibility{
MaxCount: 3,
},
}

failedCtrl, err := core.SetupControllers(mgr, queues, cCache, configuration)
gomega.Expect(err).ToNot(gomega.HaveOccurred(), "controller", failedCtrl)

sched := scheduler.New(queues, cCache, mgr.GetClient(), mgr.GetEventRecorderFor(constants.AdmissionName))
err = sched.Start(ctx)
gomega.Expect(err).NotTo(gomega.HaveOccurred())

go visibility.CreateAndStartVisibilityServerForConfig(serverCfg, queues, ctx)

// wait for the extension API server to get started
gomega.Eventually(func() error {
result := visibilityClient.RESTClient().Get().Do(ctx)
if result.Error() != nil {
return err
}
return nil
}).Should(gomega.Succeed())
})
})

var _ = ginkgo.AfterSuite(func() {
fwk.Teardown()
})
Loading

0 comments on commit 79cd7fc

Please sign in to comment.