Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ko.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
builds:
- id: composition-dynamic-controller-v2
- id: rest-dynamic-controller
#main: main.go
dir: .
env:
Expand Down
61 changes: 35 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.

<details>
<summary><b>Github Repo RestDefinition</b></summary>
<details>
<summary><b>GitHub Repo RestDefinition</b></summary>

```yaml
kind: RestDefinition
Expand All @@ -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:
Expand All @@ -69,20 +78,20 @@ spec:
org: owner
name: repo
```

</details>

## 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 | |
| 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` |
3 changes: 3 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# sample regex patterns
ignore:
- "internal/restResources/restResources.go"
2 changes: 1 addition & 1 deletion internal/client/clienttools.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@

// 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"

Check warning on line 245 in internal/client/clienttools.go

View check run for this annotation

Codecov / codecov/patch

internal/client/clienttools.go#L245

Added line #L245 was not covered by tests
err := os.MkdirAll(basePath, 0755)
defer os.RemoveAll(basePath)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package composition
package restResources

import (
"context"
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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

Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package composition
package restResources

import (
"context"
Expand Down
14 changes: 0 additions & 14 deletions internal/tools/restclient/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 10 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"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"
Expand All @@ -28,7 +28,7 @@
)

const (
serviceName = "composition-dynamic-controller"
serviceName = "rest-dynamic-controller"
)

var (
Expand All @@ -40,18 +40,18 @@
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")

Check warning on line 44 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L43-L44

Added lines #L43 - L44 were not covered by tests
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")

Check warning on line 46 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L46

Added line #L46 was not covered by tests
resourceGroup := flag.String("group",
support.EnvString("COMPOSITION_CONTROLLER_GROUP", ""), "resource api group")
support.EnvString("REST_CONTROLLER_GROUP", ""), "resource api group")

Check warning on line 48 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L48

Added line #L48 was not covered by tests
resourceVersion := flag.String("version",
support.EnvString("COMPOSITION_CONTROLLER_VERSION", ""), "resource api version")
support.EnvString("REST_CONTROLLER_VERSION", ""), "resource api version")

Check warning on line 50 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L50

Added line #L50 was not covered by tests
resourceName := flag.String("resource",
support.EnvString("COMPOSITION_CONTROLLER_RESOURCE", ""), "resource plural name")
support.EnvString("REST_CONTROLLER_RESOURCE", ""), "resource plural name")

Check warning on line 52 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L52

Added line #L52 was not covered by tests
namespace := flag.String("namespace",
support.EnvString("COMPOSITION_CONTROLLER_NAMESPACE", "default"), "namespace")
support.EnvString("REST_CONTROLLER_NAMESPACE", "default"), "namespace")

Check warning on line 54 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L54

Added line #L54 was not covered by tests
urlplurals := flag.String("urlplurals",
support.EnvString("URL_PLURALS", "http://bff.krateo-system.svc.cluster.local:8081/api-info/names"), "url plurals")

Expand Down Expand Up @@ -107,7 +107,7 @@

pluralizer := pluralizer.New(urlplurals, http.DefaultClient)

handler = restComposition.NewHandler(cfg, log, swg, *pluralizer)
handler = restResources.NewHandler(cfg, log, swg, *pluralizer)

Check warning on line 110 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L110

Added line #L110 was not covered by tests

controller := genctrl.New(genctrl.Options{
Discovery: cachedDisc,
Expand Down
12 changes: 0 additions & 12 deletions scripts/run.sh

This file was deleted.

Loading