Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mark namespace with label openfaas=1 valid #352

Merged
merged 1 commit into from
Nov 29, 2023

Conversation

nitishkumar71
Copy link
Member

Description

faasd implementation was no allowing to deploy functions into namespace with label openfaas=1. Instead code was written to validate openfaas=true.

Motivation and Context

How Has This Been Tested?

  1. Create and install a local build of faasd binary using make local
  2. Run the below script to deploy functions with label openfaas=1. Script is using go-sdk which includes label openfaas=1 for any new namespace created.
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"net/url"
	"os"
	"time"

	"github.com/openfaas/faas-provider/types"
	"github.com/openfaas/go-sdk"
)

func main() {
	username := os.Getenv("OPENFAAS_USERNAME")
	password := os.Getenv("OPENFAAS_PASSWORD")

	gatewayURL, _ := url.Parse(os.Getenv("OPENFAAS_GATEWAY_URL"))
	auth := &sdk.BasicAuth{
		Username: username,
		Password: password,
	}
	nsName := "test-sdk-ns"
	fnName := "env-store-test"

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

	// create namespace
	_, err := client.CreateNamespace(context.Background(), types.FunctionNamespace{
		Name: nsName,
		Labels: map[string]string{
			"purpose": "test",
		},
	})

	if err != nil {
		log.Fatalf("Namespace creation Failed: %s\n", err)
	}
	fmt.Printf("Namespace %s created!\n", nsName)

	// get namespace
	res, err := client.GetNamespace(context.Background(), nsName)

	if err != nil {
		log.Fatalf("Namespace get Failed: %s\n", err)
	}

	fmt.Printf("GetNamespace: %q\n", res)

	// update namespace
	_, err = client.UpdateNamespace(context.Background(), types.FunctionNamespace{
		Name: nsName,
		Labels: map[string]string{
			"purpose": "test",
			"demo":    "false",
		},
		Annotations: map[string]string{
			"anno": "true",
		},
	})

	if err != nil {
		log.Fatalf("Namespace update Failed: %s\n", err)
	}

	res, err = client.GetNamespace(context.Background(), nsName)

	if err != nil {
		log.Fatalf("Namespace get Failed: %s\n", err)
	}

	demo, found := res.Labels["demo"]

	if !found || demo != "false" {
		log.Fatalf("Namespace update Failed: %s\n", err)
	}
	fmt.Printf("Updated Namespace: %q\n", res)

	// get namespaces
	namespaces, err := client.GetNamespaces(context.Background())
	fmt.Printf("Namespaces: %q\n", namespaces)

	// deploy
	_, err = client.Deploy(context.Background(), types.FunctionDeployment{
		Service:    fnName,
		Image:      "ghcr.io/openfaas/alpine:latest",
		Namespace:  nsName,
		EnvProcess: "env",
	})

	if err != nil {
		log.Fatalf("Deploy Failed: %s\n", err)
	}
	fmt.Printf("Deploy Completed!\n")

	fmt.Printf("Sleep....\n")
	time.Sleep(15 * time.Second)
	fmt.Printf("Sleep Finish\n")

	fn, err := client.GetFunction(context.Background(), fnName, nsName)
	fmt.Printf("Function: %v\n", fn)
	if err != nil {
		log.Fatalf("GetFunction Failed: %s\n", err)
	}

	// fmt.Println("Scale to Zero")
	// err = client.ScaleFunction(context.Background(), fnName, nsName, 0)
	// if err != nil {
	// 	log.Fatalf("ScaleFunction Failed: %s\n", err)
	// }

	// fmt.Printf("Sleep....\n")
	// time.Sleep(15 * time.Second)
	// fmt.Printf("Sleep Finish\n\n\n\n")

	// // delete
	// err = client.DeleteFunction(context.Background(), fnName, nsName)
	// if err != nil {
	// 	log.Fatalf("DeleteFunction Failed: %s\n", err)
	// }
	// fmt.Print("Function Deleted!\n\n\n\n")

	// // delete namespace
	// err = client.DeleteNamespace(context.Background(), nsName)
	// if err != nil {
	// 	log.Fatalf("Namespace %s delete Failed: %s\n", nsName, err)
	// }
	// fmt.Printf("Namespace %s deleted!\n\n\n\n", nsName)
}
  1. Below output was produced for the above script
Namespace test-sdk-ns created!
GetNamespace: {"test-sdk-ns" map[] map["openfaas":"1" "purpose":"test"]}
Updated Namespace: {"test-sdk-ns" map[] map["demo":"false" "openfaas":"1" "purpose":"test"]}
Namespaces: ["openfaas-fn" "test-sdk-ns"]
Deploy Completed!
Sleep....
Sleep Finish
Function: {env-store-test ghcr.io/openfaas/alpine:latest test-sdk-ns env map[] [] [] 0xc000014228 0xc000014230 <nil> <nil> false 0 1 1 2023-11-28 20:50:06.077724974 +0000 UTC <nil>}

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

Commits:

  • I've read the CONTRIBUTION guide
  • My commit message has a body and describe how this was tested and why it is required.
  • I have signed-off my commits with git commit -s for the Developer Certificate of Origin (DCO)

Code:

  • My code follows the code style of this project.
  • I have added tests to cover my changes.

Docs:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

Signed-off-by: Nitishkumar Singh <nitishkumarsingh71@gmail.com>
Copy link
Member

@alexellis alexellis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved

@alexellis alexellis merged commit 1cb5493 into openfaas:master Nov 29, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

faasd namespace support openfaas=true instead of openfaas=1
2 participants