Skip to content

Commit

Permalink
Merge pull request #500 from weaveworks/refactor-msg-related-fn
Browse files Browse the repository at this point in the history
refactor message-related functions
  • Loading branch information
Chanwit Kaewkasi authored Jan 27, 2023
2 parents 2b547c3 + bce4401 commit 12d7d92
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
3 changes: 3 additions & 0 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
14 changes: 12 additions & 2 deletions api/v1alpha1/terraform_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"strings"
"time"
"unicode/utf8"

"github.com/fluxcd/pkg/apis/meta"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
Expand Down Expand Up @@ -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() {
Expand Down
14 changes: 14 additions & 0 deletions api/v1alpha1/trim_str_test.go
Original file line number Diff line number Diff line change
@@ -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..."))
}
13 changes: 7 additions & 6 deletions controllers/tf_controller_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
}

0 comments on commit 12d7d92

Please sign in to comment.