Skip to content

Commit 5eea70b

Browse files
authored
chore: fix kubernetes-configuration CRD installation in integration tests (#7659)
1 parent e37c1cd commit 5eea70b

File tree

3 files changed

+101
-8
lines changed

3 files changed

+101
-8
lines changed

test/util/crds.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ func DeployCRDsForCluster(ctx context.Context, cluster clusters.Cluster) error {
2323
return err
2424
}
2525

26-
fmt.Printf("INFO: deploying Kong CRDs to cluster\n")
26+
fmt.Printf("INFO: deploying Kong CRDs (%s) to cluster\n", kongCRDsKustomize)
2727
if err := clusters.KustomizeDeployForCluster(ctx, cluster, kongCRDsKustomize); err != nil {
2828
return err
2929
}
30-
fmt.Printf("INFO: deploying Kong incubator CRDs to cluster\n")
30+
fmt.Printf("INFO: deploying Kong incubator CRDs (%s) to cluster\n", kongIncubatorCRDsKustomize)
3131
if err := clusters.KustomizeDeployForCluster(ctx, cluster, kongIncubatorCRDsKustomize); err != nil {
3232
return err
3333
}
3434

35-
fmt.Printf("INFO: deploying Gateway CRDs to cluster\n")
35+
fmt.Printf("INFO: deploying Gateway CRDs (%s) to cluster\n", consts.GatewayExperimentalCRDsKustomizeURL)
3636
if err := clusters.KustomizeDeployForCluster(ctx, cluster, consts.GatewayExperimentalCRDsKustomizeURL); err != nil {
3737
return err
3838
}

test/util/gomod.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"strings"
88

9+
"github.com/blang/semver/v4"
910
"github.com/hashicorp/go-retryablehttp"
1011
"github.com/samber/lo"
1112
"golang.org/x/mod/modfile"
@@ -49,13 +50,17 @@ func DependencyModuleVersionGit(dep string) (string, error) {
4950
return "", err
5051
}
5152

53+
// NOTE: When we rely on a pseudo-version (e.g. `v1.1.1-0.20250217181409-44e5ddce290d`),
54+
// we need to extract the commit hash from it to use it in the GitHub API.
55+
// When version is set to a tag (e.g. `v1.1.1`), we could use it against the /commits
56+
// endpoint as well but there's no need for that as we can use the tag directly.
57+
5258
// The same logic as in scripts/generate-crd-kustomize.sh:11:18
53-
if versionParts := strings.Split(version, "-"); len(versionParts) >= 2 {
54-
// If there are 2 or more hyphens, extract the part after the last hyphen as
55-
// that's a git commit hash (e.g. `v1.1.1-0.20250217181409-44e5ddce290d`).
56-
version = versionParts[len(versionParts)-1]
5759

58-
resp, err := retryablehttp.Get("https://api.github.com/repos/Kong/kubernetes-configuration/commits/" + version)
60+
// If there are 2 or more hyphens, extract the part after the last hyphen as
61+
// that's a git commit hash (e.g. `v1.1.1-0.20250217181409-44e5ddce290d`).
62+
if hash, ok := GetHashFromPseudoVersion(version); ok {
63+
resp, err := retryablehttp.Get("https://api.github.com/repos/Kong/kubernetes-configuration/commits/" + hash)
5964
if err != nil {
6065
return "", fmt.Errorf("failed to fetch commit data: %w", err)
6166
}
@@ -72,3 +77,24 @@ func DependencyModuleVersionGit(dep string) (string, error) {
7277

7378
return version, nil
7479
}
80+
81+
// GetHashFromPseudoVersion extracts the commit hash from a pseudo version string.
82+
// It returns the hash and a boolean indicating whether the provided string
83+
// is a valid pseudo version.
84+
func GetHashFromPseudoVersion(pseudoVersion string) (string, bool) {
85+
pseudoVersion = strings.TrimSpace(pseudoVersion)
86+
pseudoVersion = strings.TrimPrefix(pseudoVersion, "v") // Remove leading 'v' if present
87+
88+
// The pseudo version is expected to be in the format:
89+
// v1.1.1-0.20250217181409-44e5ddce290d
90+
// We need to extract the last part after the last hyphen.
91+
parts := strings.Split(pseudoVersion, "-")
92+
if len(parts) <= 2 {
93+
return "", false
94+
}
95+
96+
if _, err := semver.Parse(parts[0]); err != nil {
97+
return "", false
98+
}
99+
return parts[len(parts)-1], true
100+
}

test/util/gomod_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package util
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestGetHashFromPseudoVersion(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
pseudoVersion string
13+
expectedHash string
14+
expectedOK bool
15+
}{
16+
{
17+
name: "valid pseudo version",
18+
pseudoVersion: "v1.1.1-20250217181409-44e5ddce290d",
19+
expectedHash: "44e5ddce290d",
20+
expectedOK: true,
21+
},
22+
{
23+
name: "valid pseudo version with different format",
24+
pseudoVersion: "v0.2.3-20210101120000-abcdef123456",
25+
expectedHash: "abcdef123456",
26+
expectedOK: true,
27+
},
28+
{
29+
name: "regular version tag",
30+
pseudoVersion: "v1.2.3",
31+
expectedHash: "",
32+
expectedOK: false,
33+
},
34+
{
35+
name: "invalid version format",
36+
pseudoVersion: "invalid-version",
37+
expectedHash: "",
38+
expectedOK: false,
39+
},
40+
{
41+
name: "empty string",
42+
pseudoVersion: "",
43+
expectedHash: "",
44+
expectedOK: false,
45+
},
46+
{
47+
name: "pseudo version with multiple hyphens",
48+
pseudoVersion: "v1.0.0-rc1-0.20250101000000-abc123def456",
49+
expectedHash: "abc123def456",
50+
expectedOK: true,
51+
},
52+
{
53+
name: "semver alpha",
54+
pseudoVersion: "v1.0.0-alpha.0",
55+
expectedHash: "",
56+
expectedOK: false,
57+
},
58+
}
59+
60+
for _, tt := range tests {
61+
t.Run(tt.name, func(t *testing.T) {
62+
hash, ok := GetHashFromPseudoVersion(tt.pseudoVersion)
63+
require.Equal(t, tt.expectedOK, ok)
64+
require.Equal(t, tt.expectedHash, hash)
65+
})
66+
}
67+
}

0 commit comments

Comments
 (0)