Skip to content

Commit

Permalink
Merge branch 'main' into commit-hash-version
Browse files Browse the repository at this point in the history
  • Loading branch information
ehearneRedHat committed Jun 10, 2024
2 parents 1dac73e + 08f8fbf commit 37c4465
Show file tree
Hide file tree
Showing 38 changed files with 948 additions and 805 deletions.
25 changes: 9 additions & 16 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,16 @@ jobs:
id: go
- name: Check out code
uses: actions/checkout@v3
- name: Create k8s Kind Cluster
uses: helm/[email protected]
with:
version: v0.20.0
config: utils/kind-cluster.yaml
cluster_name: ${{ env.KIND_CLUSTER_NAME }}
wait: 120s
- name: Check cluster info
run: |
kubectl cluster-info dump
- name: Run make env-setup
run: |
make env-setup
- name: Run tests
run: |
make test
- name: Report to CodeCov
uses: codecov/codecov-action@v2
- name: Upload test coverage reports to CodeCov
# more at https://github.com/codecov/codecov-action
# Only run if the feature branch is in your repo (not in a fork)
# as Tokenless uploading is rate limited for public repos
if: github.event.pull_request.head.repo.full_name == github.repository
uses: codecov/codecov-action@v4
with:
files: "*.coverprofile"
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false
verbose: true
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ testbin
*.out

cmd/cmd.coverprofile
pkg/utils/utils.coverprofile
/coverage/

# Binary generated by Vim
kuadrantctl

# Dependency directories (remove the comment below to include it)
# vendor/
Expand Down
31 changes: 23 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ help: Makefile
# Ginkgo tool
GINKGO = $(PROJECT_PATH)/bin/ginkgo
$(GINKGO):
$(call go-install-tool,$(GINKGO),github.com/onsi/ginkgo/[email protected])
# In order to make sure the version of the ginkgo cli installed
# is the same as the version of go.mod,
# instead of calling go-install-tool,
# running go install from the current module will pick version from current go.mod file.
GOBIN=$(PROJECT_PATH)/bin go install github.com/onsi/ginkgo/v2/ginkgo

.PHONY: ginkgo
ginkgo: $(GINKGO) ## Download ginkgo locally if necessary.

KIND = $(PROJECT_PATH)/bin/kind
KIND_VERSION = v0.20.0
KIND_VERSION = v0.22.0
$(KIND):
$(call go-install-tool,$(KIND),sigs.k8s.io/kind@$(KIND_VERSION))

Expand All @@ -34,10 +41,17 @@ kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.

## test: Run unit tests
.PHONY : test
test: fmt vet $(GINKGO)
# huffle both the order in which specs within a suite run, and the order in which different suites run
test: clean-cov fmt vet $(GINKGO)
mkdir -p $(PROJECT_PATH)/coverage
# Shuffle both the order in which specs within a suite run, and the order in which different suites run
# You can always rerun a given ordering later by passing the --seed flag a matching seed.
$(GINKGO) --randomizeAllSpecs --randomizeSuites -v -progress --trace --cover ./...
$(GINKGO) \
--randomize-all \
--randomize-suites \
--coverpkg ./pkg/...,./cmd/... \
--output-dir $(PROJECT_PATH)/coverage \
--coverprofile cover.out \
./pkg/... ./cmd/...

## install: Build and install kuadrantctl binary ($GOBIN or GOPATH/bin)
.PHONY : install
Expand All @@ -55,10 +69,7 @@ prepare-local-cluster: $(KIND) ## Deploy locally kuadrant operator from the curr

.PHONY: env-setup
env-setup:
$(MAKE) olm-install
$(MAKE) gateway-api-install
$(MAKE) istio-install
$(MAKE) deploy-gateway

## local-setup: Sets up Kind cluster with GatewayAPI manifests and istio GW, nothing Kuadrant. Build and install kuadrantctl binary
.PHONY: local-setup
Expand All @@ -79,5 +90,9 @@ fmt:
vet:
$(GO) vet ./...

.PHONY: clean-cov
clean-cov: ## Remove coverage reports
rm -rf $(PROJECT_PATH)/coverage

