Skip to content

Commit

Permalink
remove-generic-resource-from-remotewriteingress-resource (#1646)
Browse files Browse the repository at this point in the history
Signed-off-by: QuentinBisson <[email protected]>
  • Loading branch information
QuentinBisson authored Jun 6, 2024
1 parent ef180bc commit dcd3069
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 119 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package remotewriteingress

import (
"context"

"github.com/giantswarm/microerror"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/giantswarm/prometheus-meta-operator/v2/service/controller/resource/resourceutils"
)

func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
desired, err := r.toIngress(obj)
if err != nil {
return microerror.Mask(err)
}

r.config.Logger.Debugf(ctx, "creating")
current, err := r.config.K8sClient.K8sClient().NetworkingV1().Ingresses(desired.GetNamespace()).Get(ctx, desired.GetName(), metav1.GetOptions{})
if apierrors.IsNotFound(err) {
current, err = r.config.K8sClient.K8sClient().NetworkingV1().Ingresses(desired.GetNamespace()).Create(ctx, desired, metav1.CreateOptions{})
}

if err != nil {
return microerror.Mask(err)
}

if r.hasChanged(current, desired) {
resourceutils.UpdateMeta(current, desired)
_, err = r.config.K8sClient.K8sClient().NetworkingV1().Ingresses(desired.GetNamespace()).Update(ctx, desired, metav1.UpdateOptions{})
if err != nil {
return microerror.Mask(err)
}
}
r.config.Logger.Debugf(ctx, "created")

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package remotewriteingress

import (
"context"

"github.com/giantswarm/microerror"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (r *Resource) EnsureDeleted(ctx context.Context, obj interface{}) error {
object, err := r.getObjectMeta(obj)
if err != nil {
return microerror.Mask(err)
}

r.config.Logger.Debugf(ctx, "deleting")
err = r.config.K8sClient.K8sClient().NetworkingV1().Ingresses(object.GetNamespace()).Delete(ctx, object.GetName(), metav1.DeleteOptions{})
if apierrors.IsNotFound(err) {
// fall through
} else if err != nil {
return microerror.Mask(err)
}
r.config.Logger.Debugf(ctx, "deleted")

return nil
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package remotewriteingress

import (
"context"
"fmt"
"reflect"

Expand All @@ -11,7 +10,6 @@ import (
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/giantswarm/prometheus-meta-operator/v2/service/controller/resource/generic"
"github.com/giantswarm/prometheus-meta-operator/v2/service/key"
)

Expand All @@ -26,33 +24,19 @@ type Config struct {
ExternalDNS bool
}

func New(config Config) (*generic.Resource, error) {
clientFunc := func(namespace string) generic.Interface {
c := config.K8sClient.K8sClient().NetworkingV1().Ingresses(namespace)
return wrappedClient{client: c}
}
type Resource struct {
config Config
}

c := generic.Config{
ClientFunc: clientFunc,
Logger: config.Logger,
Name: Name,
GetObjectMeta: func(ctx context.Context, v interface{}) (metav1.ObjectMeta, error) {
return getObjectMeta(v, config)
},
GetDesiredObject: func(ctx context.Context, v interface{}) (metav1.Object, error) {
return toIngress(v, config)
},
HasChangedFunc: hasChanged,
}
r, err := generic.New(c)
if err != nil {
return nil, microerror.Mask(err)
}
func New(config Config) (*Resource, error) {
return &Resource{config}, nil
}

return r, nil
func (r *Resource) Name() string {
return Name
}

func getObjectMeta(v interface{}, config Config) (metav1.ObjectMeta, error) {
func (r *Resource) getObjectMeta(v interface{}) (metav1.ObjectMeta, error) {
cluster, err := key.ToCluster(v)
if err != nil {
return metav1.ObjectMeta{}, microerror.Mask(err)
Expand All @@ -62,16 +46,16 @@ func getObjectMeta(v interface{}, config Config) (metav1.ObjectMeta, error) {
Name: fmt.Sprintf("prometheus-%s-remote-write", key.ClusterID(cluster)),
Namespace: key.Namespace(cluster),
Labels: key.PrometheusLabels(cluster),
Annotations: key.RemoteWriteAuthenticationAnnotations(config.BaseDomain, config.ExternalDNS),
Annotations: key.RemoteWriteAuthenticationAnnotations(r.config.BaseDomain, r.config.ExternalDNS),
}, nil
}

func toIngress(v interface{}, config Config) (metav1.Object, error) {
func (r *Resource) toIngress(v interface{}) (*networkingv1.Ingress, error) {
if v == nil {
return nil, nil
}

objectMeta, err := getObjectMeta(v, config)
objectMeta, err := r.getObjectMeta(v)
if err != nil {
return nil, microerror.Mask(err)
}
Expand Down Expand Up @@ -108,7 +92,7 @@ func toIngress(v interface{}, config Config) (metav1.Object, error) {
IngressClassName: &ingressClassName,
Rules: []networkingv1.IngressRule{
{
Host: config.BaseDomain,
Host: r.config.BaseDomain,
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{
Paths: []networkingv1.HTTPIngressPath{
Expand All @@ -135,7 +119,7 @@ func toIngress(v interface{}, config Config) (metav1.Object, error) {
return ingress, nil
}

func hasChanged(current, desired metav1.Object) bool {
func (r *Resource) hasChanged(current, desired metav1.Object) bool {
c := current.(*networkingv1.Ingress)
d := desired.(*networkingv1.Ingress)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ var update = flag.Bool("update", false, "update the output file")

func TestIngressDefault(t *testing.T) {
testFunc := func(v interface{}) (interface{}, error) {
return toIngress(v, Config{BaseDomain: "prometheus"})
resource, err := New(Config{
BaseDomain: "prometheus",
})
if err != nil {
t.Fatal(err)
}
return resource.toIngress(v)
}
for _, flavor := range unittest.ProviderFlavors {
outputDir, err := filepath.Abs("./test/default/" + flavor)
Expand Down Expand Up @@ -41,7 +47,14 @@ func TestIngressDefault(t *testing.T) {

func TestIngressExternalDNS(t *testing.T) {
testFunc := func(v interface{}) (interface{}, error) {
return toIngress(v, Config{BaseDomain: "prometheus", ExternalDNS: true})
resource, err := New(Config{
BaseDomain: "prometheus",
ExternalDNS: true,
})
if err != nil {
t.Fatal(err)
}
return resource.toIngress(v)
}
for _, flavor := range unittest.ProviderFlavors {
outputDir, err := filepath.Abs("./test/externaldns/" + flavor)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package remotewriteingressauth

import (
"context"

"github.com/giantswarm/microerror"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/giantswarm/prometheus-meta-operator/v2/service/controller/resource/resourceutils"
)

func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
desired, err := r.toSecret(ctx, obj)
if err != nil {
return microerror.Mask(err)
}

r.config.Logger.Debugf(ctx, "creating")
current, err := r.config.K8sClient.K8sClient().CoreV1().Secrets(desired.GetNamespace()).Get(ctx, desired.GetName(), metav1.GetOptions{})
if apierrors.IsNotFound(err) {
current, err = r.config.K8sClient.K8sClient().CoreV1().Secrets(desired.GetNamespace()).Create(ctx, desired, metav1.CreateOptions{})
}

if err != nil {
return microerror.Mask(err)
}

if r.hasChanged(current, desired) {
resourceutils.UpdateMeta(current, desired)
_, err = r.config.K8sClient.K8sClient().CoreV1().Secrets(desired.GetNamespace()).Update(ctx, desired, metav1.UpdateOptions{})
if err != nil {
return microerror.Mask(err)
}
}
r.config.Logger.Debugf(ctx, "created")

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package remotewriteingressauth

import (
"context"

"github.com/giantswarm/microerror"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (r *Resource) EnsureDeleted(ctx context.Context, obj interface{}) error {
object, err := r.getObjectMeta(obj)
if err != nil {
return microerror.Mask(err)
}

r.config.Logger.Debugf(ctx, "deleting")
err = r.config.K8sClient.K8sClient().CoreV1().Secrets(object.GetNamespace()).Delete(ctx, object.GetName(), metav1.DeleteOptions{})
if apierrors.IsNotFound(err) {
// fall through
} else if err != nil {
return microerror.Mask(err)
}
r.config.Logger.Debugf(ctx, "deleted")

return nil
}
Loading

0 comments on commit dcd3069

Please sign in to comment.