|
| 1 | +package accept |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/google/go-cmp/cmp" |
| 10 | + configv1 "github.com/openshift/api/config/v1" |
| 11 | + configv1client "github.com/openshift/client-go/config/clientset/versioned" |
| 12 | + "github.com/spf13/cobra" |
| 13 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/types" |
| 16 | + "k8s.io/apimachinery/pkg/util/sets" |
| 17 | + "k8s.io/cli-runtime/pkg/genericiooptions" |
| 18 | + kcmdutil "k8s.io/kubectl/pkg/cmd/util" |
| 19 | + "k8s.io/kubectl/pkg/util/templates" |
| 20 | +) |
| 21 | + |
| 22 | +func newOptions(streams genericiooptions.IOStreams) *options { |
| 23 | + return &options{ |
| 24 | + IOStreams: streams, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +var ( |
| 29 | + acceptExample = templates.Examples(` |
| 30 | + # Accept RiskA and RiskB and stop accepting RiskC if accepted |
| 31 | + oc adm upgrade accept RiskA,RiskB,RiskC- |
| 32 | +
|
| 33 | + # Accept RiskA and RiskB and nothing else |
| 34 | + oc adm upgrade accept --replace RiskA,RiskB |
| 35 | +
|
| 36 | + # Accept no risks |
| 37 | + oc adm upgrade accept --clear |
| 38 | + `) |
| 39 | + |
| 40 | + acceptLong = templates.LongDesc(` |
| 41 | + Manage update risk acceptance. |
| 42 | +
|
| 43 | + Multiple risks are concatenated with comma. By default, the command appends the provided accepted risks into the existing |
| 44 | + list. If --replace is specified, the existing accepted risks will be replaced with the provided |
| 45 | + ones instead of appending. Placing "-" as suffix to an accepted risk will lead to |
| 46 | + removal if it exists and no-ops otherwise. If --replace is specified, the suffix "-" on the risks |
| 47 | + is not allowed. |
| 48 | +
|
| 49 | + Passing --clear removes all existing accepted risks. |
| 50 | + `) |
| 51 | +) |
| 52 | + |
| 53 | +func New(f kcmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command { |
| 54 | + o := newOptions(streams) |
| 55 | + cmd := &cobra.Command{ |
| 56 | + Use: "accept", |
| 57 | + Short: "Accept risks exposed to conditional updates.", |
| 58 | + Long: acceptLong, |
| 59 | + Example: acceptExample, |
| 60 | + Run: func(cmd *cobra.Command, args []string) { |
| 61 | + kcmdutil.CheckErr(o.Complete(f, cmd, args)) |
| 62 | + kcmdutil.CheckErr(o.Run()) |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + flags := cmd.Flags() |
| 67 | + flags.BoolVar(&o.replace, "replace", false, "Replace existing accepted risks with new ones") |
| 68 | + flags.BoolVar(&o.clear, "clear", false, "Remove all existing accepted risks") |
| 69 | + return cmd |
| 70 | +} |
| 71 | + |
| 72 | +// clusterVersionInterface is the subset of configv1client.ClusterVersionInterface |
| 73 | +// that we need, for easier mocking in unit tests. |
| 74 | +type clusterVersionInterface interface { |
| 75 | + Get(ctx context.Context, name string, opts metav1.GetOptions) (*configv1.ClusterVersion, error) |
| 76 | + Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (result *configv1.ClusterVersion, err error) |
| 77 | +} |
| 78 | + |
| 79 | +type options struct { |
| 80 | + genericiooptions.IOStreams |
| 81 | + |
| 82 | + Client configv1client.Interface |
| 83 | + replace bool |
| 84 | + clear bool |
| 85 | + add sets.Set[string] |
| 86 | + remove sets.Set[string] |
| 87 | +} |
| 88 | + |
| 89 | +func (o *options) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []string) error { |
| 90 | + if o.clear && o.replace { |
| 91 | + return kcmdutil.UsageErrorf(cmd, "--clear and --replace are mutually exclusive") |
| 92 | + } |
| 93 | + |
| 94 | + if o.clear { |
| 95 | + kcmdutil.RequireNoArguments(cmd, args) |
| 96 | + } else if len(args) == 0 { |
| 97 | + return kcmdutil.UsageErrorf(cmd, "no positional arguments given") |
| 98 | + } |
| 99 | + |
| 100 | + if len(args) > 1 { |
| 101 | + return kcmdutil.UsageErrorf(cmd, "multiple positional arguments given") |
| 102 | + } else if len(args) == 1 { |
| 103 | + o.add = sets.New[string]() |
| 104 | + o.remove = sets.New[string]() |
| 105 | + for _, s := range strings.Split(args[0], ",") { |
| 106 | + trimmed := strings.TrimSpace(s) |
| 107 | + if trimmed == "-" || trimmed == "" { |
| 108 | + return kcmdutil.UsageErrorf(cmd, "illegal risk %q", trimmed) |
| 109 | + } |
| 110 | + if strings.HasSuffix(trimmed, "-") { |
| 111 | + o.remove.Insert(strings.TrimSuffix(trimmed, "-")) |
| 112 | + } else { |
| 113 | + o.add.Insert(trimmed) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if conflict := o.add.Intersection(o.remove); conflict.Len() > 0 { |
| 119 | + return kcmdutil.UsageErrorf(cmd, "requested risks with both Risk and Risk-: %s", strings.Join(sets.List(conflict), ",")) |
| 120 | + } |
| 121 | + |
| 122 | + if o.replace && o.remove.Len() > 0 { |
| 123 | + return kcmdutil.UsageErrorf(cmd, "The suffix '-' on risks is not allowed if --replace is specified") |
| 124 | + } |
| 125 | + |
| 126 | + cfg, err := f.ToRESTConfig() |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + client, err := configv1client.NewForConfig(cfg) |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + o.Client = client |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +func (o *options) Run() error { |
| 139 | + ctx := context.TODO() |
| 140 | + cv, err := o.Client.ConfigV1().ClusterVersions().Get(ctx, "version", metav1.GetOptions{}) |
| 141 | + if err != nil { |
| 142 | + if apierrors.IsNotFound(err) { |
| 143 | + return fmt.Errorf("no cluster version information available - you must be connected to an OpenShift server to fetch the current version") |
| 144 | + } |
| 145 | + return err |
| 146 | + } |
| 147 | + |
| 148 | + var existing []configv1.AcceptRisk |
| 149 | + if cv.Spec.DesiredUpdate != nil { |
| 150 | + existing = cv.Spec.DesiredUpdate.AcceptRisks |
| 151 | + } |
| 152 | + acceptRisks := getAcceptRisks(existing, o.replace, o.clear, o.add, o.remove) |
| 153 | + |
| 154 | + if diff := cmp.Diff(acceptRisks, existing); diff != "" { |
| 155 | + if err := patchDesiredUpdate(ctx, acceptRisks, o.Client.ConfigV1().ClusterVersions(), "version"); err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + var names []string |
| 159 | + for _, risk := range acceptRisks { |
| 160 | + names = append(names, risk.Name) |
| 161 | + } |
| 162 | + fmt.Fprintf(o.Out, "info: Accept risks are [%s]\n", strings.Join(names, ", ")) //nolint:errcheck |
| 163 | + } else { |
| 164 | + fmt.Fprintf(o.Out, "info: Accept risks are not changed\n") //nolint:errcheck |
| 165 | + } |
| 166 | + |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +func getAcceptRisks(existing []configv1.AcceptRisk, replace, clear bool, add sets.Set[string], remove sets.Set[string]) []configv1.AcceptRisk { |
| 171 | + var acceptRisks []configv1.AcceptRisk |
| 172 | + |
| 173 | + if clear { |
| 174 | + return acceptRisks |
| 175 | + } |
| 176 | + |
| 177 | + if !replace { |
| 178 | + for _, risk := range existing { |
| 179 | + if !remove.Has(risk.Name) { |
| 180 | + acceptRisks = append(acceptRisks, *risk.DeepCopy()) |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + riskNames := sets.New[string]() |
| 186 | + for _, risk := range acceptRisks { |
| 187 | + riskNames.Insert(risk.Name) |
| 188 | + } |
| 189 | + |
| 190 | + for _, name := range sets.List[string](add) { |
| 191 | + if !riskNames.Has(name) && !remove.Has(name) { |
| 192 | + acceptRisks = append(acceptRisks, configv1.AcceptRisk{ |
| 193 | + Name: name, |
| 194 | + }) |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + return acceptRisks |
| 199 | +} |
| 200 | + |
| 201 | +func patchDesiredUpdate(ctx context.Context, acceptRisks []configv1.AcceptRisk, client clusterVersionInterface, |
| 202 | + clusterVersionName string) error { |
| 203 | + acceptRisksJSON, err := json.Marshal(acceptRisks) |
| 204 | + if err != nil { |
| 205 | + return fmt.Errorf("marshal ClusterVersion patch: %v", err) |
| 206 | + } |
| 207 | + patch := []byte(fmt.Sprintf(`{"spec": {"desiredUpdate": {"acceptRisks": %s}}}`, acceptRisksJSON)) |
| 208 | + if _, err := client.Patch(ctx, clusterVersionName, types.MergePatchType, patch, |
| 209 | + metav1.PatchOptions{}); err != nil { |
| 210 | + return fmt.Errorf("unable to accept risks: %v", err) |
| 211 | + } |
| 212 | + return nil |
| 213 | +} |
0 commit comments