Skip to content

Commit

Permalink
Feature/SRE 3111 watch virtual service (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
activehigh authored Jul 22, 2022
1 parent ad99c7d commit 38dfd81
Show file tree
Hide file tree
Showing 8 changed files with 431 additions and 38 deletions.
Binary file added .DS_Store
Binary file not shown.
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ help: ## Display this help.
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases

manifests-arm64: controller-gen-arm64 ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases

generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

generate-arm64: controller-gen-arm64 ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

fmt: ## Run go fmt against code.
go fmt ./...

Expand All @@ -96,6 +102,9 @@ test: manifests generate fmt vet ## Run tests.
build: generate fmt vet ## Build manager binary.
go build -o bin/manager main.go

build-arm64: generate-arm64 fmt vet ## Build manager binary.
go build -o bin/manager main.go

run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go

Expand Down Expand Up @@ -125,6 +134,10 @@ CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
controller-gen: ## Download controller-gen locally if necessary.
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])

CONTROLLER_GEN = $(shell pwd)/bin/controller-gen-arm64
controller-gen-arm64: ## Download controller-gen locally if necessary.
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])

KUSTOMIZE = $(shell pwd)/bin/kustomize
kustomize: ## Download kustomize locally if necessary.
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])
Expand Down
39 changes: 39 additions & 0 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ package controllers

import (
"context"

"github.com/monimesl/istio-virtualservice-merger/api/v1alpha1"
"github.com/monimesl/operator-helper/reconciler"
istio "istio.io/client-go/pkg/apis/networking/v1alpha3"
versionedclient "istio.io/client-go/pkg/clientset/versioned"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

type VirtualServicePatchReconciler struct {
Expand All @@ -33,6 +39,39 @@ func (r *VirtualServicePatchReconciler) Configure(ctx reconciler.Context) error
r.Context = ctx
return ctx.NewControllerBuilder().
For(&v1alpha1.VirtualServiceMerge{}).
Watches(&source.Kind{Type: &istio.VirtualService{}}, handler.EnqueueRequestsFromMapFunc(func(obj client.Object) [](reconcile.Request) {
vs := obj.(*istio.VirtualService)
requests := make([]reconcile.Request, 0)

// skip if vs is being deleted
if !vs.GetDeletionTimestamp().IsZero() {
return requests
}
// get all virtual service merge whos target is this virtual service
vsmegeList := &v1alpha1.VirtualServiceMergeList{}
if err := r.Client().List(context.TODO(), vsmegeList, &client.ListOptions{
Namespace: vs.GetNamespace(),
}); err != nil {
panic(err)
}
for _, vsmerge := range vsmegeList.Items {
targetNamespace := vsmerge.Spec.Target.Namespace
if targetNamespace == "" {
targetNamespace = vsmerge.GetNamespace()
}
// only look for vs that is a target for any of the merge
if vsmerge.Spec.Target.Name == vs.GetName() && targetNamespace == vs.GetNamespace() {
request := reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: vsmerge.GetNamespace(),
Name: vsmerge.GetName(),
},
}
requests = append(requests, request)
}
}
return requests
})).
Complete(r)
}

Expand Down
9 changes: 8 additions & 1 deletion controller/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ package controllers
import (
"context"
"fmt"

"github.com/monimesl/istio-virtualservice-merger/api/v1alpha1"
"github.com/monimesl/operator-helper/oputil"
"github.com/monimesl/operator-helper/reconciler"
versionedclient "istio.io/client-go/pkg/clientset/versioned"
kerr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -40,7 +42,12 @@ func Reconcile(ctx reconciler.Context, client *versionedclient.Clientset, patch
}
} else if oputil.Contains(patch.Finalizers, finalizerName) {
if err := updateTarget(client, patch, true); err != nil {
return err
if kerr.IsNotFound(err) {
// do not need to panic just log output
ctx.Logger().Info("Virtual service not found.")
} else {
return err
}
}
patch.Finalizers = oputil.Remove(finalizerName, patch.Finalizers)
if err := ctx.Client().Update(context.TODO(), patch); err != nil {
Expand Down
12 changes: 12 additions & 0 deletions controller/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package controllers

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"testing"
)

func TestConsumers(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Controller & Reconiler test suite")
}
36 changes: 21 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/monimesl/istio-virtualservice-merger

go 1.17
go 1.18

require (
github.com/monimesl/operator-helper v0.0.0-20211129165217-faf73a6bf8de
Expand All @@ -12,27 +12,33 @@ require (
)

require (
cloud.google.com/go v0.54.0 // indirect
cloud.google.com/go/compute v1.6.1 // indirect
github.com/stretchr/testify v1.7.1 // indirect
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/evanphx/json-patch v4.11.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-logr/logr v0.4.0 // indirect
github.com/go-logr/zapr v0.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/onsi/ginkgo/v2 v2.1.4
github.com/onsi/gomega v1.19.0
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand All @@ -41,19 +47,19 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.6 // indirect
go.uber.org/zap v1.17.0
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.26.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
istio.io/gogo-genproto v0.0.0-20210113155706-4daf5697332f // indirect
k8s.io/api v0.21.1 // indirect
k8s.io/apiextensions-apiserver v0.21.1 // indirect
Expand Down
Loading

0 comments on commit 38dfd81

Please sign in to comment.