Skip to content

Commit

Permalink
consoleplugin reconciler (#884)
Browse files Browse the repository at this point in the history
* consoleplugin task

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

* go mod tidy

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

* consoleplugins rback

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

* address lint issues

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

* prepare release: include consoleplugin image

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

* consoleplugin watcher with predicate on label

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

* consoleplugin: tests

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

* sotw conventions

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

* link consoleplugin deployment lifecycle with topolocy configmap lifecycle

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

* consoleplugin: update unittests to cover deletion

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

* make run target checks namespace from kubeconfig

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

* OPERATOR_NAMESPACE makefile variable for make run target

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

---------

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>
  • Loading branch information
eguzki authored Oct 8, 2024
1 parent c194aa4 commit bd043cd
Show file tree
Hide file tree
Showing 32 changed files with 976 additions and 38 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/build-images-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ on:
description: WASM Shim version
default: latest
type: string
consolePluginImageURL:
description: ConsolePlugin image URL
default: "quay.io/kuadrant/console-plugin:latest"
type: string
channels:
description: Bundle and catalog channels, comma separated
default: preview
Expand Down Expand Up @@ -65,6 +69,10 @@ on:
description: WASM Shim version
default: latest
type: string
consolePluginImageURL:
description: ConsolePlugin image URL
default: "quay.io/kuadrant/console-plugin:latest"
type: string
channels:
description: Bundle and catalog channels, comma separated
default: preview
Expand Down Expand Up @@ -147,6 +155,7 @@ jobs:
LIMITADOR_OPERATOR_VERSION=${{ inputs.limitadorOperatorVersion }} \
DNS_OPERATOR_VERSION=${{ inputs.dnsOperatorVersion }} \
WASM_SHIM_VERSION=${{ inputs.wasmShimVersion }} \
RELATED_IMAGE_CONSOLEPLUGIN=${{ inputs.consolePluginImageURL }} \
DEFAULT_CHANNEL=${{ inputs.defaultChannel }} \
CHANNELS=${{ inputs.channels }}
- name: Set up Docker Buildx
Expand Down Expand Up @@ -195,6 +204,7 @@ jobs:
LIMITADOR_OPERATOR_VERSION=${{ inputs.limitadorOperatorVersion }} \
DNS_OPERATOR_VERSION=${{ inputs.dnsOperatorVersion }} \
WASM_SHIM_VERSION=${{ inputs.wasmShimVersion }} \
RELATED_IMAGE_CONSOLEPLUGIN=${{ inputs.consolePluginImageURL }} \
DEFAULT_CHANNEL=${{ inputs.defaultChannel }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ on:
description: WASM Shim version
default: latest
type: string
consolePluginImageURL:
description: ConsolePlugin image URL
default: "quay.io/kuadrant/console-plugin:latest"
type: string
prerelease:
description: Is the release a pre-release?
required: false
Expand Down Expand Up @@ -60,6 +64,7 @@ jobs:
LIMITADOR_OPERATOR_VERSION=${{ inputs.limitadorOperatorVersion }} \
DNS_OPERATOR_VERSION=${{ inputs.dnsOperatorVersion }} \
WASM_SHIM_VERSION=${{ inputs.wasmShimVersion }} \
RELATED_IMAGE_CONSOLEPLUGIN=${{ inputs.consolePluginImageURL }} \
make prepare-release
- name: Commit and push
run: |
Expand All @@ -72,7 +77,7 @@ jobs:
with:
name: v${{ inputs.kuadrantOperatorVersion }}
tag_name: v${{ inputs.kuadrantOperatorVersion }}
body: "**This release enables installations of Authorino Operator v${{ inputs.authorinoOperatorVersion }}, Limitador Operator v${{ inputs.limitadorOperatorVersion }}, DNS Operator v${{ inputs.dnsOperatorVersion }} and WASM Shim v${{ inputs.wasmShimVersion }}**"
body: "**This release enables installations of Authorino Operator v${{ inputs.authorinoOperatorVersion }}, Limitador Operator v${{ inputs.limitadorOperatorVersion }}, DNS Operator v${{ inputs.dnsOperatorVersion }}, WASM Shim v${{ inputs.wasmShimVersion }} and ConsolePlugin ${{ inputs.consolePluginImageURL }}**"
generate_release_notes: true
target_commitish: release-v${{ github.event.inputs.kuadrantOperatorVersion }}
prerelease: ${{ github.event.inputs.prerelease }}
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ endif

# Kuadrant Namespace
KUADRANT_NAMESPACE ?= kuadrant-system
OPERATOR_NAMESPACE ?= $(KUADRANT_NAMESPACE)

# Kuadrant component versions
## authorino
Expand Down Expand Up @@ -340,7 +341,7 @@ build: generate fmt vet ## Build manager binary.

run: export LOG_LEVEL = debug
run: export LOG_MODE = development
run: export OPERATOR_NAMESPACE = kuadrant-system
run: export OPERATOR_NAMESPACE := $(OPERATOR_NAMESPACE)
run: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown")
run: DIRTY=$(shell $(PROJECT_PATH)/utils/check-git-dirty.sh || echo "unknown")
run: generate fmt vet ## Run a controller from your host.
Expand Down Expand Up @@ -383,12 +384,18 @@ rm -rf $$TMP_DIR ;\
}
endef

RELATED_IMAGE_CONSOLEPLUGIN ?= quay.io/kuadrant/console-plugin:latest

.PHONY: bundle
bundle: $(OPM) $(YQ) manifests dependencies-manifests kustomize operator-sdk ## Generate bundle manifests and metadata, then validate generated files.
$(OPERATOR_SDK) generate kustomize manifests -q
# Set desired operator image and related wasm shim image
# Set desired Wasm-shim image
V="$(RELATED_IMAGE_WASMSHIM)" \
$(YQ) eval '(select(.kind == "Deployment").spec.template.spec.containers[].env[] | select(.name == "RELATED_IMAGE_WASMSHIM").value) = strenv(V)' -i config/manager/manager.yaml
# Set desired ConsolePlugin image
V="$(RELATED_IMAGE_CONSOLEPLUGIN)" \
$(YQ) eval '(select(.kind == "Deployment").spec.template.spec.containers[].env[] | select(.name == "RELATED_IMAGE_CONSOLEPLUGIN").value) = strenv(V)' -i config/manager/manager.yaml
# Set desired operator image
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
# Update CSV
$(call update-csv-config,kuadrant-operator.v$(BUNDLE_VERSION),config/manifests/bases/kuadrant-operator.clusterserviceversion.yaml,.metadata.name)
Expand Down Expand Up @@ -443,11 +450,13 @@ prepare-release: ## Prepare the manifests for OLM and Helm Chart for a release.
LIMITADOR_OPERATOR_VERSION=$(LIMITADOR_OPERATOR_VERSION) \
DNS_OPERATOR_VERSION=$(DNS_OPERATOR_VERSION) \
WASM_SHIM_VERSION=$(WASM_SHIM_VERSION) \
RELATED_IMAGE_CONSOLEPLUGIN=$(RELATED_IMAGE_CONSOLEPLUGIN) \
$(MAKE) helm-build VERSION=$(VERSION) \
AUTHORINO_OPERATOR_VERSION=$(AUTHORINO_OPERATOR_VERSION) \
LIMITADOR_OPERATOR_VERSION=$(LIMITADOR_OPERATOR_VERSION) \
DNS_OPERATOR_VERSION=$(DNS_OPERATOR_VERSION) \
WASM_SHIM_VERSION=$(WASM_SHIM_VERSION)
WASM_SHIM_VERSION=$(WASM_SHIM_VERSION) \
RELATED_IMAGE_CONSOLEPLUGIN=$(RELATED_IMAGE_CONSOLEPLUGIN)
sed -i -e 's/Version = ".*"/Version = "$(VERSION)"/' $(PROJECT_PATH)/version/version.go

##@ Code Style
Expand Down
16 changes: 16 additions & 0 deletions bundle/manifests/kuadrant-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ spec:
- get
- list
- watch
- apiGroups:
- console.openshift.io
resources:
- consoleplugins
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- coordination.k8s.io
resources:
Expand Down Expand Up @@ -648,6 +660,8 @@ spec:
env:
- name: RELATED_IMAGE_WASMSHIM
value: oci://quay.io/kuadrant/wasm-shim:latest
- name: RELATED_IMAGE_CONSOLEPLUGIN
value: quay.io/kuadrant/console-plugin:latest
- name: OPERATOR_NAMESPACE
valueFrom:
fieldRef:
Expand Down Expand Up @@ -756,4 +770,6 @@ spec:
relatedImages:
- image: oci://quay.io/kuadrant/wasm-shim:latest
name: wasmshim
- image: quay.io/kuadrant/console-plugin:latest
name: consoleplugin
version: 0.0.0
14 changes: 14 additions & 0 deletions charts/kuadrant-operator/templates/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14880,6 +14880,18 @@ rules:
- get
- list
- watch
- apiGroups:
- console.openshift.io
resources:
- consoleplugins
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- coordination.k8s.io
resources:
Expand Down Expand Up @@ -15342,6 +15354,8 @@ spec:
env:
- name: RELATED_IMAGE_WASMSHIM
value: oci://quay.io/kuadrant/wasm-shim:latest
- name: RELATED_IMAGE_CONSOLEPLUGIN
value: quay.io/kuadrant/console-plugin:latest
- name: OPERATOR_NAMESPACE
valueFrom:
fieldRef:
Expand Down
2 changes: 2 additions & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ spec:
env:
- name: RELATED_IMAGE_WASMSHIM
value: "oci://quay.io/kuadrant/wasm-shim:latest"
- name: RELATED_IMAGE_CONSOLEPLUGIN
value: "quay.io/kuadrant/console-plugin:latest"
- name: OPERATOR_NAMESPACE
valueFrom:
fieldRef:
Expand Down
12 changes: 12 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ rules:
- get
- list
- watch
- apiGroups:
- console.openshift.io
resources:
- consoleplugins
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- coordination.k8s.io
resources:
Expand Down
128 changes: 128 additions & 0 deletions controllers/consoleplugin_reconciler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package controllers

import (
"context"
"sync"

"github.com/go-logr/logr"
consolev1 "github.com/openshift/api/console/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/env"
"k8s.io/utils/ptr"
ctrlruntime "sigs.k8s.io/controller-runtime"

"github.com/kuadrant/policy-machinery/controller"
"github.com/kuadrant/policy-machinery/machinery"

"github.com/kuadrant/kuadrant-operator/pkg/library/reconcilers"
"github.com/kuadrant/kuadrant-operator/pkg/library/utils"
"github.com/kuadrant/kuadrant-operator/pkg/log"
"github.com/kuadrant/kuadrant-operator/pkg/openshift"
"github.com/kuadrant/kuadrant-operator/pkg/openshift/consoleplugin"
)

//+kubebuilder:rbac:groups=console.openshift.io,resources=consoleplugins,verbs=get;list;watch;create;update;patch;delete

var (
ConsolePluginImageURL = env.GetString("RELATED_IMAGE_CONSOLEPLUGIN", "quay.io/kuadrant/console-plugin:latest")
)

type ConsolePluginReconciler struct {
*reconcilers.BaseReconciler

namespace string
}

func NewConsolePluginReconciler(mgr ctrlruntime.Manager, namespace string) *ConsolePluginReconciler {
return &ConsolePluginReconciler{
BaseReconciler: reconcilers.NewBaseReconciler(
mgr.GetClient(), mgr.GetScheme(), mgr.GetAPIReader(),
log.Log.WithName("consoleplugin"),
mgr.GetEventRecorderFor("ConsolePlugin"),
),
namespace: namespace,
}
}

func (r *ConsolePluginReconciler) Subscription() *controller.Subscription {
return &controller.Subscription{
ReconcileFunc: r.Run,
Events: []controller.ResourceEventMatcher{
{Kind: ptr.To(openshift.ConsolePluginGVK.GroupKind())},
{
Kind: ptr.To(ConfigMapGroupKind),
ObjectNamespace: r.namespace,
ObjectName: TopologyConfigMapName,
EventType: ptr.To(controller.CreateEvent),
},
{
Kind: ptr.To(ConfigMapGroupKind),
ObjectNamespace: r.namespace,
ObjectName: TopologyConfigMapName,
EventType: ptr.To(controller.DeleteEvent),
},
},
}
}

func (r *ConsolePluginReconciler) Run(eventCtx context.Context, _ []controller.ResourceEvent, topology *machinery.Topology, _ error, _ *sync.Map) error {
logger := r.Logger()
logger.V(1).Info("task started")
ctx := logr.NewContext(eventCtx, logger)

existingTopologyConfigMaps := topology.Objects().Items(func(object machinery.Object) bool {
return object.GetName() == TopologyConfigMapName && object.GetNamespace() == r.namespace && object.GroupVersionKind().Kind == ConfigMapGroupKind.Kind
})

topologyExists := len(existingTopologyConfigMaps) > 0

// Service
service := consoleplugin.Service(r.namespace)
if !topologyExists {
utils.TagObjectToDelete(service)
}
err := r.ReconcileResource(ctx, &corev1.Service{}, service, reconcilers.CreateOnlyMutator)
if err != nil {
logger.Error(err, "reconciling service")
return err
}

// Deployment
deployment := consoleplugin.Deployment(r.namespace, ConsolePluginImageURL)
deploymentMutators := make([]reconcilers.DeploymentMutateFn, 0)
deploymentMutators = append(deploymentMutators, reconcilers.DeploymentImageMutator)
if !topologyExists {
utils.TagObjectToDelete(deployment)
}
err = r.ReconcileResource(ctx, &appsv1.Deployment{}, deployment, reconcilers.DeploymentMutator(deploymentMutators...))
if err != nil {
logger.Error(err, "reconciling deployment")
return err
}

// Nginx ConfigMap
nginxConfigMap := consoleplugin.NginxConfigMap(r.namespace)
if !topologyExists {
utils.TagObjectToDelete(nginxConfigMap)
}
err = r.ReconcileResource(ctx, &corev1.ConfigMap{}, nginxConfigMap, reconcilers.CreateOnlyMutator)
if err != nil {
logger.Error(err, "reconciling nginx configmap")
return err
}

// ConsolePlugin
consolePlugin := consoleplugin.ConsolePlugin(r.namespace)
if !topologyExists {
utils.TagObjectToDelete(consolePlugin)
}
err = r.ReconcileResource(ctx, &consolev1.ConsolePlugin{}, consolePlugin, reconcilers.CreateOnlyMutator)
if err != nil {
logger.Error(err, "reconciling consoleplugin")
return err
}

logger.V(1).Info("task ended")
return nil
}
Loading

0 comments on commit bd043cd

Please sign in to comment.