List ClusterServiceVersions using K8S Go client - go

I'm trying to use the K8S Go client to list the ClusterServiceVersions.
It could be enough to have the raw response body.
I've tried this:
data, err := clientset.RESTClient().Get().Namespace(namespace).
Resource("ClusterServiceVersions").
DoRaw(context.TODO())
if err != nil {
panic(err.Error())
}
fmt.Printf("%v", string(data))
But it returns the following error:
panic: the server could not find the requested resource (get ClusterServiceVersions.meta.k8s.io)
How do I specify to use the operators.coreos.com group?
Looking at some existing code I've also tried to add
VersionedParams(&v1.ListOptions{}, scheme.ParameterCodec)
But it result in this other error:
panic: v1.ListOptions is not suitable for converting to "meta.k8s.io/v1" in scheme "pkg/runtime/scheme.go:100"

It is possible to do a raw request using the AbsPath() method.
path := fmt.Sprintf("/apis/operators.coreos.com/v1alpha1/namespaces/%s/clusterserviceversions", namespace)
data, err := clientset.RESTClient().Get().
AbsPath(path).
DoRaw(ctx)
Also notice that if you want to define clientset using the interface (kubernetes.Interface) instead of the concrete type (*kubernetes.Clientset) the method clientset.RESTClient() is not directly accessible, but you can use the following one:
clientset.Discovery().RESTClient()

Related

I add file to my API and got invalid character '-' in numeric literal in POST API

I know this code need to send a JSON instead of form data in the API
err := ctx.ShouldBindJSON(&modelAdd)
if err != nil {
return err
}
But I need to add file, is there anything like ShouldBindJSON but for FormData?
You can use ShouldBind to get data from form data as the documentation says
https://github.com/gin-gonic/gin#model-binding-and-validation

Is it possible to parse protobuf binary using descriptorpb.DescriptorProto

The use case is parsing idls in service A and parsing protobuf messages to json in service B.
Using jhump.protoreflect to parse idl and get desc.MessageDescriptor, then use protojson to marshal descriptorpb.DescriptorProto
h, _ := protojson.Marshal(md.Descriptor.AsDescriptorProto())
Using protojson to unmarshal h and get DescriptorProto
var z descriptorpb.ç
if err := protojson.Unmarshal(h, &z); err != nil {
fmt.Println(err.Error())
}
How to parse message by using DescriptorProto?
If I'm doing the whole work in the same service, I'm able to parse the message by using MessageDescriptor
dm := dynamic.NewMessage(md)
dm.Unmarshal(body)
dm.MarshalJSONPB(&jsonpb.Marshaler{OrigName: true, EnumsAsInts: true})
but the issue is parsing message is done in a different service, so I'm looking to keep some "message descriptor" in database and can be used by service B.
Any suggestion?

Unable to get mp4 tkhd info using go-mp4

Using package github.com/abema/go-mp4 when i try to run the example in the docs i get following error:
invalid operation: box (variable of type *mp4.BoxInfo) is not an interface
Here is the example that i am trying:
// extract specific boxes
boxes, err := mp4.ExtractBox(file, nil, mp4.BoxPath{mp4.BoxTypeMoov(), mp4.BoxTypeTrak(), mp4.BoxTypeTkhd()})
if err != nil {
:
}
for _, box := range boxes {
tkhd := box.(*mp4.Tkhd)
fmt.Println("track ID:", tkhd.TrackID)
}
https://pkg.go.dev/github.com/abema/go-mp4#section-readme
Sorry, it is an error of my sample code.
I created Pull Request to fix it.
https://github.com/abema/go-mp4/pull/113/files
When you find any other problems about go-mp4, I would be grateful if you could create new Issue on GitHub.
https://github.com/abema/go-mp4/issues

Save error in database Golang

I'm running http request in golang
resp, err := client.Do(req)
if err != nil {
return "", err
}
So, it returns error back to the main function, that attempts to store it in the database:
_, err = db.Exec("UPDATE test SET error = $1 WHERE id = $2", error, id)
I receive the following error: sql: converting Exec argument #1's type: unsupported type errors.errorString, a struct exit status 1
So, I understand, that error has a different type, but I can't find information on how to pass the value of the error to the string. Could someone direct me in a right way.
Use the function:
error.Error()
to get a string representation of the error.
Tip: avoid naming variables with existing type names. error is a type name and it's also your variable name, which might lead to confusion.

Golang: panic: runtime error: invalid memory address or nil pointer dereference

When uploading a file to my go app, I encounter a panic.
panic: runtime error: invalid memory address or nil pointer dereference
/Users/bob/Projects/go/src/github.com/zenazn/goji/web/middleware/recoverer.go:24 (0xbaf5b)
func.006: debug.PrintStack()
/usr/local/go/src/pkg/runtime/panic.c:248 (0x1043d)
panic: runtime·newstackcall(d->fn, (byte*)d->args, d->siz);
/usr/local/go/src/pkg/runtime/panic.c:552 (0x10eed)
panicstring: runtime·panic(err);
/usr/local/go/src/pkg/runtime/os_darwin.c:454 (0xfb8e)
sigpanic: runtime·panicstring("invalid memory address or nil pointer dereference");
/usr/local/go/src/pkg/mime/multipart/multipart.go:223 (0xb6801)
(*Reader).NextPart: if r.currentPart != nil {
/Users/bob/Projects/go/src/github.com/app/controllers/company_sheet_controller.go:32 (0x2ee18)
NewCompanySheet: part, err := mr.NextPart()
/usr/local/go/src/pkg/net/http/server.go:1235 (0x44f00)
HandlerFunc.ServeHTTP: f(w, r)
/Users/bob/Projects/go/src/github.com/zenazn/goji/web/router.go:113 (0x6bc0a)
This method handles an upload from a multipart form, extracting the file contents and boundary data. The r.FormFile method on request is used to set file and header. And in order to pull the additional data from the post, I use r.MultipartReader. From the error description I see r is already declared as ParseMultipartForm when using r.FormFile. When executing the function with the different request methods individually, I receive no errors. r.FormFile and r.MultipartReader work fine isolated. Am I unable to mix the two request methods?
func Upload(r *http.Request) {
file, header, err := r.FormFile("file")
ErrorCheck(err)
mr, err := r.MultipartReader()
ErrorCheck(err)
part, err := mr.NextPart()
ErrorCheck(err)
var b bytes.Buffer
io.CopyN(&b, part, int64(1<<20))
fmt.Println(b.String())
defer file.Close()
}
You are calling FormFile() at the beginning of your function.
This calls ParseMultipartForm() (see Request.FormFile) which populates the MultipartForm field of your http.Request.
Now the documentation for MultipartReader() states that you should use MultipartReader() instead of ParseMultipartForm() if you want to process the data as stream.
Looking at the source MultipartReader() returns an error if the MultipartForm field was already set.
So to answer your question: No, you can't use both functions for the same request.
Also your
defer file.Close()
should be right after you checked for an error from FormFile(), otherwise the file won't be closed before Garbage Collection, when your function panics.

Resources