|
| 1 | +package sts |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os/exec" |
| 6 | + "regexp" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "k8s.io/cli-runtime/pkg/genericclioptions" |
| 11 | + cmdutil "k8s.io/kubectl/pkg/cmd/util" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 13 | + |
| 14 | + "github.com/openshift/osdctl/pkg/k8s" |
| 15 | +) |
| 16 | + |
| 17 | +type policyDiffOptions struct { |
| 18 | + oldReleaseVersion string |
| 19 | + newReleaseVersion string |
| 20 | + |
| 21 | + flags *genericclioptions.ConfigFlags |
| 22 | + genericclioptions.IOStreams |
| 23 | + kubeCli client.Client |
| 24 | +} |
| 25 | + |
| 26 | +func newPolicyDiffOptions(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *policyDiffOptions { |
| 27 | + return &policyDiffOptions{ |
| 28 | + flags: flags, |
| 29 | + IOStreams: streams, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func newCmdPolicyDiff(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *cobra.Command { |
| 34 | + ops := newPolicyDiffOptions(streams, flags) |
| 35 | + policyDiffCmd := &cobra.Command{ |
| 36 | + Use: "policy-diff", |
| 37 | + Short: "Get diff between two versions of OCP STS policy", |
| 38 | + Args: cobra.ExactArgs(2), |
| 39 | + DisableAutoGenTag: true, |
| 40 | + Run: func(cmd *cobra.Command, args []string) { |
| 41 | + cmdutil.CheckErr(ops.complete(cmd, args)) |
| 42 | + cmdutil.CheckErr(ops.run(args)) |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + policyDiffCmd.Flags().StringVarP(&ops.oldReleaseVersion, "old-version", "o", "", "") |
| 47 | + policyDiffCmd.Flags().StringVarP(&ops.newReleaseVersion, "new-version", "n", "", "") |
| 48 | + |
| 49 | + return policyDiffCmd |
| 50 | +} |
| 51 | + |
| 52 | +func (o *policyDiffOptions) complete(cmd *cobra.Command, args []string) error { |
| 53 | + if len(args) != 2 { |
| 54 | + return cmdutil.UsageErrorf(cmd, "Old and new release version is required for policy-diff command") |
| 55 | + } |
| 56 | + |
| 57 | + for _, s := range args { |
| 58 | + re := regexp.MustCompile(`^[0-9]{1}\.[0-9]{1,2}\.[0-9]{1,2}$`) |
| 59 | + if !re.MatchString(s) { |
| 60 | + return cmdutil.UsageErrorf(cmd, "Release version have to be in the x.y.z format ") |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // only initialize kubernetes client when versions are set |
| 65 | + if o.oldReleaseVersion != "" && o.newReleaseVersion != "" { |
| 66 | + var err error |
| 67 | + o.kubeCli, err = k8s.NewClient(o.flags) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func (o *policyDiffOptions) run(args []string) error { |
| 77 | + // save crs files in /tmp/crs- dirs for each release version |
| 78 | + for _, s := range args { |
| 79 | + crs := "oc adm release extract quay.io/openshift-release-dev/ocp-release:" + s + "-x86_64 --credentials-requests --cloud=aws --to=/tmp/crs-" + s |
| 80 | + _, err := exec.Command("bash", "-c", crs).Output() |
| 81 | + if err != nil { |
| 82 | + fmt.Print(err) |
| 83 | + return err |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + diff := "diff /tmp/crs-" + string(args[0]) + " /tmp/crs-" + string(args[1]) |
| 88 | + output, _ := exec.Command("bash", "-c", diff).Output() |
| 89 | + fmt.Println(string(output)) |
| 90 | + |
| 91 | + return nil |
| 92 | +} |
0 commit comments