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 #385

Closed
wants to merge 1 commit 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 default
spec:
# TODO(user): Add fields here
16 changes: 8 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,28 @@ 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)
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
18 changes: 9 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 @@ -252,7 +252,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 +309,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 +330,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 +357,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 +411,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
19 changes: 10 additions & 9 deletions cyclops-ctrl/internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,19 @@ func (h *Handler) Start() error {
h.router.DELETE("/templates/store/:name", templatesController.DeleteTemplatesStore)

// modules
h.router.GET("/modules/:name", modulesController.GetModule)
h.router.GET("/modules/list", modulesController.ListModules)
h.router.DELETE("/modules/: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", modulesController.GetModule)
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", modulesController.DeleteModule)
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
3 changes: 2 additions & 1 deletion cyclops-ctrl/internal/mapper/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func RequestToModule(req dto.Module) (cyclopsv1alpha1.Module, error) {
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
15 changes: 9 additions & 6 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 Down Expand Up @@ -139,7 +139,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 +172,21 @@ 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 @@ -233,6 +233,9 @@ func (r *ModuleReconciler) generateResources(kClient *k8sclient.KubernetesClient
if labels == nil {
labels = make(map[string]string)
}
if obj.GetNamespace() == "" {
obj.SetNamespace(namespace)
}

labels["app.kubernetes.io/managed-by"] = "cyclops"
labels["cyclops.module"] = module.Name
Expand Down
14 changes: 10 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,7 @@ const EditModule = () => {
const history = useNavigate();

let { moduleName } = useParams();
let { moduleNamespace } = useParams();

const mapsToArray = (fields: any[], values: any): any => {
let out: any = {};
Expand Down Expand Up @@ -207,7 +209,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 +240,7 @@ const EditModule = () => {

setModule({
name: res.data.name,
namespace: res.data.namespace,
values: values,
template: res.data.template,
});
Expand Down Expand Up @@ -384,10 +387,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 +961,9 @@ const EditModule = () => {
</Button>{" "}
<Button
htmlType="button"
onClick={() => history("/modules/" + moduleName)}
onClick={() =>
history("/modules/" + moduleNamespace + "/" + moduleName)
}
>
Back
</Button>
Expand Down
59 changes: 40 additions & 19 deletions cyclops-ui/src/components/pages/History.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,30 @@ const ModuleHistory = () => {
const [historyEntries, setHistoryEntries] = useState([]);

let { moduleName } = useParams();
let { moduleNamespace } = useParams();

useEffect(() => {
axios.get(`/api/modules/` + moduleName + `/history`).then((res) => {
console.log(res.data);
setHistoryEntries(res.data);
});
axios
.get(`/api/modules/` + moduleNamespace + "/" + moduleName + `/history`)
.then((res) => {
console.log(res.data);
setHistoryEntries(res.data);
});

axios.get(`/api/modules/` + moduleName + `/currentManifest`).then((res) => {
setDiff({
curr: res.data,
previous: diff.previous,
axios
.get(
`/api/modules/` +
moduleNamespace +
"/" +
moduleName +
`/currentManifest`,
)
.then((res) => {
setDiff({
curr: res.data,
previous: diff.previous,
});
});
});
}, []);

const handleOk = () => {
Expand All @@ -64,10 +76,11 @@ const ModuleHistory = () => {
.post(`/api/modules/update`, {
values: target.values,
name: moduleName,
namespace: moduleNamespace,
template: target.template,
})
.then((res) => {
window.location.href = "/modules/" + moduleName;
window.location.href = "/modules/" + moduleNamespace + "/" + moduleName;
})
.catch((error) => {
// setLoading(false);
Expand Down Expand Up @@ -105,10 +118,13 @@ const ModuleHistory = () => {
});

axios
.post("/api/modules/" + moduleName + "/manifest", {
template: target.template,
values: target.values,
})
.post(
"/api/modules/" + moduleNamespace + "/" + moduleName + "/manifest",
{
template: target.template,
values: target.values,
},
)
.then(function (res) {
setDiff({
curr: diff.curr,
Expand All @@ -134,10 +150,13 @@ const ModuleHistory = () => {
});

axios
.post("/api/modules/" + moduleName + "/manifest", {
template: target.template,
values: target.values,
})
.post(
"/api/modules/" + moduleNamespace + "/" + moduleName + "/manifest",
{
template: target.template,
values: target.values,
},
)
.then(function (res) {
setManifest(res.data);
})
Expand Down Expand Up @@ -267,7 +286,9 @@ const ModuleHistory = () => {
<Button
style={{ float: "right" }}
htmlType="button"
onClick={() => history("/modules/" + moduleName)}
onClick={() =>
history("/modules/" + moduleNamespace + "/" + moduleName)
}
>
Back
</Button>
Expand Down
Loading