Skip to content

Commit 5693b48

Browse files
authored
Merge pull request #2 from gianlucam76/controller
Controller
2 parents 2a6fef2 + f8024af commit 5693b48

File tree

6 files changed

+205
-19
lines changed

6 files changed

+205
-19
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.18 as builder
2+
FROM golang:1.19 as builder
33

44
WORKDIR /workspace
55
# Copy the Go Modules manifests

README.md

Lines changed: 155 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ A Jsonnetcontroller. It can fetch Jsonnet files from:
44
1. Flux Sources (GitRepository/OCIRepository/Bucket)
55
2. ConfigMap/Secret
66

7-
process those files programmatically invoking jsonnet go module and store the output in its Status section. Sveltos addon-manager can then be used to deploy the output of the ytt-controller in all selected managed clusters.
7+
process those files programmatically invoking jsonnet go module and store the output in its Status section. Sveltos addon-manager can then be used to deploy the output of the jsonnet-controller in all selected managed clusters.
88

99
## Install
1010

@@ -18,11 +18,160 @@ or if you want a specific version
1818
kubectl apply -f https://raw.githubusercontent.com/gianlucam76/jsonnet-controller/<tag>/manifest/manifest.yaml
1919
```
2020

21-
## Contributing
21+
22+
## Using Flux GitRepository
23+
24+
For instance, this Github repository https://github.com/gianlucam76/jsonnet-examples contains jsonnet files.
25+
You can use Flux to sync from it and then simply post this [JsonnetSource](https://github.com/gianlucam76/jsonnet-controller/blob/main/api/v1alpha1/jsonnetsource_types.go) CRD instance.
26+
The jsonnet-controller will detect when Flux has synced the repo (and anytime there is a change), will programatically invoke jsonnet go module and store the outcome in its Status.Resources field.
27+
28+
```yaml
29+
apiVersion: extension.projectsveltos.io/v1alpha1
30+
kind: JsonnetSource
31+
metadata:
32+
name: jsonnetsource-flux
33+
spec:
34+
namespace: flux-system
35+
name: flux-system
36+
kind: GitRepository
37+
path: ./variables/deployment.jsonnet
38+
variables:
39+
deploymentName: eng
40+
namespace: staging
41+
replicas: "3"
42+
```
43+
44+
```yaml
45+
apiVersion: extension.projectsveltos.io/v1alpha1
46+
kind: JsonnetSource
47+
metadata:
48+
annotations:
49+
kubectl.kubernetes.io/last-applied-configuration: |
50+
{"apiVersion":"extension.projectsveltos.io/v1alpha1","kind":"JsonnetSource","metadata":{"annotations":{},"name":"jsonnetsource-flux","namespace":"default"},"spec":{"kind":"GitRepository","name":"flux-system","namespace":"flux-system","path":"./variables/deployment.jsonnet","variables":{"deploymentName":"eng","namespace":"staging","replicas":"3"}}}
51+
creationTimestamp: "2023-05-26T06:55:13Z"
52+
generation: 3
53+
name: jsonnetsource-flux
54+
namespace: default
55+
resourceVersion: "39826"
56+
uid: b4cc7584-528d-4938-b6ed-74ec5ba1d760
57+
spec:
58+
kind: GitRepository
59+
name: flux-system
60+
namespace: flux-system
61+
path: ./variables/deployment.jsonnet
62+
variables:
63+
deploymentName: eng
64+
namespace: staging
65+
replicas: "3"
66+
status:
67+
resources: |
68+
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"labels":{"app":"eng"},"name":"eng","namespace":"staging"},"spec":{"replicas":3,"selector":{"matchLabels":{"app":"eng"}},"template":{"metadata":{"labels":{"app":"eng"}},"spec":{"containers":[{"image":"nginx:latest","name":"my-container","ports":[{"containerPort":80}]}]}}}}
69+
```
70+
71+
Sveltos can used at this point to deploy resources in managed clusters:
72+
73+
```yaml
74+
apiVersion: config.projectsveltos.io/v1alpha1
75+
kind: ClusterProfile
76+
metadata:
77+
name: deploy-resources
78+
spec:
79+
clusterSelector: env=fv
80+
templateResourceRefs:
81+
- resource:
82+
apiVersion: extension.projectsveltos.io/v1alpha1
83+
kind: JsonnetSource
84+
name: jsonnetsource-flux
85+
namespace: default
86+
identifier: JsonnetSource
87+
policyRefs:
88+
- kind: ConfigMap
89+
name: info
90+
namespace: default
91+
---
92+
apiVersion: v1
93+
kind: ConfigMap
94+
metadata:
95+
name: info
96+
namespace: default
97+
annotations:
98+
projectsveltos.io/template: "true" # add annotation to indicate Sveltos content is a template
99+
data:
100+
resource.yaml: |
101+
{{ (index .MgtmResources "JsonnetSource").status.resources }}
102+
```
103+
104+
```bash
105+
kubectl exec -it -n projectsveltos sveltosctl-0 -- ./sveltosctl show addons
106+
+-------------------------------------+-----------------+-----------+------+---------+-------------------------------+------------------+
107+
| CLUSTER | RESOURCE TYPE | NAMESPACE | NAME | VERSION | TIME | CLUSTER PROFILES |
108+
+-------------------------------------+-----------------+-----------+------+---------+-------------------------------+------------------+
109+
| default/sveltos-management-workload | apps:Deployment | staging | eng | N/A | 2023-05-26 00:24:57 -0700 PDT | deploy-resources |
110+
+-------------------------------------+-----------------+-----------+------+---------+-------------------------------+------------------+
111+
```
112+
113+
## Using ConfigMap/Secret
114+
115+
JsonnetSource can also reference ConfigMap/Secret. For instance, we can create a ConfigMap whose BinaryData section contains jsonnet files.
116+
117+
```bash
118+
tar -czf jsonnet.tar.gz -C ~mgianluc/go/src/github.com/gianlucam76/jsonnet-examples/multiple-files .
119+
kubectl create configmap jsonnet --from-file=jsonnet.tar.gz=jsonnet.tar.gz
120+
```
121+
122+
Then we can have JsonnetSource reference this ConfigMap instance
123+
124+
```yaml
125+
apiVersion: extension.projectsveltos.io/v1alpha1
126+
kind: JsonnetSource
127+
metadata:
128+
name: jsonnetsource-configmap
129+
spec:
130+
namespace: default
131+
name: jsonnet
132+
kind: ConfigMap
133+
path: ./main.jsonnet
134+
variables:
135+
namespace: production
136+
```
137+
138+
and the controller will programmatically execute jsonnet go module and store the outcome in Status.Results.
139+
140+
```yaml
141+
apiVersion: extension.projectsveltos.io/v1alpha1
142+
kind: JsonnetSource
143+
metadata:
144+
annotations:
145+
kubectl.kubernetes.io/last-applied-configuration: |
146+
{"apiVersion":"extension.projectsveltos.io/v1alpha1","kind":"JsonnetSource","metadata":{"annotations":{},"name":"jsonnetsource-configmap","namespace":"default"},"spec":{"kind":"ConfigMap","name":"jsonnet","namespace":"default","path":"./main.jsonnet","variables":{"namespace":"production"}}}
147+
creationTimestamp: "2023-05-26T08:28:48Z"
148+
generation: 1
149+
name: jsonnetsource-configmap
150+
namespace: default
151+
resourceVersion: "121599"
152+
uid: eea93390-771d-4176-92fe-2b761b803764
153+
spec:
154+
kind: ConfigMap
155+
name: jsonnet
156+
namespace: default
157+
path: ./main.jsonnet
158+
variables:
159+
namespace: production
160+
status:
161+
resources: |
162+
---
163+
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"name":"my-deployment","namespace":"production"},"spec":{"replicas":3,"selector":{"matchLabels":{"app":"my-app"}},"template":{"metadata":{"labels":{"app":"my-app"}},"spec":{"containers":[{"image":"my-image:latest","name":"my-container","ports":[{"containerPort":8080}]}]}}}}
164+
---
165+
{"apiVersion":"v1","kind":"Service","metadata":{"name":"my-service","namespace":"production"},"spec":{"ports":[{"port":80,"protocol":"TCP","targetPort":8080}],"selector":{"app":"my-app"},"type":"LoadBalancer"}}
166+
```
167+
168+
At this point [Sveltos addon-manager](https://github.com/projectsveltos/addon-manager) to use the output of the jsonnet-controller and deploy those resources in all selected managed clusters. To know more refer to [Sveltos documentation](https://projectsveltos.github.io/sveltos/ytt_extension/)
169+
170+
## Contributing
22171
23172
❤️ Your contributions are always welcome! If you want to contribute, have questions, noticed any bug or want to get the latest project news, you can connect with us in the following ways:
24173
25-
Read contributing guidelines
26-
Open a bug/feature enhancement on github contributions welcome
27-
Chat with us on the Slack in the #projectsveltos channel Slack
28-
Contact Us
174+
1. Read contributing [guidelines](CONTRIBUTING.md)
175+
2. Open a bug/feature enhancement on github [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/projectsveltos/addon-manager/issues)
176+
3. Chat with us on the Slack in the #projectsveltos channel [![Slack](https://img.shields.io/badge/join%20slack-%23projectsveltos-brighteen)](https://join.slack.com/t/projectsveltos/shared_invite/zt-1hraownbr-W8NTs6LTimxLPB8Erj8Q6Q)
177+
4. [Contact Us](mailto:support@projectsveltos.io)

api/v1alpha1/jsonnetsource_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type JsonnetSourceSpec struct {
4747
Path string `json:"path,omitempty"`
4848

4949
// Variables contains set of variable to pass to jsonnet
50-
Variables map[string]string `json:"data,omitempty"`
50+
Variables map[string]string `json:"variables,omitempty"`
5151
}
5252

5353
// JsonnetSourceStatus defines the observed state of JsonnetSource

config/crd/bases/extension.projectsveltos.io_jsonnetsources.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ spec:
3434
spec:
3535
description: JsonnetSourceSpec defines the desired state of JsonnetSource
3636
properties:
37-
data:
38-
additionalProperties:
39-
type: string
40-
description: Variables contains set of variable to pass to jsonnet
41-
type: object
4237
kind:
4338
description: 'Kind of the resource. Supported kinds are: - flux GitRepository;OCIRepository;Bucket
4439
- ConfigMap/Secret (which will be mounted as volume)'
@@ -62,6 +57,11 @@ spec:
6257
description: Path to the jsonnet file. Defaults to 'None', which translates
6358
to the root path of the SourceRef.
6459
type: string
60+
variables:
61+
additionalProperties:
62+
type: string
63+
description: Variables contains set of variable to pass to jsonnet
64+
type: object
6565
required:
6666
- kind
6767
- name

controllers/jsonnetsource_controller.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,25 @@ func (r *JsonnetSourceReconciler) reconcileNormal(
160160
}
161161

162162
vm := jsonnet.MakeVM()
163+
164+
var subdirectories []string
165+
subdirectories, err = getAllSubdirectories(filepath.Dir(filePath))
166+
if err != nil {
167+
logger.V(logs.LogInfo).Info(fmt.Sprintf("Failed to get subdirectories in %s: %v\n",
168+
tmpDir, err))
169+
return "", err
170+
}
171+
172+
for _, subdir := range subdirectories {
173+
logger.V(logs.LogDebug).Info(fmt.Sprintf("including subdirectory: %s", subdir))
174+
}
175+
163176
vm.Importer(&jsonnet.FileImporter{
164-
JPaths: []string{tmpDir},
177+
JPaths: subdirectories,
165178
})
166179

167180
for key, value := range jsonnetSource.Spec.Variables {
181+
logger.V(logs.LogDebug).Info(fmt.Sprintf("setting variable: %s:%s", key, value))
168182
vm.ExtVar(key, value)
169183
}
170184

@@ -533,3 +547,26 @@ func processData(jsonData string) (string, error) {
533547

534548
return result, nil
535549
}
550+
551+
func getAllSubdirectories(directory string) ([]string, error) {
552+
var subdirectories []string
553+
subdirectories = append(subdirectories, directory)
554+
555+
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
556+
if err != nil {
557+
return err
558+
}
559+
560+
if info.IsDir() && path != directory {
561+
subdirectories = append(subdirectories, path)
562+
}
563+
564+
return nil
565+
})
566+
567+
if err != nil {
568+
return nil, err
569+
}
570+
571+
return subdirectories, nil
572+
}

manifest/manifest.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ spec:
4040
spec:
4141
description: JsonnetSourceSpec defines the desired state of JsonnetSource
4242
properties:
43-
data:
44-
additionalProperties:
45-
type: string
46-
description: Variables contains set of variable to pass to jsonnet
47-
type: object
4843
kind:
4944
description: 'Kind of the resource. Supported kinds are: - flux GitRepository;OCIRepository;Bucket
5045
- ConfigMap/Secret (which will be mounted as volume)'
@@ -68,6 +63,11 @@ spec:
6863
description: Path to the jsonnet file. Defaults to 'None', which translates
6964
to the root path of the SourceRef.
7065
type: string
66+
variables:
67+
additionalProperties:
68+
type: string
69+
description: Variables contains set of variable to pass to jsonnet
70+
type: object
7171
required:
7272
- kind
7373
- name

0 commit comments

Comments
 (0)