Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list custom resource managed resources #634

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions cyclops-ctrl/.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ WATCH_NAMESPACE=cyclops
WATCH_NAMESPACE_HELM=
CYCLOPS_VERSION=v0.0.0
MODULE_TARGET_NAMESPACE=
CONFIG_PATH=config.yaml
24 changes: 17 additions & 7 deletions cyclops-ctrl/api/v1alpha1/module_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,23 @@ const (
TemplateSourceTypeGit TemplateSourceType = "git"
TemplateSourceTypeHelm TemplateSourceType = "helm"
TemplateSourceTypeOCI TemplateSourceType = "oci"
TemplateSourceTypeCRD TemplateSourceType = "crd"

GitOpsWriteRepoAnnotation = "cyclops-ui.com/write-repo"
GitOpsWritePathAnnotation = "cyclops-ui.com/write-path"
GitOpsWriteRevisionAnnotation = "cyclops-ui.com/write-revision"
)

type TemplateRef struct {
URL string `json:"repo"`
Path string `json:"path"`
// +kubebuilder:validation:Optional
URL string `json:"repo"`
// +kubebuilder:validation:Optional
Path string `json:"path"`
// +kubebuilder:validation:Optional
Version string `json:"version"`

// +kubebuilder:validation:Enum=git;helm;oci
// +kubebuilder:validation:Optional
CRDName string `json:"CRDName"`
// +kubebuilder:validation:Enum=git;helm;oci;crd
// +kubebuilder:validation:Optional
SourceType TemplateSourceType `json:"sourceType,omitempty"`
}
Expand Down Expand Up @@ -101,11 +106,16 @@ type ModuleStatus struct {
}

type HistoryTemplateRef struct {
URL string `json:"repo"`
Path string `json:"path"`
// +kubebuilder:validation:Optional
URL string `json:"repo"`
// +kubebuilder:validation:Optional
Path string `json:"path"`
// +kubebuilder:validation:Optional
Version string `json:"version"`
// +kubebuilder:validation:Optional
CRDName string `json:"CRDName"`

// +kubebuilder:validation:Enum=git;helm;oci
// +kubebuilder:validation:Enum=git;helm;oci;crd
// +kubebuilder:validation:Optional
SourceType TemplateSourceType `json:"sourceType,omitempty"`
}
Expand Down
3 changes: 2 additions & 1 deletion cyclops-ctrl/cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func main() {
templatesRepo := template.NewRepo(
credsResolver,
cache.NewInMemoryTemplatesCache(),
k8sClient,
)

monitor, err := prometheus.NewMonitor(setupLog)
Expand Down Expand Up @@ -126,7 +127,7 @@ func main() {
}),
Cache: ctrlCache.Options{
DefaultNamespaces: map[string]ctrlCache.Config{
watchNamespace: {},
getWatchNamespace(): {},
},
},
})
Expand Down
13 changes: 13 additions & 0 deletions cyclops-ctrl/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
childLabels:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment so that people know it's an example for testing

- group: kro.run
version: v1alpha1
kind: Application
matchLabels:
kro.run/instance-name: "{{ .metadata.name }}"
managedGVKs:
- group: apps
version: v1
resource: deployments
- group: ""
version: v1
resource: services
17 changes: 6 additions & 11 deletions cyclops-ctrl/config/crd/bases/cyclops-ui.com_modules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ spec:
type: integer
template:
properties:
CRDName:
type: string
path:
type: string
repo:
Expand All @@ -43,13 +45,10 @@ spec:
- git
- helm
- oci
- crd
type: string
version:
type: string
required:
- path
- repo
- version
type: object
values:
x-kubernetes-preserve-unknown-fields: true
Expand All @@ -76,6 +75,8 @@ spec:
type: string
template:
properties:
CRDName:
type: string
path:
type: string
repo:
Expand All @@ -85,13 +86,10 @@ spec:
- git
- helm
- oci
- crd
type: string
version:
type: string
required:
- path
- repo
- version
type: object
values:
x-kubernetes-preserve-unknown-fields: true
Expand Down Expand Up @@ -137,9 +135,6 @@ spec:
type: object
templateResolvedVersion:
type: string
required:
- reconciliationStatus
- templateResolvedVersion
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ spec:
type: object
spec:
properties:
CRDName:
type: string
path:
type: string
repo:
Expand All @@ -48,13 +50,10 @@ spec:
- git
- helm
- oci
- crd
type: string
version:
type: string
required:
- path
- repo
- version
type: object
type: object
served: true
Expand Down
18 changes: 18 additions & 0 deletions cyclops-ctrl/internal/controller/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,21 @@ func (c *Cluster) ListNamespaces(ctx *gin.Context) {

ctx.JSON(http.StatusOK, namespaces)
}

