diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1302006..8b64521 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -6,41 +6,41 @@ jobs: test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-go@v2 - with: - go-version: 1.19 - - run: make test + - uses: actions/checkout@v4 + - uses: actions/setup-go@v2 + with: + go-version: 1.19 + - run: make test docker-image: name: "Publish image on dockerhub" needs: - - test + - test runs-on: ubuntu-latest if: github.event_name != 'pull_request' steps: - - uses: actions/checkout@v2 - - uses: docker/setup-buildx-action@v1 - - uses: actions/cache@v2 - with: - path: /tmp/.buildx-cache - key: ${{ runner.os }}-buildx-${{ github.sha }} - restore-keys: | - ${{ runner.os }}-buildx- - - uses: Surgo/docker-smart-tag-action@v1 - id: smarttag - with: - docker_image: tsuru/acl-operator - default_branch: main - tag_with_sha: "true" - - uses: docker/login-action@v1 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_PASSWORD }} - - uses: docker/build-push-action@v2 - with: - file: ./Dockerfile - push: true - tags: ${{ steps.smarttag.outputs.tag }} - cache-from: type=local,src=/tmp/.buildx-cache - cache-to: type=local,dest=/tmp/.buildx-cache + - uses: actions/checkout@v4 + - uses: docker/setup-buildx-action@v1 + - uses: actions/cache@v4 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + - uses: Surgo/docker-smart-tag-action@v1 + id: smarttag + with: + docker_image: tsuru/acl-operator + default_branch: main + tag_with_sha: "true" + - uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_PASSWORD }} + - uses: docker/build-push-action@v2 + with: + file: ./Dockerfile + push: true + tags: ${{ steps.smarttag.outputs.tag }} + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,dest=/tmp/.buildx-cache diff --git a/.gitignore b/.gitignore index 66fd13c..5347416 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.dll *.so *.dylib +./bin # Test binary, built with `go test -c` *.test diff --git a/Dockerfile b/Dockerfile index c25ba50..92c1af3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Build the manager binary -FROM golang:1.19 as builder +FROM golang:1.19 AS builder WORKDIR /workspace # Copy the Go Modules manifests diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 4944ae4..34990c4 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -58,7 +58,7 @@ func (in *ACLDNSEntry) DeepCopyInto(out *ACLDNSEntry) { *out = *in out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - out.Spec = in.Spec + in.Spec.DeepCopyInto(&out.Spec) in.Status.DeepCopyInto(&out.Status) } @@ -115,6 +115,11 @@ func (in *ACLDNSEntryList) DeepCopyObject() runtime.Object { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ACLDNSEntrySpec) DeepCopyInto(out *ACLDNSEntrySpec) { *out = *in + if in.AdditionalIPs != nil { + in, out := &in.AdditionalIPs, &out.AdditionalIPs + *out = make([]string, len(*in)) + copy(*out, *in) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ACLDNSEntrySpec. @@ -524,7 +529,7 @@ func (in *TsuruAppAddress) DeepCopyInto(out *TsuruAppAddress) { *out = *in out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - out.Spec = in.Spec + in.Spec.DeepCopyInto(&out.Spec) in.Status.DeepCopyInto(&out.Status) } @@ -581,6 +586,11 @@ func (in *TsuruAppAddressList) DeepCopyObject() runtime.Object { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *TsuruAppAddressSpec) DeepCopyInto(out *TsuruAppAddressSpec) { *out = *in + if in.AdditionalIPs != nil { + in, out := &in.AdditionalIPs, &out.AdditionalIPs + *out = make([]string, len(*in)) + copy(*out, *in) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TsuruAppAddressSpec. diff --git a/bin/controller-gen b/bin/controller-gen deleted file mode 100755 index bddecb6..0000000 Binary files a/bin/controller-gen and /dev/null differ diff --git a/clients/tsuruapi/client.go b/clients/tsuruapi/client.go index 932ca3a..9dbac64 100644 --- a/clients/tsuruapi/client.go +++ b/clients/tsuruapi/client.go @@ -6,7 +6,6 @@ import ( "fmt" "net/http" - "github.com/pkg/errors" "github.com/tsuru/tsuru/app" ) @@ -59,7 +58,7 @@ func (c *client) AppInfo(ctx context.Context, appName string) (*app.App, error) return nil, err } if appData.Pool == "" || appData.Name == "" { - return nil, errors.Errorf("empty data for app %q", appName) + return nil, fmt.Errorf("empty data for app %q", appName) } return &appData, nil diff --git a/config/crd/bases/extensions.tsuru.io_acldnsentries.yaml b/config/crd/bases/extensions.tsuru.io_acldnsentries.yaml index d39582e..484b11e 100644 --- a/config/crd/bases/extensions.tsuru.io_acldnsentries.yaml +++ b/config/crd/bases/extensions.tsuru.io_acldnsentries.yaml @@ -42,6 +42,10 @@ spec: spec: description: ACLDNSEntrySpec defines the desired state of ACLDNSEntry properties: + additionalIPs: + items: + type: string + type: array host: type: string required: diff --git a/config/crd/bases/extensions.tsuru.io_acls.yaml b/config/crd/bases/extensions.tsuru.io_acls.yaml index b24bbeb..d532edc 100644 --- a/config/crd/bases/extensions.tsuru.io_acls.yaml +++ b/config/crd/bases/extensions.tsuru.io_acls.yaml @@ -112,6 +112,8 @@ spec: type: object tsuruApp: type: string + tsuruJob: + type: string type: object required: - destinations diff --git a/config/crd/bases/extensions.tsuru.io_tsuruappaddresses.yaml b/config/crd/bases/extensions.tsuru.io_tsuruappaddresses.yaml index f08d9bd..7f444f1 100644 --- a/config/crd/bases/extensions.tsuru.io_tsuruappaddresses.yaml +++ b/config/crd/bases/extensions.tsuru.io_tsuruappaddresses.yaml @@ -42,6 +42,10 @@ spec: spec: description: TsuruAppAddressSpec defines the desired state of TsuruAppAddress properties: + additionalIPs: + items: + type: string + type: array name: type: string type: object diff --git a/controllers/acl_controller.go b/controllers/acl_controller.go index 3f75c2e..4790d31 100644 --- a/controllers/acl_controller.go +++ b/controllers/acl_controller.go @@ -28,9 +28,7 @@ import ( "sync/atomic" "time" - "github.com/pkg/errors" tsuruErrors "github.com/tsuru/tsuru/errors" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" @@ -84,7 +82,7 @@ func (r *ACLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R l := log.FromContext(ctx) acl := &v1alpha1.ACL{} - err := r.Client.Get(ctx, req.NamespacedName, acl) + err := r.Get(ctx, req.NamespacedName, acl) if k8sErrors.IsNotFound(err) { } else if err != nil { l.Error(err, "could not get ACL object") @@ -99,7 +97,7 @@ func (r *ACLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R networkPolicyName = "acl-" + req.Name } - err = r.Client.Get(ctx, client.ObjectKey{ + err = r.Get(ctx, client.ObjectKey{ Namespace: req.Namespace, Name: networkPolicyName, }, networkPolicy) @@ -112,11 +110,11 @@ func (r *ACLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R networkPolicyHasChanges := false statusNeedsUpdate := false - networkPolicy.ObjectMeta.Namespace = acl.ObjectMeta.Namespace - networkPolicy.ObjectMeta.Name = networkPolicyName + networkPolicy.Namespace = acl.Namespace + networkPolicy.Name = networkPolicyName if len(networkPolicy.OwnerReferences) == 0 { - networkPolicy.OwnerReferences = []v1.OwnerReference{ + networkPolicy.OwnerReferences = []metav1.OwnerReference{ *metav1.NewControllerRef(acl, acl.GroupVersionKind()), } @@ -163,7 +161,7 @@ func (r *ACLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R ruleIDErrors[destination.RuleID] = err.Error() egressRules = mapStaleEgress[destination.RuleID] // try to use stale ruleIDDestinations[destination.RuleID] = copyEgressRules(egressRules) - } else if err == nil && destination.RuleID != "" { + } else if destination.RuleID != "" { ruleIDDestinations[destination.RuleID] = copyEgressRules(egressRules) } @@ -218,7 +216,7 @@ func (r *ACLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R } if networkPolicy.CreationTimestamp.IsZero() { - err = r.Client.Create(ctx, networkPolicy) + err = r.Create(ctx, networkPolicy) if err != nil { l.Error(err, "could not create NetworkPolicy object") statusErr := r.setUnreadyStatus(ctx, acl, "could not create NetworkPolicy object, err: "+err.Error()) @@ -235,7 +233,7 @@ func (r *ACLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R statusNeedsUpdate = true } else if networkPolicyHasChanges { - err = r.Client.Update(ctx, networkPolicy) + err = r.Update(ctx, networkPolicy) if err != nil { l.Error(err, "could not update NetworkPolicy object") statusErr := r.setUnreadyStatus(ctx, acl, "could not update NetworkPolicy object, err: "+err.Error()) @@ -252,7 +250,7 @@ func (r *ACLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R } if statusNeedsUpdate { - err = r.Client.Status().Update(ctx, acl) + err = r.Status().Update(ctx, acl) if err != nil { l.Error(err, "could not update status for ACL object") return ctrl.Result{}, err @@ -271,7 +269,7 @@ func (r *ACLReconciler) setUnreadyStatus(ctx context.Context, acl *v1alpha1.ACL, acl.Status.Ready = false acl.Status.Reason = reason - err := r.Client.Status().Update(ctx, acl) + err := r.Status().Update(ctx, acl) if err != nil { l.Error(err, "could not update acl status") } @@ -326,7 +324,6 @@ func (r *ACLReconciler) egressRulesForTsuruApp(ctx context.Context, tsuruApp str } existingTsuruAppAddress, err := r.ensureTsuruAppAddress(ctx, tsuruApp) - if err != nil { l.Error(err, "could not get TsuruAppAddress", "appName", tsuruApp) return nil, err @@ -377,9 +374,8 @@ func (r *ACLReconciler) egressRulesForResourceAddressStatus(ctx context.Context, addrEgresses, err := r.egressRulesForExternalIP(ctx, &v1alpha1.ACLSpecExternalIP{ IP: routerIP, }) - if err != nil { - errs = append(errs, errors.Wrapf(err, "could not generate egress rule for: %q", routerIP)) + errs = append(errs, fmt.Errorf("could not generate egress rule for %q: %w", routerIP, err)) } egresses = append(egresses, addrEgresses...) @@ -388,7 +384,7 @@ func (r *ACLReconciler) egressRulesForResourceAddressStatus(ctx context.Context, return egresses, errs } -func (r *ACLReconciler) egressRulesForTsuruAppPool(ctx context.Context, tsuruAppPool string) ([]netv1.NetworkPolicyEgressRule, error) { +func (r *ACLReconciler) egressRulesForTsuruAppPool(_ context.Context, tsuruAppPool string) ([]netv1.NetworkPolicyEgressRule, error) { egress := []netv1.NetworkPolicyEgressRule{ { To: []netv1.NetworkPolicyPeer{ @@ -426,7 +422,6 @@ func (r *ACLReconciler) egressRulesForExternalDNS(ctx context.Context, externalD } existingDNSEntry, err := r.ensureDNSEntry(ctx, externalDNS.Name) - if err != nil { l.Error(err, "could not get ACLDNSEntry", "destination", externalDNS.Name) return nil, err @@ -481,7 +476,7 @@ func ipToCIDR(address string) string { return "" } -func (r *ACLReconciler) egressRulesForExternalIP(ctx context.Context, externalIP *v1alpha1.ACLSpecExternalIP) ([]netv1.NetworkPolicyEgressRule, error) { +func (r *ACLReconciler) egressRulesForExternalIP(_ context.Context, externalIP *v1alpha1.ACLSpecExternalIP) ([]netv1.NetworkPolicyEgressRule, error) { var cidr string cidr = externalIP.IP @@ -526,7 +521,6 @@ func (r *ACLReconciler) egressRulesForRpaasInstance(ctx context.Context, rpaasIn } existingRpaasInstanceAddress, err := r.ensureRpaasInstanceAddress(ctx, rpaasInstance) - if err != nil { l.Error(err, "could not get RpaasInstanceAddress", "rpaasInstance", rpaasInstance.Instance, @@ -562,7 +556,7 @@ func (r *ACLReconciler) ensureDNSEntry(ctx context.Context, host string) (*v1alp existingDNSEntry := &v1alpha1.ACLDNSEntry{} resourceName := validResourceName(host) - err := r.Client.Get(ctx, types.NamespacedName{ + err := r.Get(ctx, types.NamespacedName{ Name: resourceName, }, existingDNSEntry) @@ -576,7 +570,7 @@ func (r *ACLReconciler) ensureDNSEntry(ctx context.Context, host string) (*v1alp }, } - err = r.Client.Create(ctx, dnsEntry) + err = r.Create(ctx, dnsEntry) if err != nil { l.Error(err, "could not create ACLDNSEntry object") return nil, err @@ -588,12 +582,16 @@ func (r *ACLReconciler) ensureDNSEntry(ctx context.Context, host string) (*v1alp Resolver: r.Resolver, } + operationStart := time.Now() err = subReconciler.FillStatus(ctx, dnsEntry) - + operationDuration := time.Since(operationStart) + subReconcilerTime.WithLabelValues("acl", "acldnsentry").Observe(operationDuration.Seconds()) if err != nil { + subReconcilerTotal.WithLabelValues("acl", "acldnsentry", "error").Inc() l.Error(err, "could not fill status for DNSEntry", "dnsEntryName", resourceName) return nil, err } + subReconcilerTotal.WithLabelValues("acl", "acldnsentry", "success").Inc() return dnsEntry, nil } else if err != nil { @@ -609,7 +607,7 @@ func (r *ACLReconciler) ensureTsuruAppAddress(ctx context.Context, appName strin existingTsuruAppAddress := &v1alpha1.TsuruAppAddress{} resourceName := validResourceName(appName) - err := r.Client.Get(ctx, types.NamespacedName{ + err := r.Get(ctx, types.NamespacedName{ Name: resourceName, }, existingTsuruAppAddress) @@ -623,7 +621,7 @@ func (r *ACLReconciler) ensureTsuruAppAddress(ctx context.Context, appName strin }, } - err = r.Client.Create(ctx, tsuruAppAddress) + err = r.Create(ctx, tsuruAppAddress) if err != nil { l.Error(err, "could not create ACLDNSEntry object") return nil, err @@ -636,11 +634,16 @@ func (r *ACLReconciler) ensureTsuruAppAddress(ctx context.Context, appName strin TsuruAPI: r.TsuruAPI, } + operationStart := time.Now() err = subReconciler.FillStatus(ctx, tsuruAppAddress) + operationDuration := time.Since(operationStart) + subReconcilerTime.WithLabelValues("acl", "tsuruappaddress").Observe(operationDuration.Seconds()) if err != nil { + subReconcilerTotal.WithLabelValues("acl", "tsuruappaddress", "error").Inc() l.Error(err, "could not fill status of TsuruAppAddress", "tsuruAppName", resourceName) return nil, err } + subReconcilerTotal.WithLabelValues("acl", "tsuruappaddress", "success").Inc() return tsuruAppAddress, nil } else if err != nil { @@ -656,7 +659,7 @@ func (r *ACLReconciler) ensureRpaasInstanceAddress(ctx context.Context, rpaasIns existingRpaasInstanceAddress := &v1alpha1.RpaasInstanceAddress{} resourceName := validResourceName(rpaasInstance.ServiceName + "-" + rpaasInstance.Instance) - err := r.Client.Get(ctx, types.NamespacedName{ + err := r.Get(ctx, types.NamespacedName{ Name: resourceName, }, existingRpaasInstanceAddress) @@ -671,7 +674,7 @@ func (r *ACLReconciler) ensureRpaasInstanceAddress(ctx context.Context, rpaasIns }, } - err = r.Client.Create(ctx, rpaasInstanceAddress) + err = r.Create(ctx, rpaasInstanceAddress) if err != nil { l.Error(err, "could not create RpaasInstanceAddress object") return nil, err @@ -684,12 +687,16 @@ func (r *ACLReconciler) ensureRpaasInstanceAddress(ctx context.Context, rpaasIns TsuruAPI: r.TsuruAPI, } + operationStart := time.Now() err = subReconciler.FillStatus(ctx, rpaasInstanceAddress) - + operationDuration := time.Since(operationStart) + subReconcilerTime.WithLabelValues("acl", "rpaasinstanceaddress").Observe(operationDuration.Seconds()) if err != nil { + subReconcilerTotal.WithLabelValues("acl", "rpaasinstanceaddress", "error").Inc() l.Error(err, "could not fill status of RpaasInstanceAddress", "name", resourceName) return nil, err } + subReconcilerTotal.WithLabelValues("acl", "rpaasinstanceaddress", "success").Inc() return rpaasInstanceAddress, err } else if err != nil { l.Error(err, "could not get RpaasInstanceAddress", "name", resourceName) @@ -800,7 +807,6 @@ func (r *ACLReconciler) SetupWithManager(mgr ctrl.Manager) error { WithOptions(controller.Options{MaxConcurrentReconciles: 4, RecoverPanic: true}). Owns(&netv1.NetworkPolicy{}). Build(r) - if err != nil { return err } @@ -903,7 +909,6 @@ func (r *ACLReconciler) setupWatchers(ctrl controller.Controller) error { value := rpaasInstanceAddress.Spec.ServiceName + "/" + rpaasInstanceAddress.Spec.Instance return r.reconcileRequestsForIndex(rpaasInstanceIndex, value) - }), ) if err != nil { @@ -929,10 +934,9 @@ func (r *ACLReconciler) setupWatchers(ctrl controller.Controller) error { func (r *ACLReconciler) reconcileRequestsForIndex(index, value string) []reconcile.Request { list := &v1alpha1.ACLList{} - err := r.Client.List(context.Background(), list, &client.ListOptions{FieldSelector: fields.SelectorFromSet(fields.Set{ + err := r.List(context.Background(), list, &client.ListOptions{FieldSelector: fields.SelectorFromSet(fields.Set{ index: value, })}) - if err != nil { log.Log.Error(err, "could not list ACLs") return nil diff --git a/controllers/acl_controller_test.go b/controllers/acl_controller_test.go index 17e1627..16583f2 100644 --- a/controllers/acl_controller_test.go +++ b/controllers/acl_controller_test.go @@ -16,7 +16,6 @@ import ( corev1 "k8s.io/api/core/v1" netv1 "k8s.io/api/networking/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/validation" @@ -28,7 +27,7 @@ import ( func (suite *ControllerSuite) TestACLReconcilerSimpleReconcile() { ctx := context.Background() acl := &v1alpha1.ACL{ - ObjectMeta: v1.ObjectMeta{ + ObjectMeta: metav1.ObjectMeta{ Name: "myapp", Namespace: "default", }, @@ -161,7 +160,7 @@ func (suite *ControllerSuite) TestACLReconcilerSimpleReconcile() { func (suite *ControllerSuite) TestACLReconcilerStaleReconcile() { ctx := context.Background() acl := &v1alpha1.ACL{ - ObjectMeta: v1.ObjectMeta{ + ObjectMeta: metav1.ObjectMeta{ Name: "myapp", Namespace: "default", }, @@ -263,7 +262,7 @@ func (suite *ControllerSuite) TestACLReconcilerStaleReconcile() { func (suite *ControllerSuite) TestACLReconcilerDestinationAppReconcile() { ctx := context.Background() acl := &v1alpha1.ACL{ - ObjectMeta: v1.ObjectMeta{ + ObjectMeta: metav1.ObjectMeta{ Name: "myapp", Namespace: "default", }, @@ -280,7 +279,7 @@ func (suite *ControllerSuite) TestACLReconcilerDestinationAppReconcile() { } dnsEntry1 := &v1alpha1.ACLDNSEntry{ - ObjectMeta: v1.ObjectMeta{ + ObjectMeta: metav1.ObjectMeta{ Name: "myapp.io", }, Spec: v1alpha1.ACLDNSEntrySpec{ @@ -297,7 +296,7 @@ func (suite *ControllerSuite) TestACLReconcilerDestinationAppReconcile() { }, } dnsEntry2 := &v1alpha1.ACLDNSEntry{ - ObjectMeta: v1.ObjectMeta{ + ObjectMeta: metav1.ObjectMeta{ Name: "http.myapp.io", }, Spec: v1alpha1.ACLDNSEntrySpec{ @@ -315,7 +314,7 @@ func (suite *ControllerSuite) TestACLReconcilerDestinationAppReconcile() { } tsuruAppAddress := &v1alpha1.TsuruAppAddress{ - ObjectMeta: v1.ObjectMeta{ + ObjectMeta: metav1.ObjectMeta{ Name: "my-other-app", }, Spec: v1alpha1.TsuruAppAddressSpec{ @@ -336,7 +335,7 @@ func (suite *ControllerSuite) TestACLReconcilerDestinationAppReconcile() { // 1.1.1.1 is also running on kubernetes svc := &corev1.Service{ - ObjectMeta: v1.ObjectMeta{ + ObjectMeta: metav1.ObjectMeta{ Name: "my-awesome-service", Namespace: "default", }, @@ -446,7 +445,7 @@ func (suite *ControllerSuite) TestACLReconcilerDestinationAppReconcile() { func (suite *ControllerSuite) TestACLReconcilerDestinationExternalDNSReconcile() { ctx := context.Background() acl := &v1alpha1.ACL{ - ObjectMeta: v1.ObjectMeta{ + ObjectMeta: metav1.ObjectMeta{ Name: "myapp", Namespace: "default", }, @@ -465,7 +464,7 @@ func (suite *ControllerSuite) TestACLReconcilerDestinationExternalDNSReconcile() } dnsEntry1 := &v1alpha1.ACLDNSEntry{ - ObjectMeta: v1.ObjectMeta{ + ObjectMeta: metav1.ObjectMeta{ Name: "myapp.io", }, Spec: v1alpha1.ACLDNSEntrySpec{ @@ -533,7 +532,7 @@ func (suite *ControllerSuite) TestACLReconcilerDestinationExternalDNSReconcile() func (suite *ControllerSuite) TestACLReconcilerDestinationRPaaSReconcile() { ctx := context.Background() acl := &v1alpha1.ACL{ - ObjectMeta: v1.ObjectMeta{ + ObjectMeta: metav1.ObjectMeta{ Name: "myapp", Namespace: "default", }, @@ -553,7 +552,7 @@ func (suite *ControllerSuite) TestACLReconcilerDestinationRPaaSReconcile() { } dnsEntry1 := &v1alpha1.ACLDNSEntry{ - ObjectMeta: v1.ObjectMeta{ + ObjectMeta: metav1.ObjectMeta{ Name: "myapp.io", }, Spec: v1alpha1.ACLDNSEntrySpec{ @@ -570,7 +569,7 @@ func (suite *ControllerSuite) TestACLReconcilerDestinationRPaaSReconcile() { }, } dnsEntry2 := &v1alpha1.ACLDNSEntry{ - ObjectMeta: v1.ObjectMeta{ + ObjectMeta: metav1.ObjectMeta{ Name: "http.myapp.io", }, Spec: v1alpha1.ACLDNSEntrySpec{ @@ -635,11 +634,9 @@ func (suite *ControllerSuite) TestACLReconcilerDestinationRPaaSReconcile() { CIDR: "3.3.3.3/32", }, }, existingNP.Spec.Egress[1].To[0]) - } -type fakeTsuruAPI struct { -} +type fakeTsuruAPI struct{} func (f *fakeTsuruAPI) AppInfo(ctx context.Context, appName string) (*app.App, error) { if appName == "my-other-app" { @@ -697,5 +694,4 @@ func TestValidResourceName(t *testing.T) { assert.Len(t, errs, 0) } - } diff --git a/controllers/acl_dns_entry_controller.go b/controllers/acl_dns_entry_controller.go index fc413a4..52998bb 100644 --- a/controllers/acl_dns_entry_controller.go +++ b/controllers/acl_dns_entry_controller.go @@ -30,8 +30,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/log" - "github.com/tsuru/acl-operator/api/v1alpha1" - extensionstsuruiov1alpha1 "github.com/tsuru/acl-operator/api/v1alpha1" + aclv1alpha1 "github.com/tsuru/acl-operator/api/v1alpha1" ) const dayFormat = "2006-01-02" @@ -56,7 +55,7 @@ type ACLDNSEntryReconciler struct { func (r *ACLDNSEntryReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { l := log.FromContext(ctx) - dnsEntry := &v1alpha1.ACLDNSEntry{} + dnsEntry := &aclv1alpha1.ACLDNSEntry{} err := r.Client.Get(ctx, req.NamespacedName, dnsEntry) if k8sErrors.IsNotFound(err) { @@ -69,7 +68,6 @@ func (r *ACLDNSEntryReconciler) Reconcile(ctx context.Context, req ctrl.Request) existingStatus := dnsEntry.Status.DeepCopy() err = r.FillStatus(ctx, dnsEntry) - if err != nil { l.Error(err, "could not resolve address", "host", dnsEntry.Spec.Host) @@ -98,11 +96,10 @@ func (r *ACLDNSEntryReconciler) Reconcile(ctx context.Context, req ctrl.Request) return ctrl.Result{}, nil } -func (r *ACLDNSEntryReconciler) FillStatus(ctx context.Context, dnsEntry *v1alpha1.ACLDNSEntry) error { +func (r *ACLDNSEntryReconciler) FillStatus(ctx context.Context, dnsEntry *aclv1alpha1.ACLDNSEntry) error { timoutCtx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() ipAddrs, err := r.Resolver.LookupIPAddr(timoutCtx, dnsEntry.Spec.Host) - if err != nil { return err } @@ -124,7 +121,7 @@ statusLoop: } for _, foundIP := range missingIpAddrs { - dnsEntry.Status.IPs = append(dnsEntry.Status.IPs, extensionstsuruiov1alpha1.ACLDNSEntryStatusIP{ + dnsEntry.Status.IPs = append(dnsEntry.Status.IPs, aclv1alpha1.ACLDNSEntryStatusIP{ Address: foundIP.IP.String(), ValidUntil: validUntil.Format(dayFormat), }) @@ -153,7 +150,7 @@ statusLoop: // SetupWithManager sets up the controller with the Manager. func (r *ACLDNSEntryReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). - For(&extensionstsuruiov1alpha1.ACLDNSEntry{}). + For(&aclv1alpha1.ACLDNSEntry{}). WithOptions(controller.Options{MaxConcurrentReconciles: 4, RecoverPanic: true}). Complete(r) } diff --git a/controllers/acl_dns_entry_controller_test.go b/controllers/acl_dns_entry_controller_test.go index 41a3e9b..316dcde 100644 --- a/controllers/acl_dns_entry_controller_test.go +++ b/controllers/acl_dns_entry_controller_test.go @@ -49,7 +49,6 @@ func (f *fakeResolver) LookupIPAddr(ctx context.Context, host string) ([]net.IPA if host == "timeout.com.br" { return nil, errors.New("timeout for host") - } return nil, errors.New("no mocks for host") @@ -79,7 +78,7 @@ func (suite *ControllerSuite) TestACLDNSEntryReconcilerSimpleReconcile() { suite.Require().NoError(err) existingResolver := &v1alpha1.ACLDNSEntry{} - err = reconciler.Client.Get(ctx, client.ObjectKeyFromObject(resolver), existingResolver) + err = reconciler.Get(ctx, client.ObjectKeyFromObject(resolver), existingResolver) suite.Require().NoError(err) suite.Assert().True(existingResolver.Status.Ready) @@ -128,7 +127,7 @@ func (suite *ControllerSuite) TestACLDNSEntryReconcilerSimpleReconcileExisting() suite.Require().NoError(err) existingResolver := &v1alpha1.ACLDNSEntry{} - err = reconciler.Client.Get(ctx, client.ObjectKeyFromObject(resolver), existingResolver) + err = reconciler.Get(ctx, client.ObjectKeyFromObject(resolver), existingResolver) suite.Require().NoError(err) suite.Assert().True(existingResolver.Status.Ready) @@ -162,7 +161,7 @@ func (suite *ControllerSuite) TestACLDNSEntryReconcilerTimeoutReconcile() { suite.Require().NoError(err) existingResolver := &v1alpha1.ACLDNSEntry{} - err = reconciler.Client.Get(ctx, client.ObjectKeyFromObject(resolver), existingResolver) + err = reconciler.Get(ctx, client.ObjectKeyFromObject(resolver), existingResolver) suite.Require().NoError(err) suite.Require().Len(existingResolver.Status.IPs, 0) diff --git a/controllers/acl_garbage_collector.go b/controllers/acl_garbage_collector.go index e7990c0..98cc1c4 100644 --- a/controllers/acl_garbage_collector.go +++ b/controllers/acl_garbage_collector.go @@ -25,6 +25,7 @@ type appACLKey struct { App string Namespace string } + type jobACLKey struct { Job string Namespace string @@ -43,17 +44,13 @@ func (a *ACLGarbageCollector) Run(ctx context.Context) { } func (a *ACLGarbageCollector) Loop(ctx context.Context) error { - appACLs := map[appACLKey]struct{}{} jobACLs := map[jobACLKey]struct{}{} - dnsEntries := map[string]struct{}{} - tsuruApps := map[string]struct{}{} - rpaaInstances := map[v1alpha1.ACLSpecRpaasInstance]string{} allDNSEntries, err := a.allDNSEntries(ctx) if err != nil { return err } - dnsEntries = make(map[string]struct{}, len(allDNSEntries)) + dnsEntries := make(map[string]struct{}, len(allDNSEntries)) for _, dnsEntry := range allDNSEntries { dnsEntries[dnsEntry.Spec.Host] = struct{}{} } @@ -62,8 +59,8 @@ func (a *ACLGarbageCollector) Loop(ctx context.Context) error { if err != nil { return err } - tsuruApps = make(map[string]struct{}, len(allTsuruAppAddress)) - appACLs = make(map[appACLKey]struct{}, len(allTsuruAppAddress)) // fair aproximation + tsuruApps := make(map[string]struct{}, len(allTsuruAppAddress)) + appACLs := make(map[appACLKey]struct{}, len(allTsuruAppAddress)) // fair aproximation for _, tsuruAppAddress := range allTsuruAppAddress { tsuruApps[tsuruAppAddress.Spec.Name] = struct{}{} } @@ -72,14 +69,14 @@ func (a *ACLGarbageCollector) Loop(ctx context.Context) error { if err != nil { return err } - rpaaInstances = make(map[v1alpha1.ACLSpecRpaasInstance]string, len(allRPaaSInstancesAddresses)) + rpaaInstances := make(map[v1alpha1.ACLSpecRpaasInstance]string, len(allRPaaSInstancesAddresses)) for _, rpaaInstanceAddress := range allRPaaSInstancesAddresses { key := v1alpha1.ACLSpecRpaasInstance{ ServiceName: rpaaInstanceAddress.Spec.ServiceName, Instance: rpaaInstanceAddress.Spec.Instance, } - rpaaInstances[key] = rpaaInstanceAddress.ObjectMeta.Name + rpaaInstances[key] = rpaaInstanceAddress.Name } allACLSs, err := a.allACLs(ctx) @@ -174,7 +171,7 @@ func (a *ACLGarbageCollector) Loop(ctx context.Context) error { } for dnsEntry := range dnsEntries { - err = a.Client.Delete(ctx, &v1alpha1.ACLDNSEntry{ + err = a.Delete(ctx, &v1alpha1.ACLDNSEntry{ ObjectMeta: v1.ObjectMeta{ Name: validResourceName(dnsEntry), }, @@ -185,7 +182,7 @@ func (a *ACLGarbageCollector) Loop(ctx context.Context) error { } for tsuruApp := range tsuruApps { - err = a.Client.Delete(ctx, &v1alpha1.TsuruAppAddress{ + err = a.Delete(ctx, &v1alpha1.TsuruAppAddress{ ObjectMeta: v1.ObjectMeta{ Name: validResourceName(tsuruApp), }, @@ -196,7 +193,7 @@ func (a *ACLGarbageCollector) Loop(ctx context.Context) error { } for _, rpaasInstanceName := range rpaaInstances { - err = a.Client.Delete(ctx, &v1alpha1.RpaasInstanceAddress{ + err = a.Delete(ctx, &v1alpha1.RpaasInstanceAddress{ ObjectMeta: v1.ObjectMeta{ Name: rpaasInstanceName, }, @@ -207,7 +204,7 @@ func (a *ACLGarbageCollector) Loop(ctx context.Context) error { } for appACL := range appACLs { - err = a.Client.Delete(ctx, &v1alpha1.ACL{ + err = a.Delete(ctx, &v1alpha1.ACL{ ObjectMeta: v1.ObjectMeta{ Namespace: appACL.Namespace, Name: appACL.App, @@ -219,7 +216,7 @@ func (a *ACLGarbageCollector) Loop(ctx context.Context) error { } for jobACL := range jobACLs { - err = a.Client.Delete(ctx, &v1alpha1.ACL{ + err = a.Delete(ctx, &v1alpha1.ACL{ ObjectMeta: v1.ObjectMeta{ Namespace: jobACL.Namespace, Name: tsuruJobACLPrefix + jobACL.Job, @@ -241,7 +238,7 @@ func (a *ACLGarbageCollector) allACLs(ctx context.Context) ([]v1alpha1.ACL, erro for { allACLSs := &v1alpha1.ACLList{} - err := a.Client.List(ctx, allACLSs, &client.ListOptions{ + err := a.List(ctx, allACLSs, &client.ListOptions{ Continue: continueToken, }) if err != nil { @@ -267,7 +264,7 @@ func (a *ACLGarbageCollector) allDNSEntries(ctx context.Context) ([]v1alpha1.ACL for { allDNSEntries := &v1alpha1.ACLDNSEntryList{} - err := a.Client.List(ctx, allDNSEntries, &client.ListOptions{ + err := a.List(ctx, allDNSEntries, &client.ListOptions{ Continue: continueToken, }) if err != nil { @@ -293,7 +290,7 @@ func (a *ACLGarbageCollector) allTsuruAppAddress(ctx context.Context) ([]v1alpha for { allTsuruAppAddress := &v1alpha1.TsuruAppAddressList{} - err := a.Client.List(ctx, allTsuruAppAddress, &client.ListOptions{ + err := a.List(ctx, allTsuruAppAddress, &client.ListOptions{ Continue: continueToken, }) if err != nil { @@ -319,7 +316,7 @@ func (a *ACLGarbageCollector) allRPaaSInstancesAddresses(ctx context.Context) ([ for { allRPaaSInstancesAddress := &v1alpha1.RpaasInstanceAddressList{} - err := a.Client.List(ctx, allRPaaSInstancesAddress, &client.ListOptions{ + err := a.List(ctx, allRPaaSInstancesAddress, &client.ListOptions{ Continue: continueToken, }) if err != nil { @@ -345,7 +342,7 @@ func (a *ACLGarbageCollector) allTsuruApps(ctx context.Context) ([]tsuruv1.App, for { allTsuruApps := &tsuruv1.AppList{} - err := a.Client.List(ctx, allTsuruApps, &client.ListOptions{ + err := a.List(ctx, allTsuruApps, &client.ListOptions{ Continue: continueToken, }) if err != nil { @@ -371,7 +368,7 @@ func (a *ACLGarbageCollector) allTsuruJobs(ctx context.Context) ([]batchv1.CronJ for { allTsuruJobs := &batchv1.CronJobList{} - err := a.Client.List(ctx, allTsuruJobs, &client.ListOptions{ + err := a.List(ctx, allTsuruJobs, &client.ListOptions{ Continue: continueToken, }) if err != nil { diff --git a/controllers/metrics.go b/controllers/metrics.go new file mode 100644 index 0000000..c86a7f2 --- /dev/null +++ b/controllers/metrics.go @@ -0,0 +1,22 @@ +package controllers + +import ( + "github.com/prometheus/client_golang/prometheus" + "sigs.k8s.io/controller-runtime/pkg/metrics" +) + +var subReconcilerTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ + Name: "subreconciler_reconcile_total", + Help: "Total number of reconciliations per subcontroller", +}, []string{"controller", "subcontroller", "result"}) + +var subReconcilerTime = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Name: "subreconciler_reconcile_time_seconds", + Help: "Length of time per reconciliation per subcontroller per controller", + Buckets: prometheus.ExponentialBuckets(0.001, 2, 20), +}, []string{"controller", "subccontroller"}) + +func init() { + metrics.Registry.MustRegister(subReconcilerTotal) + metrics.Registry.MustRegister(subReconcilerTime) +} diff --git a/controllers/rpaas_instance_address_controller.go b/controllers/rpaas_instance_address_controller.go index 8e4bc96..cece4df 100644 --- a/controllers/rpaas_instance_address_controller.go +++ b/controllers/rpaas_instance_address_controller.go @@ -29,12 +29,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/log" - extensionstsuruiov1alpha1 "github.com/tsuru/acl-operator/api/v1alpha1" - v1alpha1 "github.com/tsuru/acl-operator/api/v1alpha1" + aclv1alpha1 "github.com/tsuru/acl-operator/api/v1alpha1" "github.com/tsuru/acl-operator/clients/tsuruapi" ) -var errInstanceNotFound = errors.New("Service instance not found") +var errInstanceNotFound = errors.New("service instance not found") // RpaasInstanceAddressReconciler reconciles a RpaasInstanceAddress object type RpaasInstanceAddressReconciler struct { @@ -51,8 +50,8 @@ type RpaasInstanceAddressReconciler struct { func (r *RpaasInstanceAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { l := log.FromContext(ctx) - rpaasInstanceAddress := &v1alpha1.RpaasInstanceAddress{} - err := r.Client.Get(ctx, req.NamespacedName, rpaasInstanceAddress) + rpaasInstanceAddress := &aclv1alpha1.RpaasInstanceAddress{} + err := r.Get(ctx, req.NamespacedName, rpaasInstanceAddress) if k8sErrors.IsNotFound(err) { return ctrl.Result{}, nil } else if err != nil { @@ -62,7 +61,6 @@ func (r *RpaasInstanceAddressReconciler) Reconcile(ctx context.Context, req ctrl oldStatus := rpaasInstanceAddress.Status.DeepCopy() err = r.FillStatus(ctx, rpaasInstanceAddress) - if err != nil { rpaasInstanceAddress.Status.Ready = false rpaasInstanceAddress.Status.Reason = err.Error() @@ -88,9 +86,8 @@ func (r *RpaasInstanceAddressReconciler) Reconcile(ctx context.Context, req ctrl return ctrl.Result{}, nil } -func (r *RpaasInstanceAddressReconciler) FillStatus(ctx context.Context, rpaasInstanceAddress *v1alpha1.RpaasInstanceAddress) error { +func (r *RpaasInstanceAddressReconciler) FillStatus(ctx context.Context, rpaasInstanceAddress *aclv1alpha1.RpaasInstanceAddress) error { serviceInfo, err := r.TsuruAPI.ServiceInstanceInfo(ctx, rpaasInstanceAddress.Spec.ServiceName, rpaasInstanceAddress.Spec.Instance) - if err != nil { return err } @@ -131,7 +128,7 @@ func (r *RpaasInstanceAddressReconciler) FillStatus(ctx context.Context, rpaasIn // SetupWithManager sets up the controller with the Manager. func (r *RpaasInstanceAddressReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). - For(&extensionstsuruiov1alpha1.RpaasInstanceAddress{}). + For(&aclv1alpha1.RpaasInstanceAddress{}). WithOptions(controller.Options{MaxConcurrentReconciles: 2, RecoverPanic: true}). Complete(r) } diff --git a/controllers/rpaas_instance_controller.go b/controllers/rpaas_instance_controller.go index 6de9fa7..fe5ef29 100644 --- a/controllers/rpaas_instance_controller.go +++ b/controllers/rpaas_instance_controller.go @@ -25,7 +25,6 @@ import ( k8sErrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -47,7 +46,7 @@ func (r *RpaasInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques rpaasInstance := &rpaasv1alpha1.RpaasInstance{} - err := r.Client.Get(ctx, req.NamespacedName, rpaasInstance) + err := r.Get(ctx, req.NamespacedName, rpaasInstance) if err != nil { l.Error(err, "could not get RPaaS Instance object") return ctrl.Result{}, err @@ -63,7 +62,7 @@ func (r *RpaasInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques } acl := &v1alpha1.ACL{} - err = r.Client.Get(ctx, client.ObjectKey{ + err = r.Get(ctx, client.ObjectKey{ Name: rpaasInstance.Name, Namespace: rpaasInstance.Namespace, }, acl) @@ -73,11 +72,11 @@ func (r *RpaasInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques return ctrl.Result{}, nil } - err = r.Client.Create(ctx, &v1alpha1.ACL{ + err = r.Create(ctx, &v1alpha1.ACL{ ObjectMeta: metav1.ObjectMeta{ Name: rpaasInstance.Name, Namespace: rpaasInstance.Namespace, - OwnerReferences: []v1.OwnerReference{ + OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(rpaasInstance, rpaasInstance.GroupVersionKind()), }, }, @@ -94,7 +93,6 @@ func (r *RpaasInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques WarningErrors: warningErrors, }, }) - if err != nil { return ctrl.Result{}, err } @@ -107,15 +105,14 @@ func (r *RpaasInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques l.Error(err, "could not get ACL object") return ctrl.Result{}, err } else if len(destinations) == 0 { - err = r.Client.Delete(ctx, acl) - + err = r.Delete(ctx, acl) if err != nil { l.Error(err, "could not remove unused ACL") } return ctrl.Result{}, nil } - acl.OwnerReferences = []v1.OwnerReference{ + acl.OwnerReferences = []metav1.OwnerReference{ *metav1.NewControllerRef(rpaasInstance, rpaasInstance.GroupVersionKind()), } @@ -127,7 +124,7 @@ func (r *RpaasInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques } acl.Spec.Destinations = destinations - err = r.Client.Update(ctx, acl) + err = r.Update(ctx, acl) if err != nil { return ctrl.Result{}, err } @@ -135,7 +132,7 @@ func (r *RpaasInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques if len(warningErrors) > 0 || len(acl.Status.WarningErrors) > 0 { acl.Status.WarningErrors = warningErrors - err := r.Client.Status().Update(ctx, acl) + err := r.Status().Update(ctx, acl) if err != nil { l.Error(err, "could not remove update status of ACL") return ctrl.Result{}, err diff --git a/controllers/rpaas_instance_controller_test.go b/controllers/rpaas_instance_controller_test.go index 770fe15..a1d2455 100644 --- a/controllers/rpaas_instance_controller_test.go +++ b/controllers/rpaas_instance_controller_test.go @@ -44,7 +44,7 @@ func (suite *ControllerSuite) TestRPaaSInstanceReconcilerEmptyReconcile() { suite.Require().NoError(err) existingACL := &v1alpha1.ACL{} - err = reconciler.Client.Get(ctx, types.NamespacedName{ + err = reconciler.Get(ctx, types.NamespacedName{ Name: rpaasInstance.Name, Namespace: rpaasInstance.Namespace, }, existingACL) @@ -91,7 +91,7 @@ func (suite *ControllerSuite) TestRPaaSInstanceReconcilerDelete() { suite.Require().NoError(err) existingACL := &v1alpha1.ACL{} - err = reconciler.Client.Get(ctx, types.NamespacedName{ + err = reconciler.Get(ctx, types.NamespacedName{ Name: rpaasInstance.Name, Namespace: rpaasInstance.Namespace, }, existingACL) @@ -162,7 +162,7 @@ func (suite *ControllerSuite) TestRPaaSInstanceReconcilerSimpleReconcile() { suite.Require().NoError(err) existingACL := &v1alpha1.ACL{} - err = reconciler.Client.Get(ctx, types.NamespacedName{ + err = reconciler.Get(ctx, types.NamespacedName{ Name: rpaasInstance.Name, Namespace: rpaasInstance.Namespace, }, existingACL) @@ -324,7 +324,7 @@ func (suite *ControllerSuite) TestRPaaSInstanceReconcilerExistingObjectReconcile suite.Require().NoError(err) existingACL := &v1alpha1.ACL{} - err = reconciler.Client.Get(ctx, types.NamespacedName{ + err = reconciler.Get(ctx, types.NamespacedName{ Name: rpaasInstance.Name, Namespace: rpaasInstance.Namespace, }, existingACL) diff --git a/controllers/services_cache.go b/controllers/services_cache.go index 6c0178f..f1a28fa 100644 --- a/controllers/services_cache.go +++ b/controllers/services_cache.go @@ -2,9 +2,8 @@ package controllers import ( "context" - "time" - "sync/atomic" + "time" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -38,7 +37,7 @@ func (s *serviceCache) GetByIP(ctx context.Context, ip string) (*corev1.Service, func (s *serviceCache) fillCache(ctx context.Context) (*mapServiceCache, error) { allServices := corev1.ServiceList{} - err := s.Client.List(ctx, &allServices, &client.ListOptions{Namespace: metav1.NamespaceAll}) + err := s.List(ctx, &allServices, &client.ListOptions{Namespace: metav1.NamespaceAll}) if err != nil { return nil, err } diff --git a/controllers/tsuru_app_address_controller.go b/controllers/tsuru_app_address_controller.go index aabe769..26dd7eb 100644 --- a/controllers/tsuru_app_address_controller.go +++ b/controllers/tsuru_app_address_controller.go @@ -32,13 +32,12 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/log" - "github.com/tsuru/acl-operator/api/v1alpha1" - extensionstsuruiov1alpha1 "github.com/tsuru/acl-operator/api/v1alpha1" + aclv1alpha1 "github.com/tsuru/acl-operator/api/v1alpha1" "github.com/tsuru/acl-operator/clients/tsuruapi" tsuruNet "github.com/tsuru/tsuru/net" ) -var errAppNotFound = errors.New("App not found") +var errAppNotFound = errors.New("app not found") // TsuruAppAddressReconciler reconciles a TsuruAppAddress object type TsuruAppAddressReconciler struct { @@ -55,8 +54,8 @@ type TsuruAppAddressReconciler struct { func (r *TsuruAppAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { l := log.FromContext(ctx) - appAddress := &v1alpha1.TsuruAppAddress{} - err := r.Client.Get(ctx, req.NamespacedName, appAddress) + appAddress := &aclv1alpha1.TsuruAppAddress{} + err := r.Get(ctx, req.NamespacedName, appAddress) if k8sErrors.IsNotFound(err) { return ctrl.Result{}, nil } else if err != nil { @@ -72,14 +71,14 @@ func (r *TsuruAppAddressReconciler) Reconcile(ctx context.Context, req ctrl.Requ } if oldStatus.Pool != appAddress.Status.Pool || oldStatus.Ready != appAddress.Status.Ready || !reflect.DeepEqual(oldStatus.IPs, appAddress.Status.IPs) { - err = r.Client.Status().Update(ctx, appAddress) + err = r.Status().Update(ctx, appAddress) return ctrl.Result{}, err } return ctrl.Result{}, nil } -func (r *TsuruAppAddressReconciler) FillStatus(ctx context.Context, appAddress *v1alpha1.TsuruAppAddress) error { +func (r *TsuruAppAddressReconciler) FillStatus(ctx context.Context, appAddress *aclv1alpha1.TsuruAppAddress) error { appInfo, err := r.TsuruAPI.AppInfo(ctx, appAddress.Spec.Name) if err != nil { return err @@ -143,7 +142,7 @@ func (r *TsuruAppAddressReconciler) resolveAddress(ctx context.Context, addr str // SetupWithManager sets up the controller with the Manager. func (r *TsuruAppAddressReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). - For(&extensionstsuruiov1alpha1.TsuruAppAddress{}). + For(&aclv1alpha1.TsuruAppAddress{}). WithOptions(controller.Options{MaxConcurrentReconciles: 2, RecoverPanic: true}). Complete(r) } diff --git a/controllers/tsuru_app_address_controller_test.go b/controllers/tsuru_app_address_controller_test.go index e5a6667..4321011 100644 --- a/controllers/tsuru_app_address_controller_test.go +++ b/controllers/tsuru_app_address_controller_test.go @@ -38,7 +38,7 @@ func TestControllerResolveEmpty(t *testing.T) { TsuruAPI: &fakeTsuruAPI{}, Resolver: &fakeResolver{ hosts: map[string][]string{ - "myapp.io": []string{}, + "myapp.io": {}, }, }, } @@ -53,7 +53,7 @@ func TestControllerResolveEmpty(t *testing.T) { require.NoError(t, err) existingTsuruAppAddress := &v1alpha1.TsuruAppAddress{} - err = controller.Client.Get(context.Background(), types.NamespacedName{ + err = controller.Get(context.Background(), types.NamespacedName{ Name: tsuruAppAddress.Name, Namespace: tsuruAppAddress.Namespace, }, existingTsuruAppAddress) @@ -101,7 +101,7 @@ func TestControllerResolveWithError(t *testing.T) { require.NoError(t, err) existingTsuruAppAddress := &v1alpha1.TsuruAppAddress{} - err = controller.Client.Get(context.Background(), types.NamespacedName{ + err = controller.Get(context.Background(), types.NamespacedName{ Name: tsuruAppAddress.Name, Namespace: tsuruAppAddress.Namespace, }, existingTsuruAppAddress) diff --git a/controllers/tsuru_app_controller.go b/controllers/tsuru_app_controller.go index dd341bc..d304c40 100644 --- a/controllers/tsuru_app_controller.go +++ b/controllers/tsuru_app_controller.go @@ -48,7 +48,7 @@ func (r *TsuruAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c app := &tsuruv1.App{} - err := r.Client.Get(ctx, req.NamespacedName, app) + err := r.Get(ctx, req.NamespacedName, app) if err != nil { l.Error(err, "could not get Tsuru App object") return ctrl.Result{}, err @@ -68,7 +68,7 @@ func (r *TsuruAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c } acl := &v1alpha1.ACL{} - err = r.Client.Get(ctx, client.ObjectKey{ + err = r.Get(ctx, client.ObjectKey{ Name: app.Name, Namespace: app.Spec.NamespaceName, }, acl) @@ -78,7 +78,7 @@ func (r *TsuruAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c return ctrl.Result{}, nil } - err = r.Client.Create(ctx, &v1alpha1.ACL{ + err = r.Create(ctx, &v1alpha1.ACL{ ObjectMeta: metav1.ObjectMeta{ Name: app.Name, Namespace: app.Spec.NamespaceName, @@ -93,7 +93,6 @@ func (r *TsuruAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c WarningErrors: warningErrors, }, }) - if err != nil { return ctrl.Result{}, err } @@ -106,8 +105,7 @@ func (r *TsuruAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c l.Error(err, "could not get ACL object") return ctrl.Result{}, err } else if len(destinations) == 0 { - err = r.Client.Delete(ctx, acl) - + err = r.Delete(ctx, acl) if err != nil { l.Error(err, "could not remove unused ACL") } @@ -119,7 +117,7 @@ func (r *TsuruAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c } acl.Spec.Destinations = destinations - err = r.Client.Update(ctx, acl) + err = r.Update(ctx, acl) if err != nil { return ctrl.Result{}, err } @@ -127,7 +125,7 @@ func (r *TsuruAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c if len(warningErrors) > 0 || len(acl.Status.WarningErrors) > 0 { acl.Status.WarningErrors = warningErrors - err := r.Client.Status().Update(ctx, acl) + err := r.Status().Update(ctx, acl) if err != nil { l.Error(err, "could not remove update status of ACL") return ctrl.Result{}, err diff --git a/controllers/tsuru_app_controller_test.go b/controllers/tsuru_app_controller_test.go index b6a0c00..ac23afd 100644 --- a/controllers/tsuru_app_controller_test.go +++ b/controllers/tsuru_app_controller_test.go @@ -19,12 +19,14 @@ type fakeACLAPI struct{} func (f *fakeACLAPI) AppRules(ctx context.Context, appName string) ([]aclapi.Rule, error) { return f.mockRules(ctx, appName) } + func (f *fakeACLAPI) JobRules(ctx context.Context, jobName string) ([]aclapi.Rule, error) { return f.mockRules(ctx, jobName) } -func (f *fakeACLAPI) mockRules(ctx context.Context, resourceName string) ([]aclapi.Rule, error) { - if resourceName == "myapp" || resourceName == "myjob" { +func (f *fakeACLAPI) mockRules(_ context.Context, resourceName string) ([]aclapi.Rule, error) { + switch resourceName { + case "myapp", "myjob": return []aclapi.Rule{ { Destination: aclapi.RuleType{ @@ -75,9 +77,9 @@ func (f *fakeACLAPI) mockRules(ctx context.Context, resourceName string) ([]acla }, }, }, nil - } else if resourceName == "myapp-no-rules" || resourceName == "myjob-no-rules" { + case "myapp-no-rules", "myjob-no-rules": return []aclapi.Rule{}, nil - } else if resourceName == "myapp-with-errors" || resourceName == "myjob-with-errors" { + case "myapp-with-errors", "myjob-with-errors": return []aclapi.Rule{ { Destination: aclapi.RuleType{ @@ -134,7 +136,7 @@ func (suite *ControllerSuite) TestTsuruAppReconcilerSimpleReconcile() { suite.Require().NoError(err) existingACL := &v1alpha1.ACL{} - err = reconciler.Client.Get(ctx, types.NamespacedName{ + err = reconciler.Get(ctx, types.NamespacedName{ Namespace: app.Spec.NamespaceName, Name: app.Name, }, existingACL) @@ -203,7 +205,7 @@ func (suite *ControllerSuite) TestTsuruAppReconcilerReconcileAppWithNoRules() { suite.Require().NoError(err) existingACL := &v1alpha1.ACL{} - err = reconciler.Client.Get(ctx, types.NamespacedName{ + err = reconciler.Get(ctx, types.NamespacedName{ Namespace: app.Spec.NamespaceName, Name: app.Name, }, existingACL) @@ -247,7 +249,7 @@ func (suite *ControllerSuite) TestTsuruAppReconcilerReconcileExistingAppWithNoRu suite.Require().NoError(err) existingACL := &v1alpha1.ACL{} - err = reconciler.Client.Get(ctx, types.NamespacedName{ + err = reconciler.Get(ctx, types.NamespacedName{ Namespace: app.Spec.NamespaceName, Name: app.Name, }, existingACL) @@ -291,7 +293,7 @@ func (suite *ControllerSuite) TestTsuruAppReconcilerReconcileExistingApp() { suite.Require().NoError(err) existingACL := &v1alpha1.ACL{} - err = reconciler.Client.Get(ctx, types.NamespacedName{ + err = reconciler.Get(ctx, types.NamespacedName{ Namespace: app.Spec.NamespaceName, Name: app.Name, }, existingACL) @@ -325,7 +327,7 @@ func (suite *ControllerSuite) TestTsuruAppReconcilerReconcileAppWithErrors() { suite.Require().NoError(err) existingACL := &v1alpha1.ACL{} - err = reconciler.Client.Get(ctx, types.NamespacedName{ + err = reconciler.Get(ctx, types.NamespacedName{ Namespace: app.Spec.NamespaceName, Name: app.Name, }, existingACL) diff --git a/controllers/tsuru_cronjob_controller.go b/controllers/tsuru_cronjob_controller.go index 592cb0c..f509df4 100644 --- a/controllers/tsuru_cronjob_controller.go +++ b/controllers/tsuru_cronjob_controller.go @@ -101,7 +101,6 @@ func (r *TsuruCronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request WarningErrors: warningErrors, }, }) - if err != nil { return ctrl.Result{}, err } @@ -115,7 +114,6 @@ func (r *TsuruCronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request return ctrl.Result{}, err } else if len(destinations) == 0 { err = r.Client.Delete(ctx, acl) - if err != nil { l.Error(err, "could not remove unused ACL") } diff --git a/controllers/tsuru_cronjob_controller_test.go b/controllers/tsuru_cronjob_controller_test.go index f23ddda..06475d8 100644 --- a/controllers/tsuru_cronjob_controller_test.go +++ b/controllers/tsuru_cronjob_controller_test.go @@ -39,7 +39,7 @@ func (suite *ControllerSuite) TestTsuruCronJobReconcilerSimpleReconcile() { suite.Require().NoError(err) existingACL := &v1alpha1.ACL{} - err = reconciler.Client.Get(ctx, types.NamespacedName{ + err = reconciler.Get(ctx, types.NamespacedName{ Namespace: job.Namespace, Name: tsuruJobACLPrefix + job.Name, }, existingACL) @@ -107,7 +107,7 @@ func (suite *ControllerSuite) TestTsuruCronJobReconcilerReconcileJobWithNoRules( suite.Require().NoError(err) existingACL := &v1alpha1.ACL{} - err = reconciler.Client.Get(ctx, types.NamespacedName{ + err = reconciler.Get(ctx, types.NamespacedName{ Namespace: job.Namespace, Name: tsuruJobACLPrefix + job.Name, }, existingACL) @@ -151,7 +151,7 @@ func (suite *ControllerSuite) TestTsuruCronJobReconcilerReconcileExistingJobWith suite.Require().NoError(err) existingACL := &v1alpha1.ACL{} - err = reconciler.Client.Get(ctx, types.NamespacedName{ + err = reconciler.Get(ctx, types.NamespacedName{ Namespace: job.Namespace, Name: tsuruJobACLPrefix + job.Name, }, existingACL) @@ -195,7 +195,7 @@ func (suite *ControllerSuite) TestTsuruCronJobReconcilerReconcileExistingJob() { suite.Require().NoError(err) existingACL := &v1alpha1.ACL{} - err = reconciler.Client.Get(ctx, types.NamespacedName{ + err = reconciler.Get(ctx, types.NamespacedName{ Namespace: job.Namespace, Name: tsuruJobACLPrefix + job.Name, }, existingACL) @@ -229,7 +229,7 @@ func (suite *ControllerSuite) TestTsuruJobReconcilerReconcileJobWithErrors() { suite.Require().NoError(err) existingACL := &v1alpha1.ACL{} - err = reconciler.Client.Get(ctx, types.NamespacedName{ + err = reconciler.Get(ctx, types.NamespacedName{ Namespace: job.Namespace, Name: tsuruJobACLPrefix + job.Name, }, existingACL) diff --git a/go.mod b/go.mod index 747e737..ec6ff79 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/tsuru/acl-operator go 1.19 require ( - github.com/pkg/errors v0.9.1 + github.com/go-logr/logr v1.2.3 github.com/stretchr/testify v1.8.0 github.com/tsuru/rpaas-operator v0.29.0 github.com/tsuru/tsuru v0.0.0-20220928174619-1ab0249a35be @@ -41,7 +41,6 @@ require ( github.com/fsouza/go-dockerclient v1.7.4 // indirect github.com/ghodss/yaml v1.0.0 // indirect github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8 // indirect - github.com/go-logr/logr v1.2.3 // indirect github.com/go-logr/zapr v1.2.3 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect github.com/go-openapi/jsonreference v0.20.0 // indirect @@ -74,6 +73,7 @@ require ( github.com/opencontainers/runc v1.1.1 // indirect github.com/opentracing-contrib/go-stdlib v1.0.1-0.20201028152118-adbfc141dfc2 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmorie/go-open-service-broker-client v0.0.0-20180330214919-dca737037ce6 // indirect github.com/prometheus/client_golang v1.13.0 // indirect diff --git a/main.go b/main.go index a7fd009..e385194 100644 --- a/main.go +++ b/main.go @@ -39,9 +39,7 @@ import ( //+kubebuilder:scaffold:imports ) -var ( - setupLog = ctrl.Log.WithName("setup") -) +var setupLog = ctrl.Log.WithName("setup") func main() { var metricsAddr string