Skip to content

Commit

Permalink
ingress not found; config identity check
Browse files Browse the repository at this point in the history
  • Loading branch information
wasaga committed Aug 19, 2021
1 parent e9c2302 commit 31c7066
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
18 changes: 13 additions & 5 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
- support TLS certs

* monitor referenced secret & service for changes
# TODO

- config transformation tests
- record ingress state change events
- config transformation tests
- envoy config validation
- watch only specific namespace(s)
- run against k8s ingress conformance tests
- test with cert-manager
- support http01 challenge
- recover after redis wipe: currently not detecting that
- potential leak of ingresses if removed while controller is unavailable
- certificate matching: if a matching cert already exists in the databroker config, then it might be chosen
even if tls spec says otherwise

# Done

- monitor referenced secret & service for changes
- map annotations to route props
- support TLS certs
5 changes: 4 additions & 1 deletion controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ func (r Controller) getDependantIngressFn(kind string) func(a client.Object) []r

func (r Controller) isIngressNotFound(err error) bool {
if status := apierrors.APIStatus(nil); errors.As(err, &status) {
return status.Status().Reason == metav1.StatusReasonNotFound && status.Status().Kind == r.ingressKind
s := status.Status()
return s.Reason == metav1.StatusReasonNotFound &&
s.Details != nil &&
s.Details.Kind == r.ingressKind
}
return false
}
16 changes: 0 additions & 16 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
Copyright 2021.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers

import (
Expand Down
38 changes: 26 additions & 12 deletions pomerium/sync.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pomerium

import (
"bytes"
"context"
"fmt"

Expand All @@ -10,6 +11,7 @@ import (
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/pomerium/ingress-controller/model"
pomerium "github.com/pomerium/pomerium/pkg/grpc/config"
Expand All @@ -30,7 +32,7 @@ type ConfigReconciler struct {

// Upsert should update or create the pomerium routes corresponding to this ingress
func (r *ConfigReconciler) Upsert(ctx context.Context, ic *model.IngressConfig) error {
cfg, err := r.getConfig(ctx)
cfg, prevBytes, err := r.getConfig(ctx)
if err != nil {
return fmt.Errorf("get config: %w", err)
}
Expand All @@ -40,28 +42,31 @@ func (r *ConfigReconciler) Upsert(ctx context.Context, ic *model.IngressConfig)
if err := upsertCerts(cfg, ic); err != nil {
return fmt.Errorf("updating certs: %w", err)
}
if err := r.saveConfig(ctx, cfg); err != nil {
if err := r.saveConfig(ctx, cfg, prevBytes); err != nil {
return fmt.Errorf("updating pomerium config: %w", err)
}
return nil
}

// Delete should delete pomerium routes corresponding to this ingress name
func (r *ConfigReconciler) Delete(ctx context.Context, namespacedName types.NamespacedName) error {
cfg, err := r.getConfig(ctx)
cfg, prevBytes, err := r.getConfig(ctx)
if err != nil {
return fmt.Errorf("get pomerium config: %w", err)
}
if err := deleteRoutes(cfg, namespacedName); err != nil {
return fmt.Errorf("deleting pomerium config records %s: %w", namespacedName.String(), err)
}
if err := r.saveConfig(ctx, cfg); err != nil {
if err := removeUnusedCerts(cfg); err != nil {
return fmt.Errorf("removing unused certs: %w", err)
}
if err := r.saveConfig(ctx, cfg, prevBytes); err != nil {
return fmt.Errorf("updating pomerium config: %w", err)
}
return nil
}

func (r *ConfigReconciler) getConfig(ctx context.Context) (*pomerium.Config, error) {
func (r *ConfigReconciler) getConfig(ctx context.Context) (*pomerium.Config, []byte, error) {
cfg := new(pomerium.Config)
any := protoutil.NewAny(cfg)
var hdr metadata.MD
Expand All @@ -70,22 +75,27 @@ func (r *ConfigReconciler) getConfig(ctx context.Context) (*pomerium.Config, err
Id: configID,
}, grpc.Header(&hdr))
if status.Code(err) == codes.NotFound {
return &pomerium.Config{}, nil
return &pomerium.Config{}, nil, nil
} else if err != nil {
return nil, fmt.Errorf("get pomerium config: %w", err)
return nil, nil, fmt.Errorf("get pomerium config: %w", err)
}

if err := resp.GetRecord().GetData().UnmarshalTo(cfg); err != nil {
return nil, fmt.Errorf("unmarshal current config: %w", err)
return nil, nil, fmt.Errorf("unmarshal current config: %w", err)
}

return cfg, nil
return cfg, resp.GetRecord().GetData().GetValue(), nil
}

func (r *ConfigReconciler) saveConfig(ctx context.Context, cfg *pomerium.Config) error {
fmt.Println(protojson.Format(cfg))

func (r *ConfigReconciler) saveConfig(ctx context.Context, cfg *pomerium.Config, prevBytes []byte) error {
logger := log.FromContext(ctx)
any := protoutil.NewAny(cfg)

if bytes.Equal(prevBytes, any.GetValue()) {
logger.Info("no changes in pomerium config")
return nil
}

if _, err := r.Put(ctx, &databroker.PutRequest{
Record: &databroker.Record{
Type: any.GetTypeUrl(),
Expand All @@ -95,5 +105,9 @@ func (r *ConfigReconciler) saveConfig(ctx context.Context, cfg *pomerium.Config)
}); err != nil {
return err
}

logger.Info("new pomerium config applied")
fmt.Println(protojson.Format(cfg))

return nil
}

0 comments on commit 31c7066

Please sign in to comment.