Skip to content

Commit

Permalink
Merge pull request #101 from stefanprodan/fix-diff-ns
Browse files Browse the repository at this point in the history
Fix apply dry-run for new namespaces
  • Loading branch information
stefanprodan authored May 11, 2023
2 parents f6abe95 + 290461b commit 2905324
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 79 deletions.
90 changes: 52 additions & 38 deletions cmd/timoni/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ func runApplyCmd(cmd *cobra.Command, args []string) error {
exists = true
}

nsExists, err := sm.NamespaceExists(ctx, *kubeconfigArgs.Namespace)
if err != nil {
return fmt.Errorf("instance init failed: %w", err)
}

if !applyArgs.overwriteOwnership && exists {
err = instanceOwnershipConflicts(*instance)
if err != nil {
Expand All @@ -242,39 +247,7 @@ func runApplyCmd(cmd *cobra.Command, args []string) error {
}

if applyArgs.dryrun || applyArgs.diff {
diffOpts := ssa.DefaultDiffOptions()
sort.Sort(ssa.SortableUnstructureds(objects))
for _, r := range objects {
change, liveObject, mergedObject, err := rm.Diff(ctx, r, diffOpts)
if err != nil {
logger.Println(err)
continue
}

logger.Println(change.String(), "(server dry run)")

if applyArgs.diff && change.Action == ssa.ConfiguredAction {
liveYAML, _ := yaml.Marshal(liveObject)
liveFile := filepath.Join(tmpDir, "live.yaml")
if err := os.WriteFile(liveFile, liveYAML, 0644); err != nil {
return err
}

mergedYAML, _ := yaml.Marshal(mergedObject)
mergedFile := filepath.Join(tmpDir, "merged.yaml")
if err := os.WriteFile(mergedFile, mergedYAML, 0644); err != nil {
return err
}

out, _ := exec.Command("diff", "-N", "-u", liveFile, mergedFile).Output()
for i, line := range strings.Split(string(out), "\n") {
if i > 1 && len(line) > 0 {
logger.Println(line)
}
}
}
}
return nil
return dryRun(ctx, rm, objects, nsExists, tmpDir)
}

im := runtime.NewInstanceManager(applyArgs.name, *kubeconfigArgs.Namespace, finalValues, *mod)
Expand All @@ -286,11 +259,6 @@ func runApplyCmd(cmd *cobra.Command, args []string) error {
if !exists {
logger.Printf("installing %s in namespace %s", applyArgs.name, *kubeconfigArgs.Namespace)

nsExists, err := sm.NamespaceExists(ctx, *kubeconfigArgs.Namespace)
if err != nil {
return fmt.Errorf("instance init failed: %w", err)
}

if err := sm.Apply(ctx, &im.Instance, true); err != nil {
return fmt.Errorf("instance init failed: %w", err)
}
Expand Down Expand Up @@ -370,3 +338,49 @@ func instanceOwnershipConflicts(instance apiv1.Instance) error {
}
return nil
}

func dryRun(ctx context.Context, rm *ssa.ResourceManager, objects []*unstructured.Unstructured, nsExists bool, tmpDir string) error {
diffOpts := ssa.DefaultDiffOptions()
sort.Sort(ssa.SortableUnstructureds(objects))

if !nsExists {
logger.Printf("Namespace/%s created (server dry run)", *kubeconfigArgs.Namespace)
}

for _, r := range objects {
if !nsExists {
logger.Printf("%s created (server dry run)", ssa.FmtUnstructured(r))
continue
}

change, liveObject, mergedObject, err := rm.Diff(ctx, r, diffOpts)
if err != nil {
logger.Println(err)
continue
}

logger.Println(change.String(), "(server dry run)")

if applyArgs.diff && change.Action == ssa.ConfiguredAction {
liveYAML, _ := yaml.Marshal(liveObject)
liveFile := filepath.Join(tmpDir, "live.yaml")
if err := os.WriteFile(liveFile, liveYAML, 0644); err != nil {
return err
}

mergedYAML, _ := yaml.Marshal(mergedObject)
mergedFile := filepath.Join(tmpDir, "merged.yaml")
if err := os.WriteFile(mergedFile, mergedYAML, 0644); err != nil {
return err
}

out, _ := exec.Command("diff", "-N", "-u", liveFile, mergedFile).Output()
for i, line := range strings.Split(string(out), "\n") {
if i > 1 && len(line) > 0 {
logger.Println(line)
}
}
}
}
return nil
}
49 changes: 8 additions & 41 deletions cmd/timoni/bundle_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"time"

"cuelang.org/go/cue/cuecontext"
"github.com/fluxcd/pkg/ssa"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"

apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
"github.com/stefanprodan/timoni/internal/engine"
Expand Down Expand Up @@ -245,41 +241,17 @@ func applyBundleInstance(instance engine.BundleInstance) error {
exists = true
}

if bundleApplyArgs.dryrun || bundleApplyArgs.diff {
diffOpts := ssa.DefaultDiffOptions()
sort.Sort(ssa.SortableUnstructureds(objects))
for _, r := range objects {
change, liveObject, mergedObject, err := rm.Diff(ctx, r, diffOpts)
if err != nil {
logger.Println(err)
continue
}
nsExists, err := sm.NamespaceExists(ctx, instance.Namespace)
if err != nil {
return fmt.Errorf("instance init failed: %w", err)
}

logger.Println(change.String(), "(server dry run)")

if bundleApplyArgs.diff && change.Action == ssa.ConfiguredAction {
liveYAML, _ := yaml.Marshal(liveObject)
liveFile := filepath.Join(tmpDir, "live.yaml")
if err := os.WriteFile(liveFile, liveYAML, 0644); err != nil {
return err
}

mergedYAML, _ := yaml.Marshal(mergedObject)
mergedFile := filepath.Join(tmpDir, "merged.yaml")
if err := os.WriteFile(mergedFile, mergedYAML, 0644); err != nil {
return err
}

out, _ := exec.Command("diff", "-N", "-u", liveFile, mergedFile).Output()
for i, line := range strings.Split(string(out), "\n") {
if i > 1 && len(line) > 0 {
logger.Println(line)
}
}
}
if bundleApplyArgs.dryrun || bundleApplyArgs.diff {
if err := dryRun(ctx, rm, objects, nsExists, tmpDir); err != nil {
return err
}

logger.Println("bundle applied successfully")
logger.Println("bundle applied successfully (server dry run)")
return nil
}

Expand All @@ -297,11 +269,6 @@ func applyBundleInstance(instance engine.BundleInstance) error {
if !exists {
logger.Printf("installing %s in namespace %s", instance.Name, instance.Namespace)

nsExists, err := sm.NamespaceExists(ctx, instance.Namespace)
if err != nil {
return fmt.Errorf("instance init failed: %w", err)
}

if err := sm.Apply(ctx, &im.Instance, true); err != nil {
return fmt.Errorf("instance init failed: %w", err)
}
Expand Down

0 comments on commit 2905324

Please sign in to comment.