Skip to content

Commit 3740ec3

Browse files
committed
move brotli compression to kubecost Render function
1 parent e0bc316 commit 3740ec3

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

cmd/render-kubecost-manifest/main.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import (
77
)
88

99
func main() {
10-
out := kubecost.Render(kubecost.KubeCostConfig{
10+
out, err := kubecost.Render(kubecost.KubeCostConfig{
1111
ApiKey: "my-kubecost-key",
12-
})
12+
}, true)
13+
if err != nil {
14+
panic(err)
15+
}
1316
fmt.Println(string(out))
1417
}

kubecost/kubecost.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package kubecost
22

33
import (
4+
"bytes"
45
_ "embed"
56
"fmt"
67
"os"
78

89
yttcmd "carvel.dev/ytt/pkg/cmd/template"
910
yttui "carvel.dev/ytt/pkg/cmd/ui"
1011
yttfiles "carvel.dev/ytt/pkg/files"
12+
"github.com/andybalholm/brotli"
1113
)
1214

1315
//go:embed kubecost.yaml
@@ -53,7 +55,7 @@ data:
5355
`, token)
5456
}
5557

56-
func Render(config KubeCostConfig) []byte {
58+
func Render(config KubeCostConfig, compress bool) ([]byte, error) {
5759
opts := yttcmd.NewOptions()
5860
noopUI := yttui.NewCustomWriterTTY(false, os.Stderr, os.Stderr)
5961

@@ -67,13 +69,24 @@ func Render(config KubeCostConfig) []byte {
6769

6870
output := opts.RunWithFiles(inputs, noopUI)
6971
if output.Err != nil {
70-
panic(output.Err)
72+
return nil, output.Err
7173
}
72-
outputS, err := output.DocSet.AsBytes()
74+
manifest, err := output.DocSet.AsBytes()
7375
if err != nil {
74-
panic(err)
76+
return nil, err
77+
}
78+
if compress {
79+
var buf bytes.Buffer
80+
w := brotli.NewWriterV2(&buf, 7)
81+
if _, err := w.Write(manifest); err != nil {
82+
return nil, err
83+
}
84+
if err := w.Close(); err != nil {
85+
return nil, err
86+
}
87+
manifest = buf.Bytes()
7588
}
76-
return outputS
89+
return manifest, nil
7790
}
7891

7992
type noopWriter struct{}

pkg/controller/lifecycle/actuator.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
package lifecycle
66

77
import (
8-
"bytes"
98
"context"
109
_ "embed"
1110
"errors"
1211
"time"
1312

14-
"github.com/andybalholm/brotli"
1513
"github.com/go-logr/logr"
1614

1715
"github.com/liquid-reply/gardener-extension-shoot-kubecost/kubecost"
@@ -142,16 +140,11 @@ func getKubeCostConfig(cmData map[string]string) (kubecost.KubeCostConfig, error
142140
}
143141

144142
func createShootResourceKubeCostInstall(config kubecost.KubeCostConfig) (map[string][]byte, error) {
145-
manifest := kubecost.Render(config)
146-
var buf bytes.Buffer
147-
w := brotli.NewWriterV2(&buf, 7)
148-
if _, err := w.Write(manifest); err != nil {
149-
return nil, err
150-
}
151-
if err := w.Close(); err != nil {
143+
manifest, err := kubecost.Render(config, true)
144+
if err != nil {
152145
return nil, err
153146
}
154147
return map[string][]byte{
155-
"kubecost.br": buf.Bytes(),
148+
"kubecost.br": manifest,
156149
}, nil
157150
}

0 commit comments

Comments
 (0)