func (c *Cluster) ListCRDs(ctx *gin.Context) {
ctx.Header("Access-Control-Allow-Origin", "*")

crds, err := c.kubernetesClient.ListCRDs()
if err != nil {
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching namespaces", err.Error()))
return
}

crdNames := make([]string, 0)

for _, crd := range crds {
crdNames = append(crdNames, crd.Name)
}

ctx.JSON(http.StatusOK, crdNames)
}
20 changes: 14 additions & 6 deletions cyclops-ctrl/internal/controller/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func (m *Modules) Manifest(ctx *gin.Context) {
request.TemplateRef.Path,
request.TemplateRef.Version,
"",
request.TemplateRef.CRDName,
request.TemplateRef.SourceType,
)
if err != nil {
Expand All @@ -203,7 +204,7 @@ func (m *Modules) Manifest(ctx *gin.Context) {
return
}

manifest, err := m.renderer.HelmTemplate(v1alpha1.Module{
manifest, err := m.renderer.RenderManifest(v1alpha1.Module{
ObjectMeta: metav1.ObjectMeta{
Name: ctx.Param("name"),
},
Expand All @@ -212,6 +213,7 @@ func (m *Modules) Manifest(ctx *gin.Context) {
URL: request.TemplateRef.URL,
Path: request.TemplateRef.Path,
Version: request.TemplateRef.Version,
CRDName: request.TemplateRef.CRDName,
SourceType: request.TemplateRef.SourceType,
},
Values: request.Values,
Expand Down Expand Up @@ -244,6 +246,7 @@ func (m *Modules) CurrentManifest(ctx *gin.Context) {
module.Spec.TemplateRef.Path,
module.Spec.TemplateRef.Version,
module.Status.TemplateResolvedVersion,
module.Spec.TemplateRef.CRDName,
module.Spec.TemplateRef.SourceType,
)
if err != nil {
Expand All @@ -252,7 +255,7 @@ func (m *Modules) CurrentManifest(ctx *gin.Context) {
return
}

manifest, err := m.renderer.HelmTemplate(*module, targetTemplate)
manifest, err := m.renderer.RenderManifest(*module, targetTemplate)
if err != nil {
fmt.Println(err)
ctx.Status(http.StatusInternalServerError)
Expand Down Expand Up @@ -414,6 +417,7 @@ func (m *Modules) UpdateModule(ctx *gin.Context) {
URL: curr.Spec.TemplateRef.URL,
Path: curr.Spec.TemplateRef.Path,
Version: curr.Status.TemplateResolvedVersion,
CRDName: curr.Spec.TemplateRef.CRDName,
SourceType: curr.Spec.TemplateRef.SourceType,
},
Values: curr.Spec.Values,
Expand Down Expand Up @@ -495,6 +499,7 @@ func (m *Modules) ResourcesForModule(ctx *gin.Context) {
module.Spec.TemplateRef.Path,
templateVersion,
module.Status.TemplateResolvedVersion,
module.Spec.TemplateRef.CRDName,
module.Spec.TemplateRef.SourceType,
)
if err != nil {
Expand All @@ -509,7 +514,7 @@ func (m *Modules) ResourcesForModule(ctx *gin.Context) {
return
}

manifest, err := m.renderer.HelmTemplate(*module, t)
manifest, err := m.renderer.RenderManifest(*module, t)
if err != nil {
fmt.Println("error rendering manifest", err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error rendering Module manifest", err.Error()))
Expand Down Expand Up @@ -541,6 +546,7 @@ func (m *Modules) Template(ctx *gin.Context) {
module.Spec.TemplateRef.Path,
module.Spec.TemplateRef.Version,
module.Status.TemplateResolvedVersion,
module.Spec.TemplateRef.CRDName,
module.Spec.TemplateRef.SourceType,
)
if err != nil {
Expand All @@ -549,7 +555,7 @@ func (m *Modules) Template(ctx *gin.Context) {
return
}

currentManifest, err := m.renderer.HelmTemplate(*module, currentTemplate)
currentManifest, err := m.renderer.RenderManifest(*module, currentTemplate)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error templating current", err.Error()))
Expand All @@ -561,6 +567,7 @@ func (m *Modules) Template(ctx *gin.Context) {
module.Spec.TemplateRef.Path,
module.Spec.TemplateRef.Version,
module.Status.TemplateResolvedVersion,
module.Spec.TemplateRef.CRDName,
module.Spec.TemplateRef.SourceType,
)
if err != nil {
Expand All @@ -569,7 +576,7 @@ func (m *Modules) Template(ctx *gin.Context) {
return
}

proposedManifest, err := m.renderer.HelmTemplate(*module, proposedTemplate)
proposedManifest, err := m.renderer.RenderManifest(*module, proposedTemplate)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error templating proposed", err.Error()))
Expand Down Expand Up @@ -599,6 +606,7 @@ func (m *Modules) HelmTemplate(ctx *gin.Context) {
module.Spec.TemplateRef.Path,
module.Spec.TemplateRef.Version,
module.Status.TemplateResolvedVersion,
module.Spec.TemplateRef.CRDName,
module.Spec.TemplateRef.SourceType,
)
if err != nil {
Expand All @@ -607,7 +615,7 @@ func (m *Modules) HelmTemplate(ctx *gin.Context) {
return
}

_, err = m.renderer.HelmTemplate(*module, currentTemplate)
_, err = m.renderer.RenderManifest(*module, currentTemplate)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error templating", err.Error()))
Expand Down
16 changes: 16 additions & 0 deletions cyclops-ctrl/internal/controller/sse/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ func (s *Server) ReleaseResources(ctx *gin.Context) {
s.streamResources(ctx, resources)
}

func (s *Server) CRDResources(ctx *gin.Context) {
r, err := s.k8sClient.GetResource(ctx.Query("group"), ctx.Query("version"), ctx.Query("kind"), ctx.Query("name"), ctx.Query("namespace"))
if err != nil {
ctx.String(http.StatusInternalServerError, err.Error())
return
}

other, ok := r.(*dto.Other)
if !ok {
ctx.String(http.StatusInternalServerError, "failed to fetch resource")
return
}

s.streamResources(ctx, other.Children)
}

func (s *Server) streamResources(ctx *gin.Context, resources []dto.Resource) {
watchSpecs := make([]k8sclient.ResourceWatchSpec, 0, len(resources))
for _, resource := range resources {
Expand Down
Loading