Skip to content

Commit

Permalink
test: extract version from go.mod (#561)
Browse files Browse the repository at this point in the history
* extract version from go.mod

* add util func to get project root
  • Loading branch information
randmonkey authored Sep 10, 2024
1 parent d6b69e3 commit 6457d52
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/kr/pretty v0.3.1
github.com/samber/lo v1.47.0
github.com/stretchr/testify v1.9.0
golang.org/x/mod v0.17.0
k8s.io/api v0.31.0
k8s.io/apimachinery v0.31.0
k8s.io/client-go v0.31.0
Expand Down Expand Up @@ -101,7 +102,6 @@ require (
go.opentelemetry.io/otel/trace v1.28.0 // indirect
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
Expand Down
15 changes: 15 additions & 0 deletions pkg/utils/test/projectroot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package test

import (
"path/filepath"
"runtime"
)

// ProjectRootPath returns the root directory of this project.
func ProjectRootPath() string {
_, b, _, _ := runtime.Caller(0) //nolint:dogsled

// Returns root directory of this project.
// NOTE: it depends on the path of this file itself. When the file is moved, the second param may need updating.
return filepath.Join(filepath.Dir(b), "../../..")
}
44 changes: 40 additions & 4 deletions pkg/utils/test/setup_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import (
"context"
"crypto/tls"
"fmt"
"go/build"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"time"

"github.com/kong/kubernetes-testing-framework/pkg/clusters"
"github.com/kong/kubernetes-testing-framework/pkg/clusters/types/gke"
"github.com/kong/kubernetes-testing-framework/pkg/clusters/types/kind"
"github.com/kong/kubernetes-testing-framework/pkg/environments"
"github.com/samber/lo"
"golang.org/x/mod/modfile"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand All @@ -26,7 +30,9 @@ import (
operatorclient "github.com/kong/gateway-operator/pkg/clientset"
)

const kongCRDsKustomizeURL = "https://github.com/Kong/kubernetes-configuration/config/crd"
const (
kubernetesConfigurationModuleName = "github.com/kong/kubernetes-configuration"
)

func noOpClose() error {
return nil
Expand Down Expand Up @@ -183,6 +189,27 @@ func BuildMTLSCredentials(ctx context.Context, k8sClient *kubernetes.Clientset,
}
}

// ExtractModuleVersion extracts version of an imported module in go.mod.
// If the module is not found, or we failed to parse the module version, it will return an error.
func ExtractModuleVersion(moduleName string) (string, error) {
projectRoot := ProjectRootPath()
content, err := os.ReadFile(filepath.Join(projectRoot, "go.mod"))
if err != nil {
return "", err
}
f, err := modfile.Parse("go.mod", content, nil)
if err != nil {
return "", err
}
module, found := lo.Find(f.Require, func(r *modfile.Require) bool {
return r.Mod.Path == moduleName
})
if !found {
return "", fmt.Errorf("module %s not found", moduleName)
}
return module.Mod.Version, nil
}

// DeployCRDs deploys the CRDs commonly used in tests.
func DeployCRDs(ctx context.Context, crdPath string, operatorClient *operatorclient.Clientset, env environments.Environment) error {
// CRDs for stable features
Expand All @@ -192,16 +219,25 @@ func DeployCRDs(ctx context.Context, crdPath string, operatorClient *operatorcli
return err
}

// CRDs for gateway APIs
fmt.Printf("INFO: deploying Gateway API CRDs: %s\n", GatewayStandardCRDsKustomizeURL)
if err := clusters.KustomizeDeployForCluster(ctx, env.Cluster(), GatewayStandardCRDsKustomizeURL); err != nil {
return err
}

fmt.Printf("INFO: deploying Kong (kubernetes-configuration) CRDs: %s\n", kongCRDsKustomizeURL)
if err := clusters.KustomizeDeployForCluster(ctx, env.Cluster(), kongCRDsKustomizeURL); err != nil {
// CRDs for Kong configuration
// First extract version of `kong/kubernetes-configuration` module used
kongCRDVersion, err := ExtractModuleVersion(kubernetesConfigurationModuleName)
if err != nil {
return fmt.Errorf("failed to extract Kong CRDs (%s) module's version: %w", kubernetesConfigurationModuleName, err)
}
// Then install CRDs from the module found in `$GOPATH`.
kongCRDPath := filepath.Join(build.Default.GOPATH, "pkg", "mod", "github.com", "kong",
"kubernetes-configuration@"+kongCRDVersion, "config", "crd")
fmt.Printf("INFO: deploying Kong (kubernetes-configuration) CRDs: %s\n", kongCRDPath)
if err := clusters.KustomizeDeployForCluster(ctx, env.Cluster(), kongCRDPath); err != nil {
return err
}

// CRDs for alpha/experimental features
fmt.Printf("INFO: deploying KGO AIGateway CRD: %s\n", crdPath)
if err := clusters.ApplyManifestByURL(ctx, env.Cluster(), path.Join(crdPath, AIGatewayCRDPath)); err != nil {
Expand Down

0 comments on commit 6457d52

Please sign in to comment.