|
| 1 | +/* |
| 2 | +SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and component-operator-runtime contributors |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +*/ |
| 5 | + |
| 6 | +package cmd |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/spf13/cobra" |
| 14 | + |
| 15 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 16 | + utilerrors "k8s.io/apimachinery/pkg/util/errors" |
| 17 | + |
| 18 | + "github.com/sap/component-operator-runtime/clm/internal/backoff" |
| 19 | + "github.com/sap/component-operator-runtime/clm/internal/manifests" |
| 20 | + "github.com/sap/component-operator-runtime/clm/internal/release" |
| 21 | + "github.com/sap/component-operator-runtime/pkg/component" |
| 22 | + "github.com/sap/component-operator-runtime/pkg/reconciler" |
| 23 | + "github.com/sap/go-generics/slices" |
| 24 | +) |
| 25 | + |
| 26 | +const applyUsage = `Apply component manifests to Kubernetes cluster` |
| 27 | + |
| 28 | +type applyOptions struct { |
| 29 | + valuesSources []string |
| 30 | + timeout time.Duration |
| 31 | +} |
| 32 | + |
| 33 | +func newApplyCmd() *cobra.Command { |
| 34 | + options := &applyOptions{} |
| 35 | + |
| 36 | + cmd := &cobra.Command{ |
| 37 | + Use: "apply NAME SOURCE...", |
| 38 | + Short: "Apply component", |
| 39 | + Long: applyUsage, |
| 40 | + SilenceUsage: true, |
| 41 | + Args: cobra.MinimumNArgs(2), |
| 42 | + PreRunE: func(c *cobra.Command, args []string) error { |
| 43 | + return nil |
| 44 | + }, |
| 45 | + RunE: func(c *cobra.Command, args []string) (err error) { |
| 46 | + name := args[0] |
| 47 | + manifestSources := args[1:] |
| 48 | + namespace := c.Flag("namespace").Value.String() |
| 49 | + |
| 50 | + clnt, err := getClient(c.Flag("kubeconfig").Value.String()) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + reconciler := reconciler.NewReconciler(fullName, clnt, reconciler.ReconcilerOptions{ |
| 56 | + UpdatePolicy: ref(reconciler.UpdatePolicySsaOverride), |
| 57 | + }) |
| 58 | + |
| 59 | + releaseClient := release.NewClient(fullName, clnt) |
| 60 | + |
| 61 | + ownerId := fullName + "/" + namespace + "/" + name |
| 62 | + |
| 63 | + objects, err := manifests.Generate(manifestSources, options.valuesSources, fullName, clnt, namespace, name) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + release, err := releaseClient.Get(context.TODO(), namespace, name) |
| 69 | + if err != nil { |
| 70 | + if apierrors.IsNotFound(err) { |
| 71 | + release, err = releaseClient.Create(context.TODO(), namespace, name) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + } else { |
| 76 | + return err |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if release.IsDeleting() { |
| 81 | + return fmt.Errorf("release %s/%s is being deleted; updates are not allowed in this state", release.GetNamespace(), release.GetName()) |
| 82 | + } |
| 83 | + |
| 84 | + release.Revision += 1 |
| 85 | + |
| 86 | + backoff := backoff.New() |
| 87 | + |
| 88 | + var timeout <-chan time.Time |
| 89 | + if options.timeout > 0 { |
| 90 | + timeout = time.After(options.timeout) |
| 91 | + } |
| 92 | + |
| 93 | + defer func() { |
| 94 | + if err != nil { |
| 95 | + release.State = component.StateError |
| 96 | + } |
| 97 | + if updateErr := releaseClient.Update(context.TODO(), release); updateErr != nil { |
| 98 | + err = utilerrors.NewAggregate([]error{err, updateErr}) |
| 99 | + } |
| 100 | + }() |
| 101 | + |
| 102 | + for { |
| 103 | + release.State = component.StateProcessing |
| 104 | + ok, err := reconciler.Apply(context.TODO(), &release.Inventory, objects, namespace, ownerId, release.Revision) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + if ok { |
| 109 | + release.State = component.StateReady |
| 110 | + break |
| 111 | + } |
| 112 | + if err := releaseClient.Update(context.TODO(), release); err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + select { |
| 116 | + case <-time.After(backoff.Next()): |
| 117 | + case <-timeout: |
| 118 | + return fmt.Errorf("timeout applying release %s/%s", release.GetNamespace(), release.GetName()) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + fmt.Printf("Release %s/%s successfully applied\n", release.GetNamespace(), release.GetName()) |
| 123 | + |
| 124 | + return nil |
| 125 | + }, |
| 126 | + ValidArgsFunction: func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 127 | + if len(args) > 0 { |
| 128 | + return nil, cobra.ShellCompDirectiveDefault |
| 129 | + } |
| 130 | + if clnt, err := getClient(c.Flag("kubeconfig").Value.String()); err == nil { |
| 131 | + releaseClient := release.NewClient(fullName, clnt) |
| 132 | + namespace := c.Flag("namespace").Value.String() |
| 133 | + if namespace == "" { |
| 134 | + namespace = "default" |
| 135 | + } |
| 136 | + ctx, cancel := context.WithTimeout(context.TODO(), 3*time.Second) |
| 137 | + defer cancel() |
| 138 | + if releases, err := releaseClient.List(ctx, namespace); err == nil { |
| 139 | + return slices.Collect(releases, func(release *release.Release) string { return release.GetName() }), cobra.ShellCompDirectiveNoFileComp |
| 140 | + } |
| 141 | + } |
| 142 | + return nil, cobra.ShellCompDirectiveDefault |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + flags := cmd.Flags() |
| 147 | + flags.StringArrayVarP(&options.valuesSources, "values", "f", nil, "Path to values file in yaml format (can be repeated, values will be merged in order of appearance)") |
| 148 | + flags.DurationVar(&options.timeout, "timeout", 0, "Time to wait for the operation to complete (default is to wait forever)") |
| 149 | + |
| 150 | + return cmd |
| 151 | +} |
0 commit comments