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

Add management vm #151

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/push-to-main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ on:
- main

jobs:
update_function_app_dist:
name: Update function_app_dist
update_app_code_dist:
name: Update app_code_dist
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Update function_app_dist default to 'release'
- name: Update app_code_dist default to 'release'
run: sed -i 's/^\( *default =\) "dev"/\1 "release"/' variables.tf

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: 'chore: set function_app_dist=release'
commit_message: 'chore: set app_code_dist=release'
file_pattern: 'variables.tf'
12 changes: 6 additions & 6 deletions .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
defaults:
run:
shell: bash
working-directory: ./function-app/distribution
working-directory: ./app/distribution
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -60,18 +60,18 @@ jobs:
# see https://github.com/Azure/login#configure-a-service-principal-with-a-secret
creds: ${{ secrets.RND_AZURE_CREDENTIALS }}

- name: Run create_and_upload_zip
- name: Run create_and_upload_bin
if: github.event_name != 'workflow_dispatch'
run: task create_and_upload_zip DIST=$DIST
run: task create_and_upload_bin DIST=$DIST

- name: Run create_and_upload_zip (workflow_dispatch)
- name: Run create_and_upload_bin (workflow_dispatch)
if: github.event_name == 'workflow_dispatch'
run: task create_and_upload_zip DIST=${{ inputs.dist }}
run: task create_and_upload_bin DIST=${{ inputs.dist }}

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: 'chore: update function_app_version'
commit_message: 'chore: update app_code_hash'
file_pattern: 'variables.tf'


Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ weka-deployment

.DS_Store

.tf-function-app/
.tf-binaries/
59 changes: 24 additions & 35 deletions README.md

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,38 @@ version: '3'

env:
DIST: '{{.DIST | default "dev"}}'
FUNCTION_CODE_PATH: '{{.TASKFILE_DIR}}/function-app/code'
FUNCTION_ZIP_DIR: '{{.TASKFILE_DIR}}/.tf-function-app'
FUNCTION_CODE_PATH: '{{.TASKFILE_DIR}}/app/code'
FUNCTION_BIN_DIR: '{{.TASKFILE_DIR}}/.tf-binaries'

tasks:
get_function_app_hash:
get_code_hash:
cmds:
- ./zip_function_app_creation/get_function_app_hash.sh {{OS}} ${FUNCTION_CODE_PATH}
- ./binary_app_creation/get_code_hash.sh {{OS}} ${FUNCTION_CODE_PATH}
silent: true

create_function_app_zip:
create_binary:
cmds:
- ./zip_function_app_creation/create_function_app_zip.sh {{OS}} ${FUNCTION_CODE_PATH} ${FUNCTION_ZIP_DIR}
desc: Generate function app zip
- ./binary_app_creation/create_binary.sh {{OS}} ${FUNCTION_CODE_PATH} ${FUNCTION_BIN_DIR}
desc: Generate function app bin

upload_to_storage_accounts:
env:
RESOURCE_GROUP: weka-tf-functions
REGIONS_FILE_DIR: '{{.TASKFILE_DIR}}/supported_regions' # File containing list of supported Azure regions
deps: [create_function_app_zip]
deps: [create_binary]
cmds:
- ./zip_function_app_creation/upload_to_storage_accounts.sh ${REGIONS_FILE_DIR} ${DIST} {{OS}} ${FUNCTION_CODE_PATH} ${FUNCTION_ZIP_DIR} ${RESOURCE_GROUP}
desc: Upload zip to multiple Azure Storage accounts
- ./binary_app_creation/upload_to_storage_accounts.sh ${REGIONS_FILE_DIR} ${DIST} {{OS}} ${FUNCTION_CODE_PATH} ${FUNCTION_BIN_DIR} ${RESOURCE_GROUP}
desc: Upload bin to multiple Azure Storage accounts

write_function_hash_to_variables:
write_code_hash_to_variables:
cmds:
- ./zip_function_app_creation/write_function_hash_to_variables.sh {{OS}} ${FUNCTION_CODE_PATH}
- ./binary_app_creation/write_code_hash_to_variables.sh {{OS}} ${FUNCTION_CODE_PATH}

create_and_upload_zip:
create_and_upload_bin:
preconditions:
- sh: "[ $DIST == 'dev' ] || [ $DIST == 'release' ]"
msg: "Valid value for DIST is one of the following: dev, release."
deps: [upload_to_storage_accounts, write_function_hash_to_variables]
deps: [upload_to_storage_accounts, write_code_hash_to_variables]
cmds:
- echo "Ran distribution for $DIST"
desc: Generate function app zip and upload to Azure Storage accounts
desc: Generate function app bin and upload to Azure Storage accounts
75 changes: 64 additions & 11 deletions function-app/code/common/common.go → app/code/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"

