Skip to content

Commit

Permalink
Merge pull request #306 from gianlucam76/diff
Browse files Browse the repository at this point in the history
(feat) show dryrun
  • Loading branch information
gianlucam76 authored Nov 30, 2024
2 parents 4ec5b6b + d480129 commit f45a474
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 50 deletions.
24 changes: 12 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ require (
github.com/go-logr/logr v1.4.2
github.com/hexops/gotextdiff v1.0.3
github.com/olekukonko/tablewriter v0.0.5
github.com/onsi/ginkgo/v2 v2.21.0
github.com/onsi/gomega v1.35.1
github.com/onsi/ginkgo/v2 v2.22.0
github.com/onsi/gomega v1.36.0
github.com/pkg/errors v0.9.1
github.com/projectsveltos/addon-controller v0.42.0
github.com/projectsveltos/event-manager v0.42.0
github.com/projectsveltos/libsveltos v0.42.0
github.com/projectsveltos/addon-controller v0.38.1-0.20241130093051-67a9bcd4999c
github.com/projectsveltos/event-manager v0.38.1-0.20241130135738-05ad83315f36
github.com/projectsveltos/libsveltos v0.42.1
github.com/robfig/cron/v3 v3.0.1
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.31.2
k8s.io/apiextensions-apiserver v0.31.2
k8s.io/apimachinery v0.31.2
k8s.io/client-go v0.31.2
k8s.io/api v0.31.3
k8s.io/apiextensions-apiserver v0.31.3
k8s.io/apimachinery v0.31.3
k8s.io/client-go v0.31.3
k8s.io/klog/v2 v2.130.1
k8s.io/kubectl v0.31.2
k8s.io/kubectl v0.31.3
sigs.k8s.io/cluster-api v1.8.5
sigs.k8s.io/controller-runtime v0.19.1
sigs.k8s.io/controller-runtime v0.19.2
sigs.k8s.io/yaml v1.4.0
)

