Kubernetes go client: list events - go

I am trying to get a list of events in the namespace, but with or without FieldSelector I get an empty list. Is this the right way to do it?
eventListOptions := metav1.ListOptions{FieldSelector: fields.OneTermEqualSelector("involvedObject.name", job.Name).String()}
jobEvents, _ := clientset.EventsV1beta1().Events(GetNamespace()).List(eventListOptions)

If you print error return by List, you should get error something like "involvedObject.name" is not a known field selector: only "metadata.name", "metadata.namespace"
use CoreV1 instead of EventsV1beta1
The line will be something like below:
jobEvents, _ := clientset.CoreV1().Events(GetNamespace()).List(eventListOptions)
"involvedObject.name", job.Name isn't supported by EventsV1beta1
Hope it'll help.

Related

how to filter pods against multiple label selectors with terratest?

I have a code that uses k8s module of terratest to list pods based on label selector.
pods, err := k8s.ListPodsE(
t,
k8soptions,
filter,
)
where the filter is initialized with a string labelSelector like this,
filter := metav1.ListOptions{
LabelSelector: "kubeslice.io/app=foo",
}
So if I want to filter pods against more than one labels, say pods which has bothkubeslice.io/pod-type=gateway & kubeslice.io/app=foo, how can I achieve that in this method ?
As pointed out by doublethink and I tested, we could provide a comma separated string of label selectors like this.
filter := metav1.ListOptions{
LabelSelector: "kubeslice.io/app=foo,kubeslice.io/app=bar",
}

Go template - syntax for range

In Go template, I have a map setup like this:
{{$key}}map := make(map[string]interface{})
And I want to iterate through the map using this:
{{ range $mapKey, $mapValue := {{$key}}map}}
And I am getting this error:
unexpected "{" in range
Looks like it does not allow nested {{}} inside another {{}}. Is there anyway I can solve this issue ???
You cannot generate variable names to be used in templates using the template engine itself. You seem to be in need of having multiple maps, one for each $key. So, use a map of maps:
m := make(map[string]map[string]interface{})
where m[key] gives the map for the key.
Then you can do:
{{ range $mapKey, $mapValue := (index $.m $.key)}}
...
{{end}}

Programmatically set an url parameter in gin context for testing purpose

I am writing some test suites for gin middlewares. I found a solution to test them without having to run a full router engine, by creating a gin context like this :
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
The goal is to test my function by calling :
MyMiddleware(c)
// Then I use c.MustGet() to check if every expected parameter has been transmitted to gin
// context, with correct values.
One of my middlewares relies on c.Param(). Is it possible to programatically set an Url param in gin (something like c.SetParam(key, value)) before calling the middleware ? This is only for test purpose so I don't mind non-optimized solutions.
Finally figured it out by using IntelliJ to inspect the structure, I can just set it the raw way :
c.Params = []gin.Param{
{
Key: "id",
Value: "first document",
},
}
I was not able to get the accepted answer to work due to the c.Request.URL being nil in some of my tests.
Instead, you can set the query like this:
c.Request.URL, _ = url.Parse("?id=mock")

How to delete a document found by a search in the FaunaDB?

I can get a searched document as following golang code:
ret, err := client.Query(f.Get(f.MatchTerm(f.Index("label_search_by_externalID"), externalID)))
Then, I tried to delete a searched document as similar manner as follow:
ret, err := client.Query(f.Delete(f.MatchTerm(f.Index("label_search_by_externalID"), externalID)))
But, this code occurs an error:
Response error 400. Errors: [delete](invalid argument): Ref expected, Set provided.
I'm confused, by the API document, both Get and Delete request Ref for a document as param, and MatchTerm returns a Set, not Ref.
Then I have 2 questions.
How can I get the document Ref which the result of the search?
Is there any way to get the document ref from the search result with Index like RefCollection for collection, for example like RefIndex as follows?
ret, err := client.Query(f.Delete(f.RefIndex(f.Index("label_search_by_externalID"), externalID)))
Why my code for getting search result document works well? Is there more good coding for getting search result document?
Thank you for your suggestion!
Get will only return 1 result. If you have multiple values returned from this index search you will have problems. I would suggest the function Paginate(). This will return a set of results, which you can then map over and execute other functionality, such as a get() or delete(). I would strong suggest you look at the following tutorial (https://docs.fauna.com/fauna/current/tutorials/indexes/pagination).
This is pseudo code to delete a page of documents found. You can adjust the page size either up or down:
Map(
Paginate(Match(Index("label_search_by_externalID"), true)),
Lambda("X", Delete(Var("X")))
)

GoREST endpoint path

I'm writting a web service with Go and I'd like to have url like :
http://example.com/WEB/service.wfs?param1=2&param2=test.....
I'm using GoREST and my Endpoint url is :
method:"GET" path:"/WEB/service.wfs?{param:string}" output:"string"
My problem is that it never return the "param" but it does if I use the endpoint :
method:"GET" path:"/WEB/service.wfs/{param:string}" output:"string"
Is there a way to handle the "?" ?
You can do this in gorest though it's not as nice as gorest's preferred mechanism.
Don't include your query parameters in your endpoint definition
method:"GET" path:"/WEB/service.wfs" output:"string"
Instead, you can get at the context from your registered end point and get the query parameters using something like
func (serv MyService) HelloWorld() (result string) {
r := serv.Context.Request()
u, _ := url.Parse(r.URL.String())
q := u.Query()
result = "Buono estente " + q["hi"][0]
return
}
I have had a look at the GoREST package you are using and can not see any way of doing this.
I have always used gorillatoolkit pat package.
gorillatoolkit
There is an example of what you want to do about half way down.
category := req.URL.Query().Get(":category")
This way you can get the query parameters on the request URL by the key.
Hope this helps.

Resources