Skip to content

Commit

Permalink
Merge pull request #30 from sacharya/route
Browse files Browse the repository at this point in the history
Create a route for Cincinnati service
  • Loading branch information
openshift-merge-robot authored May 21, 2020
2 parents 9d7bc0c + 1fdf865 commit d369a11
Show file tree
Hide file tree
Showing 15 changed files with 4,139 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager/signals"

configv1 "github.com/openshift/api/config/v1"
routev1 "github.com/openshift/api/route/v1"
"github.com/openshift/cincinnati-operator/pkg/apis"
"github.com/openshift/cincinnati-operator/pkg/controller"
cincontroller "github.com/openshift/cincinnati-operator/pkg/controller/cincinnati"
Expand Down Expand Up @@ -129,6 +130,11 @@ func main() {
os.Exit(1)
}

if err := routev1.Install(mgr.GetScheme()); err != nil {
log.Error(err, "")
os.Exit(1)
}

opts := cincontroller.Options{
OperandImage: os.Getenv("OPERAND_IMAGE"),
}
Expand Down
11 changes: 11 additions & 0 deletions deploy/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ rules:
- get
- list
- watch
- apiGroups:
- route.openshift.io
resources:
- routes
verbs:
- create
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
Expand Down
38 changes: 38 additions & 0 deletions pkg/controller/cincinnati/cincinnati_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

apicfgv1 "github.com/openshift/api/config/v1"
cv1beta1 "github.com/openshift/cincinnati-operator/pkg/apis/cincinnati/v1beta1"
routev1 "github.com/openshift/api/route/v1"
"github.com/openshift/cluster-image-registry-operator/pkg/defaults"
)

