Skip to content

Commit

Permalink
(feat) dryRun message for helm charts
Browse files Browse the repository at this point in the history
Use `--raw-diff` options to display how helm charts are changing
  • Loading branch information
mgianluc committed Dec 5, 2024
1 parent bb139d7 commit 3a3a83f
Show file tree
Hide file tree
Showing 26 changed files with 390 additions and 321 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
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.43.1-0.20241202100654-55c0786240ee
github.com/projectsveltos/addon-controller v0.43.1-0.20241205122806-925d0b3abd6d
github.com/projectsveltos/event-manager v0.43.1-0.20241202114051-1c0a0d0cadee
github.com/projectsveltos/libsveltos v0.43.1-0.20241201131544-c4c2550af4af
github.com/robfig/cron/v3 v3.0.1
Expand All @@ -24,7 +24,7 @@ require (
k8s.io/klog/v2 v2.130.1
k8s.io/kubectl v0.31.3
sigs.k8s.io/cluster-api v1.8.5
sigs.k8s.io/controller-runtime v0.19.2
sigs.k8s.io/controller-runtime v0.19.3
sigs.k8s.io/yaml v1.4.0
)

Expand All @@ -35,7 +35,6 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ github.com/evanphx/json-patch/v5 v5.9.0 h1:kcBlZQbplgElYIlo/n1hJbls2z/1awpXxpRi0
github.com/evanphx/json-patch/v5 v5.9.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E=
github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
github.com/gdexlab/go-render v1.0.1 h1:rxqB3vo5s4n1kF0ySmoNeSPRYkEsyHgln4jFIQY7v0U=
Expand Down Expand Up @@ -102,8 +100,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
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.43.1-0.20241202100654-55c0786240ee h1:rkbzO6c5ttqDk1KAtbr3HPSquT5AO4eaa388NNgiCRc=
github.com/projectsveltos/addon-controller v0.43.1-0.20241202100654-55c0786240ee/go.mod h1:Mg0CsrS5NLlvKL5kIjqLznmvVE63Guj2aK+23k0kf0Y=
github.com/projectsveltos/addon-controller v0.43.1-0.20241205122806-925d0b3abd6d h1:fVysR/pJ6ynBDcLBY4Hj9swrFiFr14+aJynGjCNu3w4=
github.com/projectsveltos/addon-controller v0.43.1-0.20241205122806-925d0b3abd6d/go.mod h1:6iBdXE7wlecOH/c4BeLFNaoIHzk5lEvdrzD1s/w2ReE=
github.com/projectsveltos/event-manager v0.43.1-0.20241202114051-1c0a0d0cadee h1:JtYRf4FocJZCwswqMMdB2J7+9hQITW3kYnt9ftdLOMA=
github.com/projectsveltos/event-manager v0.43.1-0.20241202114051-1c0a0d0cadee/go.mod h1:XyJhynQqut5bUGdjGHCDpIOyf/rJv4KETlvJ7cr/V+s=
github.com/projectsveltos/libsveltos v0.43.1-0.20241201131544-c4c2550af4af h1:rkf6H9XqWmO10dTmZIsns7Zgcw4+vvnLa1bUrURPoJA=
Expand Down Expand Up @@ -226,8 +224,8 @@ k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078 h1:jGnCPejIetjiy2gqaJ5V0NLwTpF4w
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.2 h1:3sPrF58XQEPzbE8T81TN6selQIMGbtYwuaJ6eDssDF8=
sigs.k8s.io/controller-runtime v0.19.2/go.mod h1:iRmWllt8IlaLjvTTDLhRBXIEtkCK6hwVBJJsYS9Ajf4=
sigs.k8s.io/controller-runtime v0.19.3 h1:XO2GvC9OPftRst6xWCpTgBZO04S2cbp0Qqkj8bX1sPw=
sigs.k8s.io/controller-runtime v0.19.3/go.mod h1:j4j87DqtsThvwTv5/Tc5NFRyyF/RF0ip4+62tbTSIUM=
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
12 changes: 6 additions & 6 deletions internal/collector/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
. "github.com/onsi/gomega"
"k8s.io/klog/v2/textlogger"

configv1alpha1 "github.com/projectsveltos/addon-controller/api/v1beta1"
configv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1"
"github.com/projectsveltos/sveltosctl/internal/collector"
)

