Skip to content

Commit 0846af8

Browse files
nitishkumar71alexellis
authored andcommitted
re-structrue logs
Signed-off-by: Nitishkumar Singh <[email protected]>
1 parent 4af1e4c commit 0846af8

File tree

14 files changed

+610
-409
lines changed

14 files changed

+610
-409
lines changed

example/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### Examples
2+
Folder containes examples about how to use [openfaas](https://www.openfaas.com/) `go-sdk` API for different supported operations. Each subfolder contains a `main.go` file which can be executed independently. Before running them, please expose relevant environment variables such as `OPENFAAS_USERNAME`, `OPENFAAS_PASSWORD` and `OPENFAAS_GATEWAY_URL`. Do note, these are not standard environment variables. You can change them in your usecase.
3+
4+
Below is list of examples
5+
1. [Deploy Function](./deploy-function/main.go)
6+
2. [Update Function](./update-function/main.go)
7+
3. [Scale Function](./scale-function/main.go)
8+
4. [Get All Functions Of A Namespace](./get-functions/main.go)
9+
5. [Create Namespace](./create-namespace/main.go)
10+
6. [Update Namespace](./update-namespace/main.go)
11+
7. [Get All Namespaces](./get-namepsaces/main.go)
12+
8. [Create Secret](./create-secret/main.go)
13+
9. [Update Secret](./update-secret/main.go)
14+
10. [Get Logs Of A Function](./logs/main.go)
15+
16+
17+

example/create-namespace/main.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
"net/url"
9+
"os"
10+
"time"
11+
12+
"github.com/openfaas/faas-provider/types"
13+
"github.com/openfaas/go-sdk"
14+
)
15+
16+
func main() {
17+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
18+
username := os.Getenv("OPENFAAS_USERNAME")
19+
password := os.Getenv("OPENFAAS_PASSWORD")
20+
21+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
22+
auth := &sdk.BasicAuth{
23+
Username: username,
24+
Password: password,
25+
}
26+
27+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
28+
29+
status, err := client.CreateNamespace(context.Background(), types.FunctionNamespace{
30+
Name: "test-namespace",
31+
Labels: map[string]string{
32+
"env": "dev",
33+
},
34+
Annotations: map[string]string{
35+
"imageregistry": "https://hub.docker.com/",
36+
},
37+
})
38+
// non 200 status value will have some error
39+
if err != nil {
40+
log.Printf("Status: %d Create Failed: %s", status, err)
41+
}
42+
43+
fmt.Println("Wait for 15 seconds....")
44+
fmt.Println("Get Namespace")
45+
time.Sleep(15 * time.Second)
46+
ns, err := client.GetNamespace(context.Background(), "test-namespace")
47+
if err != nil {
48+
log.Printf("Get Failed: %s", err)
49+
}
50+
fmt.Printf("Namespace: %v \n", ns)
51+
52+
// delete namespace
53+
err = client.DeleteNamespace(context.Background(), "test-namespace")
54+
// non 200 status value will have some error
55+
if err != nil {
56+
log.Printf("Delete Failed: %s", err)
57+
}
58+
}

example/create-secret/main.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
"net/url"
9+
"os"
10+
11+
"github.com/openfaas/faas-provider/types"
12+
"github.com/openfaas/go-sdk"
13+
)
14+
15+
func main() {
16+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
17+
username := os.Getenv("OPENFAAS_USERNAME")
18+
password := os.Getenv("OPENFAAS_PASSWORD")
19+
20+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
21+
auth := &sdk.BasicAuth{
22+
Username: username,
23+
Password: password,
24+
}
25+
26+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
27+
28+
status, err := client.CreateSecret(context.Background(), types.Secret{
29+
Name: "env-store-test",
30+
Namespace: "openfaas-fn",
31+
// secret support both binary and string values
32+
// Use Value field to store string values
33+
RawValue: []byte("this is secret"),
34+
})
35+
// non 200 status value will have some error
36+
if err != nil {
37+
log.Printf("Status: %d Create Failed: %s", status, err)
38+
}
39+
40+
// Get Secrets
41+
secrets, err := client.GetSecrets(context.Background(), "openfaas-fn")
42+
// non 200 status value will have some error
43+
if err != nil {
44+
log.Printf("Get Failed: %s", err)
45+
}
46+
47+
for _, s := range secrets {
48+
fmt.Printf("Secret: %v \n", s)
49+
}
50+
51+
err = client.DeleteSecret(context.Background(), "env-store-test", "openfaas-fn")
52+
// non 200 status value will have some error
53+
if err != nil {
54+
log.Printf("Delete Failed: %s", err)
55+
}
56+
57+
}

example/deploy-function/main.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
"net/url"
9+
"os"
10+
"time"
11+
12+
"github.com/openfaas/faas-provider/types"
13+
"github.com/openfaas/go-sdk"
14+
)
15+
16+
func main() {
17+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
18+
username := os.Getenv("OPENFAAS_USERNAME")
19+
password := os.Getenv("OPENFAAS_PASSWORD")
20+
21+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
22+
auth := &sdk.BasicAuth{
23+
Username: username,
24+
Password: password,
25+
}
26+
27+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
28+
29+
status, err := client.Deploy(context.Background(), types.FunctionDeployment{
30+
Service: "env-store-test",
31+
Image: "ghcr.io/openfaas/alpine:latest",
32+
Namespace: "openfaas-fn",
33+
EnvProcess: "env",
34+
Labels: &map[string]string{
35+
"purpose": "test",
36+
},
37+
})
38+
// non 200 status value will have some error
39+
if err != nil {
40+
log.Printf("Status: %d Deploy Failed: %s", status, err)
41+
}
42+
43+
fmt.Println("Wait for 15 seconds....")
44+
fmt.Println("Get Function")
45+
time.Sleep(15 * time.Second)
46+
fn, err := client.GetFunction(context.Background(), "env-store-test", "openfaas-fn")
47+
if err != nil {
48+
log.Printf("Get Failed: %s", err)
49+
}
50+
fmt.Printf("Function: %v \n", fn)
51+
52+
// delete function
53+
err = client.DeleteFunction(context.Background(), "env-store-test", "openfaas-fn")
54+
// non 200 status value will have some error
55+
if err != nil {
56+
log.Printf("Delete Failed: %s", err)
57+
}
58+
}

