Skip to content

Commit

Permalink
refactor templates to accept different sources
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-cvit committed Jan 20, 2024
1 parent 15f1aed commit 3341d8b
Show file tree
Hide file tree
Showing 16 changed files with 358 additions and 240 deletions.
39 changes: 17 additions & 22 deletions cyclops-ctrl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,29 @@ require (
)

require (
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/Microsoft/hcsshim v0.11.4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/containerd/containerd v1.7.11 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/containerd v1.7.0 // indirect
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/docker/cli v23.0.3+incompatible // indirect
github.com/docker/cli v20.10.21+incompatible // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v23.0.8+incompatible // indirect
github.com/docker/docker v20.10.24+incompatible // indirect
github.com/docker/docker-credential-helpers v0.7.0 // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
Expand Down Expand Up @@ -102,7 +99,7 @@ require (
github.com/prometheus/procfs v0.9.0 // indirect
github.com/sergi/go-diff v1.0.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/src-d/gcfg v1.4.0 // indirect
Expand All @@ -112,27 +109,25 @@ require (
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.45.0 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.10.0 // indirect
golang.org/x/tools v0.9.1 // indirect
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
86 changes: 39 additions & 47 deletions cyclops-ctrl/go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cyclops-ctrl/internal/cluster/k8sclient/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (k *KubernetesClient) GetResourcesForModule(name string) ([]dto.Resource, e
func (k *KubernetesClient) GetDeletedResources(
resources []dto.Resource,
module cyclopsv1alpha1.Module,
template models.Template,
template *models.Template,
) ([]dto.Resource, error) {
manifest, err := template2.HelmTemplate(module, template)
if err != nil {
Expand Down
38 changes: 31 additions & 7 deletions cyclops-ctrl/internal/controller/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ func (m *Modules) Manifest(ctx *gin.Context) {
return
}

targetTemplate, err := m.templates.GetConfig(request.TemplateRef)
targetTemplate, err := template.GetTemplate(
request.TemplateRef.URL,
request.TemplateRef.Path,
request.TemplateRef.Version,
)
if err != nil {
fmt.Println(err)
ctx.Status(http.StatusInternalServerError)
Expand Down Expand Up @@ -139,7 +143,11 @@ func (m *Modules) CurrentManifest(ctx *gin.Context) {
return
}

targetTemplate, err := m.templates.GetConfig(module.Spec.TemplateRef)
targetTemplate, err := template.GetTemplate(
module.Spec.TemplateRef.URL,
module.Spec.TemplateRef.Path,
module.Spec.TemplateRef.Version,
)
if err != nil {
fmt.Println(err)
ctx.Status(http.StatusInternalServerError)
Expand Down Expand Up @@ -267,7 +275,11 @@ func (m *Modules) ResourcesForModule(ctx *gin.Context) {
return
}

template, err := m.templates.GetConfig(module.Spec.TemplateRef)
t, err := template.GetTemplate(
module.Spec.TemplateRef.URL,
module.Spec.TemplateRef.Path,
module.Spec.TemplateRef.Version,
)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching template", err.Error()))
Expand All @@ -281,7 +293,7 @@ func (m *Modules) ResourcesForModule(ctx *gin.Context) {
return
}

resources, err = m.kubernetesClient.GetDeletedResources(resources, *module, template)
resources, err = m.kubernetesClient.GetDeletedResources(resources, *module, t)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching deleted module resources", err.Error()))
Expand All @@ -301,7 +313,11 @@ func (m *Modules) Template(ctx *gin.Context) {
return
}

currentTemplate, err := m.templates.GetConfig(module.Spec.TemplateRef)
currentTemplate, err := template.GetTemplate(
module.Spec.TemplateRef.URL,
module.Spec.TemplateRef.Path,
module.Spec.TemplateRef.Version,
)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching template", err.Error()))
Expand All @@ -315,7 +331,11 @@ func (m *Modules) Template(ctx *gin.Context) {
return
}

proposedTemplate, err := m.templates.GetConfig(module.Spec.TemplateRef)
proposedTemplate, err := template.GetTemplate(
module.Spec.TemplateRef.URL,
module.Spec.TemplateRef.Path,
module.Spec.TemplateRef.Version,
)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error creating proposed template", err.Error()))
Expand Down Expand Up @@ -347,7 +367,11 @@ func (m *Modules) HelmTemplate(ctx *gin.Context) {
return
}

currentTemplate, err := m.templates.GetConfig(module.Spec.TemplateRef)
currentTemplate, err := template.GetTemplate(
module.Spec.TemplateRef.URL,
module.Spec.TemplateRef.Path,
module.Spec.TemplateRef.Version,
)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching template", err.Error()))
Expand Down
38 changes: 9 additions & 29 deletions cyclops-ctrl/internal/controller/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"time"

"github.com/gin-gonic/gin"
json "github.com/json-iterator/go"

cyclopsv1alpha1 "github.com/cyclops-ui/cycops-ctrl/api/v1alpha1"
"github.com/cyclops-ui/cycops-ctrl/internal/cluster/k8sclient"
git "github.com/cyclops-ui/cycops-ctrl/internal/git/templates"
"github.com/cyclops-ui/cycops-ctrl/internal/mapper"
"github.com/cyclops-ui/cycops-ctrl/internal/models"
"github.com/cyclops-ui/cycops-ctrl/internal/models/dto"
"github.com/cyclops-ui/cycops-ctrl/internal/storage/templates"
"github.com/cyclops-ui/cycops-ctrl/internal/template"
)

type Templates struct {
Expand Down Expand Up @@ -74,18 +73,6 @@ func (c *Templates) GetConfiguration(ctx *gin.Context) {
name := ctx.Param("name")
version := ctx.Query("version")

// TODO delete this is for testing
if name == "git" {
template, err := git.LoadTemplate("https://github.com/cyclops-ui/templates", "application", "")
if err != nil {
panic(err)
}

ctx.Header("Access-Control-Allow-Origin", "*")
ctx.JSON(http.StatusOK, template)
return
}

configuration, err := c.templates.GetConfigByVersion(name, version)
if err != nil {
fmt.Println(err)
Expand All @@ -102,7 +89,7 @@ func (c *Templates) GetConfiguration(ctx *gin.Context) {

related := make([]cyclopsv1alpha1.Module, 0)
for _, module := range modules {
if name != module.Spec.TemplateRef.Name {
if name != module.Spec.TemplateRef.Path {
continue
}

Expand Down Expand Up @@ -142,7 +129,7 @@ func (c *Templates) GetConfigurationsVersions(ctx *gin.Context) {
ctx.JSON(http.StatusOK, versions)
}

func (c *Templates) GetTemplateFromGit(ctx *gin.Context) {
func (c *Templates) GetTemplate(ctx *gin.Context) {
ctx.Header("Access-Control-Allow-Origin", "*")

repo := ctx.Query("repo")
Expand All @@ -154,17 +141,17 @@ func (c *Templates) GetTemplateFromGit(ctx *gin.Context) {
return
}

template, err := git.LoadTemplate(repo, path, commit)
t, err := template.GetTemplate(repo, path, commit)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusBadRequest, dto.NewError("Error loading template", err.Error()))
return
}

ctx.JSON(http.StatusOK, template)
ctx.JSON(http.StatusOK, t)
}

func (c *Templates) GetTemplateInitialValuesFromGit(ctx *gin.Context) {
func (c *Templates) GetTemplateInitialValues(ctx *gin.Context) {
ctx.Header("Access-Control-Allow-Origin", "*")

repo := ctx.Query("repo")
Expand All @@ -176,19 +163,12 @@ func (c *Templates) GetTemplateInitialValuesFromGit(ctx *gin.Context) {
return
}

initial, err := git.LoadInitialTemplateValues(repo, path, commit)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusBadRequest, dto.NewError("Error loading template", err.Error()))
return
}

data, err := json.Marshal(initial)
initial, err := template.GetTemplateInitialValues(repo, path, commit)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error marshaling data template", err.Error()))
ctx.JSON(http.StatusBadRequest, dto.NewError("Error loading template initial values", err.Error()))
return
}

ctx.Data(http.StatusOK, gin.MIMEJSON, data)
ctx.Data(http.StatusOK, gin.MIMEJSON, initial)
}
4 changes: 2 additions & 2 deletions cyclops-ctrl/internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func (h *Handler) Start() error {
h.router.GET("/create-config/:name", templatesController.GetConfiguration)
h.router.GET("/configuration-details", templatesController.GetConfigurationsDetails)
h.router.GET("/configuration/:name/versions", templatesController.GetConfigurationsVersions)
h.router.GET("/templates/git", templatesController.GetTemplateFromGit)
h.router.GET("/templates/git/initial", templatesController.GetTemplateInitialValuesFromGit)
h.router.GET("/templates", templatesController.GetTemplate)
h.router.GET("/templates/initial", templatesController.GetTemplateInitialValues)

// modules
h.router.GET("/modules/:name", modulesController.GetModule)
Expand Down
16 changes: 4 additions & 12 deletions cyclops-ctrl/internal/mapper/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,17 @@ func ModuleListToDTO(modules []cyclopsv1alpha1.Module) []dto.Module {

func DtoTemplateRefToK8s(dto dto.Template) cyclopsv1alpha1.TemplateRef {
return cyclopsv1alpha1.TemplateRef{
Name: dto.Name,
URL: dto.URL,
Path: dto.Path,
Version: dto.Version,
TemplateGitRef: cyclopsv1alpha1.TemplateGitRef{
Repo: dto.GitRef.Repo,
Path: dto.GitRef.Path,
Commit: dto.GitRef.Commit,
},
}
}

func k8sTemplateRefToDTO(templateRef cyclopsv1alpha1.TemplateRef) dto.Template {
return dto.Template{
Name: templateRef.Name,
URL: templateRef.URL,
Path: templateRef.Path,
Version: templateRef.Version,
GitRef: dto.TemplateGitRef{
Repo: templateRef.TemplateGitRef.Repo,
Path: templateRef.TemplateGitRef.Path,
Commit: templateRef.TemplateGitRef.Commit,
},
}
}

Expand Down
12 changes: 3 additions & 9 deletions cyclops-ctrl/internal/models/dto/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@ type Module struct {
}

type Template struct {
Name string `json:"name"`
Version string `json:"version"`
GitRef TemplateGitRef `json:"git"`
}

type TemplateGitRef struct {
Repo string `json:"repo"`
Path string `json:"path"`
Commit string `json:"commit"`
URL string `json:"repo"`
Path string `json:"path"`
Version string `json:"version"`
}

type TemplatesResponse struct {
Expand Down
12 changes: 8 additions & 4 deletions cyclops-ctrl/internal/modulecontroller/module_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/cyclops-ui/cycops-ctrl/internal/cluster/k8sclient"
"github.com/cyclops-ui/cycops-ctrl/internal/models"
"github.com/cyclops-ui/cycops-ctrl/internal/storage/templates"
template2 "github.com/cyclops-ui/cycops-ctrl/internal/template"
"github.com/cyclops-ui/cycops-ctrl/internal/template"
)

// ModuleReconciler reconciles a Module object
Expand Down Expand Up @@ -130,7 +130,11 @@ func (r *ModuleReconciler) moduleToResources(name string) error {
return err
}

template, err := r.templates.GetConfig(module.Spec.TemplateRef)
template, err := template.GetTemplate(
module.Spec.TemplateRef.URL,
module.Spec.TemplateRef.Path,
module.Spec.TemplateRef.Version,
)
if err != nil {
return err
}
Expand All @@ -142,8 +146,8 @@ func (r *ModuleReconciler) moduleToResources(name string) error {
return nil
}

func (r *ModuleReconciler) generateResources(kClient *k8sclient.KubernetesClient, module cyclopsv1alpha1.Module, moduleTemplate models.Template) error {
out, err := template2.HelmTemplate(module, moduleTemplate)
func (r *ModuleReconciler) generateResources(kClient *k8sclient.KubernetesClient, module cyclopsv1alpha1.Module, moduleTemplate *models.Template) error {
out, err := template.HelmTemplate(module, moduleTemplate)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 3341d8b

Please sign in to comment.