Expand Down Expand Up @@ -72,6 +73,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
&corev1.ConfigMap{},
&corev1.Service{},
&policyv1beta1.PodDisruptionBudget{},
&routev1.Route{},
} {
err = c.Watch(&source.Kind{Type: obj}, &handler.EnqueueRequestForOwner{
IsController: true,
Expand Down Expand Up @@ -161,6 +163,7 @@ func (r *ReconcileCincinnati) Reconcile(request reconcile.Request) (reconcile.Re
r.ensureGraphBuilderService,
r.ensurePolicyEngineService,
r.ensurePodDisruptionBudget,
r.ensurePolicyEngineRoute,
} {
err = f(ctx, reqLogger, instanceCopy, resources)
if err != nil {
Expand Down Expand Up @@ -469,6 +472,41 @@ func (r *ReconcileCincinnati) ensurePolicyEngineService(ctx context.Context, req
return nil
}

func (r *ReconcileCincinnati) ensurePolicyEngineRoute(ctx context.Context, reqLogger logr.Logger, instance *cv1beta1.Cincinnati, resources *kubeResources) error {
route := resources.policyEngineRoute
// Set Cincinnati instance as the owner and controller
if err := controllerutil.SetControllerReference(instance, route, r.scheme); err != nil {
return err
}

// Check if it already exists
found := &routev1.Route{}
err := r.client.Get(ctx, types.NamespacedName{Name: route.Name, Namespace: route.Namespace}, found)
if err != nil && errors.IsNotFound(err) {
reqLogger.Info("Creating Route", "Namespace", route.Namespace, "Name", route.Name)
if err = r.client.Create(ctx, route); err != nil {
handleErr(reqLogger, &instance.Status, "CreateRouteFailed", err)
}
return err
} else if err != nil {
handleErr(reqLogger, &instance.Status, "GetRouteFailed", err)
return err
}

// found existing resource; let's compare and update if needed
if !reflect.DeepEqual(found.Spec, route.Spec) {
reqLogger.Info("Updating Route", "Namespace", route.Namespace, "Name", route.Name)
updated := found.DeepCopy()
updated.Spec = route.Spec
err = r.client.Update(ctx, updated)
if err != nil {
handleErr(reqLogger, &instance.Status, "UpdateRouteFailed", err)
}
}

return nil
}

func (r *ReconcileCincinnati) ensureService(ctx context.Context, reqLogger logr.Logger, service *corev1.Service) error {
// Check if this Service already exists
found := &corev1.Service{}
Expand Down
43 changes: 43 additions & 0 deletions pkg/controller/cincinnati/cincinnati_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

configv1 "github.com/openshift/api/config/v1"
routev1 "github.com/openshift/api/route/v1"
"github.com/openshift/cincinnati-operator/pkg/apis"
cv1beta1 "github.com/openshift/cincinnati-operator/pkg/apis/cincinnati/v1beta1"
"github.com/openshift/cluster-image-registry-operator/pkg/defaults"
Expand All @@ -19,6 +20,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -49,6 +51,11 @@ func TestMain(m *testing.M) {
os.Exit(1)
}

if err := routev1.AddToScheme(scheme.Scheme); err != nil {
log.Error(err, "Failed adding apis to scheme")
os.Exit(1)
}

exitVal := m.Run()
os.Exit(exitVal)
}
Expand Down Expand Up @@ -355,6 +362,42 @@ func TestEnsurePodDisruptionBudget(t *testing.T) {
}
}

func TestEnsurePolicyEngineRoute(t *testing.T) {
tests := []struct {
name string
cincinnati *cv1beta1.Cincinnati
}{
{
name: "EnsurePolicyEngineRoute",
cincinnati: newDefaultCincinnati(),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cincinnati := test.cincinnati
r := newTestReconciler(cincinnati)

resources, err := newKubeResources(test.cincinnati, testOperandImage)
err = r.ensurePolicyEngineRoute(context.TODO(), log, cincinnati, resources)
if err != nil {
t.Fatal(err)
}

found := &routev1.Route{}
err = r.client.Get(context.TODO(), types.NamespacedName{Name: namePolicyEngineRoute(cincinnati), Namespace: cincinnati.Namespace}, found)
if err != nil {
t.Fatal(err)
}

verifyOwnerReference(t, found.ObjectMeta.OwnerReferences[0], cincinnati)
assert.Equal(t, found.ObjectMeta.Labels["app"], nameDeployment(cincinnati))
assert.Equal(t, found.Spec.To.Kind, "Service")
assert.Equal(t, found.Spec.To.Name, namePolicyEngineService(cincinnati))
assert.Equal(t, found.Spec.Port.TargetPort, intstr.FromString("policy-engine"))
})
}
}

func newRequest(cincinnati *cv1beta1.Cincinnati) reconcile.Request {
namespacedName := types.NamespacedName{
Namespace: cincinnati.ObjectMeta.Namespace,
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/cincinnati/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func nameGraphBuilderService(instance *cv1beta1.Cincinnati) string {
return instance.Name + "-graph-builder"
}

func namePolicyEngineRoute(instance *cv1beta1.Cincinnati) string {
return namePolicyEngineService(instance) + "-route"
}

func nameAdditionalTrustedCA(instance *cv1beta1.Cincinnati) string {
return instance.Name + "-trusted-ca"
}
Expand Down
29 changes: 29 additions & 0 deletions pkg/controller/cincinnati/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"text/template"

routev1 "github.com/openshift/api/route/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
Expand Down Expand Up @@ -75,6 +76,7 @@ type kubeResources struct {
policyEngineContainer *corev1.Container
graphBuilderService *corev1.Service
policyEngineService *corev1.Service
policyEngineRoute *routev1.Route
}

func newKubeResources(instance *cv1beta1.Cincinnati, image string) (*kubeResources, error) {
Expand Down Expand Up @@ -106,6 +108,7 @@ func newKubeResources(instance *cv1beta1.Cincinnati, image string) (*kubeResourc
k.deployment = k.newDeployment(instance)
k.graphBuilderService = k.newGraphBuilderService(instance)
k.policyEngineService = k.newPolicyEngineService(instance)
k.policyEngineRoute = k.newPolicyEngineRoute(instance)
return &k, nil
}

Expand Down Expand Up @@ -195,6 +198,32 @@ func (k *kubeResources) newPolicyEngineService(instance *cv1beta1.Cincinnati) *c
}
}

func (k *kubeResources) newPolicyEngineRoute(instance *cv1beta1.Cincinnati) *routev1.Route {
name := namePolicyEngineRoute(instance)
return &routev1.Route{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: instance.Namespace,
Labels: map[string]string{
"app": nameDeployment(instance),
},
},
Spec: routev1.RouteSpec{
Port: &routev1.RoutePort{
TargetPort: intstr.FromString("policy-engine"),
},
To: routev1.RouteTargetReference{
Kind: "Service",
Name: namePolicyEngineService(instance),
},
TLS: &routev1.TLSConfig{
Termination: routev1.TLSTerminationEdge,
InsecureEdgeTerminationPolicy: routev1.InsecureEdgeTerminationPolicyAllow,
},
},
}
}

func (k *kubeResources) newEnvConfig(instance *cv1beta1.Cincinnati) *corev1.ConfigMap {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Expand Down
8 changes: 8 additions & 0 deletions vendor/github.com/openshift/api/route/v1/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d369a11

Please sign in to comment.