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

Template resolved version #250

Merged
merged 18 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 20 additions & 4 deletions cyclops-ctrl/api/v1alpha1/client/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ModuleInterface interface {
Update(*cyclopsv1alpha1.Module) (*cyclopsv1alpha1.Module, error)
Watch(opts metav1.ListOptions) (watch.Interface, error)
Delete(name string) error
UpdateSubresource(module *cyclopsv1alpha1.Module, subresources ...string) (*cyclopsv1alpha1.Module, error)
}

type moduleClient struct {
Expand Down Expand Up @@ -64,17 +65,17 @@ func (c *moduleClient) Create(project *cyclopsv1alpha1.Module) (*cyclopsv1alpha1
return &result, err
}

// Update takes the representation of a service and updates it. Returns the server's representation of the service, and an error, if there is any.
func (c *moduleClient) Update(module *cyclopsv1alpha1.Module) (project *cyclopsv1alpha1.Module, err error) {
func (c *moduleClient) Update(module *cyclopsv1alpha1.Module) (*cyclopsv1alpha1.Module, error) {
result := &cyclopsv1alpha1.Module{}
err = c.restClient.Put().
err := c.restClient.Put().
Namespace(c.ns).
Resource("modules").
Name(module.Name).
Body(module).
Do(context.TODO()).
Into(result)
return

return result, err
}

func (c *moduleClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
Expand All @@ -99,3 +100,18 @@ func (c *moduleClient) Delete(name string) error {
Do(context.Background()).
Error()
}

func (c *moduleClient) UpdateSubresource(module *cyclopsv1alpha1.Module, subresources ...string) (*cyclopsv1alpha1.Module, error) {
result := &cyclopsv1alpha1.Module{}

err := c.restClient.Put().
Namespace(c.ns).
Resource("modules").
Name(module.Name).
SubResource(subresources...).
Body(module).
Do(context.TODO()).
Into(result)

return result, err
}
20 changes: 13 additions & 7 deletions cyclops-ctrl/api/v1alpha1/module_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ import (

// ModuleSpec defines the desired state of Module
type ModuleSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

TemplateRef TemplateRef `json:"template"`
Values apiextensionsv1.JSON `json:"values"`
}
Expand Down Expand Up @@ -59,22 +56,31 @@ const (
)

type ReconciliationStatus struct {
// +kubebuilder:validation:Optional
// +kubebuilder:validation:Enum=unknown;succeeded;failed
// +kubebuilder:default:=unknown
Status ReconciliationStatusState `json:"status"`
Reason string `json:"reason"`
Status ReconciliationStatusState `json:"status,omitempty"`
// +kubebuilder:validation:Optional
Reason string `json:"reason,omitempty"`
// +kubebuilder:validation:Optional
Errors []string `json:"errors"`
}

// ModuleStatus defines the observed state of Module
type ModuleStatus struct {
ReconciliationStatus ReconciliationStatus `json:"reconciliationStatus"`
ReconciliationStatus ReconciliationStatus `json:"reconciliationStatus"`
TemplateResolvedVersion string `json:"templateResolvedVersion"`
}

type HistoryTemplateRef struct {
URL string `json:"repo"`
Path string `json:"path"`
Version string `json:"version"`
}

type HistoryEntry struct {
Generation int64 `json:"generation"`
TemplateRef TemplateRef `json:"template"`
TemplateRef HistoryTemplateRef `json:"template"`
Values apiextensionsv1.JSON `json:"values"`
}

Expand Down
15 changes: 15 additions & 0 deletions cyclops-ctrl/api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions cyclops-ctrl/config/crd/bases/cyclops-ui.com_modules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ spec:
- succeeded
- failed
type: string
required:
- reason
- status
type: object
templateResolvedVersion:
type: string
required:
- reconciliationStatus
- templateResolvedVersion
type: object
type: object
served: true
Expand Down
8 changes: 6 additions & 2 deletions cyclops-ctrl/internal/cluster/k8sclient/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ func (k *KubernetesClient) CreateModule(module cyclopsv1alpha1.Module) error {
return err
}

func (k *KubernetesClient) UpdateModule(module cyclopsv1alpha1.Module) error {
_, err := k.moduleset.Modules(cyclopsNamespace).Update(&module)
func (k *KubernetesClient) UpdateModule(module *cyclopsv1alpha1.Module) error {
_, err := k.moduleset.Modules(cyclopsNamespace).Update(module)
return err
}

func (k *KubernetesClient) UpdateModuleStatus(module *cyclopsv1alpha1.Module) (*cyclopsv1alpha1.Module, error) {
return k.moduleset.Modules(cyclopsNamespace).UpdateSubresource(module, "status")
}

func (k *KubernetesClient) DeleteModule(name string) error {
return k.moduleset.Modules(cyclopsNamespace).Delete(name)
}
Expand Down
43 changes: 35 additions & 8 deletions cyclops-ctrl/internal/controller/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (m *Modules) GetModuleHistory(ctx *gin.Context) {
func (m *Modules) Manifest(ctx *gin.Context) {
ctx.Header("Access-Control-Allow-Origin", "*")

var request v1alpha1.ModuleSpec
var request v1alpha1.HistoryEntry
if err := ctx.BindJSON(&request); err != nil {
fmt.Println("error binding request", request)
ctx.JSON(http.StatusBadRequest, dto.NewError("Error loading template", err.Error()))
Expand All @@ -136,7 +136,16 @@ func (m *Modules) Manifest(ctx *gin.Context) {
return
}

manifest, err := m.renderer.HelmTemplate(v1alpha1.Module{Spec: request}, targetTemplate)
manifest, err := m.renderer.HelmTemplate(v1alpha1.Module{
Spec: v1alpha1.ModuleSpec{
TemplateRef: v1alpha1.TemplateRef{
URL: request.TemplateRef.URL,
Path: request.TemplateRef.Path,
Version: request.TemplateRef.Version,
},
Values: request.Values,
},
}, targetTemplate)
if err != nil {
fmt.Println(err)
ctx.Status(http.StatusInternalServerError)
Expand Down Expand Up @@ -246,7 +255,7 @@ func (m *Modules) UpdateModule(ctx *gin.Context) {
curr, err := m.kubernetesClient.GetModule(request.Name)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetcing module", err.Error()))
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching module", err.Error()))
return
}

Expand All @@ -263,9 +272,13 @@ func (m *Modules) UpdateModule(ctx *gin.Context) {
}

module.History = append([]v1alpha1.HistoryEntry{{
Generation: curr.Generation,
TemplateRef: curr.Spec.TemplateRef,
Values: curr.Spec.Values,
Generation: curr.Generation,
TemplateRef: v1alpha1.HistoryTemplateRef{
URL: curr.Spec.TemplateRef.URL,
Path: curr.Spec.TemplateRef.Path,
Version: curr.Status.TemplateResolvedVersion,
},
Values: curr.Spec.Values,
}}, history...)

if len(module.History) > 10 {
Expand All @@ -274,7 +287,16 @@ func (m *Modules) UpdateModule(ctx *gin.Context) {

module.SetResourceVersion(curr.GetResourceVersion())

err = m.kubernetesClient.UpdateModule(module)
module.Status.TemplateResolvedVersion = request.Template.ResolvedVersion
result, err := m.kubernetesClient.UpdateModuleStatus(&module)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error updating module status", err.Error()))
return
}

module.ResourceVersion = result.ResourceVersion
err = m.kubernetesClient.UpdateModule(&module)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error updating module", err.Error()))
Expand All @@ -294,10 +316,15 @@ func (m *Modules) ResourcesForModule(ctx *gin.Context) {
return
}

templateVersion := module.Status.TemplateResolvedVersion
if len(templateVersion) == 0 {
templateVersion = module.Spec.TemplateRef.Version
}

t, err := m.templatesRepo.GetTemplate(
module.Spec.TemplateRef.URL,
module.Spec.TemplateRef.Path,
module.Spec.TemplateRef.Version,
templateVersion,
)
if err != nil {
fmt.Println(err)
Expand Down
13 changes: 7 additions & 6 deletions cyclops-ctrl/internal/mapper/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func ModuleToDTO(module cyclopsv1alpha1.Module) (dto.Module, error) {
Name: module.Name,
Namespace: module.Namespace,
Version: module.Spec.TemplateRef.Version,
Template: k8sTemplateRefToDTO(module.Spec.TemplateRef),
Template: k8sTemplateRefToDTO(module.Spec.TemplateRef, module.Status.TemplateResolvedVersion),
Values: module.Spec.Values,
}, nil
}
Expand All @@ -55,7 +55,7 @@ func ModuleListToDTO(modules []cyclopsv1alpha1.Module) []dto.Module {
Name: module.Name,
Namespace: module.Namespace,
Version: module.Spec.TemplateRef.Version,
Template: k8sTemplateRefToDTO(module.Spec.TemplateRef),
Template: k8sTemplateRefToDTO(module.Spec.TemplateRef, module.Status.TemplateResolvedVersion),
Values: values,
})
}
Expand All @@ -71,11 +71,12 @@ func DtoTemplateRefToK8s(dto dto.Template) cyclopsv1alpha1.TemplateRef {
}
}

func k8sTemplateRefToDTO(templateRef cyclopsv1alpha1.TemplateRef) dto.Template {
func k8sTemplateRefToDTO(templateRef cyclopsv1alpha1.TemplateRef, templateResolvedVersion string) dto.Template {
return dto.Template{
URL: templateRef.URL,
Path: templateRef.Path,
Version: templateRef.Version,
URL: templateRef.URL,
Path: templateRef.Path,
Version: templateRef.Version,
ResolvedVersion: templateResolvedVersion,
}
}

Expand Down
45 changes: 29 additions & 16 deletions cyclops-ctrl/internal/mapper/modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,39 +57,52 @@ var _ = Describe("Module mapper test", func() {
})

Describe("k8sTemplateRefToDTO", func() {
type inCase struct {
template cyclopsv1alpha1.TemplateRef
resolvedVersion string
}

testCases := []struct {
in cyclopsv1alpha1.TemplateRef
in inCase
out dto.Template
}{
{
in: cyclopsv1alpha1.TemplateRef{
URL: "https://my.template.com",
Path: "templates",
Version: "develop",
in: inCase{
template: cyclopsv1alpha1.TemplateRef{
URL: "https://my.template.com",
Path: "templates",
Version: "develop",
},
resolvedVersion: "abc123",
},
out: dto.Template{
URL: "https://my.template.com",
Path: "templates",
Version: "develop",
URL: "https://my.template.com",
Path: "templates",
Version: "develop",
ResolvedVersion: "abc123",
},
},
{
in: cyclopsv1alpha1.TemplateRef{
URL: "oci://my.template.com",
Path: "templates",
Version: "rest-api",
in: inCase{
template: cyclopsv1alpha1.TemplateRef{
URL: "oci://my.template.com",
Path: "templates",
Version: "0.x.x",
},
resolvedVersion: "0.3.4",
},
out: dto.Template{
URL: "oci://my.template.com",
Path: "templates",
Version: "rest-api",
URL: "oci://my.template.com",
Path: "templates",
Version: "0.x.x",
ResolvedVersion: "0.3.4",
},
},
}

It("maps template refs correctly", func() {
for _, testCase := range testCases {
actual := k8sTemplateRefToDTO(testCase.in)
actual := k8sTemplateRefToDTO(testCase.in.template, testCase.in.resolvedVersion)

Expect(actual).To(BeEquivalentTo(testCase.out))
}
Expand Down
7 changes: 4 additions & 3 deletions cyclops-ctrl/internal/models/dto/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ type Module struct {
}

type Template struct {
URL string `json:"repo" binding:"required"`
Path string `json:"path" binding:"required"`
Version string `json:"version"`
URL string `json:"repo" binding:"required"`
Path string `json:"path" binding:"required"`
Version string `json:"version"`
ResolvedVersion string `json:"resolvedVersion"`
}

type TemplatesResponse struct {
Expand Down
15 changes: 8 additions & 7 deletions cyclops-ctrl/internal/models/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
)

type Template struct {
Name string `json:"name"`
Manifest string `json:"manifest"`
RootField Field `json:"root"`
Created string `json:"created"`
Edited string `json:"edited"`
Modules []dto.Module `json:"modules"`
Version string `json:"version"`
Name string `json:"name"`
Manifest string `json:"manifest"`
RootField Field `json:"root"`
Created string `json:"created"`
Edited string `json:"edited"`
Modules []dto.Module `json:"modules"`
Version string `json:"version"`
ResolvedVersion string `json:"resolvedVersion"`

Files []*chart.File `json:"files"`
Templates []*chart.File `json:"templates"`
Expand Down
Loading
Loading