Skip to content

Commit 0f35846

Browse files
authored
Added docs about Go types usage (#465)
Added docs covering module with go types for cozystack applications. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Documentation * Updated Cozystack API documentation with improved organization of usage methods (kubectl, Terraform, Go code) * Updated resource examples in API documentation to reflect currently available services * Added new "Go Types" documentation page explaining how to use Cozystack as a Go module for programmatic resource management, including installation, examples, and deployment approaches <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 88bfea8 + 23ecec9 commit 0f35846

2 files changed

Lines changed: 254 additions & 27 deletions

File tree

content/en/docs/v1/cozystack-api/_index.md

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,67 @@
11
---
22
title: Cozystack API
3-
description: Cozystack API
3+
description: Cozystack API for managing services and resources
44
weight: 70
55
aliases:
66
- /docs/v1/development/cozystack-api
77
---
88

99
## Cozystack API
1010

11-
Cozystack provides a powerful API that allows you to deploy services using various tools.
11+
Cozystack provides a powerful API that allows you to deploy services using various tools. You can manage resources through kubectl, Terraform, or programmatically using Go.
1212

1313
**The best way to learn the Cozystack API is to:**
1414

1515
1. Use the dashboard to deploy an application.
1616
2. Examine the deployed resource in the Cozystack API and use it as a reference.
1717
3. Parameterize and replicate the example resource to create your own resources through the API.
1818

19+
## Discovering Resources
20+
1921
You can list all available resources using `kubectl`:
2022

2123
```bash
22-
# kubectl api-resources | grep cozystack
23-
buckets apps.cozystack.io/v1alpha1 true Bucket
24-
clickhouses apps.cozystack.io/v1alpha1 true ClickHouse
25-
etcds apps.cozystack.io/v1alpha1 true Etcd
26-
ferretdb apps.cozystack.io/v1alpha1 true FerretDB
27-
httpcaches apps.cozystack.io/v1alpha1 true HTTPCache
28-
ingresses apps.cozystack.io/v1alpha1 true Ingress
29-
kafkas apps.cozystack.io/v1alpha1 true Kafka
30-
kuberneteses apps.cozystack.io/v1alpha1 true Kubernetes
31-
monitorings apps.cozystack.io/v1alpha1 true Monitoring
32-
mysqls apps.cozystack.io/v1alpha1 true MySQL
33-
natses apps.cozystack.io/v1alpha1 true NATS
34-
postgreses apps.cozystack.io/v1alpha1 true Postgres
35-
rabbitmqs apps.cozystack.io/v1alpha1 true RabbitMQ
36-
redises apps.cozystack.io/v1alpha1 true Redis
37-
seaweedfses apps.cozystack.io/v1alpha1 true SeaweedFS
38-
tcpbalancers apps.cozystack.io/v1alpha1 true TCPBalancer
39-
tenants apps.cozystack.io/v1alpha1 true Tenant
40-
virtualmachines apps.cozystack.io/v1alpha1 true VirtualMachine
41-
vmdisks apps.cozystack.io/v1alpha1 true VMDisk
42-
vminstances apps.cozystack.io/v1alpha1 true VMInstance
43-
vpns apps.cozystack.io/v1alpha1 true VPN
24+
# kubectl api-resources | grep apps.cozystack
25+
buckets apps.cozystack.io/v1alpha1 true Bucket
26+
clickhouses apps.cozystack.io/v1alpha1 true ClickHouse
27+
etcds apps.cozystack.io/v1alpha1 true Etcd
28+
foundationdbs apps.cozystack.io/v1alpha1 true FoundationDB
29+
harbors apps.cozystack.io/v1alpha1 true Harbor
30+
httpcaches apps.cozystack.io/v1alpha1 true HTTPCache
31+
infos apps.cozystack.io/v1alpha1 true Info
32+
ingresses apps.cozystack.io/v1alpha1 true Ingress
33+
kafkas apps.cozystack.io/v1alpha1 true Kafka
34+
kuberneteses apps.cozystack.io/v1alpha1 true Kubernetes
35+
mariadbs apps.cozystack.io/v1alpha1 true MariaDB
36+
mongodbs apps.cozystack.io/v1alpha1 true MongoDB
37+
monitorings apps.cozystack.io/v1alpha1 true Monitoring
38+
natses apps.cozystack.io/v1alpha1 true NATS
39+
openbaos apps.cozystack.io/v1alpha1 true OpenBAO
40+
postgreses apps.cozystack.io/v1alpha1 true Postgres
41+
qdrants apps.cozystack.io/v1alpha1 true Qdrant
42+
rabbitmqs apps.cozystack.io/v1alpha1 true RabbitMQ
43+
redises apps.cozystack.io/v1alpha1 true Redis
44+
seaweedfses apps.cozystack.io/v1alpha1 true SeaweedFS
45+
tcpbalancers apps.cozystack.io/v1alpha1 true TCPBalancer
46+
tenants apps.cozystack.io/v1alpha1 true Tenant
47+
virtualprivate apps.cozystack.io/v1alpha1 true VirtualPrivateCloud
48+
vmdisks apps.cozystack.io/v1alpha1 true VMDisk
49+
vminstances apps.cozystack.io/v1alpha1 true VMInstance
50+
vpns apps.cozystack.io/v1alpha1 true VPN
51+
4452
```
4553

46-
Next, request a specific resource type in your tenant namespace:
54+
## Using kubectl
55+
56+
Request a specific resource type in your tenant namespace:
4757

4858
```bash
4959
# kubectl get postgreses -n tenant-test
5060
NAME READY AGE VERSION
5161
test True 46s 0.7.1
5262
```
5363

54-
To view the YAML output:
64+
View the YAML output:
5565

5666
```yaml
5767
# kubectl get postgreses -n tenant-test test -o yaml
@@ -88,7 +98,7 @@ You can use this resource as an example to create a similar service via the API.
8898
kubectl apply -f postgres.yaml
8999
```
90100

91-
#### Using Terraform with the Cozystack API
101+
## Using Terraform
92102

93103
Cozystack integrates with Terraform. You can use the default `kubernetes` provider to create resources in the Cozystack API.
94104

@@ -124,3 +134,7 @@ terraform apply
124134
```
125135

126136
Your new Postgres cluster will be deployed.
137+
138+
## Using Go code
139+
140+
Cozystack publishes its custom Kubernetes resource types as a Go module, enabling management of Cozystack resources from any Go code. For details and examples, see the [Go Types]({{< relref "go-types.md" >}}) page.
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
---
2+
title: Go Types
3+
description: Programmatic management of Cozystack resources using Go types
4+
weight: 2
5+
---
6+
7+
## Go Types
8+
9+
Cozystack publishes its Kubernetes resource types as a Go module, enabling management of Cozystack resources from any Go code. The types are available at [pkg.go.dev/github.com/cozystack/cozystack/api/apps/v1alpha1](https://pkg.go.dev/github.com/cozystack/cozystack/api/apps/v1alpha1).
10+
11+
## Installation
12+
13+
Add the dependency to your Go module:
14+
15+
```bash
16+
go get github.com/cozystack/cozystack/api/apps/v1alpha1@v1.2.0
17+
```
18+
19+
## Use Cases
20+
21+
The Go types are useful for:
22+
23+
- **Building custom automation tools** - Create scripts or applications that programmatically deploy and manage Cozystack resources
24+
- **Integrating with external systems** - Connect Cozystack with your own CI/CD pipelines, monitoring systems, or orchestration tools
25+
- **Validating configurations** - Use the types to validate resource specifications before applying them to the cluster
26+
- **Generating documentation** - Parse and analyze existing Cozystack resources
27+
- **Building dashboards** - Create custom UIs for Cozystack management
28+
29+
## Available Packages
30+
31+
The module contains packages for each resource type, you can explore it for your specific version in [pkg.go.dev/github.com/cozystack/cozystack/api/apps/v1alpha1](https://pkg.go.dev/github.com/cozystack/cozystack/api/apps/v1alpha1)
32+
33+
### Simple Example
34+
35+
For basic usage, importing a specific package is straightforward:
36+
37+
```go
38+
package main
39+
40+
import (
41+
"fmt"
42+
43+
"github.com/cozystack/cozystack/api/apps/v1alpha1/vmdisk"
44+
)
45+
46+
func main() {
47+
// Create a VMDisk source from a named image
48+
image := vmdisk.SourceImage{Name: "ubuntu"}
49+
fmt.Printf("Source: %+v\n", image)
50+
}
51+
```
52+
53+
## Complex Example
54+
55+
This example demonstrates creating and marshaling several Cozystack resource types:
56+
57+
```go
58+
package main
59+
60+
import (
61+
"encoding/json"
62+
"fmt"
63+
64+
"github.com/cozystack/cozystack/api/apps/v1alpha1/postgresql"
65+
"github.com/cozystack/cozystack/api/apps/v1alpha1/vminstance"
66+
"github.com/cozystack/cozystack/api/apps/v1alpha1/redis"
67+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
68+
"k8s.io/apimachinery/pkg/api/resource"
69+
)
70+
71+
func main() {
72+
// Create a PostgreSQL config with users and databases
73+
pgConfig := postgresql.Config{
74+
TypeMeta: metav1.TypeMeta{
75+
APIVersion: "apps.cozystack.io/v1alpha1",
76+
Kind: "Postgres",
77+
},
78+
ObjectMeta: metav1.ObjectMeta{
79+
Name: "my-app-db",
80+
Namespace: "tenant-myapp",
81+
},
82+
Spec: postgresql.ConfigSpec{
83+
Replicas: 3,
84+
Size: resource.MustParse("50Gi"),
85+
Version: postgresql.Version("v18"),
86+
Users: map[string]postgresql.User{
87+
"appuser": {
88+
Password: "secretpassword",
89+
Replication: false,
90+
},
91+
"readonly": {
92+
Password: "readonlypass",
93+
},
94+
},
95+
Databases: map[string]postgresql.Database{
96+
"myapp": {
97+
Extensions: []string{"pg_trgm", "uuid-ossp"},
98+
Roles: postgresql.DatabaseRoles{
99+
Admin: []string{"appuser"},
100+
Readonly: []string{"readonly"},
101+
},
102+
},
103+
},
104+
Backup: postgresql.Backup{
105+
Enabled: true,
106+
DestinationPath: "s3://mybackups/postgres/",
107+
EndpointURL: "http://minio:9000",
108+
RetentionPolicy: "30d",
109+
S3AccessKey: "myaccesskey",
110+
S3SecretKey: "mysecretkey",
111+
Schedule: "0 2 * * * *",
112+
},
113+
Quorum: postgresql.Quorum{
114+
MinSyncReplicas: 1,
115+
MaxSyncReplicas: 1,
116+
},
117+
Postgresql: postgresql.PostgreSQL{
118+
Parameters: postgresql.PostgreSQLParameters{
119+
MaxConnections: 200,
120+
},
121+
},
122+
},
123+
}
124+
125+
// Marshal to JSON for kubectl apply
126+
pgJSON, _ := json.MarshalIndent(pgConfig, "", " ")
127+
fmt.Println(string(pgJSON))
128+
129+
// Create a Redis config
130+
redisConfig := redis.Config{
131+
TypeMeta: metav1.TypeMeta{
132+
APIVersion: "apps.cozystack.io/v1alpha1",
133+
Kind: "Redis",
134+
},
135+
ObjectMeta: metav1.ObjectMeta{
136+
Name: "cache",
137+
Namespace: "tenant-myapp",
138+
},
139+
Spec: redis.ConfigSpec{
140+
Replicas: 2,
141+
Size: resource.MustParse("5Gi"),
142+
Version: redis.Version("v8"),
143+
AuthEnabled: true,
144+
ResourcesPreset: redis.ResourcesPreset("medium"),
145+
},
146+
}
147+
148+
// Create a VMInstance with disks
149+
vmConfig := vminstance.Config{
150+
TypeMeta: metav1.TypeMeta{
151+
APIVersion: "apps.cozystack.io/v1alpha1",
152+
Kind: "VMInstance",
153+
},
154+
ObjectMeta: metav1.ObjectMeta{
155+
Name: "my-vm",
156+
Namespace: "tenant-myapp",
157+
},
158+
Spec: vminstance.ConfigSpec{
159+
InstanceType: "u1.medium",
160+
InstanceProfile: "ubuntu",
161+
RunStrategy: vminstance.RunStrategy("Always"),
162+
External: true,
163+
ExternalMethod: vminstance.ExternalMethod("PortList"),
164+
ExternalPorts: []int{22, 80, 443},
165+
Resources: vminstance.Resources{
166+
Cpu: resource.MustParse("2"),
167+
Memory: resource.MustParse("4Gi"),
168+
Sockets: resource.MustParse("1"),
169+
},
170+
Disks: []vminstance.Disk{
171+
{Bus: "sata", Name: "rootdisk"},
172+
{Bus: "sata", Name: "datadisk"},
173+
},
174+
Subnets: []vminstance.Subnet{
175+
{Name: "default"},
176+
},
177+
SshKeys: []string{
178+
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ...",
179+
},
180+
CloudInit: `#cloud-config
181+
packages:
182+
- nginx`,
183+
},
184+
}
185+
}
186+
```
187+
188+
## Deploying Resources
189+
190+
After creating your resource configurations, you can deploy them using:
191+
192+
1. **kubectl** - Marshal to YAML and apply:
193+
```go
194+
yamlData, _ := json.Marshal(yourConfig)
195+
// Use YAML marshaling library to convert to YAML
196+
```
197+
198+
2. **Direct Kubernetes client** - Use client-go:
199+
```go
200+
import (
201+
"k8s.io/client-go/kubernetes"
202+
"k8s.io/apimachinery/pkg/runtime"
203+
)
204+
205+
scheme := runtime.NewScheme()
206+
// Register your types with the scheme
207+
```
208+
209+
## Additional Resources
210+
211+
- [Go Package Documentation](https://pkg.go.dev/github.com/cozystack/cozystack/api/apps/v1alpha1)
212+
- [Cozystack GitHub Repository](https://github.com/cozystack/cozystack)
213+
- [Kubernetes API Reference](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.35/)

0 commit comments

Comments
 (0)