| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- package clusters
- import (
- "encoding/base64"
- "fmt"
- "io/ioutil"
- "strings"
- "github.com/google/uuid"
- "github.com/opencost/opencost/pkg/log"
- "github.com/opencost/opencost/pkg/util/fileutil"
- "github.com/opencost/opencost/pkg/util/json"
- "sigs.k8s.io/yaml"
- )
- // The details key used to provide auth information
- const DetailsAuthKey = "auth"
- // Authentication Information
- type ClusterConfigEntryAuth struct {
- // The type of authentication provider to use
- Type string `yaml:"type"`
- // Data expressed as a secret
- SecretName string `yaml:"secretName,omitempty"`
- // Any data specifically needed by the auth provider
- Data string `yaml:"data,omitempty"`
- // User and Password as a possible input
- User string `yaml:"user,omitempty"`
- Pass string `yaml:"pass,omitempty"`
- }
- // Cluster definition from a configuration yaml
- type ClusterConfigEntry struct {
- Name string `yaml:"name"`
- Address string `yaml:"address"`
- Auth *ClusterConfigEntryAuth `yaml:"auth,omitempty"`
- Details map[string]interface{} `yaml:"details,omitempty"`
- }
- // ClusterDefinition
- type ClusterDefinition struct {
- ID string `json:"id,omitempty"`
- Name string `json:"name"`
- Address string `json:"address"`
- Details map[string]interface{} `json:"details,omitempty"`
- }
- // ClusterStorage interface defines an implementation prototype for a storage responsible
- // for ClusterDefinition instances
- type ClusterStorage interface {
- // Add only if the key does not exist
- AddIfNotExists(key string, cluster []byte) error
- // Adds the encoded cluster to storage if it doesn't exist. Otherwise, update the existing
- // value with the provided.
- AddOrUpdate(key string, cluster []byte) error
- // Removes a key from the cluster storage
- Remove(key string) error
- // Iterates through all key/values for the storage and calls the handler func. If a handler returns
- // an error, the iteration stops.
- Each(handler func(string, []byte) error) error
- // Closes the backing storage
- Close() error
- }
- // ClusterManager provides an implementation
- type ClusterManager struct {
- storage ClusterStorage
- // cache map[string]*ClusterDefinition
- }
- // Creates a new ClusterManager instance using the provided storage
- func NewClusterManager(storage ClusterStorage) *ClusterManager {
- return &ClusterManager{
- storage: storage,
- }
- }
- // Creates a new ClusterManager instance using the provided storage and populates a
- // yaml configured list of clusters
- func NewConfiguredClusterManager(storage ClusterStorage, config string) *ClusterManager {
- clusterManager := NewClusterManager(storage)
- exists, err := fileutil.FileExists(config)
- if !exists {
- if err != nil {
- log.Errorf("Failed to load config file: %s. Error: %s", config, err.Error())
- }
- return clusterManager
- }
- data, err := ioutil.ReadFile(config)
- if err != nil {
- return clusterManager
- }
- var entries []*ClusterConfigEntry
- err = yaml.Unmarshal(data, &entries)
- if err != nil {
- return clusterManager
- }
- for _, entry := range entries {
- details := entry.Details
- if details == nil {
- details = make(map[string]interface{})
- }
- if entry.Auth != nil {
- authData, err := getAuth(entry.Auth)
- if err != nil {
- log.Errorf("%s", err)
- } else {
- details[DetailsAuthKey] = authData
- }
- }
- clusterManager.Add(ClusterDefinition{
- ID: entry.Name,
- Name: entry.Name,
- Address: entry.Address,
- Details: details,
- })
- }
- return clusterManager
- }
- // Add Adds a cluster definition, but will not update an existing entry.
- func (cm *ClusterManager) Add(cluster ClusterDefinition) (*ClusterDefinition, error) {
- // First time add
- if cluster.ID == "" {
- cluster.ID = uuid.New().String()
- }
- data, err := json.Marshal(cluster)
- if err != nil {
- return nil, err
- }
- err = cm.storage.AddIfNotExists(cluster.ID, data)
- if err != nil {
- return nil, err
- }
- return &cluster, nil
- }
- // AddOrUpdate will add the cluster definition if it doesn't exist, or update the existing definition
- // if it does exist.
- func (cm *ClusterManager) AddOrUpdate(cluster ClusterDefinition) (*ClusterDefinition, error) {
- // First time add
- if cluster.ID == "" {
- cluster.ID = uuid.New().String()
- }
- data, err := json.Marshal(cluster)
- if err != nil {
- return nil, err
- }
- err = cm.storage.AddOrUpdate(cluster.ID, data)
- if err != nil {
- return nil, err
- }
- return &cluster, nil
- }
- // Remove will remove a cluster definition by id.
- func (cm *ClusterManager) Remove(id string) error {
- return cm.storage.Remove(id)
- }
- // GetAll will return all of the cluster definitions
- func (cm *ClusterManager) GetAll() []*ClusterDefinition {
- clusters := []*ClusterDefinition{}
- err := cm.storage.Each(func(key string, cluster []byte) error {
- var cd ClusterDefinition
- err := json.Unmarshal(cluster, &cd)
- if err != nil {
- log.Errorf("Failed to unmarshal json cluster definition for key: %s", key)
- return nil
- }
- clusters = append(clusters, &cd)
- return nil
- })
- if err != nil {
- log.Infof("[Error] Failed to load list of clusters: %s", err.Error())
- }
- return clusters
- }
- // Close will close the backing database
- func (cm *ClusterManager) Close() error {
- return cm.storage.Close()
- }
- func toBasicAuth(user, pass string) string {
- return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", user, pass)))
- }
- func fileFromSecret(secretName string) string {
- return fmt.Sprintf("/var/secrets/%s/auth", secretName)
- }
- func fromSecret(secretName string) (string, error) {
- file := fileFromSecret(secretName)
- exists, err := fileutil.FileExists(file)
- if !exists || err != nil {
- return "", fmt.Errorf("Failed to locate secret: %s", file)
- }
- data, err := ioutil.ReadFile(file)
- if err != nil {
- return "", fmt.Errorf("Failed to load secret: %s", file)
- }
- return base64.StdEncoding.EncodeToString(data), nil
- }
- func getAuth(auth *ClusterConfigEntryAuth) (string, error) {
- // We only support basic auth currently
- if !strings.EqualFold(auth.Type, "basic") {
- return "", fmt.Errorf("Authentication Type: '%s' is not supported", auth.Type)
- }
- if auth.SecretName != "" {
- return fromSecret(auth.SecretName)
- }
- if auth.Data != "" {
- return auth.Data, nil
- }
- if auth.User != "" && auth.Pass != "" {
- return toBasicAuth(auth.User, auth.Pass), nil
- }
- return "", fmt.Errorf("No valid basic auth parameters provided.")
- }
|