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

Add CRD for template references #184

Merged
merged 19 commits into from
Apr 10, 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
43 changes: 43 additions & 0 deletions cyclops-ctrl/api/v1alpha1/template_store_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2023.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

//+kubebuilder:object:root=true

// TemplateStore holds reference to a template that can be offered as a starting point
type TemplateStore struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec TemplateRef `json:"spec,omitempty"`
}

//+kubebuilder:object:root=true

// TemplateStoreList contains a list of TemplateStore
type TemplateStoreList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []TemplateStore `json:"items"`
}
58 changes: 58 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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.3
creationTimestamp: null
name: templatestores.cyclops-ui.com
spec:
group: cyclops-ui.com
names:
kind: TemplateStore
listKind: TemplateStoreList
plural: templatestores
singular: templatestore
scope: Namespaced
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: TemplateStore holds reference to a template that can be offered
as a starting point
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
properties:
path:
type: string
repo:
type: string
version:
type: string
required:
- path
- repo
- version
type: object
type: object
served: true
storage: true
31 changes: 31 additions & 0 deletions cyclops-ctrl/internal/cluster/k8sclient/templatestore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package k8sclient

import (
cyclopsv1alpha1 "github.com/cyclops-ui/cycops-ctrl/api/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (k *KubernetesClient) ListTemplateStore() ([]cyclopsv1alpha1.TemplateStore, error) {
return k.moduleset.TemplateStore(cyclopsNamespace).List(metav1.ListOptions{})
}

func (k *KubernetesClient) CreateTemplateStore(ts *cyclopsv1alpha1.TemplateStore) error {
_, err := k.moduleset.TemplateStore(cyclopsNamespace).Create(ts)
return err
}

func (k *KubernetesClient) UpdateTemplateStore(ts *cyclopsv1alpha1.TemplateStore) error {
curr, err := k.moduleset.TemplateStore(cyclopsNamespace).Get(ts.Name)
if err != nil {
return err
}

ts.SetResourceVersion(curr.GetResourceVersion())

_, err = k.moduleset.TemplateStore(cyclopsNamespace).Update(ts)
return err
}

func (k *KubernetesClient) DeleteTemplateStore(name string) error {
return k.moduleset.TemplateStore(cyclopsNamespace).Delete(name)
}
7 changes: 7 additions & 0 deletions cyclops-ctrl/internal/cluster/v1alpha1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ func (c *CyclopsV1Alpha1Client) TemplateAuthRules(namespace string) TemplateAuth
ns: namespace,
}
}

func (c *CyclopsV1Alpha1Client) TemplateStore(namespace string) TemplateStoreInterface {
return &templateStoreClient{
restClient: c.restClient,
ns: namespace,
}
}
98 changes: 98 additions & 0 deletions cyclops-ctrl/internal/cluster/v1alpha1/templatestore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package v1alpha1

import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/rest"
"time"

cyclopsv1alpha1 "github.com/cyclops-ui/cycops-ctrl/api/v1alpha1"
)

type TemplateStoreInterface interface {
List(opts metav1.ListOptions) ([]cyclopsv1alpha1.TemplateStore, error)
Get(name string) (*cyclopsv1alpha1.TemplateStore, error)
Create(*cyclopsv1alpha1.TemplateStore) (*cyclopsv1alpha1.TemplateStore, error)
Update(*cyclopsv1alpha1.TemplateStore) (*cyclopsv1alpha1.TemplateStore, error)
Watch(opts metav1.ListOptions) (watch.Interface, error)
Delete(name string) error
}

type templateStoreClient struct {
restClient rest.Interface
ns string
}

func (c *templateStoreClient) List(opts metav1.ListOptions) ([]cyclopsv1alpha1.TemplateStore, error) {
result := cyclopsv1alpha1.TemplateStoreList{}
err := c.restClient.
Get().
Namespace(c.ns).
Resource("templatestores").
Do(context.Background()).
Into(&result)

return result.Items, err
}