example/get-functions/main.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
"net/url"
9+
"os"
10+
11+
"github.com/openfaas/faas-provider/types"
12+
"github.com/openfaas/go-sdk"
13+
)
14+
15+
func main() {
16+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
17+
username := os.Getenv("OPENFAAS_USERNAME")
18+
password := os.Getenv("OPENFAAS_PASSWORD")
19+
20+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
21+
auth := &sdk.BasicAuth{
22+
Username: username,
23+
Password: password,
24+
}
25+
26+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
27+
28+
fns, err := client.GetFunctions(context.Background(), "openfaas-fn")
29+
if err != nil {
30+
log.Printf("Get Failed: %s", err)
31+
}
32+
fmt.Printf("No Of Functions: %d\n", len(fns))
33+
34+
status, err := client.Deploy(context.Background(), types.FunctionDeployment{
35+
Service: "env-store-test",
36+
Image: "ghcr.io/openfaas/alpine:latest",
37+
Namespace: "openfaas-fn",
38+
EnvProcess: "env",
39+
Labels: &map[string]string{
40+
"purpose": "test",
41+
},
42+
})
43+
// non 200 status value will have some error
44+
if err != nil {
45+
log.Printf("Status: %d Deploy Failed: %s", status, err)
46+
}
47+
48+
fns, err = client.GetFunctions(context.Background(), "openfaas-fn")
49+
if err != nil {
50+
log.Printf("Get Failed: %s", err)
51+
}
52+
fmt.Printf("No Of Functions: %d\n", len(fns))
53+
54+
err = client.DeleteFunction(context.Background(), "env-store-test", "openfaas-fn")
55+
// non 200 status value will have some error
56+
if err != nil {
57+
log.Printf("Delete Failed: %s", err)
58+
}
59+
}

example/get-namepsaces/main.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
"net/url"
9+
"os"
10+
11+
"github.com/openfaas/faas-provider/types"
12+
"github.com/openfaas/go-sdk"
13+
)
14+
15+
func main() {
16+
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
17+
username := os.Getenv("OPENFAAS_USERNAME")
18+
password := os.Getenv("OPENFAAS_PASSWORD")
19+
20+
gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
21+
auth := &sdk.BasicAuth{
22+
Username: username,
23+
Password: password,
24+
}
25+
26+
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
27+
28+
ns, err := client.GetNamespaces(context.Background())
29+
if err != nil {
30+
log.Printf("Get Failed: %s", err)
31+
}
32+
fmt.Printf("No Of Namespaces: %d\n", len(ns))
33+
34+
status, err := client.CreateNamespace(context.Background(), types.FunctionNamespace{
35+
Name: "test-namespace",
36+
})
37+
// non 200 status value will have some error
38+
if err != nil {
39+
log.Printf("Status: %d Create Failed: %s", status, err)
40+
}
41+
42+
ns, err = client.GetNamespaces(context.Background())
43+
if err != nil {
44+
log.Printf("Get Failed: %s", err)
45+
}
46+
fmt.Printf("No Of Namespaces: %d\n", len(ns))
47+
48+
// delete namespace
49+
err = client.DeleteNamespace(context.Background(), "test-namespace")
50+
// non 200 status value will have some error
51+
if err != nil {
52+
log.Printf("Delete Failed: %s", err)
53+
}
54+
}

examples/logs_example.go renamed to example/logs/main.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package examples
1+
package main
22

33
import (
44
"context"
@@ -9,11 +9,11 @@ import (
99
"os"
1010
"time"
1111

12+
"github.com/openfaas/faas-provider/types"
1213
"github.com/openfaas/go-sdk"
1314
)
1415

15-
func GetLogs() {
16-
16+
func main() {
1717
// NOTE: You can have any name for environment variables. below defined variables names are not standard names
1818
username := os.Getenv("OPENFAAS_USERNAME")
1919
password := os.Getenv("OPENFAAS_PASSWORD")
@@ -26,6 +26,18 @@ func GetLogs() {
2626

2727
client := sdk.NewClient(gatewayURL, auth, http.DefaultClient)
2828

29+
// Deploy function
30+
status, err := client.Deploy(context.Background(), types.FunctionDeployment{
31+
Service: "env-store-test",
32+
Image: "ghcr.io/openfaas/alpine:latest",
33+
Namespace: "openfaas-fn",
34+
EnvProcess: "env",
35+
})
36+
// non 200 status value will have some error
37+
if err != nil {
38+
log.Printf("Status: %d Deploy Failed: %s", status, err)
39+
}
40+
2941
// Follow is allows the user to request a stream of logs until the timeout
3042
follow := false
3143
// Tail sets the maximum number of log messages to return, <=0 means unlimited
@@ -42,5 +54,4 @@ func GetLogs() {
4254
for line := range logsChan {
4355
fmt.Println(line)
4456
}
45-
4657
}

0 commit comments

Comments
 (0)