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

remove dependencies based on conditions #557

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion cyclops-ctrl/internal/models/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Template struct {
CRDs []*chart.File `json:"crds"`

Dependencies []*Template `json:"dependencies"`
Condition string `json:"condition"`
}

type Field struct {
Expand All @@ -39,7 +40,7 @@ type Field struct {
Enum []interface{} `json:"enum"`
Required []string `json:"required"`
FileExtension string `json:"fileExtension"`
Immutable bool `json:"immutable"`
Immutable bool `json:"immutable"`

// number validation
Minimum *float64 `json:"minimum"`
Expand Down
6 changes: 4 additions & 2 deletions cyclops-ctrl/internal/template/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,17 @@ func (r Repo) mapHelmChart(chartName string, files map[string][]byte) (*models.T
continue
}

if len(parts) > 2 && parts[1] == "templates" && (parts[2] != "Notes.txt" && parts[2] != "NOTES.txt") {
if len(parts) > 2 && parts[1] == "templates" &&
(parts[2] != "Notes.txt" && parts[2] != "NOTES.txt" && parts[2] != "tests") {
templateFiles = append(templateFiles, &helmchart.File{
Name: path.Join(parts[1:]...),
Data: content,
})
continue
}

if len(parts) > 2 && parts[1] == "crds" && (parts[2] != "Notes.txt" && parts[2] != "NOTES.txt") {
if len(parts) > 2 && parts[1] == "crds" &&
(parts[2] != "Notes.txt" && parts[2] != "NOTES.txt" && parts[2] != "tests") {
crdFiles = append(crdFiles, &helmchart.File{
Name: path.Join(parts[1:]...),
Data: content,
Expand Down
43 changes: 37 additions & 6 deletions cyclops-ctrl/internal/template/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"helm.sh/helm/v3/pkg/engine"

cyclopsv1alpha1 "github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/pkg/cluster/k8sclient"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/models"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/models/helm"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/pkg/cluster/k8sclient"
)

type Renderer struct {
Expand Down Expand Up @@ -40,7 +40,16 @@ func (r *Renderer) HelmTemplate(module cyclopsv1alpha1.Module, moduleTemplate *m
Templates: moduleTemplate.Templates,
}

values := make(chartutil.Values)
if err := json.Unmarshal(module.Spec.Values.Raw, &values); err != nil {
return "", err
}

for _, dependency := range moduleTemplate.Dependencies {
if !evaluateDependencyCondition(dependency.Condition, values) {
continue
}

chart.AddDependency(&helmchart.Chart{
Raw: []*helmchart.File{},
Metadata: mapMetadata(dependency.HelmChartMetadata),
Expand All @@ -52,11 +61,6 @@ func (r *Renderer) HelmTemplate(module cyclopsv1alpha1.Module, moduleTemplate *m
})
}

values := make(chartutil.Values)
if err := json.Unmarshal(module.Spec.Values.Raw, &values); err != nil {
return "", err
}

top := make(chartutil.Values)
top["Values"] = values
top["Release"] = map[string]interface{}{
Expand Down Expand Up @@ -150,6 +154,33 @@ func mapMetadata(metadata *helm.Metadata) *helmchart.Metadata {
}
}

func evaluateDependencyCondition(condition string, values map[string]interface{}) bool {
if len(condition) == 0 {
return true
}

keys := strings.Split(condition, ".")
var current interface{} = values

for _, key := range keys {
if m, ok := current.(map[string]interface{}); ok {
if val, exists := m[key]; exists {
current = val
} else {
return false
}
} else {
return false
}
}

if result, ok := current.(bool); ok {
return result
}

return false
}

type CapabilitiesKubeVersion struct {
Version string
Minor string
Expand Down
2 changes: 2 additions & 0 deletions cyclops-ctrl/internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func (r Repo) loadDependencies(metadata *helm.Metadata) ([]*models.Template, err
return nil, err
}

dep.Condition = dependency.Condition

deps = append(deps, dep)
}

Expand Down
2 changes: 1 addition & 1 deletion cyclops-ui/src/components/pages/ModuleDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ const ModuleDetails = () => {
onOk={handleCancelManifest}
onCancel={handleCancelManifest}
cancelButtonProps={{ style: { display: "none" } }}
width={"40%"}
width={"70%"}
>
<Checkbox onChange={handleCheckboxChange} checked={showManagedFields}>
Include Managed Fields
Expand Down
Loading