Skip to content

Commit

Permalink
fix: Collect result objects after fixing namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock committed Aug 16, 2023
1 parent 9fab2e1 commit bfbe09c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
48 changes: 48 additions & 0 deletions e2e/gitops_approval_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package e2e

import (
kluctlv1 "github.com/kluctl/kluctl/v2/api/v1beta1"
"github.com/kluctl/kluctl/v2/e2e/test_project"
)

func (suite *GitopsTestSuite) TestGitOpsManualDeployment() {
p := test_project.NewTestProject(suite.T())
createNamespace(suite.T(), suite.k, p.TestSlug())

p.UpdateTarget("target1", nil)
addConfigMapDeployment(p, "d1", nil, resourceOpts{
name: "cm1",
namespace: p.TestSlug(),
})

key := suite.createKluctlDeployment(p, "target1", nil)

suite.Run("initial deployment", func() {
suite.waitForCommit(key, getHeadRevision(suite.T(), p))
assertConfigMapExists(suite.T(), suite.k, p.TestSlug(), "cm1")
})

suite.updateKluctlDeployment(key, func(kd *kluctlv1.KluctlDeployment) {
kd.Spec.Manual = true
})
suite.waitForReconcile(key)

addConfigMapDeployment(p, "d2", nil, resourceOpts{
name: "cm2",
namespace: p.TestSlug(),
})

suite.Run("manual deployment not triggered", func() {
suite.waitForCommit(key, getHeadRevision(suite.T(), p))
assertConfigMapNotExists(suite.T(), suite.k, p.TestSlug(), "cm2")
})

suite.updateKluctlDeployment(key, func(kd *kluctlv1.KluctlDeployment) {
kd.Spec.ManualObjectsHash = &kd.Status.LastObjectsHash
})

suite.Run("manual deployment triggered", func() {
suite.waitForCommit(key, getHeadRevision(suite.T(), p))
assertConfigMapExists(suite.T(), suite.k, p.TestSlug(), "cm2")
})
}
14 changes: 14 additions & 0 deletions pkg/deployment/deployment_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ func (c *DeploymentCollection) fixNamespaces() error {
return nil
}

func (c *DeploymentCollection) collectResultObjects() error {
for _, d := range c.Deployments {
err := d.collectResultObjects()
if err != nil {
return err
}
}
return nil
}

func (c *DeploymentCollection) buildNamespacedFromCRDs() map[schema.GroupKind]*bool {
namespacedFromCRDs := map[schema.GroupKind]*bool{}
for _, d := range c.Deployments {
Expand Down Expand Up @@ -361,6 +371,10 @@ func (c *DeploymentCollection) Prepare() error {
if err != nil {
return err
}
err = c.collectResultObjects()
if err != nil {
return err
}
return nil
}

Expand Down
8 changes: 7 additions & 1 deletion pkg/deployment/deployment_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,6 @@ func (di *DeploymentItem) buildKustomize() error {
}
o := uo.FromMap(y)
di.Objects = append(di.Objects, o)
di.Config.RenderedObjects = append(di.Config.RenderedObjects, o.GetK8sRef())
}

return nil
Expand Down Expand Up @@ -580,6 +579,13 @@ func (di *DeploymentItem) postprocessObjects(images *Images) error {
return errs.ErrorOrNil()
}

func (di *DeploymentItem) collectResultObjects() error {
for _, o := range di.Objects {
di.Config.RenderedObjects = append(di.Config.RenderedObjects, o.GetK8sRef())
}
return nil
}

func (di *DeploymentItem) writeRenderedYaml() error {
if di.dir == nil {
return nil
Expand Down
13 changes: 0 additions & 13 deletions pkg/k8s/k8s_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,19 +445,6 @@ func (k *K8sCluster) IsNamespaced(gvk schema.GroupVersionKind) *bool {
return &ret
}

func (k *K8sCluster) FixNamespaceInRef(ref k8s.ObjectRef) k8s.ObjectRef {
namespaced := k.IsNamespaced(ref.GroupVersionKind())
if namespaced == nil {
return ref
}
if !*namespaced && ref.Namespace != "" {
ref.Namespace = ""
} else if *namespaced && ref.Namespace == "" {
ref.Namespace = "default"
}
return ref
}

func (k *K8sCluster) GetSchemaForGVK(gvk schema.GroupVersionKind) (*uo.UnstructuredObject, error) {
rms, err := k.mapper.RESTMappings(gvk.GroupKind(), gvk.Version)
if err != nil {
Expand Down

0 comments on commit bfbe09c

Please sign in to comment.