Expand Down Expand Up @@ -242,7 +242,7 @@ var _ = Describe("Client", func() {
u, err := instance.GetUnstructured([]byte(clusterConfigurationInstance))
Expect(err).To(BeNil())
Expect(u).ToNot(BeNil())
Expect(u.GetKind()).To(Equal(configv1alpha1.ClusterConfigurationKind))
Expect(u.GetKind()).To(Equal(configv1beta1.ClusterConfigurationKind))
})

It("getResourcesForKind returns all resources of a given namespaced kind", func() {
Expand All @@ -258,7 +258,7 @@ var _ = Describe("Client", func() {
namespaceFolder := filepath.Join(snapshotFolder, files[i].Name())
By(fmt.Sprintf("finding resources in folder %s", namespaceFolder))
instance := collector.GetClient()
list, err := collector.GetResourcesForKind(instance, namespaceFolder, configv1alpha1.ClusterConfigurationKind,
list, err := collector.GetResourcesForKind(instance, namespaceFolder, configv1beta1.ClusterConfigurationKind,
textlogger.NewLogger(textlogger.NewConfig(textlogger.Verbosity(1))))
Expect(err).To(BeNil())
Expect(list).ToNot(BeNil())
Expand All @@ -277,7 +277,7 @@ var _ = Describe("Client", func() {

By(fmt.Sprintf("finding resources in folder %s", snapshotFolder))
instance := collector.GetClient()
list, err := collector.GetResourcesForKind(instance, snapshotFolder, configv1alpha1.ClusterProfileKind,
list, err := collector.GetResourcesForKind(instance, snapshotFolder, configv1beta1.ClusterProfileKind,
textlogger.NewLogger(textlogger.NewConfig(textlogger.Verbosity(1))))
Expect(err).To(BeNil())
Expect(list).ToNot(BeNil())
Expand All @@ -289,7 +289,7 @@ var _ = Describe("Client", func() {
defer os.RemoveAll(snapshotFolder)

d := collector.GetClient()
resourceMap, err := d.GetNamespacedResources(snapshotFolder, configv1alpha1.ClusterConfigurationKind,
resourceMap, err := d.GetNamespacedResources(snapshotFolder, configv1beta1.ClusterConfigurationKind,
textlogger.NewLogger(textlogger.NewConfig(textlogger.Verbosity(1))))
Expect(err).To(BeNil())
Expect(resourceMap).ToNot(BeNil())
Expand All @@ -300,7 +300,7 @@ var _ = Describe("Client", func() {
for j := range resources {
u := resources[j]
Expect(u.GetNamespace()).To(Equal(k))
Expect(u.GetKind()).To(Equal(configv1alpha1.ClusterConfigurationKind))
Expect(u.GetKind()).To(Equal(configv1beta1.ClusterConfigurationKind))
}
}
})
Expand Down
18 changes: 9 additions & 9 deletions internal/collector/collector_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/cluster-api/util"

configv1alpha1 "github.com/projectsveltos/addon-controller/api/v1beta1"
configv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1"
libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1"
)

Expand All @@ -39,30 +39,30 @@ func randomString() string {
return util.RandomString(length)
}

func generateClusterProfile() *configv1alpha1.ClusterProfile {
return &configv1alpha1.ClusterProfile{
func generateClusterProfile() *configv1beta1.ClusterProfile {
return &configv1beta1.ClusterProfile{
ObjectMeta: metav1.ObjectMeta{
Name: randomString(),
},
Spec: configv1alpha1.Spec{
Spec: configv1beta1.Spec{
ClusterSelector: libsveltosv1beta1.Selector{
LabelSelector: metav1.LabelSelector{
MatchLabels: map[string]string{"zone": "west"},
},
},
SyncMode: configv1alpha1.SyncModeContinuous,
SyncMode: configv1beta1.SyncModeContinuous,
},
}
}

func generateClusterConfiguration() *configv1alpha1.ClusterConfiguration {
return &configv1alpha1.ClusterConfiguration{
func generateClusterConfiguration() *configv1beta1.ClusterConfiguration {
return &configv1beta1.ClusterConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: randomString(),
Namespace: randomString(),
},
Status: configv1alpha1.ClusterConfigurationStatus{
ClusterProfileResources: []configv1alpha1.ClusterProfileResource{
Status: configv1beta1.ClusterConfigurationStatus{
ClusterProfileResources: []configv1beta1.ClusterProfileResource{
{
ClusterProfileName: randomString(),
},
Expand Down
4 changes: 2 additions & 2 deletions internal/commands/show/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/go-logr/logr"
"github.com/olekukonko/tablewriter"

configv1alpha1 "github.com/projectsveltos/addon-controller/api/v1beta1"
configv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1"
logs "github.com/projectsveltos/libsveltos/lib/logsettings"
"github.com/projectsveltos/sveltosctl/internal/utils"
)
Expand Down Expand Up @@ -118,7 +118,7 @@ func displayAddOnsInNamespace(ctx context.Context, namespace, passedCluster, pas
return nil
}

func displayAddOnsForCluster(clusterConfiguration *configv1alpha1.ClusterConfiguration, passedProfile string,
func displayAddOnsForCluster(clusterConfiguration *configv1beta1.ClusterConfiguration, passedProfile string,
table *tablewriter.Table, logger logr.Logger) {

instance := utils.GetAccessInstance()
Expand Down
22 changes: 11 additions & 11 deletions internal/commands/show/addons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

configv1alpha1 "github.com/projectsveltos/addon-controller/api/v1beta1"
configv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1"
"github.com/projectsveltos/sveltosctl/internal/commands/show"
"github.com/projectsveltos/sveltosctl/internal/utils"
)

var _ = Describe("AddOnss", func() {
var clusterConfiguration *configv1alpha1.ClusterConfiguration
var clusterConfiguration *configv1beta1.ClusterConfiguration
var ns *corev1.Namespace

BeforeEach(func() {
namespace := namePrefix + randomString()

clusterConfiguration = &configv1alpha1.ClusterConfiguration{
clusterConfiguration = &configv1beta1.ClusterConfiguration{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: randomString(),
Expand All @@ -61,13 +61,13 @@ var _ = Describe("AddOnss", func() {

It("show addons displays deployed helm charts", func() {
clusterProfileName1 := randomString()
charts1 := []configv1alpha1.Chart{
charts1 := []configv1beta1.Chart{
*generateChart(), *generateChart(),
}
clusterConfiguration = addDeployedHelmCharts(clusterConfiguration, clusterProfileName1, charts1)

clusterProfileName2 := randomString()
charts2 := []configv1alpha1.Chart{
charts2 := []configv1beta1.Chart{
*generateChart(), *generateChart(), *generateChart(),
}
clusterConfiguration = addDeployedHelmCharts(clusterConfiguration, clusterProfileName2, charts2)
Expand Down Expand Up @@ -112,13 +112,13 @@ var _ = Describe("AddOnss", func() {

It("show addonss display deployed resources", func() {
clusterProfileName1 := randomString()
resource1 := []configv1alpha1.Resource{
resource1 := []configv1beta1.Resource{
*generateResource(), *generateResource(), *generateResource(),
}
clusterConfiguration = addDeployedResources(clusterConfiguration, clusterProfileName1, resource1)

clusterProfileName2 := randomString()
resource2 := []configv1alpha1.Resource{
resource2 := []configv1beta1.Resource{
*generateResource(), *generateResource(), *generateResource(),
}
clusterConfiguration = addDeployedResources(clusterConfiguration, clusterProfileName2, resource2)
Expand Down Expand Up @@ -163,15 +163,15 @@ var _ = Describe("AddOnss", func() {
})

func verifyCharts(lines []string, clusterInfo, clusterProfileName string,
charts []configv1alpha1.Chart) {
charts []configv1beta1.Chart) {

for i := range charts {
verifyChart(lines, clusterInfo, clusterProfileName, &charts[i])
}
}

func verifyChart(lines []string, clusterInfo, clusterProfileName string,
chart *configv1alpha1.Chart) {
chart *configv1beta1.Chart) {

found := false
for i := range lines {
Expand All @@ -192,15 +192,15 @@ func verifyChart(lines []string, clusterInfo, clusterProfileName string,
}

func verifyResources(lines []string, clusterInfo, clusterProfileName string,
resources []configv1alpha1.Resource) {
resources []configv1beta1.Resource) {

for i := range resources {
verifyResource(lines, clusterInfo, clusterProfileName, &resources[i])
}
}

func verifyResource(lines []string, clusterInfo, clusterProfileName string,
resource *configv1alpha1.Resource) {
resource *configv1beta1.Resource) {

found := false
for i := range lines {
Expand Down
72 changes: 58 additions & 14 deletions internal/commands/show/dryrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ import (
"github.com/docopt/docopt-go"
"github.com/go-logr/logr"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

configv1alpha1 "github.com/projectsveltos/addon-controller/api/v1beta1"
configv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1"
logs "github.com/projectsveltos/libsveltos/lib/logsettings"
"github.com/projectsveltos/sveltosctl/internal/utils"
)
Expand Down Expand Up @@ -131,39 +134,58 @@ 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, rawDiff)
err = displayDryRunForCluster(cr, profileName, table, rawDiff)
if err != nil {
return err
}
}
}

return nil
}

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

clusterInfo := fmt.Sprintf("%s/%s", clusterReport.Spec.ClusterNamespace, clusterReport.Spec.ClusterName)
profileOwner, err := getProfileOwnerReference(clusterReport)
if err != nil {
return err
}

updateMessage := "use --raw-diff to see full diff for helm values"
for i := range clusterReport.Status.ReleaseReports {
report := &clusterReport.Status.ReleaseReports[i]
message := report.Message
if report.Action == string(configv1beta1.UpdateHelmValuesAction) {
message = updateMessage
}
table.Append(genDryRunRow(clusterInfo, "helm release", report.ReleaseNamespace, report.ReleaseName,
report.Action, report.Message, profileName))
report.Action, message, profileName))
if rawDiff {
if rawDiff && report.Message != "" && report.Action == string(configv1beta1.UpdateHelmValuesAction) {
//nolint: forbidigo // print diff
fmt.Printf("Profile: %s:%s Cluster: %s/%s\n%s\n", profileOwner.Kind, profileOwner.Name,
clusterReport.Spec.ClusterNamespace, clusterReport.Spec.ClusterName, report.Message)
}
}
}

updateMessage := "use --raw-diff to see full diff"
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) {
if report.Action == string(configv1beta1.UpdateResourceAction) {
message = updateMessage
}
table.Append(genDryRunRow(clusterInfo, groupKind, report.Resource.Namespace, report.Resource.Name,
report.Action, message, profileName))
if rawDiff {
if rawDiff && report.Message != "" && report.Action == string(configv1alpha1.UpdateResourceAction) {
if rawDiff && report.Message != "" && report.Action == string(configv1beta1.UpdateResourceAction) {
//nolint: forbidigo // print diff
fmt.Printf("Cluster: %s/%s\n%s\n", clusterReport.Spec.ClusterNamespace, clusterReport.Spec.ClusterName,
report.Message)
fmt.Printf("Profile: %s:%s Cluster: %s/%s\n%s\n", profileOwner.Kind, profileOwner.Name,
clusterReport.Spec.ClusterNamespace, clusterReport.Spec.ClusterName, report.Message)
}
}
}
Expand All @@ -172,19 +194,21 @@ func displayDryRunForCluster(clusterReport *configv1alpha1.ClusterReport, profil
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) {
if report.Action == string(configv1beta1.UpdateResourceAction) {
message = updateMessage
}
table.Append(genDryRunRow(clusterInfo, groupKind, report.Resource.Namespace, report.Resource.Name,
report.Action, message, profileName))
if rawDiff {
if rawDiff && report.Message != "" && report.Action == string(configv1alpha1.UpdateResourceAction) {
if rawDiff && report.Message != "" && report.Action == string(configv1beta1.UpdateResourceAction) {
//nolint: forbidigo // print diff
fmt.Printf("Cluster: %s/%s\n%s\n", clusterReport.Spec.ClusterNamespace, clusterReport.Spec.ClusterName,
report.Message)
fmt.Printf("Profile: %s:%s Cluster: %s/%s\n%s\n", profileOwner.Kind, profileOwner.Name,
clusterReport.Spec.ClusterNamespace, clusterReport.Spec.ClusterName, report.Message)
}
}
}

return nil
}

// DryRun displays information about which Kubernetes addons would change in which cluster due
Expand Down Expand Up @@ -249,3 +273,23 @@ Description:

return displayDryRun(ctx, namespace, cluster, profile, rawDiff, logger)
}

// getProfileOwnerReference returns the ClusterProfile/Profile owning a given ClusterReport
func getProfileOwnerReference(clusterReport *configv1beta1.ClusterReport) (*metav1.OwnerReference, error) {
for _, ref := range clusterReport.OwnerReferences {
if ref.Kind != configv1beta1.ClusterProfileKind &&
ref.Kind != configv1beta1.ProfileKind {

continue
}
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
return nil, errors.WithStack(err)
}
if gv.Group == configv1beta1.GroupVersion.Group {
return &ref, nil
}
}

return nil, fmt.Errorf("(Cluster)Profile owner not found")
}
Loading

0 comments on commit 3a3a83f

Please sign in to comment.