Skip to content

Commit

Permalink
Merge branch 'cyclops-ui:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Abiji-2020 authored Sep 24, 2024
2 parents f015da6 + 0a59a6e commit b44748f
Show file tree
Hide file tree
Showing 32 changed files with 751 additions and 187 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Cyclops can either be installed manually by applying the latest manifest, by usi
To install Cyclops using `kubectl` into your cluster, run the commands below:

```bash
kubectl apply -f https://raw.githubusercontent.com/cyclops-ui/cyclops/v0.11.1/install/cyclops-install.yaml && kubectl apply -f https://raw.githubusercontent.com/cyclops-ui/cyclops/v0.11.1/install/demo-templates.yaml
kubectl apply -f https://raw.githubusercontent.com/cyclops-ui/cyclops/v0.12.0/install/cyclops-install.yaml && kubectl apply -f https://raw.githubusercontent.com/cyclops-ui/cyclops/v0.12.0/install/demo-templates.yaml
```

It will create a new namespace called `cyclops` and deploy everything you need for your Cyclops instance to run.
Expand Down
6 changes: 6 additions & 0 deletions cyclops-ctrl/internal/controller/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func (m *Modules) Manifest(ctx *gin.Context) {
request.TemplateRef.URL,
request.TemplateRef.Path,
request.TemplateRef.Version,
"",
)
if err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -204,6 +205,7 @@ func (m *Modules) CurrentManifest(ctx *gin.Context) {
module.Spec.TemplateRef.URL,
module.Spec.TemplateRef.Path,
module.Spec.TemplateRef.Version,
module.Status.TemplateResolvedVersion,
)
if err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -396,6 +398,7 @@ func (m *Modules) ResourcesForModule(ctx *gin.Context) {
module.Spec.TemplateRef.URL,
module.Spec.TemplateRef.Path,
templateVersion,
module.Status.TemplateResolvedVersion,
)
if err != nil {
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching template", err.Error()))
Expand Down Expand Up @@ -440,6 +443,7 @@ func (m *Modules) Template(ctx *gin.Context) {
module.Spec.TemplateRef.URL,
module.Spec.TemplateRef.Path,
module.Spec.TemplateRef.Version,
module.Status.TemplateResolvedVersion,
)
if err != nil {
fmt.Println(err)
Expand All @@ -458,6 +462,7 @@ func (m *Modules) Template(ctx *gin.Context) {
module.Spec.TemplateRef.URL,
module.Spec.TemplateRef.Path,
module.Spec.TemplateRef.Version,
module.Status.TemplateResolvedVersion,
)
if err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -494,6 +499,7 @@ func (m *Modules) HelmTemplate(ctx *gin.Context) {
module.Spec.TemplateRef.URL,
module.Spec.TemplateRef.Path,
module.Spec.TemplateRef.Version,
module.Status.TemplateResolvedVersion,
)
if err != nil {
fmt.Println(err)
Expand Down
87 changes: 87 additions & 0 deletions cyclops-ctrl/internal/controller/sse/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,85 @@ import (
"net/http"
"time"

"github.com/pkg/errors"

"github.com/gin-gonic/gin"
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/cyclops-ui/cyclops/cyclops-ctrl/pkg/cluster/k8sclient"
)

func (s *Server) Resources(ctx *gin.Context) {
resources, err := s.k8sClient.GetWorkloadsForModule(ctx.Param("name"))
if err != nil {
ctx.String(http.StatusInternalServerError, err.Error())
return
}

watchSpecs := make([]k8sclient.ResourceWatchSpec, 0, len(resources))
for _, resource := range resources {
if !k8sclient.IsWorkload(resource.GetGroup(), resource.GetVersion(), resource.GetKind()) {
continue
}

resourceName, err := kindToResource(resource.GetKind())
if err != nil {
ctx.String(http.StatusInternalServerError, err.Error())
return
}

watchSpecs = append(watchSpecs, k8sclient.ResourceWatchSpec{
GVR: schema.GroupVersionResource{
Group: resource.GetGroup(),
Version: resource.GetVersion(),
Resource: resourceName,
},
Namespace: resource.GetNamespace(),
Name: resource.GetName(),
})
}

stopCh := make(chan struct{})

watchResource, err := s.k8sClient.WatchKubernetesResources(watchSpecs, stopCh)
if err != nil {
ctx.String(http.StatusInternalServerError, err.Error())
return
}

ctx.Stream(func(w io.Writer) bool {
for {
select {
case u, ok := <-watchResource:
if !ok {
return false
}

res, err := s.k8sClient.GetResource(
u.GroupVersionKind().Group,
u.GroupVersionKind().Version,
u.GroupVersionKind().Kind,
u.GetName(),
u.GetNamespace(),
)
if err != nil {
continue
}

ctx.SSEvent("resource-update", res)
return true
case <-ctx.Request.Context().Done():
close(stopCh)
return false
case <-ctx.Done():
close(stopCh)
return false
}
}
})
}

func (s *Server) SingleResource(ctx *gin.Context) {
type Ref struct {
Group string `json:"group" form:"group"`
Version string `json:"version" form:"version"`
Expand Down Expand Up @@ -73,3 +147,16 @@ func (s *Server) Resources(ctx *gin.Context) {
}
})
}

func kindToResource(kind string) (string, error) {
switch kind {
case "Deployment":
return "deployments", nil
case "StatefulSet":
return "statefulsets", nil
case "DaemonSet":
return "daemonsets", nil
default:
return "", errors.Errorf("kind %v is not a workload", kind)
}
}
16 changes: 13 additions & 3 deletions cyclops-ctrl/internal/controller/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (c *Templates) GetTemplate(ctx *gin.Context) {
return
}

t, err := c.templatesRepo.GetTemplate(repo, path, commit)
t, err := c.templatesRepo.GetTemplate(repo, path, commit, "")
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusBadRequest, dto.NewError("Error loading template", err.Error()))
Expand Down Expand Up @@ -131,7 +131,12 @@ func (c *Templates) CreateTemplatesStore(ctx *gin.Context) {
return
}

tmpl, err := c.templatesRepo.GetTemplate(templateStore.TemplateRef.URL, templateStore.TemplateRef.Path, templateStore.TemplateRef.Version)
tmpl, err := c.templatesRepo.GetTemplate(
templateStore.TemplateRef.URL,
templateStore.TemplateRef.Path,
templateStore.TemplateRef.Version,
"",
)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusBadRequest, dto.NewError("Error loading template", err.Error()))
Expand Down Expand Up @@ -169,7 +174,12 @@ func (c *Templates) EditTemplatesStore(ctx *gin.Context) {
return
}