Expand All @@ -23,17 +24,6 @@ import (
"github.com/weka/go-cloud-lib/protocol"
)

type InvokeRequest struct {
Data map[string]json.RawMessage
Metadata map[string]interface{}
}

type InvokeResponse struct {
Outputs map[string]interface{}
Logs []string
ReturnValue interface{}
}

type ClusterState struct {
InitialSize int `json:"initial_size"`
DesiredSize int `json:"desired_size"`
Expand Down Expand Up @@ -477,6 +467,33 @@ func GetPublicIp(ctx context.Context, subscriptionId, resourceGroupName, vmScale
return
}

func GetPrivateIp(ctx context.Context, subscriptionId, resourceGroupName, vmScaleSetName, prefix, clusterName, instanceIndex string) (privateIp string, err error) {
logger := logging.LoggerFromCtx(ctx)

credential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
logger.Error().Err(err).Send()
return
}

client, err := armnetwork.NewInterfacesClient(subscriptionId, credential, nil)
if err != nil {
logger.Error().Err(err).Send()
return
}

interfaceName := fmt.Sprintf("%s-%s-backend-nic-0", prefix, clusterName)
resp, err := client.GetVirtualMachineScaleSetIPConfiguration(ctx, resourceGroupName, vmScaleSetName, instanceIndex, interfaceName, "ipconfig0", nil)
if err != nil {
logger.Error().Err(err).Send()
return "", err
}
if resp.InterfaceIPConfiguration.Properties.PrivateIPAddress != nil {
privateIp = *resp.InterfaceIPConfiguration.Properties.PrivateIPAddress
}
return privateIp, nil
}

func GetVmsPrivateIps(ctx context.Context, subscriptionId, resourceGroupName, vmScaleSetName string) (vmsPrivateIps map[string]string, err error) {
//returns compute_name to private ip map

Expand Down Expand Up @@ -999,3 +1016,39 @@ func UpdateStateReportingWithoutLocking(ctx context.Context, stateContainerName,
}
return
}

func RespondWithError(w http.ResponseWriter, err error, errCode int) {
data := make(map[string]any)
data["error"] = err.Error()

w.WriteHeader(errCode)
w.Header().Set("Content-Type", "application/json")

responseJson, _ := json.Marshal(data)
w.Write(responseJson)
}

func RespondWithMessage(w http.ResponseWriter, msg string, statusCode int) {
data := make(map[string]any)
data["message"] = msg

w.WriteHeader(statusCode)
w.Header().Set("Content-Type", "application/json")

responseJson, _ := json.Marshal(data)
w.Write(responseJson)
}

func RespondWithJson(w http.ResponseWriter, input any, statusCode int) {
responseJson, _ := json.Marshal(input)

w.WriteHeader(statusCode)
w.Header().Set("Content-Type", "application/json")
w.Write(responseJson)
}

