Helm go sdk install chart from external location - go

Im using the following code to install chart that is bounded in my source code (eg. in my app/chart/chart1 in my go bin app), Now I need to move the chart to git repository or to artifactory,
My question is how can I install the chart from outside my program?
This is the code I use which works for bundled chart
I use the helm3 loader package which works when I have the chart bundled in my app
chart, err := loader.Load(“chart/chart1”)
https://pkg.go.dev/helm.sh/helm/v3#v3.5.4/pkg/chart/loader
Should I load it somehow with an http call or helm have some built in functionality ? we need some efficient way to handle it

It seems that helm during its upgrade/install commands checks first a couple of different locations which you can see getting called here. The content of that function is here.
And then continues here with loader.Load

You can use something like this for installing nginx chart
myChart, err := loader.Load("https://charts.bitnami.com/bitnami/nginx-8.8.4.tgz")
...
install := action.NewInstall(m.actionConfig)
install.ReleaseName = "my-release"
...
myRelease, err := install.Run(myChart, myValues)
It would be similar to:
helm install my-release https://charts.bitnami.com/bitnami/nginx-8.8.4.tgz

loader.load checks only for files and directories. If you want to use URL helm sdk provides LocateChart method in Install interface. Here is an example:
settings := cli.New()
actionConfig := new(action.Configuration)
if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), os.Getenv("HELM_DRIVER"), log.Printf); err != nil {
log.Printf("%+v", err)
os.Exit(1)
}
client := action.NewInstall(actionConfig)
chrt_path, err := client.LocateChart("https://github.com/kubernetes/ingress-nginx/releases/download/helm-chart-4.0.6/ingress-nginx-4.0.6.tgz", settings); if err != nil {
panic(err)
}
myChart, err := loader.Load(chrt_path); if err != nil {
panic(err)
}
Then you can simple setup install options and call client.Run method.

Related

wkhtmltopdf not found - Golang Ubuntu

After trying lot of solutions for this error, I am posting this issue here. I have written a method which converts html to pdf and returns pdf bytes as output.
import (
"bytes"
"github.com/SebastiaanKlippert/go-wkhtmltopdf"
)
func HtmlToPdf(htmlData *string) ([]byte, error) {
pdfg, err := wkhtmltopdf.NewPDFGenerator()
if err != nil {
return nil, err
}
pdfg.AddPage(wkhtmltopdf.NewPageReader(bytes.NewReader([]byte(*htmlData))))
//nolint: gomnd
pdfg.Dpi.Set(600)
jb, err := pdfg.ToJSON()
if err != nil {
return nil, err
}
pdfgFromJSON, err := wkhtmltopdf.NewPDFGeneratorFromJSON(bytes.NewReader(jb))
if err != nil {
return nil, err
}
err = pdfgFromJSON.Create()
if err != nil {
return nil, err
}
pdfBytes := pdfgFromJSON.Bytes()
return pdfBytes, nil }
Calling this method returns error wkhtmltopdf not found
I have tried the following solutions
which wkhtmltopdf
/usr/local/bin/wkhtmltopdf
and then setting the WKHTMLTOPDF_PATH: /usr/local/bin/wkhtmltopdf in environment section of my code
Using setPath at the top of HtmlToPdf Method like
wkhtmltopdf.SetPath("/usr/local/bin/wkhtmltopdf")
In this case the error changes to fork/exec /usr/local/bin/wkhtmltopdf: no such file or directory
Also tried after moving the wkhtml files to /usr/local/go/bin/ and using path
/usr/local/go/bin/wkhtmltopdf
Converting any url to pdf using command line also works fine.
Note : Hitting wkhtmltopdf --version in terminal gives wkhtmltopdf 0.12.6 (with patched qt) and package is installed using go get github.com/SebastiaanKlippert/go-wkhtmltopdf
Any other solutions?
hello in my case u should install wkhtmltopdf ,but if u are using MacOS so just install on terminal
brew install wkhtmltopdf
and make sure path directory same like you installed wkhtmltopdf

How to pass YAML files while installing charts using Helm Go SDK

We are using the Go helm.sh/helm/v3/pkg/action package to install Helm charts.
We are able to pass installation values to Helm chart Run functions via map[string]interface{}, as in Samples on kubernetes helm golang client:
rel, err := client.Run(myChart, vals)
if err != nil {
log.Println(err)
panic(err)
}
But we have a values.yaml file also which was passed as -f values.yaml when installing charts from the CLI.
Is there a way to pass these values.yaml files via the action Go package during installation (client.Run())?
Or do we need to unmarshal the YAML file and pass that also as map:
data2 := make(map[string]interface{})
yfile2, err := ioutil.ReadFile("./utils/values.yaml")
fmt.Println(err)
err = yaml.Unmarshal(yfile2, &data2)
One straightforward thing to do could be to reuse the helm.sh/helm/v3/pkg/cli/values package. This has the logic to handle the -f option (and also --set and its variants) and return a unified map[string]interface{}. This could look like:
import (
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/cli/values"
"helm.sh/helm/v3/pkg/getter"
)
envSettings := cli.New()
providers := getter.All(envSettings)
options := values.Options{
ValueFiles: []string{"./utils/values.yaml"},
}
theValues := options.MergeValues(providers)
Now theValues is the map[string]interface{} that results from reading those files. You can customize the values further if required (as Go data) and then pass that to install.Run().

