Skip to content

Commit 2c68250

Browse files
committed
Start to implement help wrapper
1 parent ca7b8d3 commit 2c68250

10 files changed

Lines changed: 1529 additions & 9 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ which = { workspace = true }
2121
serde = { workspace = true }
2222
url = { workspace = true }
2323

24+
[build-dependencies]
25+
gobuild = "0.1.0-alpha.1"
26+
2427
[workspace.dependencies]
2528
tokio = { version = "1.27.0", features = ["rt-multi-thread", "macros"] }
2629
reqwest = { version = "0.11.16", features = ["native-tls"] }

build.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use gobuild::BuildMode;
2+
3+
fn main() {
4+
gobuild::Build::new()
5+
.file("go-helm-wrapper/main.go")
6+
.buildmode(BuildMode::CArchive)
7+
.compile("go-helm-wrapper");
8+
}

go-helm-wrapper/main.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package main
2+
3+
import (
4+
"C"
5+
"context"
6+
"encoding/json"
7+
"time"
8+
"helm.sh/helm/v3/pkg/action"
9+
"helm.sh/helm/v3/pkg/repo"
10+
gohelm "github.com/mittwald/go-helm-client"
11+
12+
// Needed for authentication against clusters, e.g. GCP
13+
// see https://github.com/kubernetes/client-go/issues/242
14+
_ "k8s.io/client-go/plugin/pkg/client/auth"
15+
)
16+
17+
func main() {
18+
19+
}
20+
21+
//export go_install_helm_release
22+
func go_install_helm_release(releaseName string, chartName string, chartVersion string, valuesYaml string, namespace string, suppressOutput bool) {
23+
helmClient := getHelmClient(namespace, suppressOutput)
24+
25+
timeout, _ := time.ParseDuration("10m")
26+
chartSpec := gohelm.ChartSpec{
27+
ReleaseName: releaseName,
28+
ChartName: chartName,
29+
Version: chartVersion,
30+
ValuesYaml: valuesYaml,
31+
Namespace: namespace,
32+
UpgradeCRDs: true,
33+
Wait: true,
34+
Timeout: timeout,
35+
}
36+
37+
if _, err := helmClient.InstallChart(context.Background(), &chartSpec, nil); err != nil {
38+
panic(err)
39+
}
40+
}
41+
42+
//export go_uninstall_helm_release
43+
func go_uninstall_helm_release(releaseName string, namespace string, suppressOutput bool) {
44+
helmClient := getHelmClient(namespace, suppressOutput)
45+
46+
if err := helmClient.UninstallReleaseByName(releaseName); err != nil {
47+
panic(err)
48+
}
49+
}
50+
51+
//export go_helm_release_exists
52+
func go_helm_release_exists(releaseName string, namespace string) bool {
53+
helmClient := getHelmClient(namespace, true)
54+
55+
release, _ := helmClient.GetRelease(releaseName)
56+
return release != nil
57+
}
58+
59+
type Release struct {
60+
Name string `json:"name"`
61+
Version string `json:"version"`
62+
Namespace string `json:"namespace"`
63+
Status string `json:"status"`
64+
LastUpdated string `json:"lastUpdated"`
65+
}
66+
67+
//export go_helm_list_releases
68+
//We are returning a JSON document as GoSlices (array) of objects was a nightmare to share between Go and Rust
69+
func go_helm_list_releases(namespace string) *C.char {
70+
helmClient := getHelmClient(namespace, true)
71+
72+
// List all releases, not only the deployed ones (e.g. include pending installations)
73+
releases, err := helmClient.ListReleasesByStateMask(action.ListAll)
74+
if err != nil {
75+
panic(err)
76+
}
77+
var result = make([]Release, len(releases))
78+
for i, release := range releases {
79+
result[i] = Release{
80+
Name: release.Name,
81+
Version: release.Chart.Metadata.Version,
82+
Namespace: release.Namespace,
83+
Status: release.Info.Status.String(),
84+
LastUpdated: release.Info.LastDeployed.String(),
85+
};
86+
}
87+
88+
json, err := json.Marshal(result)
89+
if err != nil {
90+
panic(err)
91+
}
92+
93+
return C.CString(string(json))
94+
}
95+
96+
//export go_add_helm_repo
97+
func go_add_helm_repo(name string, url string) {
98+
helmClient := getHelmClient("default", true) // Namespace doesn't matter
99+
100+
chartRepo := repo.Entry{
101+
Name: name,
102+
URL: url,
103+
}
104+
105+
if err := helmClient.AddOrUpdateChartRepo(chartRepo); err != nil {
106+
panic(err)
107+
}
108+
}
109+
110+
func getHelmClient(namespace string, suppressOutput bool) gohelm.Client {
111+
options := gohelm.Options {
112+
Namespace: namespace,
113+
Debug: false,
114+
}
115+
116+
if suppressOutput {
117+
options.DebugLog = func(format string, v ...interface{}) {}
118+
}
119+
120+
helmClient, err := gohelm.New(&options)
121+
122+
if err != nil {
123+
panic(err)
124+
}
125+
126+
return helmClient
127+
}

