...

Source file src/scheduler/faas_openfaas/utils.go

Documentation: scheduler/faas_openfaas

     1  /*
     2   * P2PFaaS - A framework for FaaS Load Balancing
     3   * Copyright (c) 2019 - 2022. Gabriele Proietti Mattia <pm.gabriele@outlook.com>
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17   */
    18  
    19  package faas_openfaas
    20  
    21  import (
    22  	"crypto/md5"
    23  	"encoding/json"
    24  	"fmt"
    25  	"io"
    26  )
    27  
    28  type IdentifiableFunction struct {
    29  	Name         string            `json:"name,omitempty" bson:"name"`
    30  	Service      string            `json:"service,omitempty" bson:"service"`
    31  	Network      string            `json:"network,omitempty" bson:"network"`
    32  	Image        string            `json:"image,omitempty" bson:"image"`
    33  	EnvProcess   string            `json:"envProcess,omitempty" bson:"envProcess"`
    34  	EnvVars      map[string]string `json:"envVars,omitempty" bson:"envVars"`
    35  	Constraints  []string          `json:"constraints,omitempty" bson:"constraints"`
    36  	Labels       map[string]string `json:"labels,omitempty" bson:"labels"`
    37  	Annotations  []string          `json:"annotations,omitempty" bson:"annotations"`
    38  	Secrets      []string          `json:"secrets,omitempty" bson:"secrets"`
    39  	RegistryAuth string            `json:"registryAuth,omitempty" bson:"registryAuth"`
    40  	Limits       MachineResources  `json:"limits,omitempty" bson:"limits"`
    41  	Requests     MachineResources  `json:"requests,omitempty" bson:"requests"`
    42  }
    43  
    44  // ComputeFunctionMD5 computes the ID of a function
    45  func ComputeFunctionMD5(fn *Function) string {
    46  	idFn := IdentifiableFunction{
    47  		Name:         fn.Name,
    48  		Service:      fn.Service,
    49  		Network:      fn.Network,
    50  		Image:        fn.Image,
    51  		EnvProcess:   fn.EnvProcess,
    52  		EnvVars:      fn.EnvVars,
    53  		Constraints:  fn.Constraints,
    54  		Labels:       fn.Labels,
    55  		Annotations:  fn.Annotations,
    56  		Secrets:      fn.Secrets,
    57  		RegistryAuth: fn.RegistryAuth,
    58  		Limits:       fn.Limits,
    59  		Requests:     fn.Requests,
    60  	}
    61  
    62  	out, _ := json.Marshal(idFn)
    63  	jsonString := string(out)
    64  
    65  	h := md5.New()
    66  	io.WriteString(h, jsonString)
    67  	return fmt.Sprintf("%x", h.Sum(nil))
    68  }
    69  

View as plain text