test: add concurrency to OpenShift env setup

This commit is contained in:
Marc Nuri
2025-05-16 13:14:22 +02:00
parent 6c51c9d9e6
commit e0fe25af3c
2 changed files with 28 additions and 12 deletions

4
go.mod
View File

@@ -5,10 +5,12 @@ go 1.24.1
require (
github.com/fsnotify/fsnotify v1.9.0
github.com/mark3labs/mcp-go v0.27.1
github.com/pkg/errors v0.9.1
github.com/spf13/afero v1.14.0
github.com/spf13/cobra v1.9.1
github.com/spf13/viper v1.20.1
golang.org/x/net v0.40.0
golang.org/x/sync v0.14.0
helm.sh/helm/v3 v3.17.3
k8s.io/api v0.33.1
k8s.io/apiextensions-apiserver v0.33.1
@@ -107,7 +109,6 @@ require (
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.22.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.62.0 // indirect
@@ -135,7 +136,6 @@ require (
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.38.0 // indirect
golang.org/x/oauth2 v0.27.0 // indirect
golang.org/x/sync v0.14.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/term v0.32.0 // indirect
golang.org/x/text v0.25.0 // indirect

View File

@@ -7,7 +7,9 @@ import (
"github.com/mark3labs/mcp-go/client"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
"github.com/pkg/errors"
"github.com/spf13/afero"
"golang.org/x/sync/errgroup"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextensionsv1spec "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -212,16 +214,28 @@ func inOpenShift(c *mcpContext) {
"names": {"plural": "%s","singular": "%s","kind": "%s"}
}
}`
c.crdApply(fmt.Sprintf(crdTemplate, "projects.project.openshift.io", "project.openshift.io",
"Cluster", "projects", "project", "Project"))
c.crdApply(fmt.Sprintf(crdTemplate, "routes.route.openshift.io", "route.openshift.io",
"Namespaced", "routes", "route", "Route"))
tasks, _ := errgroup.WithContext(c.ctx)
tasks.Go(func() error {
return c.crdApply(fmt.Sprintf(crdTemplate, "projects.project.openshift.io", "project.openshift.io",
"Cluster", "projects", "project", "Project"))
})
tasks.Go(func() error {
return c.crdApply(fmt.Sprintf(crdTemplate, "routes.route.openshift.io", "route.openshift.io",
"Namespaced", "routes", "route", "Route"))
})
if err := tasks.Wait(); err != nil {
panic(err)
}
}
// inOpenShiftClear clears the kubernetes environment so it no longer seems to be running OpenShift
func inOpenShiftClear(c *mcpContext) {
c.crdDelete("projects.project.openshift.io")
c.crdDelete("routes.route.openshift.io")
tasks, _ := errgroup.WithContext(c.ctx)
tasks.Go(func() error { return c.crdDelete("projects.project.openshift.io") })
tasks.Go(func() error { return c.crdDelete("routes.route.openshift.io") })
if err := tasks.Wait(); err != nil {
panic(err)
}
}
// newKubernetesClient creates a new Kubernetes client with the envTest kubeconfig
@@ -247,19 +261,20 @@ func (c *mcpContext) newApiExtensionsClient() *apiextensionsv1.ApiextensionsV1Cl
}
// crdApply creates a CRD from the provided resource string and waits for it to be established
func (c *mcpContext) crdApply(resource string) {
func (c *mcpContext) crdApply(resource string) error {
apiExtensionsV1Client := c.newApiExtensionsClient()
var crd = &apiextensionsv1spec.CustomResourceDefinition{}
err := json.Unmarshal([]byte(resource), crd)
_, err = apiExtensionsV1Client.CustomResourceDefinitions().Create(c.ctx, crd, metav1.CreateOptions{})
if err != nil {
panic(fmt.Errorf("failed to create CRD %v", err))
return fmt.Errorf("failed to create CRD %v", err)
}
c.crdWaitUntilReady(crd.Name)
return nil
}
// crdDelete deletes a CRD by name and waits for it to be removed
func (c *mcpContext) crdDelete(name string) {
func (c *mcpContext) crdDelete(name string) error {
apiExtensionsV1Client := c.newApiExtensionsClient()
err := apiExtensionsV1Client.CustomResourceDefinitions().Delete(c.ctx, name, metav1.DeleteOptions{
GracePeriodSeconds: ptr.To(int64(0)),
@@ -273,8 +288,9 @@ func (c *mcpContext) crdDelete(name string) {
iteration++
}
if err != nil {
panic(fmt.Errorf("failed to delete CRD %v", err))
return errors.Wrap(err, "failed to delete CRD")
}
return nil
}
// crdWaitUntilReady waits for a CRD to be established