Skip to content

Commit

Permalink
Add support for Kustomize components
Browse files Browse the repository at this point in the history
Fix fluxcd#753

Signed-off-by: Kristian Klausen <kristian@klausen.dk>
  • Loading branch information
klausenbusk committed Nov 10, 2022
1 parent 9582513 commit e82b8d8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api/v1beta2/kustomization_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ type KustomizationSpec struct {
// +kubebuilder:validation:Enum=none;client;server
// +optional
Validation string `json:"validation,omitempty"`

// Components specifies relative paths to specifications of other Components
// via relative paths, absolute paths, or URLs.
// +optional
Components []string `json:"components,omitempty"`
}

// Decryption defines how decryption is handled for Kubernetes manifests.
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,12 @@ spec:
description: KustomizationSpec defines the configuration to calculate
the desired state from a Source using Kustomize.
properties:
components:
description: Components specifies relative paths to specifications
of other Components via relative paths, absolute paths, or URLs.
items:
type: string
type: array
decryption:
description: Decrypt Kubernetes secrets before applying them on the
cluster.
Expand Down
4 changes: 4 additions & 0 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (kg *KustomizeGenerator) WriteFile(dirPath string) (string, error) {
})
}

for _, m := range kg.kustomization.Spec.Components {
kus.Components = append(kus.Components, m)
}

for _, m := range kg.kustomization.Spec.PatchesStrategicMerge {
kus.PatchesStrategicMerge = append(kus.PatchesStrategicMerge, kustypes.PatchStrategicMerge(m.Raw))
}
Expand Down
49 changes: 49 additions & 0 deletions internal/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,52 @@ func TestGenerator_WriteFile(t *testing.T) {
})
}
}

func TestGenerator_Components(t *testing.T) {
tests := []struct {
name string
dir string
fluxComponents []string
expectedComponents []string
}{
{
name: "test kustomization.yaml with components and Flux Kustomization without components",
dir: "components",
fluxComponents: []string{},
expectedComponents: []string{"componentA"},
},
{
name: "test kustomization.yaml without components and Flux Kustomization with components",
dir: "zero-components",
fluxComponents: []string{"componentB", "componentC"},
expectedComponents: []string{"componentB", "componentC"},
},
{
name: "test kustomization.yaml with components and Flux Kustomization with components",
dir: "components",
fluxComponents: []string{"componentB", "componentC"},
expectedComponents: []string{"componentA", "componentB", "componentC"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
tmpDir := t.TempDir()
g.Expect(copy.Copy("./testdata/components", tmpDir)).To(Succeed())
ks := v1beta2.Kustomization{
Spec: v1beta2.KustomizationSpec{
Components: tt.fluxComponents,
},
}
kfile, err := NewGenerator(filepath.Join(tmpDir, tt.dir), &ks).WriteFile(filepath.Join(tmpDir, tt.dir))
g.Expect(err).ToNot(HaveOccurred())

kfileYAML, err := os.ReadFile(kfile)
g.Expect(err).ToNot(HaveOccurred())
var k kustypes.Kustomization
g.Expect(k.Unmarshal(kfileYAML)).To(Succeed())
g.Expect(k.Components).Should(Equal(tt.expectedComponents))
})
}
}

0 comments on commit e82b8d8

Please sign in to comment.