Expand Down Expand Up @@ -82,7 +82,7 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/cluster-bootstrap v0.31.1 // indirect
k8s.io/component-base v0.31.2 // indirect
k8s.io/component-base v0.31.3 // indirect
k8s.io/kube-openapi v0.0.0-20241009091222-67ed5848f094 // indirect
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
48 changes: 24 additions & 24 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,21 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo/v2 v2.21.0 h1:7rg/4f3rB88pb5obDgNZrNHrQ4e6WpjonchcpuBRnZM=
github.com/onsi/ginkgo/v2 v2.21.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
github.com/onsi/gomega v1.35.1 h1:Cwbd75ZBPxFSuZ6T+rN/WCb/gOc6YgFBXLlZLhC7Ds4=
github.com/onsi/gomega v1.35.1/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
github.com/onsi/ginkgo/v2 v2.22.0 h1:Yed107/8DjTr0lKCNt7Dn8yQ6ybuDRQoMGrNFKzMfHg=
github.com/onsi/ginkgo/v2 v2.22.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
github.com/onsi/gomega v1.36.0 h1:Pb12RlruUtj4XUuPUqeEWc6j5DkVVVA49Uf6YLfC95Y=
github.com/onsi/gomega v1.36.0/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/projectsveltos/addon-controller v0.42.0 h1:omVSsxFEwjubtRcq8TYRGzpZPo1MGcsrk818WUcusKY=
github.com/projectsveltos/addon-controller v0.42.0/go.mod h1:zFbm1vOgqWclKbhHml5EslUh0wntqIEGRdYtg6kar2E=
github.com/projectsveltos/event-manager v0.42.0 h1:UqGuXez2Lh4E4lyNsQsYev9BuHJnYdn2cp/IG/wj464=
github.com/projectsveltos/event-manager v0.42.0/go.mod h1:yosnUdLubDWbKNCJV7oaYy5ZOpq3Nc9N3mTx0Wq6ejI=
github.com/projectsveltos/libsveltos v0.42.0 h1:U+mFYi2K4IPY1rvpg4jAPBqG4PaznUK4huo/xnFk7jc=
github.com/projectsveltos/libsveltos v0.42.0/go.mod h1:DvifWRZuPtGKi4oGzQzcGe8XBKlzIlx2DcaoukAio+M=
github.com/projectsveltos/addon-controller v0.38.1-0.20241130093051-67a9bcd4999c h1:RBZu6TgZAUjC/0KuXVZ9Ip/1Yt6tzMXpnsAnNmPF4XA=
github.com/projectsveltos/addon-controller v0.38.1-0.20241130093051-67a9bcd4999c/go.mod h1:WB/ean/I1EqRG9Mfw6+Jq371y1ykrF8Jetsh4lnKXMc=
github.com/projectsveltos/event-manager v0.38.1-0.20241130135738-05ad83315f36 h1:9AEVtIHarbzwQg1CPgvaZEW7Jqn6jPk4yGwwflqhRSM=
github.com/projectsveltos/event-manager v0.38.1-0.20241130135738-05ad83315f36/go.mod h1:lf7okHRrhjMv/0WtzJkqLp4QuCw0j9kzkB95fI5TgQg=
github.com/projectsveltos/libsveltos v0.42.1 h1:B7c8cF+vhR5AZJT8D4h19PSJfZqWUCR3CKEDUf032JI=
github.com/projectsveltos/libsveltos v0.42.1/go.mod h1:XPev2TKsMxVG5LwhbbMkcCs/U0730ZMmOXIvu2HEtWo=
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
Expand Down Expand Up @@ -204,30 +204,30 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/api v0.31.2 h1:3wLBbL5Uom/8Zy98GRPXpJ254nEFpl+hwndmk9RwmL0=
k8s.io/api v0.31.2/go.mod h1:bWmGvrGPssSK1ljmLzd3pwCQ9MgoTsRCuK35u6SygUk=
k8s.io/apiextensions-apiserver v0.31.2 h1:W8EwUb8+WXBLu56ser5IudT2cOho0gAKeTOnywBLxd0=
k8s.io/apiextensions-apiserver v0.31.2/go.mod h1:i+Geh+nGCJEGiCGR3MlBDkS7koHIIKWVfWeRFiOsUcM=
k8s.io/apimachinery v0.31.2 h1:i4vUt2hPK56W6mlT7Ry+AO8eEsyxMD1U44NR22CLTYw=
k8s.io/apimachinery v0.31.2/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo=
k8s.io/client-go v0.31.2 h1:Y2F4dxU5d3AQj+ybwSMqQnpZH9F30//1ObxOKlTI9yc=
k8s.io/client-go v0.31.2/go.mod h1:NPa74jSVR/+eez2dFsEIHNa+3o09vtNaWwWwb1qSxSs=
k8s.io/api v0.31.3 h1:umzm5o8lFbdN/hIXbrK9oRpOproJO62CV1zqxXrLgk8=
k8s.io/api v0.31.3/go.mod h1:UJrkIp9pnMOI9K2nlL6vwpxRzzEX5sWgn8kGQe92kCE=
k8s.io/apiextensions-apiserver v0.31.3 h1:+GFGj2qFiU7rGCsA5o+p/rul1OQIq6oYpQw4+u+nciE=
k8s.io/apiextensions-apiserver v0.31.3/go.mod h1:2DSpFhUZZJmn/cr/RweH1cEVVbzFw9YBu4T+U3mf1e4=
k8s.io/apimachinery v0.31.3 h1:6l0WhcYgasZ/wk9ktLq5vLaoXJJr5ts6lkaQzgeYPq4=
k8s.io/apimachinery v0.31.3/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo=
k8s.io/client-go v0.31.3 h1:CAlZuM+PH2cm+86LOBemaJI/lQ5linJ6UFxKX/SoG+4=
k8s.io/client-go v0.31.3/go.mod h1:2CgjPUTpv3fE5dNygAr2NcM8nhHzXvxB8KL5gYc3kJs=
k8s.io/cluster-bootstrap v0.31.1 h1:lS5aJi2r6WEKnjO5UhbYsz8e3xmEfoF4Hiob/gnB/Nk=
k8s.io/cluster-bootstrap v0.31.1/go.mod h1:dxroRr4eQ0ekxis/kzGa1qODprQXAxQZrgDLfTk8Pug=
k8s.io/component-base v0.31.2 h1:Z1J1LIaC0AV+nzcPRFqfK09af6bZ4D1nAOpWsy9owlA=
k8s.io/component-base v0.31.2/go.mod h1:9PeyyFN/drHjtJZMCTkSpQJS3U9OXORnHQqMLDz0sUQ=
k8s.io/component-base v0.31.3 h1:DMCXXVx546Rfvhj+3cOm2EUxhS+EyztH423j+8sOwhQ=
k8s.io/component-base v0.31.3/go.mod h1:xME6BHfUOafRgT0rGVBGl7TuSg8Z9/deT7qq6w7qjIU=
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
k8s.io/kube-openapi v0.0.0-20241009091222-67ed5848f094 h1:MErs8YA0abvOqJ8gIupA1Tz6PKXYUw34XsGlA7uSL1k=
k8s.io/kube-openapi v0.0.0-20241009091222-67ed5848f094/go.mod h1:7ioBJr1A6igWjsR2fxq2EZ0mlMwYLejazSIc2bzMp2U=
k8s.io/kubectl v0.31.2 h1:gTxbvRkMBwvTSAlobiTVqsH6S8Aa1aGyBcu5xYLsn8M=
k8s.io/kubectl v0.31.2/go.mod h1:EyASYVU6PY+032RrTh5ahtSOMgoDRIux9V1JLKtG5xM=
k8s.io/kubectl v0.31.3 h1:3r111pCjPsvnR98oLLxDMwAeM6OPGmPty6gSKaLTQes=
k8s.io/kubectl v0.31.3/go.mod h1:lhMECDCbJN8He12qcKqs2QfmVo9Pue30geovBVpH5fs=
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078 h1:jGnCPejIetjiy2gqaJ5V0NLwTpF4wbQ6cZIItJCSHno=
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
sigs.k8s.io/cluster-api v1.8.5 h1:lNA2fPN4fkXEs+oOQlnwxT/4VwRFBpv5kkSoJG8nqBA=
sigs.k8s.io/cluster-api v1.8.5/go.mod h1:pXv5LqLxuIbhGIXykyNKiJh+KrLweSBajVHHitPLyoY=
sigs.k8s.io/controller-runtime v0.19.1 h1:Son+Q40+Be3QWb+niBXAg2vFiYWolDjjRfO8hn/cxOk=
sigs.k8s.io/controller-runtime v0.19.1/go.mod h1:iRmWllt8IlaLjvTTDLhRBXIEtkCK6hwVBJJsYS9Ajf4=
sigs.k8s.io/controller-runtime v0.19.2 h1:3sPrF58XQEPzbE8T81TN6selQIMGbtYwuaJ6eDssDF8=
sigs.k8s.io/controller-runtime v0.19.2/go.mod h1:iRmWllt8IlaLjvTTDLhRBXIEtkCK6hwVBJJsYS9Ajf4=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 h1:150L+0vs/8DA78h1u02ooW1/fFq/Lwr+sGiqlzvrtq4=
Expand Down
52 changes: 40 additions & 12 deletions internal/commands/show/dryrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,25 @@ var (
)

