Skip to content

Commit 43888cf

Browse files
committed
template ref crd initial
1 parent 85675cf commit 43888cf

File tree

11 files changed

+344
-9
lines changed

11 files changed

+344
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright 2023.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
)
22+
23+
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
24+
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
25+
26+
//+kubebuilder:object:root=true
27+
28+
// TemplateStore holds reference to a template that can be offered as a starting point
29+
type TemplateStore struct {
30+
metav1.TypeMeta `json:",inline"`
31+
metav1.ObjectMeta `json:"metadata,omitempty"`
32+
33+
Spec TemplateRef `json:"spec,omitempty"`
34+
}
35+
36+
//+kubebuilder:object:root=true
37+
38+
// TemplateStoreList contains a list of TemplateStore
39+
type TemplateStoreList struct {
40+
metav1.TypeMeta `json:",inline"`
41+
metav1.ListMeta `json:"metadata,omitempty"`
42+
Items []TemplateStore `json:"items"`
43+
}

cyclops-ctrl/api/v1alpha1/zz_generated.deepcopy.go

+58
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
annotations:
6+
controller-gen.kubebuilder.io/version: v0.11.3
7+
creationTimestamp: null
8+
name: templatestores.cyclops-ui.com
9+
spec:
10+
group: cyclops-ui.com
11+
names:
12+
kind: TemplateStore
13+
listKind: TemplateStoreList
14+
plural: templatestores
15+
singular: templatestore
16+
scope: Namespaced
17+
versions:
18+
- name: v1alpha1
19+
schema:
20+
openAPIV3Schema:
21+
description: TemplateStore holds reference to a template that can be offered
22+
as a starting point
23+
properties:
24+
apiVersion:
25+
description: 'APIVersion defines the versioned schema of this representation
26+
of an object. Servers should convert recognized schemas to the latest
27+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
28+
type: string
29+
kind:
30+
description: 'Kind is a string value representing the REST resource this
31+
object represents. Servers may infer this from the endpoint the client
32+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
33+
type: string
34+
metadata:
35+
type: object
36+
spec:
37+
properties:
38+
path:
39+
type: string
40+
repo:
41+
type: string
42+
version:
43+
type: string
44+
required:
45+
- path
46+
- repo
47+
- version
48+
type: object
49+
type: object
50+
served: true
51+
storage: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package k8sclient
2+
3+
import (
4+
cyclopsv1alpha1 "github.com/cyclops-ui/cycops-ctrl/api/v1alpha1"
5+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6+
)
7+
8+
func (k *KubernetesClient) ListTemplateStore() ([]cyclopsv1alpha1.TemplateStore, error) {
9+
return k.moduleset.TemplateStore(cyclopsNamespace).List(metav1.ListOptions{})
10+
}

cyclops-ctrl/internal/cluster/v1alpha1/api.go

+7
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,10 @@ func (c *CyclopsV1Alpha1Client) TemplateAuthRules(namespace string) TemplateAuth
4040
ns: namespace,
4141
}
4242
}
43+
44+
func (c *CyclopsV1Alpha1Client) TemplateStore(namespace string) TemplateStoreInterface {
45+
return &templateStoreClient{
46+
restClient: c.restClient,
47+
ns: namespace,
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package v1alpha1
2+
3+
import (
4+
"context"
5+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6+
"k8s.io/client-go/rest"
7+
8+
cyclopsv1alpha1 "github.com/cyclops-ui/cycops-ctrl/api/v1alpha1"
9+
)
10+
11+
type TemplateStoreInterface interface {
12+
List(opts metav1.ListOptions) ([]cyclopsv1alpha1.TemplateStore, error)
13+
Get(name string) (*cyclopsv1alpha1.TemplateStore, error)
14+
}
15+
16+
type templateStoreClient struct {
17+
restClient rest.Interface
18+
ns string
19+
}
20+
21+
func (c *templateStoreClient) List(opts metav1.ListOptions) ([]cyclopsv1alpha1.TemplateStore, error) {
22+
result := cyclopsv1alpha1.TemplateStoreList{}
23+
err := c.restClient.
24+
Get().
25+
Namespace(c.ns).
26+
Resource("templatestores").
27+
Do(context.Background()).
28+
Into(&result)
29+
30+
return result.Items, err
31+
}
32+
33+
func (c *templateStoreClient) Get(name string) (*cyclopsv1alpha1.TemplateStore, error) {
34+
result := cyclopsv1alpha1.TemplateStore{}
35+
err := c.restClient.
36+
Get().
37+
Namespace(c.ns).
38+
Resource("templatestores").
39+
Name(name).
40+
Do(context.Background()).
41+
Into(&result)
42+
43+
return &result, err
44+
}

cyclops-ctrl/internal/controller/templates.go

+14
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,17 @@ func (c *Templates) GetTemplateInitialValues(ctx *gin.Context) {
178178

179179
ctx.Data(http.StatusOK, gin.MIMEJSON, initial)
180180
}
181+
182+
func (c *Templates) ListTemplatesStore(ctx *gin.Context) {
183+
ctx.Header("Access-Control-Allow-Origin", "*")
184+
185+
store, err := c.kubernetesClient.ListTemplateStore()
186+
if err != nil {
187+
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching templates store", err.Error()))
188+
return
189+
}
190+
191+
storeDTO := mapper.TemplateStoreListToDTO(store)
192+
193+
ctx.JSON(http.StatusOK, storeDTO)
194+
}

cyclops-ctrl/internal/handler/handler.go

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ func (h *Handler) Start() error {
5656
h.router.GET("/templates", templatesController.GetTemplate)
5757
h.router.GET("/templates/initial", templatesController.GetTemplateInitialValues)
5858

59+
// templates store
60+
h.router.GET("/templates/store", templatesController.ListTemplatesStore)
61+
5962
// modules
6063
h.router.GET("/modules/:name", modulesController.GetModule)
6164
h.router.GET("/modules/list", modulesController.ListModules)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package mapper
2+
3+
import (
4+
cyclopsv1alpha1 "github.com/cyclops-ui/cycops-ctrl/api/v1alpha1"
5+
"github.com/cyclops-ui/cycops-ctrl/internal/models/dto"
6+
)
7+
8+
func TemplateStoreListToDTO(store []cyclopsv1alpha1.TemplateStore) []dto.TemplateStore {
9+
out := make([]dto.TemplateStore, 0, len(store))
10+
11+
for _, templateStore := range store {
12+
out = append(out, dto.TemplateStore{
13+
Name: templateStore.Name,
14+
TemplateRef: dto.Template{
15+
URL: templateStore.Spec.URL,
16+
Path: templateStore.Spec.Path,
17+
Version: templateStore.Spec.Version,
18+
},
19+
})
20+
}
21+
22+
return out
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package dto
2+
3+
type TemplateStore struct {
4+
Name string `json:"name"`
5+
TemplateRef Template `json:"ref"`
6+
}

0 commit comments

Comments
 (0)