-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement namespace support using go-sdk
Signed-off-by: Nitishkumar Singh <[email protected]>
- Loading branch information
1 parent
d12888b
commit 76053b3
Showing
28 changed files
with
1,781 additions
and
800 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) OpenFaaS Author(s) 2023. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
package commands | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
// Setup flags that are used by multiple commands (variables defined in faas.go) | ||
namespaceCmd.PersistentFlags().StringVarP(&gateway, "gateway", "g", defaultGateway, "Gateway URL starting with http(s)://") | ||
namespaceCmd.PersistentFlags().BoolVar(&tlsInsecure, "tls-no-verify", false, "Disable TLS validation") | ||
namespaceCmd.PersistentFlags().StringVarP(&token, "token", "k", "", "Pass a JWT token to use instead of basic auth") | ||
|
||
faasCmd.AddCommand(namespaceCmd) | ||
} | ||
|
||
var namespaceCmd = &cobra.Command{ | ||
Use: `namespace [--gateway GATEWAY_URL] [--tls-no-verify] [--token JWT_TOKEN]`, | ||
Aliases: []string{"ns"}, | ||
Short: "Manage OpenFaaS namespace", | ||
Long: "Manage OpenFaaS namespace either on local or remote gateway", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) OpenFaaS Author(s) 2023. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
package commands | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/openfaas/faas-cli/util" | ||
"github.com/openfaas/faas-provider/types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// NamespaceCreateFlags holds flags that are to be added to commands. | ||
type NamespaceCreateFlags struct { | ||
labelOpts []string | ||
annotationOpts []string | ||
} | ||
|
||
var namespaceCreateFlags NamespaceCreateFlags | ||
|
||
var namespaceCreateCmd = &cobra.Command{ | ||
Use: `create NAMESPACE_NAME | ||
[--label LABEL=VALUE ...] | ||
[--annotation ANNOTATION=VALUE ...]`, | ||
Short: "Create a new namespace", | ||
Long: "Create command creates a new namespace", | ||
Example: `faas-cli namespace create namespace-name | ||
faas-cli namespace create namespace-name --label demo=true | ||
faas-cli namespace create namespace-name --annotation demo=true | ||
faas-cli namespace create namespace-name --label demo=true --annotation demo=true --gateway=http://127.0.0.1:8080`, | ||
RunE: createNamespace, | ||
PreRunE: preCreateNamespace, | ||
} | ||
|
||
func init() { | ||
namespaceCreateCmd.Flags().StringArrayVarP(&namespaceCreateFlags.labelOpts, "label", "l", []string{}, "Set one or more label (LABEL=VALUE)") | ||
namespaceCreateCmd.Flags().StringArrayVarP(&namespaceCreateFlags.annotationOpts, "annotation", "", []string{}, "Set one or more annotation (ANNOTATION=VALUE)") | ||
|
||
namespaceCmd.AddCommand(namespaceCreateCmd) | ||
} | ||
|
||
func preCreateNamespace(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
return fmt.Errorf("namespace name required") | ||
} | ||
|
||
if len(args) > 1 { | ||
return fmt.Errorf("too many values for namespace name") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func createNamespace(cmd *cobra.Command, args []string) error { | ||
client, err := GetDefaultSDKClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
labels, err := util.ParseMap(namespaceCreateFlags.labelOpts, "labels") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
annotations, err := util.ParseMap(namespaceCreateFlags.annotationOpts, "annotations") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req := types.FunctionNamespace{ | ||
Name: args[0], | ||
Labels: labels, | ||
Annotations: annotations, | ||
} | ||
|
||
fmt.Printf("Creating Namespace: %s\n", req.Name) | ||
_, err = client.CreateNamespace(context.Background(), req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Namespace %s Created!\n", req.Name) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package commands | ||
|
||
import "testing" | ||
|
||
func Test_preCreateNamespace_NoArgs_Fails(t *testing.T) { | ||
res := preCreateNamespace(nil, []string{}) | ||
|
||
want := "namespace name required" | ||
if res.Error() != want { | ||
t.Errorf("want %q, got %q", want, res.Error()) | ||
} | ||
} | ||
|
||
func Test_preCreateNamespace_MoreThan1Arg_Fails(t *testing.T) { | ||
res := preCreateNamespace(nil, []string{ | ||
"secret1", | ||
"secret2", | ||
}) | ||
|
||
want := "too many values for namespace name" | ||
if res.Error() != want { | ||
t.Errorf("want %q, got %q", want, res.Error()) | ||
} | ||
} | ||
|
||
func Test_preCreateNamespace_ExtactlyOneArgIsFine(t *testing.T) { | ||
res := preCreateNamespace(nil, []string{ | ||
"namespace1", | ||
}) | ||
|
||
if res != nil { | ||
t.Errorf("expected no validation error, but got %q", res.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) OpenFaaS Author(s) 2023. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
package commands | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var namespaceDeleteCmd = &cobra.Command{ | ||
Use: `delete NAMESPACE_NAME`, | ||
Short: "Delete existing namespace", | ||
Long: "Delete existing namespace", | ||
Example: `faas-cli namespace delete namespace-name | ||
faas-cli namespace delete namespace-name --gateway=http://127.0.0.1:8080`, | ||
RunE: deleteNamespace, | ||
PreRunE: preDeleteNamespace, | ||
} | ||
|
||
func init() { | ||
namespaceCmd.AddCommand(namespaceDeleteCmd) | ||
} | ||
|
||
func preDeleteNamespace(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
return fmt.Errorf("namespace name required") | ||
} | ||
|
||
if len(args) > 1 { | ||
return fmt.Errorf("too many values for namespace name") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func deleteNamespace(cmd *cobra.Command, args []string) error { | ||
client, err := GetDefaultSDKClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ns := args[0] | ||
|
||
fmt.Printf("Deleting Namespace: %s\n", ns) | ||
err = client.DeleteNamespace(context.Background(), ns) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Namespace %s Deleted!\n", ns) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package commands | ||
|
||
import "testing" | ||
|
||
func Test_preDeleteNamespace_NoArgs_Fails(t *testing.T) { | ||
res := preDeleteNamespace(nil, []string{}) | ||
|
||
want := "namespace name required" | ||
if res.Error() != want { | ||
t.Errorf("want %q, got %q", want, res.Error()) | ||
} | ||
} | ||
|
||
func Test_preDeleteNamespace_MoreThan1Arg_Fails(t *testing.T) { | ||
res := preDeleteNamespace(nil, []string{ | ||
"secret1", | ||
"secret2", | ||
}) | ||
|
||
want := "too many values for namespace name" | ||
if res.Error() != want { | ||
t.Errorf("want %q, got %q", want, res.Error()) | ||
} | ||
} | ||
|
||
func Test_preDeleteNamespace_ExtactlyOneArgIsFine(t *testing.T) { | ||
res := preDeleteNamespace(nil, []string{ | ||
"namespace1", | ||
}) | ||
|
||
if res != nil { | ||
t.Errorf("expected no validation error, but got %q", res.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) OpenFaaS Author(s) 2023. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
package commands | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/openfaas/faas-provider/types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var namespaceGetCmd = &cobra.Command{ | ||
Use: `get NAMESPACE_NAME`, | ||
Short: "Get existing namespace", | ||
Long: "Get existing namespace", | ||
Example: `faas-cli namespace get namespace-name | ||
faas-cli namespace get namespace-name | ||
faas-cli namespace get namespace-name --gateway=http://127.0.0.1:8080`, | ||
RunE: get_namespace, | ||
PreRunE: preGetNamespace, | ||
} | ||
|
||
func init() { | ||
namespaceCmd.AddCommand(namespaceGetCmd) | ||
} | ||
|
||
func preGetNamespace(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
return fmt.Errorf("namespace name required") | ||
} | ||
|
||
if len(args) > 1 { | ||
return fmt.Errorf("too many values for namespace name") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func get_namespace(cmd *cobra.Command, args []string) error { | ||
client, err := GetDefaultSDKClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ns := args[0] | ||
|
||
fmt.Printf("Getting Namespace: %s\n", ns) | ||
res, err := client.GetNamespace(context.Background(), ns) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
printNamespaceDetail(res) | ||
|
||
return nil | ||
} | ||
|
||
func printNamespaceDetail(nsDetail types.FunctionNamespace) { | ||
fmt.Printf("Name:\t%s\n", nsDetail.Name) | ||
if len(nsDetail.Labels) > 1 { | ||
fmt.Printf("Labels %v\n", nsDetail.Labels) | ||
} else { | ||
fmt.Printf("Labels %v\n", map[string]string{}) | ||
} | ||
if len(nsDetail.Annotations) > 1 { | ||
fmt.Printf("Annotations %v\n", nsDetail.Annotations) | ||
} else { | ||
fmt.Printf("Annotations %v\n", map[string]string{}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package commands | ||
|
||
import "testing" | ||
|
||
func Test_preGetNamespace_NoArgs_Fails(t *testing.T) { | ||
res := preGetNamespace(nil, []string{}) | ||
|
||
want := "namespace name required" | ||
if res.Error() != want { | ||
t.Errorf("want %q, got %q", want, res.Error()) | ||
} | ||
} | ||
|
||
func Test_preGetNamespace_MoreThan1Arg_Fails(t *testing.T) { | ||
res := preGetNamespace(nil, []string{ | ||
"secret1", | ||
"secret2", | ||
}) | ||
|
||
want := "too many values for namespace name" | ||
if res.Error() != want { | ||
t.Errorf("want %q, got %q", want, res.Error()) | ||
} | ||
} | ||
|
||
func Test_preGetNamespace_ExtactlyOneArgIsFine(t *testing.T) { | ||
res := preGetNamespace(nil, []string{ | ||
"namespace1", | ||
}) | ||
|
||
if res != nil { | ||
t.Errorf("expected no validation error, but got %q", res.Error()) | ||
} | ||
} |
Oops, something went wrong.