From 9670d7685c589c376acbc0a7305caf6953d0486e Mon Sep 17 00:00:00 2001 From: Matteo Gastaldello Date: Mon, 11 Nov 2024 11:28:15 +0100 Subject: [PATCH 1/4] chore: docs and code refactoring --- .ko.yaml | 2 +- README.md | 61 +++++++++++++++++------------ internal/client/clienttools.go | 2 +- internal/tools/restclient/getter.go | 14 ------- main.go | 16 ++++---- scripts/run.sh | 12 ------ 6 files changed, 45 insertions(+), 62 deletions(-) delete mode 100755 scripts/run.sh diff --git a/.ko.yaml b/.ko.yaml index 40fcf87..0220a4e 100644 --- a/.ko.yaml +++ b/.ko.yaml @@ -1,5 +1,5 @@ builds: -- id: composition-dynamic-controller-v2 +- id: rest-dynamic-controller #main: main.go dir: . env: diff --git a/README.md b/README.md index e8b9236..2c290ca 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,30 @@ -# Composition Dynamic Controller V2 -The composition-dynamic-controller-v2 is an operator that is instantiated by the [oasgen-provider](https://github.com/krateoplatformops/core-provider) to manage the Custom Resources whose Custom Resource Definition is generated by the oasgen-provider. +# Rest Dynamic Controller -## Summary +The `rest-dynamic-controller` is an operator instantiated by the [oasgen-provider](https://github.com/krateoplatformops/oasgen-provider) to manage Custom Resources whose Custom Resource Definitions (CRDs) are generated by the `oasgen-provider`. + +## Table of Contents -- [Summary](#summary) - [Overview](#overview) -- [Examples](#examples) +- [How It Works](#how-it-works) +- [Usage Examples](#usage-examples) - [Configuration](#configuration) - + ## Overview -The `composition-dynamic-controller-v2` is a fork of [`composition-dynamic-controller`](https://github.com/krateoplatformops/rest-dynamic-controller) that adds the capability to manage Remote Resources (via REST APIs) in addition to managing Helm charts. +The `rest-dynamic-controller` is a dynamic controller that manages Remote Resources through REST APIs. It's considered "dynamic" because it can manage any type of remote resource represented by a Custom Resource Definition and its related Custom Resource. The controller is configured at startup through environment variables (or CLI parameters) to manage a specific Group Version Resource. + +## How It Works -In practice, when a Custom Resource (CR) is created and its Custom Resource Definition (CRD) has been generated by a `RestDefinition` from [`oasgen-provider`](https://github.com/krateoplatformops/oasgen-provider), the instance of `composition-dynamic-controller-v2` enabled to manage this specific Group Version Kind (GVK) checks if the remote resource already exists by following the instructions defined in the [`RestDefinition`](https://doc.crds.dev/github.com/krateoplatformops/oasgen-provider). If the resource does not exist, it performs [the action described](https://doc.crds.dev/github.com/krateoplatformops/oasgen-provider/swaggergen.krateo.io/RestDefinition/v1alpha1@0.1.4#spec-resource-verbsDescription-action) in the `verbsDescription` field of the `RestDefinition` CR. The same process occurs when a request is made to delete the resource. Additionally, when observing the resource, `composition-dynamic-controller-v2` checks if the remote resource is up-to-date with the CR and performs an update if it is not. +When a Custom Resource (CR) is created and its Custom Resource Definition (CRD) has been generated by a `RestDefinition` from the `oasgen-provider`, the following process occurs: -## Examples +1. The `rest-dynamic-controller` instance checks if the remote resource exists by following the instructions defined in the [`RestDefinition`](https://doc.crds.dev/github.com/krateoplatformops/oasgen-provider). +2. If the resource doesn't exist, the controller performs the action described in the `verbsDescription` field of the `RestDefinition` CR. +3. For deletion requests, a similar process is followed. +4. During resource observation, the controller verifies if the remote resource is synchronized with the CR and performs updates if necessary. + +## Usage Examples + +Here's an example of a Custom Resource manifest: ```yaml kind: Repo @@ -31,10 +41,10 @@ spec: bearerAuthRef: bearer-gh-ref ``` -The manifest described above represents a Custom Resource (CR) of kind `Repo`, and its apiVersion is `gen.github.com/v1alpha1`. This CRD was generated by the oasgen-provider based on the specifications outlined in the RestDefinition below. Applying this manifest within the cluster initiates the reconciliation process for the resource described. +This manifest represents a CR of kind `Repo` with apiVersion `gen.github.com/v1alpha1`. The CRD was generated by the oasgen-provider based on the specifications in the RestDefinition shown below. -
-Github Repo RestDefinition +
+GitHub Repo RestDefinition ```yaml kind: RestDefinition @@ -43,13 +53,12 @@ metadata: name: def-github namespace: default spec: - deletionPolicy: Delete oasPath: https://github.com/krateoplatformops/github-oas3/raw/1-oas-specification-fixes/openapi.yaml resourceGroup: gen.github.com - resource: + resource: kind: Repo identifiers: - - id + - id - name - html_url verbsDescription: @@ -69,20 +78,20 @@ spec: org: owner name: repo ``` -
## Configuration -### Operator Env Vars +### Environment Variables -These enviroment varibles can be changed in the Deployment of the composition-dynamic-controller we need to tweak. +The following environment variables can be configured in the rest-dynamic-controller's Deployment: -| Name | Description | Default Value | -|:---------------------------------------|:---------------------------|:--------------| -| COMPOSITION_CONTROLLER_DEBUG | dump verbose output | false | -| COMPOSITION_CONTROLLER_WORKERS | number of workers | 1 | -| COMPOSITION_CONTROLLER_RESYNC_INTERVAL | resync interval | 3m | -| COMPOSITION_CONTROLLER_GROUP | resource api group | | -| COMPOSITION_CONTROLLER_VERSION | resource api version | | -| COMPOSITION_CONTROLLER_RESOURCE | resource plural name | | \ No newline at end of file +| Name | Description | Default Value | +|------|-------------|---------------| +| REST_CONTROLLER_DEBUG | Enable verbose output | `false` | +| REST_CONTROLLER_WORKERS | Number of worker threads | `1` | +| REST_CONTROLLER_RESYNC_INTERVAL | Interval between resyncs | `3m` | +| REST_CONTROLLER_GROUP | Resource API group | - | +| REST_CONTROLLER_VERSION | Resource API version | - | +| REST_CONTROLLER_RESOURCE | Resource plural name | - | +| URL_PLURALS | BFF plurals endpoint | `http://bff.krateo-system.svc.cluster.local:8081/api-info/names` | \ No newline at end of file diff --git a/internal/client/clienttools.go b/internal/client/clienttools.go index 744ac20..571c87e 100644 --- a/internal/client/clienttools.go +++ b/internal/client/clienttools.go @@ -242,7 +242,7 @@ func (u *UnstructuredClient) RequestedParams(httpMethod string, path string) (pa // BuildClient is a function that builds partial client from a swagger file. func BuildClient(swaggerPath string) (*UnstructuredClient, error) { - basePath := "/tmp/composition-dynamic-controller" + basePath := "/tmp/rest-dynamic-controller" err := os.MkdirAll(basePath, 0755) defer os.RemoveAll(basePath) if err != nil { diff --git a/internal/tools/restclient/getter.go b/internal/tools/restclient/getter.go index ad30126..058de4d 100644 --- a/internal/tools/restclient/getter.go +++ b/internal/tools/restclient/getter.go @@ -240,20 +240,6 @@ func (g *dynamicGetter) getAuth(un *unstructured.Unstructured) (httplib.AuthMeth } } - // authRef, ok, err = unstructured.NestedString(un.Object, "spec", "basicAuthRef") - // if err != nil { - // return nil, fmt.Errorf() - // } - // if !ok { - // authRef, ok, err = unstructured.NestedString(un.Object, "spec", "bearerAuthRef") - // if err != nil { - // return nil, err - // } - // if !ok { - // return nil, fmt.Errorf("missing spec.basicAuthRef or spec.bearerAuthRef in definition for '%v' in namespace: %s", gvr, un.GetNamespace()) - // } - // authType = restclient.AuthTypeBearer - // } gvrForAuthentication := schema.GroupVersionResource{ Group: gvr.Group, Version: "v1alpha1", diff --git a/main.go b/main.go index 28cec03..c1b1cec 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,7 @@ import ( ) const ( - serviceName = "composition-dynamic-controller" + serviceName = "rest-dynamic-controller" ) var ( @@ -40,18 +40,18 @@ func main() { kubeconfig := flag.String("kubeconfig", support.EnvString("KUBECONFIG", ""), "absolute path to the kubeconfig file") debug := flag.Bool("debug", - support.EnvBool("COMPOSITION_CONTROLLER_DEBUG", false), "dump verbose output") - workers := flag.Int("workers", support.EnvInt("COMPOSITION_CONTROLLER_WORKERS", 1), "number of workers") + support.EnvBool("REST_CONTROLLER_DEBUG", false), "dump verbose output") + workers := flag.Int("workers", support.EnvInt("REST_CONTROLLER_WORKERS", 1), "number of workers") resyncInterval := flag.Duration("resync-interval", - support.EnvDuration("COMPOSITION_CONTROLLER_RESYNC_INTERVAL", time.Minute*1), "resync interval") + support.EnvDuration("REST_CONTROLLER_RESYNC_INTERVAL", time.Minute*1), "resync interval") resourceGroup := flag.String("group", - support.EnvString("COMPOSITION_CONTROLLER_GROUP", ""), "resource api group") + support.EnvString("REST_CONTROLLER_GROUP", ""), "resource api group") resourceVersion := flag.String("version", - support.EnvString("COMPOSITION_CONTROLLER_VERSION", ""), "resource api version") + support.EnvString("REST_CONTROLLER_VERSION", ""), "resource api version") resourceName := flag.String("resource", - support.EnvString("COMPOSITION_CONTROLLER_RESOURCE", ""), "resource plural name") + support.EnvString("REST_CONTROLLER_RESOURCE", ""), "resource plural name") namespace := flag.String("namespace", - support.EnvString("COMPOSITION_CONTROLLER_NAMESPACE", "default"), "namespace") + support.EnvString("REST_CONTROLLER_NAMESPACE", "default"), "namespace") urlplurals := flag.String("urlplurals", support.EnvString("URL_PLURALS", "http://bff.krateo-system.svc.cluster.local:8081/api-info/names"), "url plurals") diff --git a/scripts/run.sh b/scripts/run.sh deleted file mode 100755 index 6bef117..0000000 --- a/scripts/run.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - - -kind get kubeconfig >/dev/null 2>&1 || kind create cluster - -export COMPOSITION_CONTROLLER_DEBUG=true -export COMPOSITION_CONTROLLER_GROUP=composition.krateo.io -export COMPOSITION_CONTROLLER_VERSION=v0-1-0 -export COMPOSITION_CONTROLLER_RESOURCE=fireworksapps -export COMPOSITION_CONTROLLER_NAMESPACE=demo-system - -go run main.go -kubeconfig $HOME/.kube/config \ No newline at end of file From 0bec30a8d76373aa8fc4d78c16c76544ed602da1 Mon Sep 17 00:00:00 2001 From: Matteo Gastaldello Date: Mon, 11 Nov 2024 11:28:32 +0100 Subject: [PATCH 2/4] chore: docs and code refactoring --- internal/composition/composition.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/composition/composition.go b/internal/composition/composition.go index f390665..c374536 100644 --- a/internal/composition/composition.go +++ b/internal/composition/composition.go @@ -311,7 +311,7 @@ func (h *handler) Update(ctx context.Context, mg *unstructured.Unstructured) err WithValues("name", mg.GetName()). WithValues("namespace", mg.GetNamespace()) - log.Debug("Handling composition values update.") + log.Debug("Handling custom resource values update.") if h.swaggerInfoGetter == nil { return fmt.Errorf("swagger info getter must be specified") } @@ -384,7 +384,7 @@ func (h *handler) Update(ctx context.Context, mg *unstructured.Unstructured) err log.Debug("Updating status", "error", err) return err } - log.Debug("Composition values updated", "kind", mg.GetKind()) + log.Debug("Custom resource values updated", "kind", mg.GetKind()) return nil @@ -397,7 +397,7 @@ func (h *handler) Delete(ctx context.Context, mg *unstructured.Unstructured) err WithValues("name", mg.GetName()). WithValues("namespace", mg.GetNamespace()) - log.Debug("Handling composition values deletion.") + log.Debug("Handling custom resource values deletion.") if h.swaggerInfoGetter == nil { return fmt.Errorf("swagger info getter must be specified") From 8e472598e8d57f3f5901a361ca4c0d9d3be0544d Mon Sep 17 00:00:00 2001 From: Matteo Gastaldello Date: Mon, 11 Nov 2024 11:33:38 +0100 Subject: [PATCH 3/4] chore: code refactoring --- .../composition.go => restResources/restResources.go} | 2 +- internal/{composition => restResources}/support.go | 2 +- main.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) rename internal/{composition/composition.go => restResources/restResources.go} (99%) rename internal/{composition => restResources}/support.go (99%) diff --git a/internal/composition/composition.go b/internal/restResources/restResources.go similarity index 99% rename from internal/composition/composition.go rename to internal/restResources/restResources.go index c374536..bc14496 100644 --- a/internal/composition/composition.go +++ b/internal/restResources/restResources.go @@ -1,4 +1,4 @@ -package composition +package restResources import ( "context" diff --git a/internal/composition/support.go b/internal/restResources/support.go similarity index 99% rename from internal/composition/support.go rename to internal/restResources/support.go index f9f0064..077535a 100644 --- a/internal/composition/support.go +++ b/internal/restResources/support.go @@ -1,4 +1,4 @@ -package composition +package restResources import ( "context" diff --git a/main.go b/main.go index c1b1cec..3b803ce 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,7 @@ import ( "github.com/krateoplatformops/unstructured-runtime/pkg/logging" "sigs.k8s.io/controller-runtime/pkg/log/zap" - restComposition "github.com/krateoplatformops/rest-dynamic-controller/internal/composition" + restResources "github.com/krateoplatformops/rest-dynamic-controller/internal/restResources" "github.com/krateoplatformops/rest-dynamic-controller/internal/support" getter "github.com/krateoplatformops/rest-dynamic-controller/internal/tools/restclient" "github.com/krateoplatformops/unstructured-runtime/pkg/controller" @@ -107,7 +107,7 @@ func main() { pluralizer := pluralizer.New(urlplurals, http.DefaultClient) - handler = restComposition.NewHandler(cfg, log, swg, *pluralizer) + handler = restResources.NewHandler(cfg, log, swg, *pluralizer) controller := genctrl.New(genctrl.Options{ Discovery: cachedDisc, From d0423a790b07b9d6fe78f83fb389af250092f4aa Mon Sep 17 00:00:00 2001 From: Matteo Gastaldello Date: Mon, 11 Nov 2024 11:35:12 +0100 Subject: [PATCH 4/4] ci: add ignore file in codecov --- codecov.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 codecov.yml diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 0000000..6b88a42 --- /dev/null +++ b/codecov.yml @@ -0,0 +1,3 @@ +# sample regex patterns +ignore: + - "internal/restResources/restResources.go" \ No newline at end of file