Skip to content

Commit

Permalink
refactor rewrite rule generation / add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
cbarbian-sap committed Aug 21, 2023
1 parent 48c1cd3 commit 2810cd7
Show file tree
Hide file tree
Showing 21 changed files with 1,513 additions and 765 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"go.testEnvVars": {
"KUBEBUILDER_ASSETS": "${workspaceFolder}/bin/k8s/current"
"KUBEBUILDER_ASSETS": "${workspaceFolder}/bin/k8s/current",
"TEST_ASSET_COREDNS": "${workspaceFolder}/bin/coredns"
}
}
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ COPY pkg/ pkg/
COPY internal/ internal/
COPY crds/ crds/
COPY Makefile Makefile
COPY hack/ hack/

# Run tests and build
RUN make envtest \
&& CGO_ENABLED=0 KUBEBUILDER_ASSETS="/workspace/bin/k8s/current" go test ./... \
RUN make envtest coredns \
&& CGO_ENABLED=0 KUBEBUILDER_ASSETS="/workspace/bin/k8s/current" TEST_ASSET_COREDNS=/workspace/bin/coredns go test ./... \
&& CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go

# Use distroless as minimal base image to package the manager binary
Expand Down
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
IMG ?= controller:latest
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.25.0
COREDNS_VERSION= 1.10.1

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand Down Expand Up @@ -57,8 +58,8 @@ vet: ## Run go vet against code.
go vet ./...

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(LOCALBIN)/k8s/current" go test ./... -coverprofile cover.out
test: manifests generate fmt vet envtest coredns ## Run tests.
KUBEBUILDER_ASSETS="$(LOCALBIN)/k8s/current" TEST_ASSET_COREDNS="$(LOCALBIN)/coredns" go test ./... -coverprofile cover.out

##@ Build

Expand Down Expand Up @@ -132,6 +133,7 @@ $(LOCALBIN):
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
COREDNS ?= $(LOCALBIN)/coredns
CLIENT_GEN ?= $(shell pwd)/bin/client-gen
INFORMER_GEN ?= $(shell pwd)/bin/informer-gen
LISTER_GEN ?= $(shell pwd)/bin/lister-gen
Expand Down Expand Up @@ -160,6 +162,11 @@ $(ENVTEST): $(LOCALBIN)
rm -f $(LOCALBIN)/k8s/current ;\
ln -s $$ENVTESTDIR $(LOCALBIN)/k8s/current

.PHONY: coredns
coredns: $(COREDNS) ## Download coredns
$(COREDNS): $(LOCALBIN)
test -s $(LOCALBIN)/coredns || ./hack/download-coredns $(COREDNS_VERSION) $(LOCALBIN)/coredns

.PHONY: client-gen
client-gen: $(CLIENT_GEN) ## Download client-gen
$(CLIENT_GEN): $(LOCALBIN)
Expand Down
16 changes: 4 additions & 12 deletions api/v1alpha1/masqueradingrule_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package v1alpha1
import (
"fmt"

"github.com/sap/dns-masquerading-operator/internal/netutil"
"github.com/sap/dns-masquerading-operator/internal/coredns"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -59,17 +59,9 @@ func (r *MasqueradingRule) ValidateDelete() error {
}

func (r *MasqueradingRule) validate() error {
if err := netutil.CheckDnsName(r.Spec.From, true); err != nil {
return fmt.Errorf("invalid .spec.from: %s (%s)", r.Spec.From, err)
}
if netutil.IsIpAddress(r.Spec.To) {
if netutil.IsWildcardDnsName(r.Spec.From) {
return fmt.Errorf("invalid .spec.to: %s (.spec.from must not be a wildcard DNS name if .spec.to is an IP address)", r.Spec.To)
}
} else {
if err := netutil.CheckDnsName(r.Spec.To, false); err != nil {
return fmt.Errorf("invalid .spec.to: %s (%s)", r.Spec.To, err)
}
_, err := coredns.NewRewriteRule("", r.Spec.From, r.Spec.To)
if err != nil {
return fmt.Errorf("invalid rule specification: %s", err)
}
return nil
}
26 changes: 15 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.19

require (
github.com/hashicorp/go-multierror v1.1.1
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.9
github.com/pkg/errors v0.9.1
github.com/sap/go-generics v0.1.1
istio.io/client-go v1.17.3
Expand All @@ -26,51 +28,53 @@ require (
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/ginkgo/v2 v2.11.0 // indirect
github.com/onsi/gomega v1.27.9 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/common v0.39.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
golang.org/x/oauth2 v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.9.3 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221018160656-63c7b68cfc55 // indirect
google.golang.org/genproto v0.0.0-20221227171554-f9683d7f8bef // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
istio.io/api v0.0.0-20230523222408-21e7bb4b66a2 // indirect
k8s.io/apiextensions-apiserver v0.26.1 // indirect
k8s.io/component-base v0.26.1 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/klog/v2 v2.90.0 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
Expand Down
Loading

0 comments on commit 2810cd7

Please sign in to comment.