Skip to content

Commit 3657a2f

Browse files
Merge pull request #155 from Tessg22/add-sts-policy-diff
OSD-8714 - Automate STS diff in osdctl
2 parents f2365e2 + 9bb8c50 commit 3657a2f

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,15 @@ aws_account_operator_total_accounts_crs_claimed{name="aws-account-operator"} =>
260260
......
261261
```
262262

263+
### Get cluster policy and policy-diff
264+
265+
`policy` command saves the crs files in /tmp/crs- directory for given `x.y.z` release version. `policy-diff` command, in addition, compares the files of directories and outputs the diff.
266+
267+
```bash
268+
osdctl sts policy <OCP version>
269+
osdctl sts policy-diff <old version> <new version>
270+
```
271+
263272
### Hive ClusterDeployment CR list
264273

265274
```bash

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/openshift/osdctl/cmd/federatedrole"
2525
"github.com/openshift/osdctl/cmd/network"
2626
"github.com/openshift/osdctl/cmd/servicelog"
27+
"github.com/openshift/osdctl/cmd/sts"
2728
)
2829

2930
// GitCommit is the short git commit hash from the environment
@@ -75,6 +76,7 @@ func NewCmdRoot(streams genericclioptions.IOStreams) *cobra.Command {
7576
rootCmd.AddCommand(network.NewCmdNetwork(streams, kubeFlags))
7677
rootCmd.AddCommand(newCmdMetrics(streams, kubeFlags))
7778
rootCmd.AddCommand(servicelog.NewCmdServiceLog())
79+
rootCmd.AddCommand(sts.NewCmdSts(streams, kubeFlags))
7880

7981
// add docs command
8082
rootCmd.AddCommand(newCmdDocs(streams))

cmd/sts/cmd.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package sts
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"k8s.io/cli-runtime/pkg/genericclioptions"
6+
)
7+
8+
// NewCmdClusterHealth implements the base cluster health command
9+
func NewCmdSts(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *cobra.Command {
10+
clusterCmd := &cobra.Command{
11+
Use: "sts",
12+
Short: "STS related utilities",
13+
Args: cobra.NoArgs,
14+
DisableAutoGenTag: true,
15+
Run: help,
16+
}
17+
18+
clusterCmd.AddCommand(newCmdPolicyDiff(streams, flags))
19+
clusterCmd.AddCommand(newCmdPolicy(streams, flags))
20+
return clusterCmd
21+
}
22+
23+
func help(cmd *cobra.Command, _ []string) {
24+
cmd.Help()
25+
}

cmd/sts/policy.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 policyOptions struct {
18+
ReleaseVersion string
19+
20+
flags *genericclioptions.ConfigFlags
21+
genericclioptions.IOStreams
22+
kubeCli client.Client
23+
}
24+
25+
func newPolicyOptions(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *policyOptions {
26+
return &policyOptions{
27+
flags: flags,
28+
IOStreams: streams,
29+
}
30+
}
31+
32+
func newCmdPolicy(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *cobra.Command {
33+
ops := newPolicyOptions(streams, flags)
34+
policyCmd := &cobra.Command{
35+
Use: "policy",
36+
Short: "Get OCP STS policy",
37+
Args: cobra.ExactArgs(1),
38+
DisableAutoGenTag: true,
39+
Run: func(cmd *cobra.Command, args []string) {
40+
cmdutil.CheckErr(ops.complete(cmd, args))
41+
cmdutil.CheckErr(ops.run(args))
42+
},
43+
}
44+
45+
policyCmd.Flags().StringVarP(&ops.ReleaseVersion, "release-version", "r", "", "")
46+
47+
return policyCmd
48+
}
49+
50+
func (o *policyOptions) complete(cmd *cobra.Command, args []string) error {
51+
if len(args) != 1 {
52+
return cmdutil.UsageErrorf(cmd, "Release version is required for policy command")
53+
}
54+
55+
re := regexp.MustCompile(`^[0-9]{1}\.[0-9]{1,2}\.[0-9]{1,2}$`)
56+
if !re.MatchString(args[0]) {
57+
return cmdutil.UsageErrorf(cmd, "Release version have to be in the x.y.z format ")
58+
}
59+
60+
// only initialize kubernetes client when versions are set
61+
if o.ReleaseVersion != "" {
62+
var err error
63+
o.kubeCli, err = k8s.NewClient(o.flags)
64+
if err != nil {
65+
return err
66+
}
67+
}
68+
69+
return nil
70+
}
71+
72+
func (o *policyOptions) run(args []string) error {
73+
// save crs files in /tmp/crs- dir for given release version
74+
crs := "oc adm release extract quay.io/openshift-release-dev/ocp-release:" + args[0] + "-x86_64 --credentials-requests --cloud=aws --to=/tmp/crs-" + args[0]
75+
_, err := exec.Command("bash", "-c", crs).Output()
76+
if err != nil {
77+
fmt.Print(err)
78+
return err
79+
}
80+
81+
output := "OCP STS policy files have been saved in /tmp/crs-" + args[0] + " directory"
82+
fmt.Println(output)
83+
84+
return nil
85+
}

cmd/sts/policydiff.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)