func RespondWithPlainText(w http.ResponseWriter, text string, statusCode int) {
w.WriteHeader(statusCode)
w.Header().Set("Content-Type", "application/text")
w.Write([]byte(text))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,30 @@ package azure_functions_def

import (
"fmt"
"os"

"github.com/weka/go-cloud-lib/functions_def"
)

type AzureFuncDef struct {
baseFunctionUrl string
functionKey string
}

func NewFuncDef(baseFunctionUrl, functionKey string) functions_def.FunctionDef {
return &AzureFuncDef{
baseFunctionUrl: baseFunctionUrl,
functionKey: functionKey,
}
func NewFuncDef() functions_def.FunctionDef {
baseFunctionUrl := fmt.Sprintf("%s:%s", os.Getenv("HTTP_SERVER_HOST"), os.Getenv("HTTP_SERVER_PORT"))
return &AzureFuncDef{baseFunctionUrl: baseFunctionUrl}
}

// each function takes json payload as an argument
// e.g. "{\"hostname\": \"$HOSTNAME\", \"type\": \"$message_type\", \"message\": \"$message\"}"
func (d *AzureFuncDef) GetFunctionCmdDefinition(name functions_def.FunctionName) string {
functionUrl := d.baseFunctionUrl + string(name)
functionUrl := fmt.Sprintf("%s/%s", d.baseFunctionUrl, name)
reportDefTemplate := `
function %s {
local json_data=$1
curl %s?code=%s -H 'Content-Type:application/json' -d "$json_data"
curl %s -H 'Content-Type:application/json' -d "$json_data"
}
`
reportDef := fmt.Sprintf(reportDefTemplate, name, functionUrl, d.functionKey)
reportDef := fmt.Sprintf(reportDefTemplate, name, functionUrl)
return reportDef
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,6 @@ func HandleLastClusterVm(ctx context.Context, state common.ClusterState, p Clust
}
}

functionAppKey, err := common.GetKeyVaultValue(ctx, p.KeyVaultUri, "function-app-default-key")
if err != nil {
clusterizeScript = GetErrorScript(err)
return
}

wekaPassword, err := common.GetWekaClusterPassword(ctx, p.KeyVaultUri)
if err != nil {
clusterizeScript = GetErrorScript(err)
Expand Down Expand Up @@ -163,8 +157,7 @@ func HandleLastClusterVm(ctx context.Context, state common.ClusterState, p Clust
clusterParams.WekaUsername = "admin"
clusterParams.InstallDpdk = p.InstallDpdk

baseFunctionUrl := fmt.Sprintf("https://%s-%s-function-app.azurewebsites.net/api/", p.Prefix, p.Cluster.ClusterName)
funcDef := azure_functions_def.NewFuncDef(baseFunctionUrl, functionAppKey)
funcDef := azure_functions_def.NewFuncDef()

scriptGenerator := clusterize.ClusterizeScriptGenerator{
Params: clusterParams,
Expand All @@ -179,18 +172,8 @@ func HandleLastClusterVm(ctx context.Context, state common.ClusterState, p Clust
func Clusterize(ctx context.Context, p ClusterizationParams) (clusterizeScript string) {
logger := logging.LoggerFromCtx(ctx)

instanceName := strings.Split(p.VmName, ":")[0]
instanceId := common.GetScaleSetVmIndex(instanceName)
vmScaleSetName := common.GetVmScaleSetName(p.Prefix, p.Cluster.ClusterName)
vmName := p.VmName

ip, err := common.GetPublicIp(ctx, p.SubscriptionId, p.ResourceGroupName, vmScaleSetName, p.Prefix, p.Cluster.ClusterName, instanceId)
if err != nil {
logger.Error().Msg("Failed to fetch public ip")
} else {
vmName = fmt.Sprintf("%s:%s", vmName, ip)
}

state, err := common.AddInstanceToState(
ctx, p.SubscriptionId, p.ResourceGroupName, p.StateStorageName, p.StateContainerName, vmName,
)
Expand Down Expand Up @@ -240,31 +223,14 @@ func Handler(w http.ResponseWriter, r *http.Request) {
hotspare, _ := strconv.Atoi(os.Getenv("HOTSPARE"))
installDpdk, _ := strconv.ParseBool(os.Getenv("INSTALL_DPDK"))

outputs := make(map[string]interface{})
resData := make(map[string]interface{})
var invokeRequest common.InvokeRequest

ctx := r.Context()
logger := logging.LoggerFromCtx(ctx)

d := json.NewDecoder(r.Body)
err := d.Decode(&invokeRequest)
if err != nil {
logger.Error().Msg("Bad request")
return
}

var reqData map[string]interface{}
err = json.Unmarshal(invokeRequest.Data["req"], &reqData)
if err != nil {
logger.Error().Msg("Bad request")
return
}

var data RequestBody

if json.Unmarshal([]byte(reqData["Body"].(string)), &data) != nil {
logger.Error().Msg("Bad request")
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
logger.Error().Err(err).Send()
common.RespondWithError(w, err, http.StatusBadRequest)
return
}

Expand Down Expand Up @@ -298,18 +264,12 @@ func Handler(w http.ResponseWriter, r *http.Request) {
}

if data.Vm == "" {
msg := "Cluster name wasn't supplied"
logger.Error().Msgf(msg)
resData["body"] = msg
} else {
clusterizeScript := Clusterize(ctx, params)
resData["body"] = clusterizeScript
err := fmt.Errorf("cluster name wasn't supplied")
logger.Error().Err(err).Send()
common.RespondWithError(w, err, http.StatusBadRequest)
return
}
outputs["res"] = resData
invokeResponse := common.InvokeResponse{Outputs: outputs, Logs: nil, ReturnValue: nil}

responseJson, _ := json.Marshal(invokeResponse)

w.Header().Set("Content-Type", "application/json")
w.Write(responseJson)
clusterizeScript := Clusterize(ctx, params)
common.RespondWithPlainText(w, clusterizeScript, http.StatusOK)
}
Loading