tmpl, err := c.templatesRepo.GetTemplate(templateStore.TemplateRef.URL, templateStore.TemplateRef.Path, templateStore.TemplateRef.Version)
tmpl, err := c.templatesRepo.GetTemplate(
templateStore.TemplateRef.URL,
templateStore.TemplateRef.Path,
templateStore.TemplateRef.Version,
"",
)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusBadRequest, dto.NewError("Error loading template", err.Error()))
Expand Down
3 changes: 2 additions & 1 deletion cyclops-ctrl/internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func (h *Handler) Start() error {

server := sse.NewServer(h.k8sClient)

h.router.POST("/stream/resources", sse.HeadersMiddleware(), server.Resources)
h.router.GET("/stream/resources/:name", sse.HeadersMiddleware(), server.Resources)
h.router.POST("/stream/resources", sse.HeadersMiddleware(), server.SingleResource)

h.router.GET("/ping", h.pong())

Expand Down
14 changes: 12 additions & 2 deletions cyclops-ctrl/internal/mapper/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package mapper

import (
"fmt"
v1 "k8s.io/api/core/v1"
"strings"

json "github.com/json-iterator/go"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand All @@ -27,7 +29,7 @@ func RequestToModule(req dto.Module) (cyclopsv1alpha1.Module, error) {
Name: req.Name,
},
Spec: cyclopsv1alpha1.ModuleSpec{
TargetNamespace: req.Namespace,
TargetNamespace: mapTargetNamespace(req.Namespace),
TemplateRef: DtoTemplateRefToK8s(req.Template),
Values: apiextensionsv1.JSON{
Raw: data,
Expand All @@ -41,7 +43,7 @@ func ModuleToDTO(module cyclopsv1alpha1.Module) (dto.Module, error) {
return dto.Module{
Name: module.Name,
Namespace: module.Namespace,
TargetNamespace: module.Spec.TargetNamespace,
TargetNamespace: mapTargetNamespace(module.Spec.TargetNamespace),
Version: module.Spec.TemplateRef.Version,
Template: k8sTemplateRefToDTO(module.Spec.TemplateRef, module.Status.TemplateResolvedVersion),
Values: module.Spec.Values,
Expand Down Expand Up @@ -139,3 +141,11 @@ func setValuesRecursive(moduleValues map[string]interface{}, fields map[string]m

return values, nil
}

func mapTargetNamespace(targetNamespace string) string {
if len(strings.TrimSpace(targetNamespace)) == 0 {
return v1.NamespaceDefault
}

return targetNamespace
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (r *ModuleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
module.Spec.TemplateRef.URL,
module.Spec.TemplateRef.Path,
templateVersion,
module.Status.TemplateResolvedVersion,
)
if err != nil {
r.logger.Error(err, "error fetching module template", "namespaced name", req.NamespacedName)
Expand Down
13 changes: 9 additions & 4 deletions cyclops-ctrl/internal/template/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@ import (
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/template/gitproviders"
)

func (r Repo) LoadTemplate(repoURL, path, commit string) (*models.Template, error) {
func (r Repo) LoadTemplate(repoURL, path, commit, resolvedVersion string) (*models.Template, error) {
creds, err := r.credResolver.RepoAuthCredentials(repoURL)
if err != nil {
return nil, err
}

commitSHA, err := resolveRef(repoURL, commit, creds)
if err != nil {
return nil, err
commitSHA := resolvedVersion
if len(commitSHA) == 0 {
ref, err := resolveRef(repoURL, commit, creds)
if err != nil {
return nil, err
}

commitSHA = ref
}

cached, ok := r.cache.GetTemplate(repoURL, path, commitSHA)
Expand Down
6 changes: 4 additions & 2 deletions cyclops-ctrl/internal/template/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/models/helm"
)

func (r Repo) LoadHelmChart(repo, chart, version string) (*models.Template, error) {
func (r Repo) LoadHelmChart(repo, chart, version, resolvedVersion string) (*models.Template, error) {
var err error
strictVersion := version
if !isValidVersion(version) {
if len(resolvedVersion) > 0 {
strictVersion = resolvedVersion
} else if !isValidVersion(version) {
if registry.IsOCI(repo) {
strictVersion, err = getOCIStrictVersion(repo, chart, version)
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions cyclops-ctrl/internal/template/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import (
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/models"
)

func (r Repo) LoadOCIHelmChart(repo, chart, version string) (*models.Template, error) {
func (r Repo) LoadOCIHelmChart(repo, chart, version, resolvedVersion string) (*models.Template, error) {
var err error
strictVersion := version
if !isValidVersion(version) {

if len(resolvedVersion) > 0 {
strictVersion = resolvedVersion
} else if !isValidVersion(version) {
strictVersion, err = getOCIStrictVersion(repo, chart, version)
if err != nil {
return nil, err
Expand Down
10 changes: 5 additions & 5 deletions cyclops-ctrl/internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ func NewRepo(credResolver auth.TemplatesResolver, tc templateCache) *Repo {
}
}

func (r Repo) GetTemplate(repo, path, version string) (*models.Template, error) {
func (r Repo) GetTemplate(repo, path, version, resolvedVersion string) (*models.Template, error) {
// region load OCI chart
if registry.IsOCI(repo) {
return r.LoadOCIHelmChart(repo, path, version)
return r.LoadOCIHelmChart(repo, path, version, resolvedVersion)
}
// endregion

Expand All @@ -43,12 +43,12 @@ func (r Repo) GetTemplate(repo, path, version string) (*models.Template, error)
}

if isHelmRepo {
return r.LoadHelmChart(repo, path, version)
return r.LoadHelmChart(repo, path, version, resolvedVersion)
}
// endregion

// fallback to cloning from git
return r.LoadTemplate(repo, path, version)
return r.LoadTemplate(repo, path, version, resolvedVersion)
}

func (r Repo) GetTemplateInitialValues(repo, path, version string) (map[string]interface{}, error) {
Expand Down Expand Up @@ -80,7 +80,7 @@ func (r Repo) loadDependencies(metadata *helm.Metadata) ([]*models.Template, err
continue
}

dep, err := r.GetTemplate(dependency.Repository, dependency.Name, dependency.Version)
dep, err := r.GetTemplate(dependency.Repository, dependency.Name, dependency.Version, "")
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit b44748f

Please sign in to comment.