diff --git a/service/controller/resource/monitoring/remotewriteingress/client.go b/service/controller/resource/monitoring/remotewriteingress/client.go deleted file mode 100644 index ab315eaa4..000000000 --- a/service/controller/resource/monitoring/remotewriteingress/client.go +++ /dev/null @@ -1,29 +0,0 @@ -package remotewriteingress - -import ( - "context" - - networkingv1 "k8s.io/api/networking/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - v1 "k8s.io/client-go/kubernetes/typed/networking/v1" -) - -type wrappedClient struct { - client v1.IngressInterface -} - -func (c wrappedClient) Create(ctx context.Context, o metav1.Object, options metav1.CreateOptions) (metav1.Object, error) { - return c.client.Create(ctx, o.(*networkingv1.Ingress), options) -} - -func (c wrappedClient) Update(ctx context.Context, o metav1.Object, options metav1.UpdateOptions) (metav1.Object, error) { - return c.client.Update(ctx, o.(*networkingv1.Ingress), options) -} - -func (c wrappedClient) Get(ctx context.Context, name string, options metav1.GetOptions) (metav1.Object, error) { - return c.client.Get(ctx, name, options) -} - -func (c wrappedClient) Delete(ctx context.Context, name string, options *metav1.DeleteOptions) error { - return c.client.Delete(ctx, name, *options) -} diff --git a/service/controller/resource/monitoring/remotewriteingress/create.go b/service/controller/resource/monitoring/remotewriteingress/create.go new file mode 100644 index 000000000..37733f9df --- /dev/null +++ b/service/controller/resource/monitoring/remotewriteingress/create.go @@ -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 +} diff --git a/service/controller/resource/monitoring/remotewriteingress/delete.go b/service/controller/resource/monitoring/remotewriteingress/delete.go new file mode 100644 index 000000000..6fa61cbd8 --- /dev/null +++ b/service/controller/resource/monitoring/remotewriteingress/delete.go @@ -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 +} diff --git a/service/controller/resource/monitoring/remotewriteingress/resource.go b/service/controller/resource/monitoring/remotewriteingress/resource.go index ee85bf172..02d905cc4 100644 --- a/service/controller/resource/monitoring/remotewriteingress/resource.go +++ b/service/controller/resource/monitoring/remotewriteingress/resource.go @@ -1,7 +1,6 @@ package remotewriteingress import ( - "context" "fmt" "reflect" @@ -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" ) @@ -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) @@ -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) } @@ -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{ @@ -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) diff --git a/service/controller/resource/monitoring/remotewriteingress/resource_test.go b/service/controller/resource/monitoring/remotewriteingress/resource_test.go index 4e9fd732f..0dc9a8f03 100644 --- a/service/controller/resource/monitoring/remotewriteingress/resource_test.go +++ b/service/controller/resource/monitoring/remotewriteingress/resource_test.go @@ -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) @@ -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) diff --git a/service/controller/resource/monitoring/remotewriteingressauth/client.go b/service/controller/resource/monitoring/remotewriteingressauth/client.go deleted file mode 100644 index 30f203021..000000000 --- a/service/controller/resource/monitoring/remotewriteingressauth/client.go +++ /dev/null @@ -1,26 +0,0 @@ -package remotewriteingressauth - -import ( - "context" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - v1 "k8s.io/client-go/kubernetes/typed/core/v1" -) - -type wrappedClient struct { - client v1.SecretInterface -} - -func (c wrappedClient) Create(ctx context.Context, o metav1.Object, options metav1.CreateOptions) (metav1.Object, error) { - return c.client.Create(ctx, o.(*corev1.Secret), options) -} -func (c wrappedClient) Update(ctx context.Context, o metav1.Object, options metav1.UpdateOptions) (metav1.Object, error) { - return c.client.Update(ctx, o.(*corev1.Secret), options) -} -func (c wrappedClient) Get(ctx context.Context, name string, options metav1.GetOptions) (metav1.Object, error) { - return c.client.Get(ctx, name, options) -} -func (c wrappedClient) Delete(ctx context.Context, name string, options *metav1.DeleteOptions) error { - return c.client.Delete(ctx, name, *options) -} diff --git a/service/controller/resource/monitoring/remotewriteingressauth/create.go b/service/controller/resource/monitoring/remotewriteingressauth/create.go new file mode 100644 index 000000000..61a0730dc --- /dev/null +++ b/service/controller/resource/monitoring/remotewriteingressauth/create.go @@ -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 +} diff --git a/service/controller/resource/monitoring/remotewriteingressauth/delete.go b/service/controller/resource/monitoring/remotewriteingressauth/delete.go new file mode 100644 index 000000000..2c99d61d7 --- /dev/null +++ b/service/controller/resource/monitoring/remotewriteingressauth/delete.go @@ -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 +} diff --git a/service/controller/resource/monitoring/remotewriteingressauth/resource.go b/service/controller/resource/monitoring/remotewriteingressauth/resource.go index d75a3daf3..6e764c079 100644 --- a/service/controller/resource/monitoring/remotewriteingressauth/resource.go +++ b/service/controller/resource/monitoring/remotewriteingressauth/resource.go @@ -14,7 +14,6 @@ import ( "github.com/giantswarm/prometheus-meta-operator/v2/pkg/cluster" "github.com/giantswarm/prometheus-meta-operator/v2/pkg/password" remotewriteconfiguration "github.com/giantswarm/prometheus-meta-operator/v2/pkg/remotewrite/configuration" - "github.com/giantswarm/prometheus-meta-operator/v2/service/controller/resource/generic" "github.com/giantswarm/prometheus-meta-operator/v2/service/key" ) @@ -30,31 +29,19 @@ type Config struct { Provider cluster.Provider } -func New(config Config) (*generic.Resource, error) { - clientFunc := func(namespace string) generic.Interface { - c := config.K8sClient.K8sClient().CoreV1().Secrets(namespace) - return wrappedClient{client: c} - } +type Resource struct { + config Config +} - c := generic.Config{ - ClientFunc: clientFunc, - Logger: config.Logger, - Name: Name, - GetObjectMeta: getObjectMeta, - GetDesiredObject: func(ctx context.Context, v interface{}) (metav1.Object, error) { - return toSecret(ctx, 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(ctx context.Context, v interface{}) (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) @@ -67,8 +54,8 @@ func getObjectMeta(ctx context.Context, v interface{}) (metav1.ObjectMeta, error }, nil } -func toSecret(ctx context.Context, v interface{}, config Config) (*corev1.Secret, error) { - objectMeta, err := getObjectMeta(ctx, v) +func (r *Resource) toSecret(ctx context.Context, v interface{}) (*corev1.Secret, error) { + objectMeta, err := r.getObjectMeta(v) if err != nil { return nil, microerror.Mask(err) } @@ -78,21 +65,21 @@ func toSecret(ctx context.Context, v interface{}, config Config) (*corev1.Secret return nil, microerror.Mask(err) } - config.Logger.Debugf(ctx, "looking up for remote write secret") - username, password, err := remotewriteconfiguration.GetUsernameAndPassword(config.K8sClient.K8sClient(), ctx, cluster, config.Installation, config.Provider) + r.config.Logger.Debugf(ctx, "looking up for remote write secret") + username, password, err := remotewriteconfiguration.GetUsernameAndPassword(r.config.K8sClient.K8sClient(), ctx, cluster, r.config.Installation, r.config.Provider) if err != nil { - config.Logger.Errorf(ctx, err, "lookup for remote write secret failed") + r.config.Logger.Errorf(ctx, err, "lookup for remote write secret failed") return nil, microerror.Mask(err) } - config.Logger.Debugf(ctx, "hashing password for the prometheus agent") - hashedPassword, err := config.PasswordManager.Hash([]byte(password)) + r.config.Logger.Debugf(ctx, "hashing password for the prometheus agent") + hashedPassword, err := r.config.PasswordManager.Hash([]byte(password)) if err != nil { - config.Logger.Errorf(ctx, err, "failed to hash the prometheus agent password") + r.config.Logger.Errorf(ctx, err, "failed to hash the prometheus agent password") return nil, microerror.Mask(err) } - config.Logger.Debugf(ctx, "hashed password for the prometheus agent") + r.config.Logger.Debugf(ctx, "hashed password for the prometheus agent") secret := &corev1.Secret{ ObjectMeta: objectMeta, @@ -106,7 +93,7 @@ func toSecret(ctx context.Context, v interface{}, config Config) (*corev1.Secret return secret, nil } -func hasChanged(current, desired metav1.Object) bool { +func (r *Resource) hasChanged(current, desired metav1.Object) bool { c := current.(*corev1.Secret) d := desired.(*corev1.Secret)