kubectl get pod <pod> -n <namespace> -o yaml in kubernetes client-go

Now i have Pods as Kubernetes structs wiht the help of the command
pods , err := clientset.CoreV1().Pods("namespace_String").List(context.TODO(), metav1.ListOptions{})
now i do i get it as individual yaml files
which command should i use
for i , pod := range pods.Items{
if i==0{
t := reflect.TypeOF(&pod)
for j := 0; j<t.NumMethod(); j++{
m := t.Method(j)
fmt.Println(m.Name)
}
}
}
this function will print the list of functions in the pod item which should i use
Thanks for the answer
The yaml is just a representation of the Pod object in the kubernetes internal storage in etcd. With your client-go what you have got is the Pod instance, of the type v1.Pod. So you should be able to work with this object itself and get whatever you want, for example p.Labels() etc. But if for some reason, you are insisting on getting a yaml, you can do that via:
import (
"sigs.k8s.io/yaml"
)
b, err := yaml.Marshal(pod)
if err != nil {
// handle err
}
log.Printf("Yaml of the pod is: %q", string(b))
Note that yaml library coming here is not coming from client-go library. The documentation for the yaml library can be found in: https://pkg.go.dev/sigs.k8s.io/yaml#Marshal
Instead of yaml if you want to use json, you can simply use the Marshal function https://pkg.go.dev/k8s.io/apiserver/pkg/apis/example/v1#Pod.Marshal provided by the v1.Pod struct itself, like any other Go object.
To get individual pod using client-go:
pod, err := clientset.CoreV1().Pods("pod_namespace").Get(context.TODO(),"pod_name", metav1.GetOptions{})
if err!=nil {
log.Fatalln(err)
}
// do something with pod

Is it possible to get executable file icon by Golang?

I'm trying to write a "Download Page Website", and I trying to show the file icon to my webpage.
Like Windows system, ".exe" file has icon image inside. Or linux executable file. Can I read it?
I know python can do it with a package name "win32api", is it any package for Golang to achieve this function?
You can use the linux package in your advantage.
For example, you can use icoextract, which can be installed via apt:
apt install icoextract
And then run it like this:
icoextract /path/to/file.exe /path/to/file.ico
Go make possible to call commands and execute them using the package os/exec. So you can do something like
func ExtractIcon(executablePath string) []byte {
file, err := ioutil.TempFile("dir", "prefix")
if err != nil {
log.Fatal(err)
}
defer os.Remove(file.Name())
cmd := exec.Command("icoextract", executablePath, file.Name())
if err = cmd.Run(); err != nil {
log.Fatal(err)
}
content, _ := ioutil.ReadFile(file.Name())
return content
}

can't upload file to google cloud storage in production

I'm following this documentation to upload files to GCS.
Setting Up Authentication for Server to Server Production Applications
It works in local but in production i get this error:
Post https://www.googleapis.com/upload/storage/v1/b/[bucket-name]/o?alt=json&prettyPrint=false&projection=full&uploadType=multipart: x509: failed to load system roots and no roots provided.
func UploadIMG(ctx *context.Context, file []byte, fileName string) error {
storageClient, err := storage.NewClient(*ctx)
if err != nil {
log.Fatal(err)
}
w := storageClient.Bucket(bucketName).Object(fileName).NewWriter(*ctx)
if _, err := w.Write(file); err != nil {return err}
if err := w.Close(); err != nil {return err}
oauthClient, err := google.DefaultClient(*ctx, cloudkms.CloudPlatformScope)
if err != nil {
log.Fatal(err)
}
kmsService, err := cloudkms.New(oauthClient)
if err != nil {
log.Fatal(err)
}
_ = kmsService
return nil
}
Did you actually continue with the tutorial you linked, making sure you have the correct credentials?
The error itself is likely certificate related. When it tries to do the request, it looks for root certificates on the underlying system but can't find them or open them. On for example Ubuntu, they should be under /usr/share/ca-certificates and/or /etc/ssl/certs . Make sure you have your certificates with correct privileges in order to be able to do the request you want.
as guys said in their answers, it's related to missing Certificate Authority in my dockerFile.
In my case, in alpine there is already a package utility called ca-certificates which comes with its preinstalled certs. Just needed to add the following command to my docker.
RUN apk --no-cache add ca-certificates
For any google api's you will need a root CA of trust.
Not sure of your production environment, but if you are using Docker add this line to your Dockerfile:
COPY /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
from, say a Linux build, you can see the order go will look to find the system root trust files:
https://golang.org/src/crypto/x509/root_linux.go
"/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc.
"/etc/pki/tls/certs/ca-bundle.crt", // Fedora/RHEL 6
"/etc/ssl/ca-bundle.pem", // OpenSUSE
"/etc/pki/tls/cacert.pem", // OpenELEC
"/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", // CentOS/RHEL 7
If you do not have any of these directories in your production (linux) build, then go will have no system root trust, and you will get the error you are seeing.

Resources