Skip to content

Commit

Permalink
Takeover a bootstrapped cluster
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <[email protected]>
  • Loading branch information
stefanprodan committed Jun 1, 2024
1 parent 1e64479 commit c4d1580
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -

.PHONY: load-image
load-image: ## Load image into a Kind cluster.
kind load docker-image ${IMG}

.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
mkdir -p config/dev && cp config/default/* config/dev
Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
// +kubebuilder:scaffold:imports
)

const controllerName = "fluxcd-controller"
const controllerName = "flux-controller"

var (
scheme = runtime.NewScheme()
Expand Down
4 changes: 4 additions & 0 deletions internal/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func generate(base string, options Options) error {
return fmt.Errorf("generate namespace failed: %w", err)
}

if err := execTemplate(options, annotationsTmpl, path.Join(base, "annotations.yaml")); err != nil {
return fmt.Errorf("generate annotations failed: %w", err)
}

if err := execTemplate(options, labelsTmpl, path.Join(base, "labels.yaml")); err != nil {
return fmt.Errorf("generate labels failed: %w", err)
}
Expand Down
21 changes: 11 additions & 10 deletions internal/builder/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"sigs.k8s.io/yaml"
)

//nolint:goconst
func TestBuild_Defaults(t *testing.T) {
g := NewWithT(t)
const version = "v2.3.0"
Expand All @@ -36,7 +35,7 @@ func TestBuild_Defaults(t *testing.T) {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(result.Objects).NotTo(BeEmpty())

if os.Getenv("GEN_GOLDEN") == "true" {
if shouldGenGolden() {
err = cp.Copy(filepath.Join(dstDir, "kustomization.yaml"), goldenFile)
g.Expect(err).NotTo(HaveOccurred())
}
Expand All @@ -50,7 +49,6 @@ func TestBuild_Defaults(t *testing.T) {
g.Expect(string(genK)).To(Equal(string(goldenK)))
}

//nolint:goconst
func TestBuild_Patches(t *testing.T) {
g := NewWithT(t)
const version = "v2.3.0"
Expand Down Expand Up @@ -89,7 +87,7 @@ func TestBuild_Patches(t *testing.T) {
g.Expect(result.Objects).NotTo(BeEmpty())
g.Expect(result.Revision).To(HavePrefix(version + "@sha256:"))

if os.Getenv("GEN_GOLDEN") == "true" {
if shouldGenGolden() {
err = cp.Copy(filepath.Join(dstDir, "kustomization.yaml"), goldenFile)
g.Expect(err).NotTo(HaveOccurred())
}
Expand All @@ -115,7 +113,6 @@ func TestBuild_Patches(t *testing.T) {
g.Expect(found).To(BeTrue())
}

//nolint:goconst
func TestBuild_Profiles(t *testing.T) {
g := NewWithT(t)
const version = "v2.3.0"
Expand All @@ -139,7 +136,7 @@ func TestBuild_Profiles(t *testing.T) {
g.Expect(result.Objects).NotTo(BeEmpty())
g.Expect(result.Revision).To(HavePrefix(version + "@sha256:"))

if os.Getenv("GEN_GOLDEN") == "true" {
if shouldGenGolden() {
err = cp.Copy(filepath.Join(dstDir, "kustomization.yaml"), goldenFile)
g.Expect(err).NotTo(HaveOccurred())
}
Expand All @@ -154,17 +151,17 @@ func TestBuild_Profiles(t *testing.T) {

found := false
for _, obj := range result.Objects {
labels := obj.GetLabels()
if obj.GetKind() == "Namespace" {
found = true
labels := obj.GetLabels()
g.Expect(labels).NotTo(HaveKey("pod-security.kubernetes.io/warn"))
g.Expect(labels).NotTo(HaveKey("pod-security.kubernetes.io/warn-version"))
}
g.Expect(obj.GetLabels()).To(HaveKeyWithValue("app.kubernetes.io/managed-by", "flux-operator"))
}
g.Expect(found).To(BeTrue())
}

//nolint:goconst
func TestBuild_ArtifactStorage(t *testing.T) {
g := NewWithT(t)
const version = "v2.3.0"
Expand All @@ -191,7 +188,7 @@ func TestBuild_ArtifactStorage(t *testing.T) {
g.Expect(result.Objects).NotTo(BeEmpty())
g.Expect(result.Revision).To(HavePrefix(version + "@sha256:"))

if os.Getenv("GEN_GOLDEN") == "true" {
if shouldGenGolden() {
err = cp.Copy(filepath.Join(dstDir, "kustomization.yaml"), goldenFile)
g.Expect(err).NotTo(HaveOccurred())
}
Expand All @@ -210,11 +207,11 @@ func TestBuild_ArtifactStorage(t *testing.T) {
found = true
g.Expect(obj.GetName()).To(Equal("source-controller"))
}
g.Expect(obj.GetAnnotations()).To(HaveKeyWithValue("kustomize.toolkit.fluxcd.io/ssa", "Ignore"))
}
g.Expect(found).To(BeTrue())
}

//nolint:goconst
func TestBuild_InvalidPatches(t *testing.T) {
g := NewWithT(t)
const version = "v2.3.0"
Expand Down Expand Up @@ -259,3 +256,7 @@ func testTempDir(t *testing.T) (string, error) {

return tmpDir, err
}

func shouldGenGolden() bool {
return os.Getenv("GEN_GOLDEN") == "true"
}
13 changes: 13 additions & 0 deletions internal/builder/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: {{$namespace}}
transformers:
- annotations.yaml
- labels.yaml
resources:
- namespace.yaml
Expand Down Expand Up @@ -165,6 +166,18 @@ fieldSpecs:
create: true
`

var annotationsTmpl = `---
apiVersion: builtin
kind: AnnotationsTransformer
metadata:
name: annotations
annotations:
kustomize.toolkit.fluxcd.io/ssa: Ignore
fieldSpecs:
- path: metadata/annotations
create: true
`

var namespaceTmpl = `---
apiVersion: v1
kind: Namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: flux-system
transformers:
- annotations.yaml
- labels.yaml
resources:
- namespace.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: flux-system
transformers:
- annotations.yaml
- labels.yaml
resources:
- namespace.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: flux-system
transformers:
- annotations.yaml
- labels.yaml
resources:
- namespace.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: flux-system
transformers:
- annotations.yaml
- labels.yaml
resources:
- namespace.yaml
Expand Down

0 comments on commit c4d1580

Please sign in to comment.