-
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 <nitishkumarsingh71@gmail.com>
- Loading branch information
1 parent
d12888b
commit 850bbeb
Showing
27 changed files
with
1,725 additions
and
759 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 NAME | ||
[--label LABEL=VALUE ...] | ||
[--annotation ANNOTATION=VALUE ...]`, | ||
Short: "Create a new namespace", | ||
Long: "Create command creates a new namespace", | ||
Example: `faas-cli namespace create NAME | ||
faas-cli namespace create NAME --label demo=true | ||
faas-cli namespace create NAME --annotation demo=true | ||
faas-cli namespace create NAME --label demo=true \ | ||
--annotation demo=true`, | ||
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) | ||
if _, err = client.CreateNamespace(context.Background(), req); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Namespace Created: %s\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,54 @@ | ||
// 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 NAME`, | ||
Short: "Delete existing namespace", | ||
Long: "Delete existing namespace", | ||
Example: `faas-cli namespace delete NAME`, | ||
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) | ||
if err = client.DeleteNamespace(context.Background(), ns); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Namespace Deleted: %s\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,78 @@ | ||
// 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" | ||
"io" | ||
"text/tabwriter" | ||
|
||
"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 NAME`, | ||
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] | ||
|
||
res, err := client.GetNamespace(context.Background(), ns) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
printNamespaceDetail(cmd.OutOrStdout(), res) | ||
|
||
return nil | ||
} | ||
|
||
func printNamespaceDetail(dst io.Writer, nsDetail types.FunctionNamespace) { | ||
w := tabwriter.NewWriter(dst, 0, 0, 1, ' ', tabwriter.TabIndent) | ||
defer w.Flush() | ||
|
||
out := printer{ | ||
w: w, | ||
verbose: verbose, | ||
} | ||
out.Printf("Name:\t%s\n", nsDetail.Name) | ||
if len(nsDetail.Labels) > 1 { | ||
out.Printf("Labels", nsDetail.Labels) | ||
} else { | ||
out.Printf("Labels", map[string]string{}) | ||
} | ||
if len(nsDetail.Annotations) > 1 { | ||
out.Printf("Annotations", nsDetail.Annotations) | ||
} else { | ||
out.Printf("Annotations", 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.