Skip to content

Commit

Permalink
yoke: breaking change: removed create-crd flag and made -create-names…
Browse files Browse the repository at this point in the history
…pace flag only relevant to target release namespace
  • Loading branch information
davidmdm committed Feb 9, 2025
1 parent 5dfb842 commit 38626a1
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 89 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tasks:

test:
cmds:
- go test -timeout 10m -p 1 -v ./...
- go test -timeout 10m -p 1 -count 1 -v ./...

update-tools:
cmds:
Expand Down
3 changes: 1 addition & 2 deletions cmd/atc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ func Handler(client *k8s.Client, cache *wasm.ModuleCache, logger *slog.Logger) h
Input: bytes.NewReader(data),
Namespace: cr.GetNamespace(),
},
CreateCRDs: airway.Spec.CreateCRDs,
DryRun: true,
DryRun: true,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: cr.GetAPIVersion(),
Expand Down
7 changes: 3 additions & 4 deletions cmd/atc/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ func TestAirTrafficController(t *testing.T) {
}`),
Namespace: "atc",
},
CreateNamespaces: true,
CreateCRDs: true,
Wait: 30 * time.Second,
Poll: time.Second,
CreateNamespace: true,
Wait: 30 * time.Second,
Poll: time.Second,
}

require.NoError(t, commander.Takeoff(ctx, atcTakeoffParams))
Expand Down
3 changes: 1 addition & 2 deletions cmd/yoke/cmd_takeoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ func GetTakeoffParams(settings GlobalSettings, source io.Reader, args []string)
flagset.BoolVar(&params.DryRun, "dry", false, "only call the kubernetes api with dry-run; takes precedence over skip-dry-run.")
flagset.BoolVar(&params.SkipDryRun, "skip-dry-run", false, "disables running dry run to resources before applying them; ineffective if dry-run is true")
flagset.BoolVar(&params.ForceConflicts, "force-conflicts", false, "force apply changes on field manager conflicts")
flagset.BoolVar(&params.CreateCRDs, "create-crds", true, "applies custom resource definitions found in flights")
flagset.BoolVar(&params.CreateNamespaces, "create-namespaces", false, "applies namespace resources found in flights")
flagset.BoolVar(&params.CreateNamespace, "create-namespace", false, "create namespace of target release if not present")
flagset.BoolVar(&params.MultiNamespaces, "multi-namespaces", false, "allows releases to create resources in other namespaces than the target namespace")

flagset.BoolVar(&params.DiffOnly, "diff-only", false, "show diff between current revision and would be applied state. Does not apply anything to cluster")
Expand Down
5 changes: 3 additions & 2 deletions cmd/yoke/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ func TestReleasesInDifferentNamespaces(t *testing.T) {
TakeOff(background, TakeoffParams{
GlobalSettings: settings,
TakeoffParams: yoke.TakeoffParams{
Release: "rel",
Release: "rel",
CreateNamespace: true,
Flight: yoke.FlightParams{
Input: createBasicDeployment(t, "release", ""),
Namespace: ns,
Expand Down Expand Up @@ -385,7 +386,7 @@ func TestTakeoffWithNamespace(t *testing.T) {
Input: createBasicDeployment(t, "sample-app", ns),
Namespace: ns,
},
CreateNamespaces: true,
CreateNamespace: true,
},
}

Expand Down
1 change: 0 additions & 1 deletion internal/atc/atc.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,6 @@ func (atc atc) FlightReconciler(params FlightReconcilerParams) ctrl.HandleFunc {
Input: bytes.NewReader(data),
Namespace: event.Namespace,
},
CreateCRDs: params.CreateCrds,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: resource.GetAPIVersion(),
Expand Down
83 changes: 6 additions & 77 deletions pkg/yoke/yoke_takeoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"path/filepath"
"reflect"
"strings"
"sync"
"time"

"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -73,11 +72,8 @@ type TakeoffParams struct {
// Output diffs with ansi colors.
Color bool

// Create namespaces found in release output.
CreateNamespaces bool

// Create CRDs foudn in release output.
CreateCRDs bool
// Create namespace of target release if not exists
CreateNamespace bool

// Wait interval for resources to become ready after being applied.
Wait time.Duration
Expand Down Expand Up @@ -200,12 +196,12 @@ func (commander Commander) Takeoff(ctx context.Context, params TakeoffParams) er
return internal.Warning("resources are the same as previous revision: skipping takeoff")
}

if namespace := params.Flight.Namespace; namespace != "" {
if err := commander.k8s.EnsureNamespace(ctx, namespace); err != nil {
if params.CreateNamespace {
if err := commander.k8s.EnsureNamespace(ctx, targetNS); err != nil {
return fmt.Errorf("failed to ensure namespace: %w", err)
}
if err := commander.k8s.WaitForReady(ctx, toUnstructuredNS(namespace), k8s.WaitOptions{Interval: params.Poll}); err != nil {
return fmt.Errorf("failed to wait for namespace %s to be ready: %w", namespace, err)
if err := commander.k8s.WaitForReady(ctx, toUnstructuredNS(targetNS), k8s.WaitOptions{Interval: params.Poll}); err != nil {
return fmt.Errorf("failed to wait for namespace %s to be ready: %w", targetNS, err)
}
}

Expand Down Expand Up @@ -263,73 +259,6 @@ func (commander Commander) Takeoff(ctx context.Context, params TakeoffParams) er
return nil
}

func (commander Commander) applyDependencies(ctx context.Context, dependencies FlightDependencies, params TakeoffParams) error {
defer internal.DebugTimer(ctx, "apply-dependencies")()

wg := sync.WaitGroup{}
errs := make([]error, 2)

applyOpts := k8s.ApplyResourcesOpts{
DryRunOnly: params.DryRun,
SkipDryRun: params.SkipDryRun,
ForceConflicts: params.ForceConflicts,
}

if params.CreateCRDs {
wg.Add(1)
go func() {
defer wg.Done()
if err := commander.k8s.ApplyResources(ctx, dependencies.CRDs, applyOpts); err != nil {
errs[0] = fmt.Errorf("failed to create CRDs: %w", err)
return
}
if err := commander.k8s.WaitForReadyMany(ctx, dependencies.CRDs, k8s.WaitOptions{}); err != nil {
errs[0] = fmt.Errorf("failed to wait for CRDs to become ready: %w", err)
return
}
}()
}

if params.CreateNamespaces {
wg.Add(1)
go func() {
defer wg.Done()
if err := commander.k8s.ApplyResources(ctx, dependencies.Namespaces, applyOpts); err != nil {
errs[1] = fmt.Errorf("failed to create namespaces: %w", err)
return
}
if err := commander.k8s.WaitForReadyMany(ctx, dependencies.Namespaces, k8s.WaitOptions{}); err != nil {
errs[1] = fmt.Errorf("failed to wait for namespaces to become ready: %w", err)
return
}
}()
}

wg.Wait()

return xerr.MultiErrOrderedFrom("", errs...)
}

type FlightDependencies struct {
Namespaces []*unstructured.Unstructured
CRDs []*unstructured.Unstructured
}

func SplitResources(resources []*unstructured.Unstructured) (deps FlightDependencies, core []*unstructured.Unstructured) {
for _, resource := range resources {
gvk := resource.GroupVersionKind()
switch {
case gvk.Kind == "Namespace" && gvk.Group == "":
deps.Namespaces = append(deps.Namespaces, resource)
case gvk.Kind == "CustomResourceDefinition" && gvk.Group == "apiextensions.k8s.io":
deps.CRDs = append(deps.CRDs, resource)
default:
core = append(core, resource)
}
}
return
}

func ExportToFS(dir, release string, resources []*unstructured.Unstructured) error {
root := filepath.Join(dir, release)

Expand Down

0 comments on commit 38626a1

Please sign in to comment.