go.mod

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
module go-helm-wrapper
2+
3+
go 1.20
4+
5+
require (
6+
github.com/mittwald/go-helm-client v0.12.1
7+
helm.sh/helm/v3 v3.11.3
8+
k8s.io/client-go v0.27.1
9+
)
10+
11+
require (
12+
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1 // indirect
13+
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
14+
github.com/BurntSushi/toml v1.2.1 // indirect
15+
github.com/MakeNowJust/heredoc v1.0.0 // indirect
16+
github.com/Masterminds/goutils v1.1.1 // indirect
17+
github.com/Masterminds/semver/v3 v3.2.0 // indirect
18+
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
19+
github.com/Masterminds/squirrel v1.5.3 // indirect
20+
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
21+
github.com/beorn7/perks v1.0.1 // indirect
22+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
23+
github.com/chai2010/gettext-go v1.0.2 // indirect
24+
github.com/containerd/containerd v1.7.0 // indirect
25+
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
26+
github.com/davecgh/go-spew v1.1.1 // indirect
27+
github.com/docker/cli v20.10.21+incompatible // indirect
28+
github.com/docker/distribution v2.8.1+incompatible // indirect
29+
github.com/docker/docker v20.10.24+incompatible // indirect
30+
github.com/docker/docker-credential-helpers v0.7.0 // indirect
31+
github.com/docker/go-connections v0.4.0 // indirect
32+
github.com/docker/go-metrics v0.0.1 // indirect
33+
github.com/docker/go-units v0.5.0 // indirect
34+
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
35+
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
36+
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f // indirect
37+
github.com/fatih/color v1.13.0 // indirect
38+
github.com/go-errors/errors v1.4.2 // indirect
39+
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
40+
github.com/go-logr/logr v1.2.3 // indirect
41+
github.com/go-logr/stdr v1.2.2 // indirect
42+
github.com/go-openapi/jsonpointer v0.19.6 // indirect
43+
github.com/go-openapi/jsonreference v0.20.1 // indirect
44+
github.com/go-openapi/swag v0.22.3 // indirect
45+
github.com/gobwas/glob v0.2.3 // indirect
46+
github.com/gogo/protobuf v1.3.2 // indirect
47+
github.com/golang/protobuf v1.5.3 // indirect
48+
github.com/google/btree v1.1.2 // indirect
49+
github.com/google/gnostic v0.6.9 // indirect
50+
github.com/google/go-cmp v0.5.9 // indirect
51+
github.com/google/gofuzz v1.2.0 // indirect
52+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
53+
github.com/google/uuid v1.3.0 // indirect
54+
github.com/gorilla/mux v1.8.0 // indirect
55+
github.com/gosuri/uitable v0.0.4 // indirect
56+
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
57+
github.com/hashicorp/errwrap v1.1.0 // indirect
58+
github.com/hashicorp/go-multierror v1.1.1 // indirect
59+
github.com/huandu/xstrings v1.4.0 // indirect
60+
github.com/imdario/mergo v0.3.13 // indirect
61+
github.com/inconshreveable/mousetrap v1.0.1 // indirect
62+
github.com/jmoiron/sqlx v1.3.5 // indirect
63+
github.com/josharian/intern v1.0.0 // indirect
64+
github.com/json-iterator/go v1.1.12 // indirect
65+
github.com/klauspost/compress v1.16.0 // indirect
66+
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
67+
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
68+
github.com/lib/pq v1.10.7 // indirect
69+
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
70+
github.com/mailru/easyjson v0.7.7 // indirect
71+
github.com/mattn/go-colorable v0.1.13 // indirect
72+
github.com/mattn/go-isatty v0.0.17 // indirect
73+
github.com/mattn/go-runewidth v0.0.14 // indirect
74+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
75+
github.com/mitchellh/copystructure v1.2.0 // indirect
76+
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
77+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
78+
github.com/moby/locker v1.0.1 // indirect
79+
github.com/moby/spdystream v0.2.0 // indirect
80+
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
81+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
82+
github.com/modern-go/reflect2 v1.0.2 // indirect
83+
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
84+
github.com/morikuni/aec v1.0.0 // indirect
85+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
86+
github.com/opencontainers/go-digest v1.0.0 // indirect
87+
github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b // indirect
88+
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
89+
github.com/pkg/errors v0.9.1 // indirect
90+
github.com/prometheus/client_golang v1.14.0 // indirect
91+
github.com/prometheus/client_model v0.3.0 // indirect
92+
github.com/prometheus/common v0.37.0 // indirect
93+
github.com/prometheus/procfs v0.8.0 // indirect
94+
github.com/rivo/uniseg v0.4.2 // indirect
95+
github.com/rubenv/sql-migrate v1.3.1 // indirect
96+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
97+
github.com/shopspring/decimal v1.3.1 // indirect
98+
github.com/sirupsen/logrus v1.9.0 // indirect
99+
github.com/spf13/cast v1.5.0 // indirect
100+
github.com/spf13/cobra v1.6.1 // indirect
101+
github.com/spf13/pflag v1.0.5 // indirect
102+
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
103+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
104+
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
105+
github.com/xlab/treeprint v1.1.0 // indirect
106+
go.opentelemetry.io/otel v1.14.0 // indirect
107+
go.opentelemetry.io/otel/trace v1.14.0 // indirect
108+
go.starlark.net v0.0.0-20221020143700-22309ac47eac // indirect
109+
golang.org/x/crypto v0.5.0 // indirect
110+
golang.org/x/net v0.8.0 // indirect
111+
golang.org/x/oauth2 v0.4.0 // indirect
112+
golang.org/x/sync v0.1.0 // indirect
113+
golang.org/x/sys v0.6.0 // indirect
114+
golang.org/x/term v0.6.0 // indirect
115+
golang.org/x/text v0.9.0 // indirect
116+
golang.org/x/time v0.1.0 // indirect
117+
google.golang.org/appengine v1.6.7 // indirect
118+
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
119+
google.golang.org/grpc v1.53.0 // indirect
120+
google.golang.org/protobuf v1.28.1 // indirect
121+
gopkg.in/inf.v0 v0.9.1 // indirect
122+
gopkg.in/yaml.v2 v2.4.0 // indirect
123+
gopkg.in/yaml.v3 v3.0.1 // indirect
124+
k8s.io/api v0.27.1 // indirect
125+
k8s.io/apiextensions-apiserver v0.26.3 // indirect
126+
k8s.io/apimachinery v0.27.1 // indirect
127+
k8s.io/apiserver v0.26.3 // indirect
128+
k8s.io/cli-runtime v0.26.3 // indirect
129+
k8s.io/component-base v0.26.3 // indirect
130+
k8s.io/klog/v2 v2.90.1 // indirect
131+
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a // indirect
132+
k8s.io/kubectl v0.26.0 // indirect
133+
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5 // indirect
134+
oras.land/oras-go v1.2.2 // indirect
135+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
136+
sigs.k8s.io/kustomize/api v0.12.1 // indirect
137+
sigs.k8s.io/kustomize/kyaml v0.13.9 // indirect
138+
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
139+
sigs.k8s.io/yaml v1.3.0 // indirect
140+
)

0 commit comments

Comments
 (0)