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

Write module config to Git source #717

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4ebb1ee
gitops-dropdown
KaradzaJuraj Dec 13, 2024
078de67
Merge branch 'main' into gitops-checkbox
petar-cvit Dec 13, 2024
287192f
v0.1.5
petar-cvit Dec 16, 2024
56e0ae4
v0.1.6
petar-cvit Dec 16, 2024
51d3508
v0.1.7
petar-cvit Dec 16, 2024
c00ab83
git wirte backend
petar-cvit Dec 16, 2024
69e93b6
edit module advanced ection
petar-cvit Dec 16, 2024
9e501a4
enable edit
KaradzaJuraj Dec 16, 2024
0cb9cfb
move pulling of branch
KaradzaJuraj Dec 16, 2024
5c3aa0d
reolve non-fast-forward issue
petar-cvit Dec 17, 2024
f7458a2
module status struct
petar-cvit Dec 17, 2024
e497ec4
module status struct
petar-cvit Dec 17, 2024
e8c828d
⚙️ update cyclops to v0.16.0-rc.1
github-actions[bot] Dec 17, 2024
9405fc6
Fix empty module annotations (#715)
petar-cvit Dec 23, 2024
53786ca
handle empty status (#718)
petar-cvit Dec 23, 2024
9cdbbfd
⚙️ update cyclops to v0.16.0-rc.3
github-actions[bot] Dec 23, 2024
cbe9d56
empty status string fields
petar-cvit Dec 23, 2024
ac9d072
support deleting git write
petar-cvit Dec 23, 2024
e774efb
⚙️ update cyclops to v0.16.0-rc.4
github-actions[bot] Dec 23, 2024
b39ed3e
toggle Git state on creation
KaradzaJuraj Dec 23, 2024
7cad264
⚙️ update cyclops to v0.16.0-rc.5
github-actions[bot] Dec 23, 2024
055e313
delete module method (#721)
petar-cvit Jan 10, 2025
2ef5fcb
⚙️ update cyclops to v0.16.0-rc.6
github-actions[bot] Jan 10, 2025
5867318
Merge branch 'main' into gitops-checkbox
petar-cvit Jan 13, 2025
1f3a5f8
⚙️ update cyclops to v0.16.0-rc.7
github-actions[bot] Jan 13, 2025
a28d6d8
Merge branch 'main' into gitops-checkbox
petar-cvit Jan 14, 2025
f123774
add yarn lock to Dockerfile
petar-cvit Jan 14, 2025
e82e35f
⚙️ update cyclops to v0.16.0-rc.8
github-actions[bot] Jan 14, 2025
bc6bdb5
Merge branch 'main' into gitops-checkbox
petar-cvit Jan 23, 2025
951f5fa
gitops option in the shared component
petar-cvit Jan 23, 2025
4104e6a
custom commit message
petar-cvit Jan 26, 2025
c711459
Merge branch 'main' into gitops-checkbox
petar-cvit Jan 27, 2025
48a4589
link to the github config destination
petar-cvit Jan 27, 2025
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
12 changes: 8 additions & 4 deletions cyclops-ctrl/api/v1alpha1/module_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ const (
TemplateSourceTypeGit TemplateSourceType = "git"
TemplateSourceTypeHelm TemplateSourceType = "helm"
TemplateSourceTypeOCI TemplateSourceType = "oci"

GitOpsWriteRepoAnnotation = "cyclops-ui.com/write-repo"
GitOpsWritePathAnnotation = "cyclops-ui.com/write-path"
GitOpsWriteRevisionAnnotation = "cyclops-ui.com/write-revision"
)

type TemplateRef struct {
Expand Down Expand Up @@ -88,12 +92,12 @@ type GroupVersionResource struct {

// ModuleStatus defines the observed state of Module
type ModuleStatus struct {
ReconciliationStatus ReconciliationStatus `json:"reconciliationStatus"`
TemplateResolvedVersion string `json:"templateResolvedVersion"`
ReconciliationStatus *ReconciliationStatus `json:"reconciliationStatus,omitempty"`
TemplateResolvedVersion string `json:"templateResolvedVersion,omitempty"`
// +kubebuilder:validation:Optional
ManagedGVRs []GroupVersionResource `json:"managedGVRs"`
ManagedGVRs []GroupVersionResource `json:"managedGVRs,omitempty"`
// +kubebuilder:validation:Optional
IconURL string `json:"iconURL"`
IconURL string `json:"iconURL,omitempty"`
}

type HistoryTemplateRef struct {
Expand Down
6 changes: 5 additions & 1 deletion 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.

12 changes: 10 additions & 2 deletions cyclops-ctrl/cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/git"
"os"
"strconv"
"time"
Expand Down Expand Up @@ -86,8 +87,10 @@ func main() {
panic(err)
}

credsResolver := auth.NewTemplatesResolver(k8sClient)

templatesRepo := template.NewRepo(
auth.NewTemplatesResolver(k8sClient),
credsResolver,
cache.NewInMemoryTemplatesCache(),
)

Expand All @@ -101,8 +104,9 @@ func main() {
prometheus.StartCacheMetricsUpdater(&monitor, templatesRepo.ReturnCache(), 10*time.Second, setupLog)

helmReleaseClient := helm.NewReleaseClient(helmWatchNamespace)
gitWriteClient := git.NewWriteClient(credsResolver, getCommitMessageTemplate(), setupLog)

handler, err := handler.New(templatesRepo, k8sClient, helmReleaseClient, renderer, moduleTargetNamespace, telemetryClient, monitor)
handler, err := handler.New(templatesRepo, k8sClient, helmReleaseClient, renderer, gitWriteClient, moduleTargetNamespace, telemetryClient, monitor)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -192,3 +196,7 @@ func getHelmWatchNamespace() string {
}
return value
}

func getCommitMessageTemplate() string {
return os.Getenv("COMMIT_MESSAGE_TEMPLATE")
}
101 changes: 90 additions & 11 deletions cyclops-ctrl/internal/controller/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"time"

"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/git"

"sigs.k8s.io/yaml"

"github.com/gin-gonic/gin"
Expand All @@ -27,6 +29,7 @@ type Modules struct {
kubernetesClient k8sclient.IKubernetesClient
templatesRepo template.ITemplateRepo
renderer *render.Renderer
gitWriteClient *git.WriteClient

moduleTargetNamespace string

Expand All @@ -38,6 +41,7 @@ func NewModulesController(
templatesRepo template.ITemplateRepo,
kubernetes k8sclient.IKubernetesClient,
renderer *render.Renderer,
gitWriteClient *git.WriteClient,
moduleTargetNamespace string,
telemetryClient telemetry.Client,
monitor prometheus.Monitor,
Expand All @@ -46,6 +50,7 @@ func NewModulesController(
kubernetesClient: kubernetes,
templatesRepo: templatesRepo,
renderer: renderer,
gitWriteClient: gitWriteClient,
moduleTargetNamespace: moduleTargetNamespace,
telemetryClient: telemetryClient,
monitor: monitor,
Expand Down Expand Up @@ -126,6 +131,31 @@ func (m *Modules) ListModules(ctx *gin.Context) {

func (m *Modules) DeleteModule(ctx *gin.Context) {
ctx.Header("Access-Control-Allow-Origin", "*")
m.monitor.DecModule()

deleteMethod := ctx.Query("deleteMethod")

if deleteMethod == "git" {
module, err := m.kubernetesClient.GetModule(ctx.Param("name"))
if err != nil {
ctx.JSON(http.StatusBadRequest, dto.NewError("Error fetching module for deletion", err.Error()))
return
}

if module == nil {
ctx.JSON(http.StatusBadRequest, dto.NewError("Error fetching module for deletion", "Check that the module exists"))
return
}

err = m.gitWriteClient.DeleteModule(*module)
if err != nil {
ctx.JSON(http.StatusBadRequest, dto.NewError("Error deleting module from git", err.Error()))
return
}

ctx.Status(http.StatusOK)
return
}

err := m.kubernetesClient.DeleteModule(ctx.Param("name"))
if err != nil {
Expand All @@ -134,7 +164,6 @@ func (m *Modules) DeleteModule(ctx *gin.Context) {
return
}

m.monitor.DecModule()
ctx.Status(http.StatusOK)
}

Expand Down Expand Up @@ -279,6 +308,15 @@ func (m *Modules) CreateModule(ctx *gin.Context) {

m.telemetryClient.ModuleCreation()

if module.GetAnnotations() != nil && len(module.GetAnnotations()[v1alpha1.GitOpsWriteRepoAnnotation]) != 0 {
err := m.gitWriteClient.Write(module)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error pushing to git", err.Error()))
}
return
}

err = m.kubernetesClient.CreateModule(module)
if err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -314,6 +352,57 @@ func (m *Modules) UpdateModule(ctx *gin.Context) {
return
}

module.Spec.TemplateRef.SourceType = curr.Spec.TemplateRef.SourceType

module.Status.TemplateResolvedVersion = request.Template.ResolvedVersion
module.Status.ReconciliationStatus = curr.Status.ReconciliationStatus
module.Status.IconURL = curr.Status.IconURL
module.Status.ManagedGVRs = curr.Status.ManagedGVRs

module.Spec.TargetNamespace = curr.Spec.TargetNamespace
module.SetLabels(curr.GetLabels())

annotations := curr.GetAnnotations()
moduleAnnotations := module.GetAnnotations()

if annotations == nil {
annotations = make(map[string]string)
}

if moduleAnnotations != nil {
if _, ok := moduleAnnotations["cyclops-ui.com/write-repo"]; ok {
annotations["cyclops-ui.com/write-repo"] = moduleAnnotations["cyclops-ui.com/write-repo"]
}
if _, ok := moduleAnnotations["cyclops-ui.com/write-path"]; ok {
annotations["cyclops-ui.com/write-path"] = moduleAnnotations["cyclops-ui.com/write-path"]
}
if _, ok := moduleAnnotations["cyclops-ui.com/write-revision"]; ok {
annotations["cyclops-ui.com/write-revision"] = moduleAnnotations["cyclops-ui.com/write-revision"]
}
}

if len(moduleAnnotations) == 0 || len(moduleAnnotations[v1alpha1.GitOpsWriteRepoAnnotation]) == 0 {
delete(annotations, v1alpha1.GitOpsWriteRepoAnnotation)
}
if len(moduleAnnotations) == 0 || len(moduleAnnotations[v1alpha1.GitOpsWritePathAnnotation]) == 0 {
delete(annotations, v1alpha1.GitOpsWritePathAnnotation)
}
if len(moduleAnnotations) == 0 || len(moduleAnnotations[v1alpha1.GitOpsWriteRevisionAnnotation]) == 0 {
delete(annotations, v1alpha1.GitOpsWriteRevisionAnnotation)
}

delete(annotations, "kubectl.kubernetes.io/last-applied-configuration")
module.SetAnnotations(annotations)

if len(module.GetAnnotations()[v1alpha1.GitOpsWriteRepoAnnotation]) != 0 {
err := m.gitWriteClient.Write(module)
if err != nil {
fmt.Println(err)
ctx.JSON(http.StatusInternalServerError, dto.NewError("Error pushing to git", err.Error()))
}
return
}

history := curr.History
if curr.History == nil {
history = make([]v1alpha1.HistoryEntry, 0)
Expand All @@ -336,16 +425,6 @@ func (m *Modules) UpdateModule(ctx *gin.Context) {

module.SetResourceVersion(curr.GetResourceVersion())

module.Spec.TemplateRef.SourceType = curr.Spec.TemplateRef.SourceType

module.Status.TemplateResolvedVersion = request.Template.ResolvedVersion
module.Status.ReconciliationStatus = curr.Status.ReconciliationStatus
module.Status.IconURL = curr.Status.IconURL
module.Status.ManagedGVRs = curr.Status.ManagedGVRs

module.Spec.TargetNamespace = curr.Spec.TargetNamespace
module.SetLabels(curr.GetLabels())

result, err := m.kubernetesClient.UpdateModuleStatus(&module)
if err != nil {
fmt.Println(err)
Expand Down
Loading