# Include last to avoid changing MAKEFILE_LIST used above
include ./make/*.mk
20 changes: 1 addition & 19 deletions cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@ package cmd
import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

var (
testK8sClient client.Client
)

func TestCommands(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Commands Suite")
Expand All @@ -27,14 +19,4 @@ var _ = BeforeSuite(func() {
By("Before suite")

logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

configuration, err := config.GetConfig()
Expect(err).NotTo(HaveOccurred())

err = apiextensionsv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

testK8sClient, err = client.New(configuration, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(testK8sClient).NotTo(BeNil())
})
12 changes: 6 additions & 6 deletions cmd/generate_gatewayapi_httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/kuadrant/kuadrantctl/pkg/utils"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gatewayapiv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
)

var (
Expand Down Expand Up @@ -76,15 +76,15 @@ func runGenerateGatewayApiHttpRoute(cmd *cobra.Command, args []string) error {
return nil
}

func buildHTTPRoute(doc *openapi3.T) *gatewayapiv1beta1.HTTPRoute {
return &gatewayapiv1beta1.HTTPRoute{
func buildHTTPRoute(doc *openapi3.T) *gatewayapiv1.HTTPRoute {
return &gatewayapiv1.HTTPRoute{
TypeMeta: v1.TypeMeta{
APIVersion: "gateway.networking.k8s.io/v1beta1",
APIVersion: gatewayapiv1.GroupVersion.String(),
Kind: "HTTPRoute",
},
ObjectMeta: gatewayapi.HTTPRouteObjectMetaFromOAS(doc),
Spec: gatewayapiv1beta1.HTTPRouteSpec{
CommonRouteSpec: gatewayapiv1beta1.CommonRouteSpec{
Spec: gatewayapiv1.HTTPRouteSpec{
CommonRouteSpec: gatewayapiv1.CommonRouteSpec{
ParentRefs: gatewayapi.HTTPRouteGatewayParentRefsFromOAS(doc),
},
Hostnames: gatewayapi.HTTPRouteHostnamesFromOAS(doc),
Expand Down
141 changes: 141 additions & 0 deletions cmd/generate_gatewayapi_httproute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package cmd

import (
"bytes"
"io"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
"sigs.k8s.io/yaml"
)

var _ = Describe("Generate HTTPRoute", func() {
var (
cmd *cobra.Command
cmdStdoutBuffer *bytes.Buffer
cmdStderrBuffer *bytes.Buffer
)

BeforeEach(func() {
cmd = generateGatewayApiHttpRouteCommand()
cmdStdoutBuffer = bytes.NewBufferString("")
cmdStderrBuffer = bytes.NewBufferString("")
cmd.SetOut(cmdStdoutBuffer)
cmd.SetErr(cmdStderrBuffer)
})

Context("with invalid OAS", func() {
It("happy path", func() {
cmd.SetArgs([]string{"--oas", "testdata/invalid_oas.yaml"})
Expect(cmd.Execute()).Should(MatchError(ContainSubstring("OpenAPI validation error")))

})
})

Context("with root level kuadrant extensions", func() {
It("HTTPRoute is generated", func() {
cmd.SetArgs([]string{"--oas", "testdata/petstore_openapi.yaml"})
Expect(cmd.Execute()).ShouldNot(HaveOccurred())
out, err := io.ReadAll(cmdStdoutBuffer)
Expect(err).ShouldNot(HaveOccurred())

var httpRoute gatewayapiv1.HTTPRoute
Expect(yaml.Unmarshal(out, &httpRoute)).ShouldNot(HaveOccurred())
Expect(httpRoute.TypeMeta).To(Equal(metav1.TypeMeta{
APIVersion: gatewayapiv1.GroupVersion.String(),
Kind: "HTTPRoute",
}))
Expect(httpRoute.ObjectMeta).To(Equal(metav1.ObjectMeta{
Name: "petstore",
Namespace: "petstore-ns",
}))
Expect(httpRoute.Spec.CommonRouteSpec).To(Equal(gatewayapiv1.CommonRouteSpec{
ParentRefs: []gatewayapiv1.ParentReference{
{
Name: "gw", Namespace: ptr.To(gatewayapiv1.Namespace("gw-ns")),
},
},
}))
Expect(httpRoute.Spec.Hostnames).To(Equal([]gatewayapiv1.Hostname{
gatewayapiv1.Hostname("example.com"),
}))
Expect(httpRoute.Spec.Rules).To(HaveLen(3))
Expect(httpRoute.Spec.Rules).To(ContainElement(
gatewayapiv1.HTTPRouteRule{
Matches: []gatewayapiv1.HTTPRouteMatch{
{
Path: &gatewayapiv1.HTTPPathMatch{
Type: ptr.To(gatewayapiv1.PathMatchExact),
Value: ptr.To("/v1/cat"),
},
Method: ptr.To(gatewayapiv1.HTTPMethodGet),
},
},
BackendRefs: []gatewayapiv1.HTTPBackendRef{
{
BackendRef: gatewayapiv1.BackendRef{
BackendObjectReference: gatewayapiv1.BackendObjectReference{
Name: "petstore",
Namespace: ptr.To(gatewayapiv1.Namespace("petstore")),
Port: ptr.To(gatewayapiv1.PortNumber(80)),
},
},
},
},
},
))
Expect(httpRoute.Spec.Rules).To(ContainElement(
gatewayapiv1.HTTPRouteRule{
Matches: []gatewayapiv1.HTTPRouteMatch{
{
Path: &gatewayapiv1.HTTPPathMatch{
Type: ptr.To(gatewayapiv1.PathMatchExact),
Value: ptr.To("/v1/dog"),
},
Method: ptr.To(gatewayapiv1.HTTPMethodGet),
},
},
BackendRefs: []gatewayapiv1.HTTPBackendRef{
{
BackendRef: gatewayapiv1.BackendRef{
BackendObjectReference: gatewayapiv1.BackendObjectReference{
Name: "petstore",
Namespace: ptr.To(gatewayapiv1.Namespace("petstore")),
Port: ptr.To(gatewayapiv1.PortNumber(80)),
},
},
},
},
},
))
Expect(httpRoute.Spec.Rules).To(ContainElement(
gatewayapiv1.HTTPRouteRule{
Matches: []gatewayapiv1.HTTPRouteMatch{
{
Path: &gatewayapiv1.HTTPPathMatch{
Type: ptr.To(gatewayapiv1.PathMatchExact),
Value: ptr.To("/v1/dog"),
},
Method: ptr.To(gatewayapiv1.HTTPMethodPost),
},
},
BackendRefs: []gatewayapiv1.HTTPBackendRef{
{
BackendRef: gatewayapiv1.BackendRef{
BackendObjectReference: gatewayapiv1.BackendObjectReference{
Name: "petstore",
Namespace: ptr.To(gatewayapiv1.Namespace("petstore")),
Port: ptr.To(gatewayapiv1.PortNumber(80)),
},
},
},
},
},
))
})
})
})
20 changes: 11 additions & 9 deletions cmd/generate_kuadrant_authpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
kuadrantapiv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayapiv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/kuadrant/kuadrantctl/pkg/gatewayapi"
"github.com/kuadrant/kuadrantctl/pkg/kuadrantapi"
Expand Down Expand Up @@ -91,21 +91,23 @@ func buildAuthPolicy(doc *openapi3.T) *kuadrantapiv1beta2.AuthPolicy {
ObjectMeta: kuadrantapi.AuthPolicyObjectMetaFromOAS(doc),
Spec: kuadrantapiv1beta2.AuthPolicySpec{
TargetRef: gatewayapiv1alpha2.PolicyTargetReference{
Group: gatewayapiv1beta1.Group("gateway.networking.k8s.io"),
Kind: gatewayapiv1beta1.Kind("HTTPRoute"),
Name: gatewayapiv1beta1.ObjectName(routeMeta.Name),
Group: gatewayapiv1.GroupName,
Kind: gatewayapiv1.Kind("HTTPRoute"),
Name: gatewayapiv1.ObjectName(routeMeta.Name),
},
// Currently only authentication rules enforced
AuthScheme: kuadrantapiv1beta2.AuthSchemeSpec{
Authentication: kuadrantapi.AuthPolicyAuthenticationSchemeFromOAS(doc),
AuthPolicyCommonSpec: kuadrantapiv1beta2.AuthPolicyCommonSpec{
AuthScheme: &kuadrantapiv1beta2.AuthSchemeSpec{
Authentication: kuadrantapi.AuthPolicyAuthenticationSchemeFromOAS(doc),
},
RouteSelectors: kuadrantapi.AuthPolicyTopRouteSelectorsFromOAS(doc),
},
RouteSelectors: kuadrantapi.AuthPolicyTopRouteSelectorsFromOAS(doc),
},
}

if routeMeta.Namespace != "" {
ap.Spec.TargetRef.Namespace = &[]gatewayapiv1beta1.Namespace{
gatewayapiv1beta1.Namespace(routeMeta.Namespace),
ap.Spec.TargetRef.Namespace = &[]gatewayapiv1.Namespace{
gatewayapiv1.Namespace(routeMeta.Namespace),
}[0]
}

Expand Down
Loading

0 comments on commit 37c4465

Please sign in to comment.