From bce4401c0814bab80306692d55aa80db31242b6d Mon Sep 17 00:00:00 2001 From: Chanwit Kaewkasi Date: Mon, 23 Jan 2023 19:08:29 +0700 Subject: [PATCH] refactor message-related functions Signed-off-by: Chanwit Kaewkasi --- api/go.mod | 3 +++ api/go.sum | 4 ++++ api/v1alpha1/terraform_types.go | 14 ++++++++++++-- api/v1alpha1/trim_str_test.go | 14 ++++++++++++++ controllers/tf_controller_apply.go | 13 +++++++------ 5 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 api/v1alpha1/trim_str_test.go diff --git a/api/go.mod b/api/go.mod index 6084dda9..0e28da27 100644 --- a/api/go.mod +++ b/api/go.mod @@ -5,6 +5,7 @@ go 1.19 require ( github.com/fluxcd/pkg/apis/meta v0.18.0 github.com/fluxcd/source-controller/api v0.33.0 + github.com/onsi/gomega v1.20.1 k8s.io/api v0.25.4 k8s.io/apiextensions-apiserver v0.25.4 k8s.io/apimachinery v0.25.4 @@ -15,6 +16,7 @@ require ( github.com/fluxcd/pkg/apis/acl v0.1.0 // indirect github.com/go-logr/logr v1.2.3 // indirect github.com/gogo/protobuf v1.3.2 // indirect + github.com/google/go-cmp v0.5.8 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect @@ -23,6 +25,7 @@ require ( golang.org/x/text v0.4.0 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/klog/v2 v2.80.1 // indirect k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2 // indirect sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect diff --git a/api/go.sum b/api/go.sum index 32881a1f..06a4f8aa 100644 --- a/api/go.sum +++ b/api/go.sum @@ -14,6 +14,7 @@ github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbV github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= @@ -30,7 +31,9 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/ginkgo/v2 v2.1.6 h1:Fx2POJZfKRQcM1pH49qSZiYeu319wji004qX+GDovrU= github.com/onsi/gomega v1.20.1 h1:PA/3qinGoukvymdIDV8pii6tiZgC8kbmJO6Z5+b002Q= +github.com/onsi/gomega v1.20.1/go.mod h1:DtrZpjmvpn2mPm4YWQa0/ALMDj9v4YxLgojwPeREyVo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= @@ -78,6 +81,7 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= 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.25.4 h1:3YO8J4RtmG7elEgaWMb4HgmpS2CfY1QlaOz9nwB+ZSs= k8s.io/api v0.25.4/go.mod h1:IG2+RzyPQLllQxnhzD8KQNEu4c4YvyDTpSMztf4A0OQ= k8s.io/apiextensions-apiserver v0.25.4 h1:7hu9pF+xikxQuQZ7/30z/qxIPZc2J1lFElPtr7f+B6U= diff --git a/api/v1alpha1/terraform_types.go b/api/v1alpha1/terraform_types.go index 82b2431a..66bccb85 100644 --- a/api/v1alpha1/terraform_types.go +++ b/api/v1alpha1/terraform_types.go @@ -20,6 +20,7 @@ import ( "fmt" "strings" "time" + "unicode/utf8" "github.com/fluxcd/pkg/apis/meta" sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" @@ -824,12 +825,21 @@ func (in *TerraformSpec) GetAlwaysCleanupRunnerPod() bool { return *in.AlwaysCleanupRunnerPod } +// trimString takes in a string and an integer limit, and returns a new string with a maximum length of limit characters. +// If the length of the input string is greater than limit, the returned string will be truncated to limit characters +// and "..." will be appended to the end. +// If limit is less than 3, it will be set to 3 before continuing. +// It correctly handles unicode characters by using utf8.RuneCountInString to get the number of runes in the string. func trimString(str string, limit int) string { - if len(str) <= limit { + if limit < 3 { + limit = 3 + } + if utf8.RuneCountInString(str) <= limit { return str } - return str[0:limit] + "..." + runes := []rune(str) + return string(runes[:limit]) + "..." } func init() { diff --git a/api/v1alpha1/trim_str_test.go b/api/v1alpha1/trim_str_test.go new file mode 100644 index 00000000..4ecc6c8c --- /dev/null +++ b/api/v1alpha1/trim_str_test.go @@ -0,0 +1,14 @@ +package v1alpha1 + +import "testing" +import . "github.com/onsi/gomega" + +func TestTrimString(t *testing.T) { + g := NewGomegaWithT(t) + + g.Expect(trimString("hello world", 5)).To(Equal("hello...")) + g.Expect(trimString("hello world", 11)).To(Equal("hello world")) + g.Expect(trimString("", 5)).To(Equal("")) + g.Expect(trimString("hello world", -5)).To(Equal("hel...")) + g.Expect(trimString("hello world", 0)).To(Equal("hel...")) +} diff --git a/controllers/tf_controller_apply.go b/controllers/tf_controller_apply.go index 9b4ba884..338677e6 100644 --- a/controllers/tf_controller_apply.go +++ b/controllers/tf_controller_apply.go @@ -175,6 +175,8 @@ func (r *TerraformReconciler) apply(ctx context.Context, terraform infrav1.Terra err.Error(), ), err } + + // TODO add resource location to inventory for _, iv := range getInventoryReply.Inventories { inventoryEntries = append(inventoryEntries, infrav1.ResourceRef{ Name: iv.GetName(), @@ -189,15 +191,14 @@ func (r *TerraformReconciler) apply(ctx context.Context, terraform infrav1.Terra } } + var msg string if isDestroyApplied { - msg := fmt.Sprintf("Destroy applied successfully") - r.event(ctx, terraform, revision, events.EventSeverityInfo, msg, nil) - terraform = infrav1.TerraformApplied(terraform, revision, "Destroy applied successfully", isDestroyApplied, inventoryEntries) + msg = fmt.Sprintf("Destroy applied successfully") } else { - msg := fmt.Sprintf("Applied successfully") - r.event(ctx, terraform, revision, events.EventSeverityInfo, msg, nil) - terraform = infrav1.TerraformApplied(terraform, revision, "Applied successfully", isDestroyApplied, inventoryEntries) + msg = fmt.Sprintf("Applied successfully") } + r.event(ctx, terraform, revision, events.EventSeverityInfo, msg, nil) + terraform = infrav1.TerraformApplied(terraform, revision, msg, isDestroyApplied, inventoryEntries) return terraform, nil }