Skip to content

Commit 118dee6

Browse files
5 code refactoring and docs revision 1 (#6)
* chore: docs and code refactoring * chore: docs and code refactoring * chore: code refactoring * ci: add ignore file in codecov
1 parent e60f1c2 commit 118dee6

File tree

9 files changed

+55
-69
lines changed

9 files changed

+55
-69
lines changed

.ko.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
builds:
2-
- id: composition-dynamic-controller-v2
2+
- id: rest-dynamic-controller
33
#main: main.go
44
dir: .
55
env:

README.md

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
# Composition Dynamic Controller V2
2-
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.
1+
# Rest Dynamic Controller
32

4-
## Summary
3+
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`.
4+
5+
## Table of Contents
56

6-
- [Summary](#summary)
77
- [Overview](#overview)
8-
- [Examples](#examples)
8+
- [How It Works](#how-it-works)
9+
- [Usage Examples](#usage-examples)
910
- [Configuration](#configuration)
10-
11+
1112
## Overview
1213

13-
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.
14+
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.
15+
16+
## How It Works
1417

15-
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/[email protected]#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.
18+
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:
1619

17-
## Examples
20+
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).
21+
2. If the resource doesn't exist, the controller performs the action described in the `verbsDescription` field of the `RestDefinition` CR.
22+
3. For deletion requests, a similar process is followed.
23+
4. During resource observation, the controller verifies if the remote resource is synchronized with the CR and performs updates if necessary.
24+
25+
## Usage Examples
26+
27+
Here's an example of a Custom Resource manifest:
1828

1929
```yaml
2030
kind: Repo
@@ -31,10 +41,10 @@ spec:
3141
bearerAuthRef: bearer-gh-ref
3242
```
3343
34-
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.
44+
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.
3545

36-
<details>
37-
<summary><b>Github Repo RestDefinition</b></summary>
46+
<details>
47+
<summary><b>GitHub Repo RestDefinition</b></summary>
3848

3949
```yaml
4050
kind: RestDefinition
@@ -43,13 +53,12 @@ metadata:
4353
name: def-github
4454
namespace: default
4555
spec:
46-
deletionPolicy: Delete
4756
oasPath: https://github.com/krateoplatformops/github-oas3/raw/1-oas-specification-fixes/openapi.yaml
4857
resourceGroup: gen.github.com
49-
resource:
58+
resource:
5059
kind: Repo
5160
identifiers:
52-
- id
61+
- id
5362
- name
5463
- html_url
5564
verbsDescription:
@@ -69,20 +78,20 @@ spec:
6978
org: owner
7079
name: repo
7180
```
72-
7381
</details>
7482

7583
## Configuration
7684

77-
### Operator Env Vars
85+
### Environment Variables
7886

79-
These enviroment varibles can be changed in the Deployment of the composition-dynamic-controller we need to tweak.
87+
The following environment variables can be configured in the rest-dynamic-controller's Deployment:
8088

81-
| Name | Description | Default Value |
82-
|:---------------------------------------|:---------------------------|:--------------|
83-
| COMPOSITION_CONTROLLER_DEBUG | dump verbose output | false |
84-
| COMPOSITION_CONTROLLER_WORKERS | number of workers | 1 |
85-
| COMPOSITION_CONTROLLER_RESYNC_INTERVAL | resync interval | 3m |
86-
| COMPOSITION_CONTROLLER_GROUP | resource api group | |
87-
| COMPOSITION_CONTROLLER_VERSION | resource api version | |
88-
| COMPOSITION_CONTROLLER_RESOURCE | resource plural name | |
89+
| Name | Description | Default Value |
90+
|------|-------------|---------------|
91+
| REST_CONTROLLER_DEBUG | Enable verbose output | `false` |
92+
| REST_CONTROLLER_WORKERS | Number of worker threads | `1` |
93+
| REST_CONTROLLER_RESYNC_INTERVAL | Interval between resyncs | `3m` |
94+
| REST_CONTROLLER_GROUP | Resource API group | - |
95+
| REST_CONTROLLER_VERSION | Resource API version | - |
96+
| REST_CONTROLLER_RESOURCE | Resource plural name | - |
97+
| URL_PLURALS | BFF plurals endpoint | `http://bff.krateo-system.svc.cluster.local:8081/api-info/names` |

codecov.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# sample regex patterns
2+
ignore:
3+
- "internal/restResources/restResources.go"

internal/client/clienttools.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func (u *UnstructuredClient) RequestedParams(httpMethod string, path string) (pa
242242

243243
// BuildClient is a function that builds partial client from a swagger file.
244244
func BuildClient(swaggerPath string) (*UnstructuredClient, error) {
245-
basePath := "/tmp/composition-dynamic-controller"
245+
basePath := "/tmp/rest-dynamic-controller"
246246
err := os.MkdirAll(basePath, 0755)
247247
defer os.RemoveAll(basePath)
248248
if err != nil {

internal/composition/composition.go renamed to internal/restResources/restResources.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package composition
1+
package restResources
22

33
import (
44
"context"
@@ -311,7 +311,7 @@ func (h *handler) Update(ctx context.Context, mg *unstructured.Unstructured) err
311311
WithValues("name", mg.GetName()).
312312
WithValues("namespace", mg.GetNamespace())
313313

314-
log.Debug("Handling composition values update.")
314+
log.Debug("Handling custom resource values update.")
315315
if h.swaggerInfoGetter == nil {
316316
return fmt.Errorf("swagger info getter must be specified")
317317
}
@@ -384,7 +384,7 @@ func (h *handler) Update(ctx context.Context, mg *unstructured.Unstructured) err
384384
log.Debug("Updating status", "error", err)
385385
return err
386386
}
387-
log.Debug("Composition values updated", "kind", mg.GetKind())
387+
log.Debug("Custom resource values updated", "kind", mg.GetKind())
388388

389389
return nil
390390

@@ -397,7 +397,7 @@ func (h *handler) Delete(ctx context.Context, mg *unstructured.Unstructured) err
397397
WithValues("name", mg.GetName()).
398398
WithValues("namespace", mg.GetNamespace())
399399

400-
log.Debug("Handling composition values deletion.")
400+
log.Debug("Handling custom resource values deletion.")
401401

402402
if h.swaggerInfoGetter == nil {
403403
return fmt.Errorf("swagger info getter must be specified")

internal/composition/support.go renamed to internal/restResources/support.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package composition
1+
package restResources
22

33
import (
44
"context"

internal/tools/restclient/getter.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -240,20 +240,6 @@ func (g *dynamicGetter) getAuth(un *unstructured.Unstructured) (httplib.AuthMeth
240240
}
241241
}
242242

243-
// authRef, ok, err = unstructured.NestedString(un.Object, "spec", "basicAuthRef")
244-
// if err != nil {
245-
// return nil, fmt.Errorf()
246-
// }
247-
// if !ok {
248-
// authRef, ok, err = unstructured.NestedString(un.Object, "spec", "bearerAuthRef")
249-
// if err != nil {
250-
// return nil, err
251-
// }
252-
// if !ok {
253-
// return nil, fmt.Errorf("missing spec.basicAuthRef or spec.bearerAuthRef in definition for '%v' in namespace: %s", gvr, un.GetNamespace())
254-
// }
255-
// authType = restclient.AuthTypeBearer
256-
// }
257243
gvrForAuthentication := schema.GroupVersionResource{
258244
Group: gvr.Group,
259245
Version: "v1alpha1",

main.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/krateoplatformops/unstructured-runtime/pkg/logging"
1515
"sigs.k8s.io/controller-runtime/pkg/log/zap"
1616

17-
restComposition "github.com/krateoplatformops/rest-dynamic-controller/internal/composition"
17+
restResources "github.com/krateoplatformops/rest-dynamic-controller/internal/restResources"
1818
"github.com/krateoplatformops/rest-dynamic-controller/internal/support"
1919
getter "github.com/krateoplatformops/rest-dynamic-controller/internal/tools/restclient"
2020
"github.com/krateoplatformops/unstructured-runtime/pkg/controller"
@@ -28,7 +28,7 @@ import (
2828
)
2929

3030
const (
31-
serviceName = "composition-dynamic-controller"
31+
serviceName = "rest-dynamic-controller"
3232
)
3333

3434
var (
@@ -40,18 +40,18 @@ func main() {
4040
kubeconfig := flag.String("kubeconfig", support.EnvString("KUBECONFIG", ""),
4141
"absolute path to the kubeconfig file")
4242
debug := flag.Bool("debug",
43-
support.EnvBool("COMPOSITION_CONTROLLER_DEBUG", false), "dump verbose output")
44-
workers := flag.Int("workers", support.EnvInt("COMPOSITION_CONTROLLER_WORKERS", 1), "number of workers")
43+
support.EnvBool("REST_CONTROLLER_DEBUG", false), "dump verbose output")
44+
workers := flag.Int("workers", support.EnvInt("REST_CONTROLLER_WORKERS", 1), "number of workers")
4545
resyncInterval := flag.Duration("resync-interval",
46-
support.EnvDuration("COMPOSITION_CONTROLLER_RESYNC_INTERVAL", time.Minute*1), "resync interval")
46+
support.EnvDuration("REST_CONTROLLER_RESYNC_INTERVAL", time.Minute*1), "resync interval")
4747
resourceGroup := flag.String("group",
48-
support.EnvString("COMPOSITION_CONTROLLER_GROUP", ""), "resource api group")
48+
support.EnvString("REST_CONTROLLER_GROUP", ""), "resource api group")
4949
resourceVersion := flag.String("version",
50-
support.EnvString("COMPOSITION_CONTROLLER_VERSION", ""), "resource api version")
50+
support.EnvString("REST_CONTROLLER_VERSION", ""), "resource api version")
5151
resourceName := flag.String("resource",
52-
support.EnvString("COMPOSITION_CONTROLLER_RESOURCE", ""), "resource plural name")
52+
support.EnvString("REST_CONTROLLER_RESOURCE", ""), "resource plural name")
5353
namespace := flag.String("namespace",
54-
support.EnvString("COMPOSITION_CONTROLLER_NAMESPACE", "default"), "namespace")
54+
support.EnvString("REST_CONTROLLER_NAMESPACE", "default"), "namespace")
5555
urlplurals := flag.String("urlplurals",
5656
support.EnvString("URL_PLURALS", "http://bff.krateo-system.svc.cluster.local:8081/api-info/names"), "url plurals")
5757

@@ -107,7 +107,7 @@ func main() {
107107

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

110-
handler = restComposition.NewHandler(cfg, log, swg, *pluralizer)
110+
handler = restResources.NewHandler(cfg, log, swg, *pluralizer)
111111

112112
controller := genctrl.New(genctrl.Options{
113113
Discovery: cachedDisc,

scripts/run.sh

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)