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

feat: deploy modules to different namespaces #381

Closed
wants to merge 6 commits into from
Closed
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
1 change: 0 additions & 1 deletion cyclops-ctrl/api/v1alpha1/client/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func (c *moduleClient) List(opts metav1.ListOptions) ([]cyclopsv1alpha1.Module,
result := cyclopsv1alpha1.ModuleList{}
err := c.restClient.
Get().
Namespace(c.ns).
Resource("modules").
Do(context.Background()).
Into(&result)
Expand Down
1 change: 1 addition & 0 deletions cyclops-ctrl/config/samples/cyclops_v1alpha1_module.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ metadata:
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/created-by: kubebuilder
name: module-sample
namespace: desired-namespace # else it will be cyclops
spec:
# TODO(user): Add fields here
17 changes: 9 additions & 8 deletions cyclops-ctrl/internal/cluster/k8sclient/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,29 @@ func (k *KubernetesClient) ListModules() ([]cyclopsv1alpha1.Module, error) {
}

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

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

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

func (k *KubernetesClient) DeleteModule(name string) error {
return k.moduleset.Modules(cyclopsNamespace).Delete(name)
func (k *KubernetesClient) DeleteModule(name, namespace string) error {
return k.moduleset.Modules(namespace).Delete(name)
}

func (k *KubernetesClient) GetModule(name string) (*cyclopsv1alpha1.Module, error) {
return k.moduleset.Modules(cyclopsNamespace).Get(name)
// add namespace
func (k *KubernetesClient) GetModule(name, namespace string) (*cyclopsv1alpha1.Module, error) {
return k.moduleset.Modules(namespace).Get(name)
}

func (k *KubernetesClient) GetResourcesForModule(name string) ([]dto.Resource, error) {
func (k *KubernetesClient) GetResourcesForModule(name, namespace string) ([]dto.Resource, error) {
out := make([]dto.Resource, 0, 0)

apiResources, err := k.clientset.Discovery().ServerPreferredResources()
Expand Down
25 changes: 16 additions & 9 deletions cyclops-ctrl/internal/controller/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewModulesController(
func (m *Modules) GetModule(ctx *gin.Context) {
ctx.Header("Access-Control-Allow-Origin", "*")

module, err := m.kubernetesClient.GetModule(ctx.Param("name"))
module, err := m.kubernetesClient.GetModule(ctx.Param("name"), ctx.Param("namespace"))
if err != nil {
fmt.Println(err)
ctx.Status(http.StatusInternalServerError)
Expand Down Expand Up @@ -91,7 +91,7 @@ func (m *Modules) ListModules(ctx *gin.Context) {
func (m *Modules) DeleteModule(ctx *gin.Context) {
ctx.Header("Access-Control-Allow-Origin", "*")

err := m.kubernetesClient.DeleteModule(ctx.Param("name"))
err := m.kubernetesClient.DeleteModule(ctx.Param("name"), ctx.Param("namespace"))
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error deleting module", err.Error()))
Expand All @@ -105,7 +105,7 @@ func (m *Modules) DeleteModule(ctx *gin.Context) {
func (m *Modules) GetModuleHistory(ctx *gin.Context) {
ctx.Header("Access-Control-Allow-Origin", "*")

module, err := m.kubernetesClient.GetModule(ctx.Param("name"))
module, err := m.kubernetesClient.GetModule(ctx.Param("name"), ctx.Param("namespace"))
if err != nil {
fmt.Println(err)
ctx.Status(http.StatusInternalServerError)
Expand Down Expand Up @@ -161,7 +161,7 @@ func (m *Modules) Manifest(ctx *gin.Context) {
func (m *Modules) CurrentManifest(ctx *gin.Context) {
ctx.Header("Access-Control-Allow-Origin", "*")

module, err := m.kubernetesClient.GetModule(ctx.Param("name"))
module, err := m.kubernetesClient.GetModule(ctx.Param("name"), ctx.Param("namespace"))
if err != nil {
fmt.Println(err)
ctx.Status(http.StatusInternalServerError)
Expand Down Expand Up @@ -229,8 +229,15 @@ func (m *Modules) CreateModule(ctx *gin.Context) {
return
}

if request.Namespace != "" {
module.Namespace = request.Namespace
} else {
module.Namespace = "default"
}

m.telemetryClient.ModuleCreation()

println("Creating MODULE---------------")
err = m.kubernetesClient.CreateModule(module)
if err != nil {
fmt.Println(err)
Expand All @@ -252,7 +259,7 @@ func (m *Modules) UpdateModule(ctx *gin.Context) {
return
}

curr, err := m.kubernetesClient.GetModule(request.Name)
curr, err := m.kubernetesClient.GetModule(request.Name, request.Namespace)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching module", err.Error()))
Expand Down Expand Up @@ -309,7 +316,7 @@ func (m *Modules) UpdateModule(ctx *gin.Context) {
func (m *Modules) ResourcesForModule(ctx *gin.Context) {
ctx.Header("Access-Control-Allow-Origin", "*")

module, err := m.kubernetesClient.GetModule(ctx.Param("name"))
module, err := m.kubernetesClient.GetModule(ctx.Param("name"), ctx.Param("namespace"))
if err != nil {
ctx.JSON(http.StatusBadRequest, dto.NewError("Error mapping module request", err.Error()))
return
Expand All @@ -330,7 +337,7 @@ func (m *Modules) ResourcesForModule(ctx *gin.Context) {
return
}

resources, err := m.kubernetesClient.GetResourcesForModule(ctx.Param("name"))
resources, err := m.kubernetesClient.GetResourcesForModule(ctx.Param("name"), ctx.Param("namespace"))
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching module resources", err.Error()))
Expand All @@ -357,7 +364,7 @@ func (m *Modules) ResourcesForModule(ctx *gin.Context) {
func (m *Modules) Template(ctx *gin.Context) {
ctx.Header("Access-Control-Allow-Origin", "*")

module, err := m.kubernetesClient.GetModule(ctx.Param("name"))
module, err := m.kubernetesClient.GetModule(ctx.Param("name"), ctx.Param("namespace"))
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching module", err.Error()))
Expand Down Expand Up @@ -411,7 +418,7 @@ func (m *Modules) Template(ctx *gin.Context) {
func (m *Modules) HelmTemplate(ctx *gin.Context) {
ctx.Header("Access-Control-Allow-Origin", "*")

module, err := m.kubernetesClient.GetModule(ctx.Param("name"))
module, err := m.kubernetesClient.GetModule(ctx.Param("name"), ctx.Param("namespace"))
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error fetching module", err.Error()))
Expand Down
22 changes: 11 additions & 11 deletions cyclops-ctrl/internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,22 @@ func (h *Handler) Start() error {
// 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)
h.router.POST("/templates/store/:namespace/:name", templatesController.EditTemplatesStore)
h.router.DELETE("/templates/store/:namespace/:name", templatesController.DeleteTemplatesStore)

// modules
h.router.GET("/modules/:name", modulesController.GetModule)
h.router.GET("/modules/:namespace/:name", modulesController.GetModule)
h.router.GET("/modules/list", modulesController.ListModules)
h.router.DELETE("/modules/:name", modulesController.DeleteModule)
h.router.DELETE("/modules/:namespace/:name", modulesController.DeleteModule)
h.router.POST("/modules/new", modulesController.CreateModule)
h.router.POST("/modules/update", modulesController.UpdateModule)
h.router.GET("/modules/:name/history", modulesController.GetModuleHistory)
h.router.POST("/modules/:name/manifest", modulesController.Manifest)
h.router.GET("/modules/:name/currentManifest", modulesController.CurrentManifest)
h.router.GET("/modules/:name/resources", modulesController.ResourcesForModule)
h.router.DELETE("/modules/:name/resources", modulesController.DeleteModuleResource)
h.router.GET("/modules/:name/template", modulesController.Template)
h.router.GET("/modules/:name/helm-template", modulesController.HelmTemplate)
h.router.GET("/modules/:namespace/:name/history", modulesController.GetModuleHistory)
h.router.POST("/modules/:namespace/:name/manifest", modulesController.Manifest)
h.router.GET("/modules/:namespace/:name/currentManifest", modulesController.CurrentManifest)
h.router.GET("/modules/:namespace/:name/resources", modulesController.ResourcesForModule)
h.router.DELETE("/modules/:namespace/:name/resources", modulesController.DeleteModuleResource)
h.router.GET("/modules/:namespace/:name/template", modulesController.Template)
h.router.GET("/modules/:namespace/:name/helm-template", modulesController.HelmTemplate)
//h.router.POST("/modules/resources", modulesController.ModuleToResources)

h.router.GET("/resources/pods/:namespace/:name/:container/logs", modulesController.GetLogs)
Expand Down
5 changes: 4 additions & 1 deletion cyclops-ctrl/internal/mapper/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ func RequestToModule(req dto.Module) (cyclopsv1alpha1.Module, error) {
if err != nil {
return cyclopsv1alpha1.Module{}, err
}
println("Namespace : ", req.Namespace)
println("Namespace : ", req.Name)

return cyclopsv1alpha1.Module{
TypeMeta: metav1.TypeMeta{
Kind: "Module",
APIVersion: "cyclops-ui.com/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: req.Name,
Name: req.Name,
Namespace: req.Namespace,
},
Spec: cyclopsv1alpha1.ModuleSpec{
TemplateRef: DtoTemplateRefToK8s(req.Template),
Expand Down
22 changes: 15 additions & 7 deletions cyclops-ctrl/internal/modulecontroller/module_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (r *ModuleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
err := r.Get(ctx, req.NamespacedName, &module)
if apierrors.IsNotFound(err) {
r.logger.Info("delete module", "namespaced name", req.NamespacedName)
resources, err := r.kubernetesClient.GetResourcesForModule(req.Name)
resources, err := r.kubernetesClient.GetResourcesForModule(req.Name, req.Namespace)
if err != nil {
r.logger.Error(err, "error on get module resources", "namespaced name", req.NamespacedName)
return ctrl.Result{}, err
Expand All @@ -119,6 +119,11 @@ func (r *ModuleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr

r.logger.Info("upsert module", "namespaced name", req.NamespacedName)

namespace := module.Namespace
if namespace == "" {
namespace = "cyclops"
}

templateVersion := module.Status.TemplateResolvedVersion
if len(templateVersion) == 0 {
templateVersion = module.Spec.TemplateRef.Version
Expand All @@ -139,7 +144,7 @@ func (r *ModuleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return ctrl.Result{}, err
}

installErrors, err := r.moduleToResources(req.Name, template)
installErrors, err := r.moduleToResources(req.Name, req.Namespace, template)
if err != nil {
r.logger.Error(err, "error on upsert module", "namespaced name", req.NamespacedName)

Expand Down Expand Up @@ -172,21 +177,20 @@ func (r *ModuleReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

func (r *ModuleReconciler) moduleToResources(name string, template *models.Template) ([]string, error) {
module, err := r.kubernetesClient.GetModule(name)
func (r *ModuleReconciler) moduleToResources(name, namespace string, template *models.Template) ([]string, error) {
module, err := r.kubernetesClient.GetModule(name, namespace)
if err != nil {
return nil, err
}

installErrors, err := r.generateResources(r.kubernetesClient, *module, template)
installErrors, err := r.generateResources(r.kubernetesClient, *module, template, namespace)
if err != nil {
return nil, err
}

return installErrors, nil
}

func (r *ModuleReconciler) generateResources(kClient *k8sclient.KubernetesClient, module cyclopsv1alpha1.Module, moduleTemplate *models.Template) ([]string, error) {
func (r *ModuleReconciler) generateResources(kClient *k8sclient.KubernetesClient, module cyclopsv1alpha1.Module, moduleTemplate *models.Template, namespace string) ([]string, error) {
out, err := r.renderer.HelmTemplate(module, moduleTemplate)
if err != nil {
return nil, err
Expand Down Expand Up @@ -229,6 +233,10 @@ func (r *ModuleReconciler) generateResources(kClient *k8sclient.KubernetesClient
continue
}

if obj.GetNamespace() == "" {
obj.SetNamespace(namespace)
}

labels := obj.GetLabels()
if labels == nil {
labels = make(map[string]string)
Expand Down
1 change: 1 addition & 0 deletions cyclops-ui/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
build
./package-lock.json
15 changes: 11 additions & 4 deletions cyclops-ui/src/components/pages/EditModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ const layout = {

interface module {
name: string;
namespace: string;
values: any;
template: templateRef;
}

const EditModule = () => {
const [module, setModule] = useState<module>({
name: "",
namespace: "",
values: {},
template: {
repo: "",
Expand Down Expand Up @@ -137,6 +138,8 @@ const EditModule = () => {
const history = useNavigate();

let { moduleName } = useParams();
let { moduleNamespace } = useParams();
console.log("ns : ", moduleNamespace);

const mapsToArray = (fields: any[], values: any): any => {
let out: any = {};
Expand Down Expand Up @@ -207,7 +210,7 @@ const EditModule = () => {
useEffect(() => {
const fetchModuleData = async () => {
axios
.get("/api/modules/" + moduleName)
.get("/api/modules/" + moduleNamespace + "/" + moduleName)
.then(async (res) => {
editTemplateForm.setFieldsValue({
repo: res.data.template.repo,
Expand Down Expand Up @@ -238,6 +241,7 @@ const EditModule = () => {

setModule({
name: res.data.name,
namespace: res.data.namespace,
values: values,
template: res.data.template,
});
Expand Down Expand Up @@ -384,10 +388,11 @@ const EditModule = () => {
.post(`/api/modules/update`, {
values: values,
name: module.name,
namespace: module.namespace,
template: templateRef,
})
.then((res) => {
window.location.href = "/modules/" + moduleName;
window.location.href = "/modules/" + moduleNamespace + "/" + moduleName;
})
.catch((error) => {
setError(mapResponseError(error));
Expand Down Expand Up @@ -957,7 +962,9 @@ const EditModule = () => {
</Button>{" "}
<Button
htmlType="button"
onClick={() => history("/modules/" + moduleName)}
onClick={() =>
history("/modules/" + moduleNamespace + "/" + moduleName)
}
>
Back
</Button>
Expand Down
Loading
Loading