func (c *templateStoreClient) Get(name string) (*cyclopsv1alpha1.TemplateStore, error) {
result := cyclopsv1alpha1.TemplateStore{}
err := c.restClient.
Get().
Namespace(c.ns).
Resource("templatestores").
Name(name).
Do(context.Background()).
Into(&result)

return &result, err
}

func (c *templateStoreClient) Create(project *cyclopsv1alpha1.TemplateStore) (*cyclopsv1alpha1.TemplateStore, error) {
result := cyclopsv1alpha1.TemplateStore{}
err := c.restClient.
Post().
Namespace(c.ns).
Resource("templatestores").
Body(project).
Do(context.Background()).
Into(&result)

return &result, err
}

func (c *templateStoreClient) Update(templateStore *cyclopsv1alpha1.TemplateStore) (project *cyclopsv1alpha1.TemplateStore, err error) {
result := &cyclopsv1alpha1.TemplateStore{}
err = c.restClient.Put().
Namespace(c.ns).
Resource("templatestores").
Name(templateStore.Name).
Body(templateStore).
Do(context.TODO()).
Into(result)
return
}

func (c *templateStoreClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
var timeout time.Duration
if opts.TimeoutSeconds != nil {
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second
}
opts.Watch = true
return c.restClient.Get().
Namespace(c.ns).
Resource("templatestores").
Timeout(timeout).
Watch(context.Background())
}

func (c *templateStoreClient) Delete(name string) error {
return c.restClient.
Delete().
Namespace(c.ns).
Resource("templatestores").
Name(name).
Do(context.Background()).
Error()
}
69 changes: 69 additions & 0 deletions cyclops-ctrl/internal/controller/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,72 @@ func (c *Templates) GetTemplateInitialValues(ctx *gin.Context) {

ctx.Data(http.StatusOK, gin.MIMEJSON, initial)
}

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

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

storeDTO := mapper.TemplateStoreListToDTO(store)

ctx.JSON(http.StatusOK, storeDTO)
}

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

var templateStore *dto.TemplateStore
if err := ctx.ShouldBind(&templateStore); err != nil {
fmt.Println("error binding request", templateStore)
ctx.JSON(http.StatusBadRequest, dto.NewError("Error binding request", err.Error()))
return
}

k8sTemplateStore := mapper.DTOToTemplateStore(*templateStore)

if err := c.kubernetesClient.CreateTemplateStore(k8sTemplateStore); err != nil {
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error creating module", err.Error()))
return
}

ctx.Status(http.StatusCreated)
}

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

var templateStore *dto.TemplateStore
if err := ctx.ShouldBind(&templateStore); err != nil {
fmt.Println("error binding request", templateStore)
ctx.JSON(http.StatusBadRequest, dto.NewError("Error binding request", err.Error()))
return
}

templateStore.Name = ctx.Param("name")

k8sTemplateStore := mapper.DTOToTemplateStore(*templateStore)

if err := c.kubernetesClient.UpdateTemplateStore(k8sTemplateStore); err != nil {
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error creating module", err.Error()))
return
}

ctx.Status(http.StatusCreated)
}

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

templateRefName := ctx.Param("name")

if err := c.kubernetesClient.DeleteTemplateStore(templateRefName); err != nil {
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error deleting module", err.Error()))
return
}

ctx.Status(http.StatusOK)
}
6 changes: 6 additions & 0 deletions cyclops-ctrl/internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func (h *Handler) Start() error {
h.router.GET("/templates", templatesController.GetTemplate)
h.router.GET("/templates/initial", templatesController.GetTemplateInitialValues)

// templates store
h.router.GET("/templates/store", templatesController.ListTemplatesStore)
h.router.PUT("/templates/store", templatesController.CreateTemplatesStore)
h.router.POST("/templates/store/:name", templatesController.EditTemplatesStore)
h.router.DELETE("/templates/store/:name", templatesController.DeleteTemplatesStore)

// modules
h.router.GET("/modules/:name", modulesController.GetModule)
h.router.GET("/modules/list", modulesController.ListModules)
Expand Down
Loading
Loading