func displayDryRun(ctx context.Context, passedNamespace, passedCluster, passedProfile string,
logger logr.Logger) error {
rawDiff bool, logger logr.Logger) error {

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"CLUSTER", "RESOURCE TYPE", "NAMESPACE", "NAME", "ACTION", "MESSAGE", "PROFILE"})

if err := displayDryRunInNamespaces(ctx, passedNamespace, passedCluster,
passedProfile, table, logger); err != nil {
passedProfile, table, rawDiff, logger); err != nil {
return err
}

table.Render()
if !rawDiff {
table.Render()
}

return nil
}

func displayDryRunInNamespaces(ctx context.Context, passedNamespace, passedCluster, passedProfile string,
table *tablewriter.Table, logger logr.Logger) error {
table *tablewriter.Table, rawDiff bool, logger logr.Logger) error {

instance := utils.GetAccessInstance()

Expand All @@ -84,7 +86,7 @@ func displayDryRunInNamespaces(ctx context.Context, passedNamespace, passedClust
if doConsiderNamespace(ns, passedNamespace) {
logger.V(logs.LogDebug).Info(fmt.Sprintf("Considering namespace: %s", ns.Name))
err = displayDryRunInNamespace(ctx, ns.Name, passedCluster, passedProfile,
table, logger)
table, rawDiff, logger)
if err != nil {
return err
}
Expand All @@ -95,7 +97,7 @@ func displayDryRunInNamespaces(ctx context.Context, passedNamespace, passedClust
}

func displayDryRunInNamespace(ctx context.Context, namespace, passedCluster, passedProfile string,
table *tablewriter.Table, logger logr.Logger) error {
table *tablewriter.Table, rawDiff bool, logger logr.Logger) error {

instance := utils.GetAccessInstance()

Expand Down Expand Up @@ -129,14 +131,16 @@ func displayDryRunInNamespace(ctx context.Context, namespace, passedCluster, pas
doConsiderProfile([]string{profileName}, passedProfile) {

logger.V(logs.LogDebug).Info(fmt.Sprintf("Considering ClusterReport: %s", cr.Name))
displayDryRunForCluster(cr, profileName, table)
displayDryRunForCluster(cr, profileName, table, rawDiff)
}
}

return nil
}

func displayDryRunForCluster(clusterReport *configv1alpha1.ClusterReport, profileName string, table *tablewriter.Table) {
func displayDryRunForCluster(clusterReport *configv1alpha1.ClusterReport, profileName string,
table *tablewriter.Table, rawDiff bool) {

clusterInfo := fmt.Sprintf("%s/%s", clusterReport.Spec.ClusterNamespace, clusterReport.Spec.ClusterName)

for i := range clusterReport.Status.ReleaseReports {
Expand All @@ -145,33 +149,55 @@ func displayDryRunForCluster(clusterReport *configv1alpha1.ClusterReport, profil
report.Action, report.Message, profileName))
}

updateMessage := "use --raw-diff to see full diff"
for i := range clusterReport.Status.ResourceReports {
report := &clusterReport.Status.ResourceReports[i]
groupKind := fmt.Sprintf("%s:%s", report.Resource.Group, report.Resource.Kind)
message := report.Message
if report.Action == string(configv1alpha1.UpdateResourceAction) {
message = updateMessage
}
table.Append(genDryRunRow(clusterInfo, groupKind, report.Resource.Namespace, report.Resource.Name,
report.Action, report.Message, profileName))
report.Action, message, profileName))
if rawDiff {
if rawDiff && report.Message != "" && report.Action == string(configv1alpha1.UpdateResourceAction) {
//nolint: forbidigo // print diff
fmt.Println(report.Message)
}
}
}

for i := range clusterReport.Status.KustomizeResourceReports {
report := &clusterReport.Status.KustomizeResourceReports[i]
groupKind := fmt.Sprintf("%s:%s", report.Resource.Group, report.Resource.Kind)
message := report.Message
if report.Action == string(configv1alpha1.UpdateResourceAction) {
message = updateMessage
}
table.Append(genDryRunRow(clusterInfo, groupKind, report.Resource.Namespace, report.Resource.Name,
report.Action, report.Message, profileName))
report.Action, message, profileName))
if rawDiff {
if rawDiff && report.Message != "" && report.Action == string(configv1alpha1.UpdateResourceAction) {
//nolint: forbidigo // print diff
fmt.Println(report.Message)
}
}
}
}

// DryRun displays information about which Kubernetes addons would change in which cluster due
// to a ClusterProfile currently in DryRun mode,
func DryRun(ctx context.Context, args []string, logger logr.Logger) error {
doc := `Usage:
sveltosctl show dryrun [options] [--namespace=<name>] [--cluster=<name>] [--profile=<name>] [--verbose]
sveltosctl show dryrun [options] [--namespace=<name>] [--cluster=<name>] [--profile=<name>] [--raw-diff] [--verbose]
--namespace=<name> Show which Kubernetes addons would change in clusters in this namespace.
If not specified all namespaces are considered.
--cluster=<name> Show which Kubernetes addons would change in cluster with name.
If not specified all cluster names are considered.
--profile=<kind/name> Show which Kubernetes addons would change because of this clusterprofile/profile.
If not specified all clusterprofiles/profiles are considered.
--raw-diff With this flag, for each resource that would be update, full diff will be displayed.
Options:
-h --help Show this screen.
Expand Down Expand Up @@ -217,5 +243,7 @@ Description:
profile = passedProfile.(string)
}

return displayDryRun(ctx, namespace, cluster, profile, logger)
rawDiff := parsedArgs["--raw-diff"].(bool)

return displayDryRun(ctx, namespace, cluster, profile, rawDiff, logger)
}
4 changes: 2 additions & 2 deletions internal/commands/show/dryrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ var _ = Describe("DryRun", func() {
c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjects...).Build()

utils.InitalizeManagementClusterAcces(scheme, nil, nil, c)
err = show.DisplayDryRun(context.TODO(), "", "", "",
err = show.DisplayDryRun(context.TODO(), "", "", "", false,
textlogger.NewLogger(textlogger.NewConfig(textlogger.Verbosity(1))))
Expect(err).To(BeNil())

Expand Down Expand Up @@ -209,7 +209,7 @@ var _ = Describe("DryRun", func() {
c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjects...).Build()

utils.InitalizeManagementClusterAcces(scheme, nil, nil, c)
err = show.DisplayDryRun(context.TODO(), "", "", "",
err = show.DisplayDryRun(context.TODO(), "", "", "", false,
textlogger.NewLogger(textlogger.NewConfig(textlogger.Verbosity(1))))
Expect(err).To(BeNil())

Expand Down

0 comments on commit f45a474

Please sign in to comment.